From 2babd3ea9d35ceec381c8e06a56e79973a0569cd Mon Sep 17 00:00:00 2001 From: surmon-china Date: Sun, 14 Aug 2022 22:34:29 +0800 Subject: [PATCH] chore(ci): publish script --- .github/workflows/release.yml | 23 +++++++++++--------- scripts/exec.js | 15 ------------- scripts/publish.js | 40 ++++++++++++++++++++++++++++++++++- scripts/release.js | 32 ++++++++-------------------- scripts/utils.js | 27 +++++++++++++++++++++++ 5 files changed, 88 insertions(+), 49 deletions(-) delete mode 100644 scripts/exec.js create mode 100644 scripts/utils.js diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 8786b74..eb1d890 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -17,15 +17,18 @@ jobs: - name: Checkout code uses: actions/checkout@v3 - - name: Get package name for tag - id: tag - run: | - # `%@*` truncates @ and version number from the right side. - # https://stackoverflow.com/questions/9532654/expression-after-last-specific-character - # https://www.edwardthomson.com/blog/github_actions_15_sharing_data_between_steps.html - # https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#example-setting-an-output-parameter - pkgName=${GITHUB_REF_NAME%@*} - echo "::set-output name=pkgName::$pkgName" + # `%@*` truncates @ and version number from the right side. + # https://stackoverflow.com/questions/9532654/expression-after-last-specific-character + # https://www.edwardthomson.com/blog/github_actions_15_sharing_data_between_steps.html + # https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#example-setting-an-output-parameter + # https://docs.github.com/en/actions/learn-github-actions/environment-variables#default-environment-variables + # GITHUB_REF_NAME === The branch or tag name that triggered the workflow run. For example, `@videojs-player/vue@1.0.0`. + # MARK: package dir should not be coupled with package name. `@videojs-player/vue@1.0.0` > `vue` + # - name: Get package name for tag + # id: tag + # run: | + # pkgDirName=${GITHUB_REF_NAME%@*} + # echo "::set-output name=pkgDirName::$pkgDirName" - name: Create Release id: create_release @@ -36,4 +39,4 @@ jobs: tag_name: ${{ github.ref }} release_name: Release ${{ github.ref }} body: | - Please refer to [CHANGELOG.md](https://github.com/surmon-china/videojs-player/blob/${{ github.ref_name }}/packages/${{ steps.tag.outputs.pkgName }}/CHANGELOG.md) for details. + Please refer to [CHANGELOG.md](https://github.com/surmon-china/videojs-player#changelog) for details. diff --git a/scripts/exec.js b/scripts/exec.js deleted file mode 100644 index 23efaf5..0000000 --- a/scripts/exec.js +++ /dev/null @@ -1,15 +0,0 @@ -const util = require('util') -const exec = util.promisify(require('child_process').exec) - -module.exports = (command) => { - return exec(command) - .then((result) => { - // If exec returns content in stderr, but no error, print it as a warning - if (result.stderr) console.warn(result.stderr) - return result - }) - .catch((error) => { - console.error(error.stderr || error.message) - return Promise.reject(error) - }) -} diff --git a/scripts/publish.js b/scripts/publish.js index 00f1595..39ed385 100644 --- a/scripts/publish.js +++ b/scripts/publish.js @@ -1 +1,39 @@ -// https://github.com/vitejs/vite/blob/main/scripts/publishCI.ts +const { exec, getPackageByName, getPrereleaseTagByVersion } = require('./utils') + +async function main() { + const [tag] = process.argv.slice(2) + if (!tag) { + throw new Error('No tag specified') + } + + console.log(`Publish ${tag}`) + + const [_, _packageName, packageVersion] = tag.split('@') + const packageName = `@${_packageName}` + const package = getPackageByName(packageName) + if (!package) { + throw new Error(`Package "${packageName}" not found.`) + } + + const { dirPath, json } = package + if (packageVersion !== json.version) + throw new Error( + `Package version from tag "${packageVersion}" mismatches with current version "${json.version}"` + ) + + console.log('Publishing package...') + + const publicArgs = ['publish', '--access', 'public'] + const releaseTag = getPrereleaseTagByVersion(packageVersion) + if (releaseTag) { + publicArgs.push(`--tag`, releaseTag) + } + + await exec(`cd ${dirPath} && npm ${publicArgs.join(' ')}`) + console.log() +} + +main().catch((err) => { + console.error(err) + process.exit(1) +}) diff --git a/scripts/release.js b/scripts/release.js index 0477c58..44e4118 100644 --- a/scripts/release.js +++ b/scripts/release.js @@ -2,27 +2,18 @@ // https://github.com/vuejs/core/blob/main/scripts/release.js // https://github.com/vitejs/vite/blob/main/scripts/publishCI.ts -const fs = require('fs-extra') const path = require('path') const prompts = require('prompts') const standardVersion = require('standard-version') -const exec = require('./exec') - -const rootDirPath = path.join(__dirname, '..') -const packagesDirPath = path.join(rootDirPath, 'packages') -const packages = fs.readdirSync(packagesDirPath).map((dirName) => { - const dirPath = path.join(packagesDirPath, dirName) - const json = require(path.resolve(dirPath, 'package.json')) - return { dirName, dirPath, json } -}) +const { exec, packages, getPackageByName, getPrereleaseTagByVersion } = require('./utils') const main = async () => { // select package - const { packageDirName } = await prompts({ + const { selectedPackageName } = await prompts({ type: 'select', - name: 'packageDirName', + name: 'selectedPackageName', message: 'Select package', - choices: packages.map((i) => ({ value: i.dirName, title: i.json.name })) + choices: packages.map((i) => ({ value: i.json.name, title: i.json.name })) }) // isStandardVersion? @@ -33,22 +24,17 @@ const main = async () => { initial: false }) - const selectedPackage = packages.find((p) => p.dirName === packageDirName) + const selectedPackage = getPackageByName(selectedPackageName) const changelogFile = path.resolve(selectedPackage.dirPath, 'CHANGELOG.md') const packageFile = path.resolve(selectedPackage.dirPath, 'package.json') const packageVersion = selectedPackage.json.version const tagPrefix = `${selectedPackage.json.name}@` const tagRefName = `${tagPrefix}${packageVersion}` - const prerelease = packageVersion.includes('beta') - ? 'beta' - : packageVersion.includes('alpha') - ? 'alpha' - : false const standardVersionBaseConfig = { commitAll: true, tagPrefix, - prerelease, + prerelease: getPrereleaseTagByVersion(packageVersion) ?? false, packageFiles: [packageFile], bumpFiles: [packageFile], infile: changelogFile, @@ -88,9 +74,9 @@ const main = async () => { if (isPushToGitHub) { await exec(`git push --follow-tags origin main`) - console.log( - '\nPushed, publishing should starts shortly on CI.\nhttps://github.com/surmon-china/videojs-player/actions/workflows/publish.yml' - ) + const message = `Pushed, publishing should starts shortly on CI.` + const url = `https://github.com/surmon-china/videojs-player/actions/workflows/publish.yml` + console.log(`\n${message}\n${url}`) console.log() } } diff --git a/scripts/utils.js b/scripts/utils.js new file mode 100644 index 0000000..6b4df95 --- /dev/null +++ b/scripts/utils.js @@ -0,0 +1,27 @@ +const path = require('path') +const fs = require('fs-extra') +const util = require('util') +const exec = util.promisify(require('child_process').exec) + +const rootDirPath = path.join(__dirname, '..') +const packagesDirPath = path.join(rootDirPath, 'packages') +const packages = fs.readdirSync(packagesDirPath).map((dirName) => { + const dirPath = path.join(packagesDirPath, dirName) + const json = require(path.resolve(dirPath, 'package.json')) + return { dirName, dirPath, json } +}) + +exports.exec = exec +exports.packages = packages + +exports.getPackageByName = (packageName) => { + return packages.find((package) => package.json.name === packageName) +} + +exports.getPrereleaseTagByVersion = (packageVersion) => { + return packageVersion.includes('beta') + ? 'beta' + : packageVersion.includes('alpha') + ? 'alpha' + : void 0 +}