-
Notifications
You must be signed in to change notification settings - Fork 0
/
publish.js
66 lines (59 loc) · 2.09 KB
/
publish.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
/* eslint-disable no-undef */
/* eslint-disable no-shadow */
/* eslint-disable no-console */
/* eslint-disable new-cap */
/* eslint-disable @typescript-eslint/no-var-requires */
/** npm run publish with auto changelog **/
const { exec } = require('child_process');
const { writeFileSync } = require('fs');
const versionParser = require('./src/versionParser');
const readline = require('readline');
const rl = readline.createInterface(process.stdin, process.stdout);
const packages = require('./package.json');
const version = new versionParser(packages.version);
function updateChangelog(callback) {
exec('node changelog.js', (err, _stdout, _stderr) => {
if (!err) {
if (typeof callback === 'function') callback();
}
throw err;
});
}
if (typeof version === 'object') {
rl.question('Overwrite? [yes]/no: ', function (answer) {
if (answer.toLowerCase() === 'no' || answer.toLowerCase() === 'n') {
console.log('Publish Cancel');
} else {
packages.version = version.toString();
writeFileSync('./package.json', JSON.stringify(packages, null, 2));
console.log('Compiling...');
exec('npm run build', (err, _stdout, stderr) => {
if (!err) {
console.log('Build Typescript Successfully');
console.log('Publishing...');
exec('npm publish', (_err, _stdout, _stderr) => {
console.log('Packages Published Successfully');
// add to git
updateChangelog(() => {
exec('git add .', (err) => {
if (!err) {
console.log('Updating version');
version.result.build++;
exec(`git commit -m "Update release ${version.toString()}"`);
}
});
});
});
} else {
console.log('Publish Failed, Rollback version');
version.result.build--;
packages.version = version.toString();
writeFileSync('./package.json', JSON.stringify(packages, null, 2));
console.log(stderr);
throw err;
}
});
}
rl.close();
});
}