Skip to content

Commit

Permalink
chore(ci): publish script
Browse files Browse the repository at this point in the history
  • Loading branch information
surmon-china committed Aug 14, 2022
1 parent a76b6fe commit 2babd3e
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 49 deletions.
23 changes: 13 additions & 10 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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/[email protected]`.
# MARK: package dir should not be coupled with package name. `@videojs-player/[email protected]` > `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
Expand All @@ -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.
15 changes: 0 additions & 15 deletions scripts/exec.js

This file was deleted.

40 changes: 39 additions & 1 deletion scripts/publish.js
Original file line number Diff line number Diff line change
@@ -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)
})
32 changes: 9 additions & 23 deletions scripts/release.js
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand All @@ -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,
Expand Down Expand Up @@ -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()
}
}
Expand Down
27 changes: 27 additions & 0 deletions scripts/utils.js
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 2babd3e

Please sign in to comment.