-
Notifications
You must be signed in to change notification settings - Fork 25
/
scripts.js
111 lines (102 loc) · 3 KB
/
scripts.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
const childProcess = require('child_process');
const fs = require('fs-extra');
const semver = require('semver');
const util = require('util');
const exec = util.promisify(childProcess.exec);
const [cmd, arg] = process.argv.slice(2);
async function runScript() {
switch (cmd) {
case 'clean':
await clean();
break;
case 'copy':
await copy();
break;
case 'log_publish_version':
await logVersionToPublish();
break;
case 'prepare_release':
await prepareRelease();
break;
default:
throw new Error(`Invalid script: "${cmd}"`);
}
}
async function clean() {
await fs.remove('umd');
await fs.remove('esm');
}
async function copy() {
await fs.copy('umd/CancelablePromise.d.ts', 'esm/CancelablePromise.d.ts');
}
async function logVersionToPublish() {
const changelog = await fs.readFile('CHANGELOG.md', { encoding: 'utf8' });
const match = /(?<version>\d+\.\d+\.\d+)/.exec(changelog);
const { version } = match.groups;
let { stdout } = await exec('npm view cancelable-promise version');
const currentVersion = stdout.toString().trim();
if (semver.valid(version) && semver.gt(version, currentVersion)) {
console.log(version);
}
}
async function prepareRelease() {
const currentVersion = require('./package.json').version;
const newVersion = semver.inc(currentVersion, arg || 'patch');
const { date } = /(?<date>\d+-\d+-\d+)/.exec(new Date().toISOString()).groups;
const url = `https://github.com/alkemics/CancelablePromise/releases/tag/v${newVersion}`;
const { stdout } = await exec(
`git log --oneline --pretty=format:'- %s' origin/master..HEAD`
);
const dict = {};
const commits = stdout
.toString()
.trim()
.replace(/(\r\n)|\r|\n/g, '\n')
.split(/\n+/g)
.filter((c) => {
if (c.includes('Merge pull request #')) {
return false;
}
if (c.includes('chore(deps-dev): bump')) {
if (
[
'@babel/cli',
'@babel/core',
'@babel/preset-env',
'@babel/preset-typescript',
'core-js',
'terser',
].some((dep) => {
const result = !(dep in dict) && c.includes(dep);
if (result) {
dict[dep] = true;
}
return result;
})
) {
return true;
}
return false;
}
return true;
})
.join('\n');
let changelog = await fs.readFile('CHANGELOG.md', { encoding: 'utf8' });
changelog = `## [${newVersion}](${url}) (${date})
${commits}
${changelog}`;
await fs.writeFile('CHANGELOG.md', changelog, { encoding: 'utf8' });
if (!process.argv.includes('--no-commit')) {
await exec('git add CHANGELOG.md');
await exec(
`git commit -m "chore(release): update changelog for v${newVersion}" --no-verify`
);
await exec(
`npm version --no-commit-hooks ${newVersion} -m 'chore(release) v${newVersion}'`
);
}
}
runScript().catch((err) => {
console.error(err);
process.exit(1);
});