Skip to content

Commit

Permalink
workflow: add releasing script
Browse files Browse the repository at this point in the history
  • Loading branch information
hujiulong committed Feb 18, 2021
1 parent f6ca8f9 commit 4ab82c7
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 1 deletion.
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gcoord",
"version": "0.3.0-beta.2",
"version": "v0.3.0-beta.2",
"description": "geographic coordinate library",
"main": "dist/gcoord.js",
"module": "dist/gcoord.esm.js",
Expand All @@ -10,6 +10,7 @@
"build": "rollup -c ./rollup.config.js",
"lint": "eslint --ext .js,.ts, ./src",
"test": "jest && codecov",
"release": "node scripts/release.js",
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s",
"format": "prettier --write \"src/**/*.ts\"",
"gpr-setup": "node scripts/gpr-setup.js"
Expand Down Expand Up @@ -48,13 +49,17 @@
"chalk": "^4.1.0",
"codecov": "^3.8.1",
"conventional-changelog-cli": "^2.1.1",
"enquirer": "^2.3.6",
"eslint": "^7.20.0",
"execa": "^5.0.0",
"jest": "^26.6.3",
"lint-staged": "^10.5.4",
"minimist": "^1.2.5",
"prettier": "^2.2.1",
"rollup": "^2.39.0",
"rollup-plugin-terser": "^7.0.2",
"rollup-plugin-typescript2": "^0.29.0",
"semver": "^7.3.4",
"ts-jest": "^26.5.1",
"typescript": "^4.0.5",
"yorkie": "^2.0.0"
Expand Down
128 changes: 128 additions & 0 deletions scripts/release.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
const args = require('minimist')(process.argv.slice(2));
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 pkg = require('../package.json');

const currentVersion = pkg.version;
const preId =
args.preid ||
(semver.prerelease(currentVersion) && semver.prerelease(currentVersion)[0]);
const isDryRun = args.dry;
const skipTests = args.skipTests;
const skipBuild = args.skipBuild;

const pkgPath = path.resolve(__dirname, '../package.json');
const versionIncrements = [
'patch',
'minor',
'major',
...(preId ? ['prepatch', 'preminor', 'premajor', 'prerelease'] : []),
];

const inc = (i) => semver.inc(currentVersion, i, preId);
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;
const step = (msg) => console.log(chalk.cyan(msg));

async function main() {
let targetVersion = args._[0];

if (!targetVersion) {
// no explicit version, offer suggestions
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 } = await prompt({
type: 'confirm',
name: 'yes',
message: `Releasing v${targetVersion}. Confirm?`,
});

if (!yes) {
return;
}

// update all package versions and inter-dependencies
step('\nUpdating cross dependencies...');
if (!isDryRun) {
pkg.version = targetVersion;
fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + '\n');
} else {
console.log(`(skipped)`);
}

// run tests before release
step('\nRunning tests...');
if (!skipTests && !isDryRun) {
await run('npm', ['test']);
} else {
console.log(`(skipped)`);
}

// build package
step('\nBuilding package...');
if (!skipBuild && !isDryRun) {
await run('npm', ['build', '--release']);
} else {
console.log(`(skipped)`);
}

// generate changelog
await run(`npm`, ['run', 'changelog']);

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.');
}

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

if (isDryRun) {
console.log(`\nDry run finished - run git diff to see package changes.`);
}

console.log();
}

main().catch((err) => {
console.error(err);
});

0 comments on commit 4ab82c7

Please sign in to comment.