Skip to content
This repository was archived by the owner on Apr 1, 2023. It is now read-only.

add semantic release option #147

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ dist

# misc
.DS_Store
.env
./.env
.env.local
.env.development.local
.env.test.local
Expand Down
37 changes: 37 additions & 0 deletions docs/semantic-release.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Automated Semantic Versioning

[`semantic-release`](https://semantic-release.gitbook.io/semantic-release/) is a fully automated version management and package publishing solution:

> semantic-release automates the whole package release workflow including: determining the next version number, generating the release notes and publishing the package.

In other words, in exchange for [structuring commit messages](https://semantic-release.gitbook.io/semantic-release/#commit-message-format) you never have to worry about the versioning again. The flow of generating a GitHub release, publishing to npm, and generating a changelog, will be taken care of.

## How to

1. Choose `y` when prompted about `semantic-release` in the `create-react-library` wizard.
1. On the CI, add two environment variables: [`GITHUB_TOKEN`](https://github.com/settings/tokens) and [`NPM_TOKEN`](https://docs.npmjs.com/about-authentication-tokens).

## Not publishing to npm

If you'd rather **not publish** to npm (only generate a GitHub release & changelog), add `plugins` key to the `release` section in package.json:

```
"plugins": [
"@semantic-release/commit-analyzer",
"@semantic-release/release-notes-generator",
[
"@semantic-release/npm",
{
"npmPublish": false
}
],
"@semantic-release/github"
]
```

## Read more

- [Semantic Versioning](https://semver.org/)
- [Configuring semantic-release step-by-step](https://blog.logrocket.com/never-guess-about-project-history-again-31f65091f668)
- [`commitizen`](https://github.com/commitizen/cz-cli), a tool for structuring commit messages
- [Angular commit message format](https://github.com/angular/angular.js/blob/master/DEVELOPERS.md#-git-commit-guidelines), the default format used by `semantic-release`
2 changes: 1 addition & 1 deletion index.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const { test } = require('ava')
const test = require('ava')
const execa = require('execa')

test('--help', async (t) => {
Expand Down
4 changes: 3 additions & 1 deletion lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ module.exports = async () => {
.option('-l, --license <string>', 'package license', defaults.license)
.option('-r, --repo <string>', 'package repo path')
.option('-g, --no-git', 'generate without git init')
.option('--semantic-release', 'generate with semantic-release')
.option('-m, --manager <npm|yarn>', 'package manager to use', /^(npm|yarn)$/, defaults.manager)
.option('-t, --template <default|typescript>', 'package template to use', /^(default|typescript|custom)$/, defaults.template)
.option('-p, --template-path <string>', 'custom package template path')
Expand All @@ -37,7 +38,8 @@ module.exports = async () => {
template: program.template,
templatePath: program.templatePath,
skipPrompts: program.skipPrompts,
git: program.git
git: program.git,
semanticRelease: program.semanticRelease
}

Object.keys(opts).forEach((key) => {
Expand Down
12 changes: 11 additions & 1 deletion lib/create-library.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const path = require('path')
const pEachSeries = require('p-each-series')

const pkg = require('../package')
const initSemanticRelease = require('./modules/initSemanticRelease')

const templateBlacklist = new Set([
'example/public/favicon.ico'
Expand All @@ -21,7 +22,8 @@ module.exports = async (info) => {
template,
name,
templatePath,
git
git,
semanticRelease
} = info

// handle scoped package names
Expand Down Expand Up @@ -58,6 +60,12 @@ module.exports = async (info) => {
await promise
}

if (semanticRelease) {
const promise = module.exports.initSemanticRelease({ dest, info })
ora.promise(promise, 'Setting up semantic-release')
await promise
}

if (git) {
const promise = module.exports.initGitRepo({ dest })
ora.promise(promise, 'Initializing git repo')
Expand Down Expand Up @@ -158,3 +166,5 @@ yarn-error.log*
const cmd = `git init && git add . && git commit -m "init ${pkg.name}@${pkg.version}"`
return execa.shell(cmd, { cwd: dest })
}

module.exports.initSemanticRelease = initSemanticRelease
6 changes: 3 additions & 3 deletions lib/create-library.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const { test } = require('ava')
const test = require('ava')
const execa = require('execa')
const path = require('path')
const rmfr = require('rmfr')
Expand Down Expand Up @@ -81,8 +81,8 @@ const tests = [
}
]

tests.forEach((opts) => {
test.serial(`creating "${opts.name}" using ${opts.manager}`, async (t) => {
tests.forEach((opts, i) => {
test.serial(`creating "${opts.name}" using ${opts.manager} (${i})`, async (t) => {
console.log(`creating "${opts.name}" using ${opts.manager}...`)
let ret

Expand Down
82 changes: 82 additions & 0 deletions lib/modules/initSemanticRelease.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
'use strict'

const fs = require('fs')
const path = require('path')
const execa = require('execa')
const yaml = require('yaml')

const getReleaseSection = ({ info }) => ({
prepare: [
'@semantic-release/changelog',
'@semantic-release/npm',
{
path: '@semantic-release/git',
assets: [
'package.json',
info.manager === 'yarn' ? 'yarn.lock' : 'package-lock.json',
'CHANGELOG.md'
],
message:
// eslint-disable-next-line no-template-curly-in-string
'chore(release): ${nextRelease.version} [skip ci]\n\n${nextRelease.notes}'
}
]
})

const configSection = {
commitizen: {
path: './node_modules/cz-conventional-changelog'
}
}

module.exports = async opts => {
const { dest, info } = opts

const packagesList = [
'semantic-release',
'commitizen',
'cz-conventional-changelog',
'@semantic-release/git',
'@commitlint/cli',
'@commitlint/config-conventional',
'@semantic-release/changelog'
]

const managerInstallCommand =
info.manager === 'yarn' ? 'add --dev' : 'install --save-dev'
const cmd = `
${info.manager} ${managerInstallCommand} ${packagesList.join(' ')}
`.trim()

// add `release` and `config` sections in package.json
const packagePath = path.join(dest, 'package.json')
const pkg = require(packagePath)
const updatedPackage = {
...pkg,
version: '0.0.0',
release: getReleaseSection(opts),
config: configSection,
scripts: {
...pkg.scripts,
cm: 'git-cz',
'semantic-release': 'semantic-release'
}
}

// edit template's Travis CI config
const ciConfigPath = path.join(dest, '.travis.yml')
const ciConfig = yaml.parse(fs.readFileSync(ciConfigPath, 'utf8'))
const updatedCIConfig = {
...ciConfig,
jobs: {
include: [
{ stage: 'release', script: `${info.manager} run semantic-release` }
]
}
}
fs.writeFileSync(ciConfigPath, yaml.stringify(updatedCIConfig))

fs.writeFileSync(packagePath, JSON.stringify(updatedPackage, null, 2), 'utf8')

return execa.shell(cmd, { cwd: dest })
}
6 changes: 6 additions & 0 deletions lib/prompt-library-params.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ module.exports = async (opts) => {
resolve(true)
})
})
},
{
type: 'confirm',
name: 'semanticRelease',
message: 'Set up semantic-release?',
default: Boolean(opts.semanticRelease)
}
])

Expand Down
2 changes: 1 addition & 1 deletion lib/prompt-library-params.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const { test } = require('ava')
const test = require('ava')

const promptLibraryParams = require('./prompt-library-params')

Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"create-react-library": "index.js"
},
"scripts": {
"test": "ava -v && standard *.js lib/*.js"
"test": "CI=true ava -v *.test.js lib/*.test.js && standard *.js lib/*.js"
},
"engines": {
"node": ">=8",
Expand Down Expand Up @@ -45,10 +45,11 @@
"p-each-series": "^1.0.0",
"parse-git-config": "^3.0.0",
"validate-npm-package-name": "^3.0.0",
"which": "^1.3.1"
"which": "^1.3.1",
"yaml": "^1.4.0"
},
"devDependencies": {
"ava": "^1.2.1",
"ava": "^1.4.1",
"rmfr": "^2.0.0",
"standard": "^12.0.1"
}
Expand Down
4 changes: 4 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
- Supports CSS modules
- Optional support for TypeScript
- Sourcemap creation
- Automated [Semantic Versioning](https://semver.org/) via [`semantic-release`](https://semantic-release.gitbook.io/semantic-release/) (optional)
- Hundreds of public modules created
- Thorough documentation :heart_eyes:
- [Chinese docs](./readme.zh-CN.md) by [@monsterooo](https://github.com/monsterooo)
Expand Down Expand Up @@ -107,6 +108,9 @@ npm run deploy

This creates a production build of the example `create-react-app` that showcases your library and then runs `gh-pages` to deploy the resulting bundle.

#### Automated Semantic Versioning

see [docs/semantic-release](./docs/semantic-release)

## Examples

Expand Down
1 change: 1 addition & 0 deletions template/default/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SKIP_PREFLIGHT_CHECK=true
1 change: 0 additions & 1 deletion template/default/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ dist

# misc
.DS_Store
.env
.env.local
.env.development.local
.env.test.local
Expand Down
1 change: 1 addition & 0 deletions template/default/example/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SKIP_PREFLIGHT_CHECK=true
1 change: 1 addition & 0 deletions template/typescript/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SKIP_PREFLIGHT_CHECK=true
1 change: 0 additions & 1 deletion template/typescript/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ dist

# misc
.DS_Store
.env
.env.local
.env.development.local
.env.test.local
Expand Down
1 change: 1 addition & 0 deletions template/typescript/example/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SKIP_PREFLIGHT_CHECK=true
Loading