Skip to content

Commit e410bde

Browse files
committed
chore: add release script
1 parent b331ae5 commit e410bde

File tree

7 files changed

+275
-6
lines changed

7 files changed

+275
-6
lines changed

build/release.mjs

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
import { chalk, $, question } from 'zx';
2+
import fs from 'fs';
3+
import path from 'path';
4+
5+
const errorLog = (str) => console.log(chalk.redBright(str));
6+
const infoLog = (str) => console.log(chalk.blackBright(str));
7+
const successLog = (str) => console.log(chalk.greenBright(str));
8+
9+
const succeeDoneLog = () => console.log(chalk.greenBright('DONE!'));
10+
11+
const assert = (validation, str) => {
12+
if (!validation) {
13+
errorLog(str);
14+
process.exit(1);
15+
}
16+
};
17+
$.verbose = false;
18+
19+
(async () => {
20+
infoLog('1. Check the staged.');
21+
const isGitClean = (await $`git status --porcelain`).stdout.trim().length;
22+
assert(!isGitClean, 'You should empty the staged before release.');
23+
succeeDoneLog();
24+
25+
infoLog('2. Check the branch.');
26+
const currentBranch = (
27+
await $`git rev-parse --abbrev-ref HEAD`
28+
).stdout.trim();
29+
assert(currentBranch === 'main', `can't release on ${currentBranch}`);
30+
succeeDoneLog();
31+
32+
infoLog('3. Check the remote up to date.');
33+
const gitStatus = (await $`git status --short --branch`).stdout.trim();
34+
assert(!gitStatus.includes('behind'), `git status is behind remote`);
35+
succeeDoneLog();
36+
37+
// check npm registry
38+
infoLog('4. Check the npm registry.');
39+
const packagesDir = path.join(__dirname, '..', 'packages');
40+
const subPkgs = fs.readdirSync(packagesDir).filter((dir) => {
41+
return (
42+
!dir.startsWith('.') &&
43+
fs.existsSync(path.join(packagesDir, dir, 'package.json'))
44+
);
45+
});
46+
const isNPMRegistry = subPkgs.every((pkgDir) => {
47+
const pkg = require(path.join(packagesDir, pkgDir, 'package.json'));
48+
return pkg.publishConfig === 'https://registry.npmjs.org/';
49+
});
50+
assert(isNPMRegistry, 'npm registry is not https://registry.npmjs.org/');
51+
succeeDoneLog();
52+
53+
infoLog('5. Execute build...');
54+
await $`npm run build`;
55+
succeeDoneLog();
56+
57+
infoLog('6. Bump version.');
58+
const lastVersion = require('../package.json').version;
59+
const nextVersion = await question(
60+
`Input the next version(current version is ${lastVersion}): `
61+
);
62+
63+
infoLog(`6. Bump the version of main repository to ${nextVersion}...`);
64+
const mainPkgPath = path.join(__dirname, '..', 'package.json');
65+
const pkg = require(mainPkgPath);
66+
pkg.version = nextVersion;
67+
fs.writeFileSync(mainPkgPath, JSON.stringify(pkg, null, 4));
68+
succeeDoneLog();
69+
70+
infoLog(`7. Bump the version of packages repository to ${nextVersion}...`);
71+
subPkgs.forEach((pkgDir) => {
72+
const pkg = require(path.join(packagesDir, pkgDir, 'package.json'));
73+
pkg.version = nextVersion;
74+
fs.writeFileSync(
75+
path.join(packagesDir, pkgDir, 'package.json'),
76+
JSON.stringify(pkg, null, 4)
77+
);
78+
});
79+
succeeDoneLog();
80+
81+
infoLog(
82+
`8. Bump the dependencies of packages repository to ${nextVersion}...`
83+
);
84+
subPkgs.forEach((pkgDir) => {
85+
const pkg = require(path.join(packagesDir, pkgDir, 'package.json'));
86+
Object.keys(pkg.dependencies).forEach((key) => {
87+
if (key.startsWith('@dtinsight/molecule-')) {
88+
pkg.dependencies[key] = nextVersion;
89+
}
90+
});
91+
fs.writeFileSync(
92+
path.join(packagesDir, pkgDir, 'package.json'),
93+
JSON.stringify(pkg, null, 4)
94+
);
95+
});
96+
succeeDoneLog();
97+
98+
infoLog(`8. Update pnpm lockfile`);
99+
await $`pnpm i`;
100+
succeeDoneLog();
101+
102+
infoLog(`9. Generate Changelog`);
103+
$.verbose = true;
104+
await $`npx standard-version --release-as ${nextVersion}`;
105+
$.verbose = false;
106+
succeeDoneLog();
107+
108+
infoLog(`10. Publish`);
109+
// use npm pack --dry-run to check publish pack
110+
await Promise.all(
111+
subPkgs.map(async (pkg) => {
112+
await $`cd packages/${pkg} && npm publish`;
113+
successLog(`+ @dtinsight/molecule-${pkg}@${nextVersion}`);
114+
})
115+
);
116+
succeeDoneLog();
117+
118+
infoLog(`11. git push`);
119+
$.verbose = true;
120+
await $`git push --follow-tags origin main`;
121+
$.verbose = false;
122+
succeeDoneLog();
123+
})();

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"precommit": "pretty-quick --staged && pnpm run check-types",
1717
"prettier": "prettier --ignore-unknown --check .",
1818
"stylelint": "stylelint packages/**/*.{css,scss,sass}",
19-
"release": "standard-version --dry-run",
19+
"release": "zx ./build/release.mjs",
2020
"web": "webpack serve --env prod --config ./build/web.js"
2121
},
2222
"keywords": [
@@ -81,7 +81,8 @@
8181
"webpack": "^4.44.2",
8282
"webpack-cli": "^4.9.2",
8383
"webpack-dev-server": "^4.7.4",
84-
"webpack-merge": "^5.2.0"
84+
"webpack-merge": "^5.2.0",
85+
"zx": "^6.0.1"
8586
},
8687
"husky": {
8788
"hooks": {

packages/common/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"module": "./esm/index.js",
66
"typings": "./esm/index.d.ts",
77
"files": [
8-
"esm"
8+
"esm",
9+
"README.md"
910
],
1011
"scripts": {
1112
"build": "tsc",

packages/glue/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"module": "./esm/index.js",
66
"typings": "./esm/index.d.ts",
77
"files": [
8-
"esm"
8+
"esm",
9+
"README.md"
910
],
1011
"scripts": {
1112
"build": "tsc",

packages/ide/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"module": "./esm/index.js",
66
"typings": "./esm/index.d.ts",
77
"files": [
8-
"esm"
8+
"esm",
9+
"README.md"
910
],
1011
"scripts": {
1112
"build": "gulp --gulpfile ./build/gulpfile.js",

packages/ui/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"module": "./esm/index.js",
66
"typings": "./esm/index.d.ts",
77
"files": [
8-
"esm"
8+
"esm",
9+
"README.md"
910
],
1011
"scripts": {
1112
"build": "gulp --gulpfile ./build/gulpfile.js",

0 commit comments

Comments
 (0)