Skip to content

Commit

Permalink
release script + changelog
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Sep 14, 2021
1 parent a6377a6 commit f4a0ecd
Show file tree
Hide file tree
Showing 4 changed files with 1,336 additions and 2 deletions.
Empty file added CHANGELOG.md
Empty file.
8 changes: 7 additions & 1 deletion package.json
Expand Up @@ -19,7 +19,8 @@
"scripts": {
"dev": "vite",
"build": "vite build && tsc --emitDeclarationOnly && mv dist/src dist/types",
"prepublishOnly": "yarn build"
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s",
"release": "node scripts/release.js"
},
"repository": {
"type": "git",
Expand All @@ -37,6 +38,11 @@
"devDependencies": {
"@vue/reactivity": "^3.2.11",
"@vue/shared": "^3.2.11",
"chalk": "^4.1.1",
"conventional-changelog-cli": "^2.1.1",
"enquirer": "^2.3.6",
"execa": "^5.0.0",
"prettier": "^2.3.0",
"typescript": "^4.4.3",
"vite": "^2.5.7"
}
Expand Down
107 changes: 107 additions & 0 deletions scripts/release.js
@@ -0,0 +1,107 @@
const fs = require('fs')
const path = require('path')
const chalk = require('chalk')
const semver = require('semver')
const { prompt } = require('enquirer')
const execa = require('execa')
const currentVersion = require('../package.json').version

const versionIncrements = ['patch', 'minor', 'major']

const inc = (i) => semver.inc(currentVersion, i)
const run = (bin, args, opts = {}) =>
execa(bin, args, { stdio: 'inherit', ...opts })
const step = (msg) => console.log(chalk.cyan(msg))

async function main() {
let targetVersion

const { release } = await prompt({
type: 'select',
name: 'release',
message: 'Select release type',
choices: versionIncrements.map((i) => `${i} (${inc(i)})`).concat(['custom'])
})

if (release === 'custom') {
targetVersion = (
await prompt({
type: 'input',
name: 'version',
message: 'Input custom version',
initial: currentVersion
})
).version
} else {
targetVersion = release.match(/\((.*)\)/)[1]
}

if (!semver.valid(targetVersion)) {
throw new Error(`Invalid target version: ${targetVersion}`)
}

const { yes: tagOk } = await prompt({
type: 'confirm',
name: 'yes',
message: `Releasing v${targetVersion}. Confirm?`
})

if (!tagOk) {
return
}

// Update the package version.
step('\nUpdating the package version...')
updatePackage(targetVersion)

// Build the package.
step('\nBuilding the package...')
await run('yarn', ['build'])

// Generate the changelog.
step('\nGenerating the changelog...')
await run('yarn', ['changelog'])
await run('yarn', ['prettier', '--write', 'CHANGELOG.md'])

const { yes: changelogOk } = await prompt({
type: 'confirm',
name: 'yes',
message: `Changelog generated. Does it look good?`
})

if (!changelogOk) {
return
}

// Commit changes to the Git and create a tag.
step('\nCommitting changes...')
await run('git', ['add', 'CHANGELOG.md', 'package.json'])
await run('git', ['commit', '-m', `release: v${targetVersion}`])
await run('git', ['tag', `v${targetVersion}`])

// Publish the package.
step('\nPublishing the package...')
await run('yarn', [
'publish',
'--new-version',
targetVersion,
'--no-commit-hooks',
'--no-git-tag-version'
])

// Push to GitHub.
step('\nPushing to GitHub...')
await run('git', ['push', 'origin', `refs/tags/v${targetVersion}`])
await run('git', ['push'])
}

function updatePackage(version) {
const pkgPath = path.resolve(path.resolve(__dirname, '..'), 'package.json')
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf-8'))

pkg.version = version

fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + '\n')
}

main().catch((err) => console.error(err))

0 comments on commit f4a0ecd

Please sign in to comment.