forked from Marus/cortex-debug
-
Notifications
You must be signed in to change notification settings - Fork 1
/
release.js
132 lines (123 loc) · 3.28 KB
/
release.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
const fs = require('fs');
const child_process = require('child_process');
let prog = '??';
let tagName = '';
let isDryRun = false;
function errExit(...args) {
console.error(`${prog}: Error:`, ...args);
process.exit(1);
}
function ensureGitClean() {
const gitStatus = child_process
.execSync('git status --short')
.toString()
.trim();
if (gitStatus) {
errExit('Uncommitted changes exist. exiting. Cannot continue');
}
}
function isPreRelease() {
let obj;
const path = './package.json';
try {
const txt = fs.readFileSync(path);
obj = JSON.parse(txt.toString());
}
catch (e) {
errExit(`Could not open/read file ${path}`, e);
}
const version = obj.version;
if (!version) {
errExit(`"version" property not found in ${path}`);
}
const parts = version.split('.');
const minor = parseInt(parts[1]);
tagName = 'v' + version;
return (minor % 2) === 1;
}
function vsceRun(pkgOnly) {
const args = ["vsce", (pkgOnly ? 'package' : 'release')];
if (isPreRelease()) {
args.push('--pre-release');
}
if (!isDryRun && !pkgOnly) {
ensureGitClean();
}
runProg(args, (code) => {
if (!pkgOnly && (code === 0)) {
let gitCmd = ['git', 'tag', tagName];
runProg(gitCmd, (code) => {
if (code === 0) {
gitCmd = ['git', 'push', 'origin', tagName];
runProg(gitCmd, (code) => {
if (code !== 0) {
errExit(`Failed '${gitCmd}'`);
}
});
} else {
errExit(`Failed '${gitCmd}'`);
}
});
}
});
}
function runProg(args, cb) {
if (isDryRun) {
args.unshift('echo');
}
// console.log('Executing ' + args.join(' '));
const arg0 = args.shift();
const prog = child_process.spawn(arg0, args, {
stdio:'inherit'
});
prog.on('error', (error) => {
console.error(`error: ${error.message}`);
if (cb) {
cb(-1);
}
});
prog.on("close", (code) => {
if (!isDryRun) {
console.log(`${arg0} exited with code ${code}`);
}
if (cb) {
cb(code);
}
});
}
function run() {
let isPkg = true;
const argv = [...process.argv];
prog = argv[1];
argv.shift() ; argv.shift();
while (argv.length) {
switch (argv[0]) {
case '-h':
case '--help': {
console.log(`Usage: node ${prog} [--dryrun] [--package] [--publish]`);
console.log('\t--package is by default true');
process.exit(0);
}
case '--dryrun': {
isDryRun = true;
console.log(`${prog}: This is a dryrun`);
break;
}
case '--package': {
isPkg = true;
break;
}
case '--publish': {
isPkg = false;
break;
}
default: {
errExit(`Unknown argument '${argv[0]}'`);
process.exit(1);
}
}
argv.shift();
}
vsceRun(isPkg);
}
run();