瀏覽代碼

chore: enhance release script (#1735)

* chore: enhance release script

* chore: update release script
Kia King Ishii 5 年之前
父節點
當前提交
71372c310f
共有 4 個文件被更改,包括 133 次插入35 次删除
  1. 3 1
      package.json
  2. 113 0
      scripts/release.js
  3. 0 34
      scripts/release.sh
  4. 17 0
      yarn.lock

+ 3 - 1
package.json

@@ -26,7 +26,7 @@
     "test:ssr": "cross-env VUE_ENV=server jasmine JASMINE_CONFIG_PATH=test/unit/jasmine.json",
     "test:types": "tsc -p types/test",
     "changelog": "conventional-changelog -p angular -i CHANGELOG.md -s",
-    "release": "bash scripts/release.sh",
+    "release": "node scripts/release.js",
     "docs": "vuepress dev docs",
     "docs:build": "vuepress build docs"
   },
@@ -61,6 +61,7 @@
     "cross-env": "^5.2.0",
     "cross-spawn": "^6.0.5",
     "css-loader": "^2.1.0",
+    "enquirer": "^2.3.5",
     "eslint": "^6.8.0",
     "eslint-plugin-vue-libs": "^4.0.0",
     "execa": "^4.0.0",
@@ -71,6 +72,7 @@
     "nightwatch-helpers": "^1.2.0",
     "rollup": "^2.7.2",
     "rollup-plugin-terser": "^5.3.0",
+    "semver": "^7.3.2",
     "todomvc-app-css": "^2.1.0",
     "typescript": "^3.8.3",
     "vue": "^2.5.22",

+ 113 - 0
scripts/release.js

@@ -0,0 +1,113 @@
+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 tags = [
+  'latest',
+  'next'
+]
+
+const inc = (i) => semver.inc(currentVersion, i)
+const bin = (name) => path.resolve(__dirname, `../node_modules/.bin/${name}`)
+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 { tag } = await prompt({
+    type: 'select',
+    name: 'tag',
+    message: 'Select tag type',
+    choices: tags
+  })
+
+  console.log(tag)
+
+  const { yes } = await prompt({
+    type: 'confirm',
+    name: 'yes',
+    message: `Releasing v${targetVersion} with the "${tag}" tag. Confirm?`
+  })
+
+  if (!yes) {
+    return
+  }
+
+  // Run tests before release.
+  step('\nRunning tests...')
+  await run('yarn', ['test'])
+
+  // 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'])
+
+  // Commit changes to the Git.
+  step('\nCommitting changes...')
+  await run('git', ['add', '-A'])
+  await run('git', ['commit', '-m', `release: v${targetVersion}`])
+
+  // Publish the package.
+  step('\nPublishing the package...')
+  await run ('yarn', [
+    'publish', '--tag', tag, '--new-version', targetVersion, '--no-commit-hooks',
+    '--no-git-tag-version'
+  ])
+
+  // Push to GitHub.
+  step('\nPushing to GitHub...')
+  await run('git', ['tag', `v${targetVersion}`])
+  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 - 34
scripts/release.sh

@@ -1,34 +0,0 @@
-set -e
-echo "Enter release version: "
-read VERSION
-
-read -p "Releasing $VERSION - are you sure? (y/n)" -n 1 -r
-echo    # (optional) move to a new line
-if [[ $REPLY =~ ^[Yy]$ ]]
-then
-  echo "Releasing $VERSION ..."
-
-  # run tests
-  npm test 2>/dev/null
-
-  # build
-  VERSION=$VERSION npm run build
-
-  # generate the version so that the changelog can be generated too
-  yarn version --no-git-tag-version --no-commit-hooks --new-version $VERSION
-
-  # changelog
-  yarn changelog
-  echo "Please check the git history and the changelog and press enter"
-  read OKAY
-
-  # commit
-  git add -A
-  git commit -m "realese: v$VERSION"
-  git tag "v$VERSION"
-
-  # publish
-  git push origin refs/tags/v$VERSION
-  git push
-  npm publish
-fi

+ 17 - 0
yarn.lock

@@ -1531,6 +1531,11 @@ ansi-colors@3.2.3, ansi-colors@^3.0.0:
   resolved "https://registry.yarnpkg.com/ansi-colors/-/ansi-colors-3.2.3.tgz#57d35b8686e851e2cc04c403f1c00203976a1813"
   integrity sha512-LEHHyuhlPY3TmuUYMh2oz89lTShfvgbmzaBcxve9t/9Wuy7Dwf4yoAKcND7KFT1HAQfqZ12qtc+DUrBMeKF9nw==
 
+ansi-colors@^3.2.1:
+  version "3.2.4"
+  resolved "https://registry.yarnpkg.com/ansi-colors/-/ansi-colors-3.2.4.tgz#e3a3da4bfbae6c86a9c285625de124a234026fbf"
+  integrity sha512-hHUXGagefjN2iRrID63xckIvotOXOojhQKWIPUZ4mNUZ9nLZW+7FMNoE1lOkEhNWYsx/7ysGIuJYCiMAA9FnrA==
+
 ansi-escapes@^3.0.0:
   version "3.1.0"
   resolved "https://registry.yarnpkg.com/ansi-escapes/-/ansi-escapes-3.1.0.tgz#f73207bb81207d75fd6c83f125af26eea378ca30"
@@ -4310,6 +4315,13 @@ enhanced-resolve@^4.1.0:
     memory-fs "^0.4.0"
     tapable "^1.0.0"
 
+enquirer@^2.3.5:
+  version "2.3.5"
+  resolved "https://registry.yarnpkg.com/enquirer/-/enquirer-2.3.5.tgz#3ab2b838df0a9d8ab9e7dff235b0e8712ef92381"
+  integrity sha512-BNT1C08P9XD0vNg3J475yIUG+mVdp9T6towYFHUv897X0KoHBjB1shyrNmhmtHWKP17iSWgo7Gqh7BBuzLZMSA==
+  dependencies:
+    ansi-colors "^3.2.1"
+
 entities@~1.1.1:
   version "1.1.2"
   resolved "https://registry.yarnpkg.com/entities/-/entities-1.1.2.tgz#bdfa735299664dfafd34529ed4f8522a275fea56"
@@ -9656,6 +9668,11 @@ semver@^6.0.0, semver@^6.1.2, semver@^6.3.0:
   resolved "https://registry.yarnpkg.com/semver/-/semver-6.3.0.tgz#ee0a64c8af5e8ceea67687b133761e1becbd1d3d"
   integrity sha512-b39TBaTSfV6yBrapU89p5fKekE2m/NwnDocOVruQFS1/veMgdzuPcnOM34M6CwxW8jH/lxEa5rBoDeUwu5HHTw==
 
+semver@^7.3.2:
+  version "7.3.2"
+  resolved "https://registry.yarnpkg.com/semver/-/semver-7.3.2.tgz#604962b052b81ed0786aae84389ffba70ffd3938"
+  integrity sha512-OrOb32TeeambH6UrhtShmF7CRDqhL6/5XpPNp2DuRH6+9QLw/orhp72j87v8Qa1ScDkvrrBNpZcDejAirJmfXQ==
+
 send@0.17.1:
   version "0.17.1"
   resolved "https://registry.yarnpkg.com/send/-/send-0.17.1.tgz#c1d8b059f7900f7466dd4938bdc44e11ddb376c8"