Skip to content

Commit

Permalink
chore: update release implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
cuixiaorui committed Dec 15, 2020
1 parent 36c645d commit c1f1fd6
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 73 deletions.
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"bootstrap": "yarn",
"build": "yarn workspace element3 build && yarn workspace website build",
"test": "yarn workspace element3 test",
"lint": "eslint --ext .js packages/element3/packages/** packages/*/src/** "
"lint": "eslint --ext .js packages/element3/packages/** packages/*/src/** ",
"release": "node scripts/release.js"
},
"license": "MIT",
"gitHooks": {
Expand All @@ -19,13 +20,15 @@
"devDependencies": {
"@vue/eslint-config-prettier": "^6.0.0",
"babel-eslint": "^10.1.0",
"chalk": "^4.1.0",
"conventional-changelog-cli": "^2.1.1",
"cross-env": "^7.0.3",
"enquirer": "^2.3.6",
"eslint": "^7.15.0",
"eslint-plugin-json": "^2.1.2",
"eslint-plugin-prettier": "^3.2.0",
"eslint-plugin-vue": "^7.2.0",
"execa": "^5.0.0",
"lint-staged": "^10.5.3",
"minimist": "^1.2.5",
"prettier": "^2.2.1",
Expand Down
1 change: 0 additions & 1 deletion packages/element3/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
"i18n": "node build/bin/i18n.js",
"test": "jest --runInBand",
"dev": "rollup -wc",
"release": "node scripts/release.js",
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s"
},
"repository": {
Expand Down
71 changes: 0 additions & 71 deletions packages/element3/scripts/release.js

This file was deleted.

115 changes: 115 additions & 0 deletions scripts/release.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
const { prompt } = require('enquirer')
const path = require('path')
const fs = require('fs')
const args = require('minimist')(process.argv.slice(2))
const targetVersion = args.v
const execa = require('execa')
const chalk = require('chalk')
const isDryRun = args.dry

const step = (msg) => console.log(chalk.cyan(msg))
const run = (bin, args, opts = {}) =>
execa(bin, args, { stdio: 'inherit', ...opts })
const dryRun = (bin, args, opts = {}) =>
console.log(chalk.blue(`[dryrun] ${bin} ${args.join(' ')}`), opts)
const runIfNotDry = isDryRun ? dryRun : run

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

if (!yes) return

step('\nRunning element3 tests...')
await run('yarn', ['workspace', 'element3', 'test'])

step('\nBuilding element3...')
await run('yarn', ['workspace', 'element3', 'build'])

step('\nUpdate element3 version...')
await run('yarn', [
'workspace',
'element3',
'version',
'--new-version',
targetVersion,
'--no-git-tag-version'
])

step('\nUpdating element3 cross dependencies...')
updatePackageVersion(targetVersion)

const { stdout } = await run('git', ['diff'], { stdio: 'pipe' })
if (stdout) {
step('\nCommitting changes...')
await runIfNotDry('git', ['add', '-A'])
await runIfNotDry('git', ['commit', '-m', `release: v${targetVersion}`])
} else {
console.log('No changes to commit.')
}

step('\nPublishing element3 package...')

await runIfNotDry(
'yarn',
[
'publish',
'--new-version',
targetVersion,
'--registry',
'https://registry.npmjs.org',
'--access',
'public'
],
{
cwd: path.resolve(__dirname, '../packages/element3'),
stdio: 'pipe'
}
)

step('\nPushing to GitHub...')
await runIfNotDry('git', ['tag', `v${targetVersion}`])
await runIfNotDry('git', [
'push',
'origin',
`refs/tags/v${targetVersion}`,
'--no-verify'
])
await runIfNotDry('git', ['push', 'origin', 'master', '--no-verify'])

console.log()
console.log(chalk.green(`Successfully published v${targetVersion}`))
})()

function updatePackageVersion(version) {
getPackagePath().forEach((pkgPath) => {
const pkg = JSON.parse(fs.readFileSync(pkgPath))
updateDeps(pkg, 'dependencies', version)
fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + '\n')
})
}

function getPackagePath() {
const pkgRoot = path.resolve(__dirname, '../packages')
const packages = fs
.readdirSync(pkgRoot)
.filter((name) => !name.startsWith('.'))

return packages.map((packageName) =>
path.resolve(pkgRoot, packageName, 'package.json')
)
}

function updateDeps(packageJson, depType, version) {
const dependencies = packageJson[depType]
if (!dependencies) return

Object.keys(dependencies).forEach((key) => {
if (key === 'element3') {
dependencies[key] = version
}
})
}

0 comments on commit c1f1fd6

Please sign in to comment.