-
Notifications
You must be signed in to change notification settings - Fork 18
/
release.js
434 lines (359 loc) · 10.7 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
const outdent = require('outdent');
const execa = require('execa');
const promiseSeries = require('promise.series');
const chalk = require('chalk');
// eslint-disable-next-line import/no-dynamic-require
const pkj = require(`${process.cwd()}/package.json`);
const DEVELOP_TRUNK = 'develop';
const MASTER_TRUNK = 'master';
const RELEASE_TMP = 'release-tmp';
const STABLE = 'stable';
const UNSTABLE = 'unstable';
const HOTFIX = 'hotfix';
const MAJOR = 'major';
const MINOR = 'minor';
const PATCH = 'patch';
const BETA = 'beta';
process.stdin.setEncoding('utf8');
const execaPipeError = (file, ...rest) => {
const isCommand = rest.length === 0;
const params = isCommand ? file.split(/\s+/) : [file, ...rest];
const [command, ...args] = params;
const env = {
// This is critical for running smooth on windows behind enterprise proxies
// issue: https://github.com/axa-ch/patterns-library/issues/503
// npm@4.4.0 introduce the usage of update-notifier, which can't deal with proxies
// and times out after 3 minutes
// https://github.com/nodejs/node/issues/21632
NO_UPDATE_NOTIFIER: true,
};
const exec = execa(command, args, {
env,
});
return exec
.then((result) => {
console.log(`>>> resolved | ${command} ${args.join(' ')}`);
return result;
})
.catch((reason) => {
console.log(`>>> rejected | ${command} ${args.join(' ')}`);
throw reason;
});
};
const execaSeries = args => promiseSeries(args
.filter(arg => typeof arg === 'string' && arg.length)
.map(arg => () => execaPipeError(arg)));
console.log(chalk.cyan(outdent`
🚀 Hello Dear developer, welcome to the release assistant. 🚀
!! Please make sure you have no changes to be commited !!
I'm getting some information....
`));
promiseSeries([
() => execaPipeError('npm whoami')
.then(({ stdout }) => stdout)
.catch((reason) => {
console.log(chalk.red(outdent`
Attention: You are currently not logged into npm. I will abort the action
Please login:
${chalk.bold('npm login')}
`));
throw reason;
}),
whoami => execaPipeError('npm owner ls')
.then(({ stdout }) => {
const hasOwnership = stdout.trim().indexOf(whoami.trim()) > -1;
if (!hasOwnership) {
console.log(chalk.red(outdent`
Attention: Your account ${chalk.bold(whoami)} has no publisher rights. Please contact the administrator
`));
throw new Error('401 UNAUTHORIZED');
}
})
.catch((reason) => {
try {
const isNew = reason.message.indexOf('404 Not found') > -1;
if (isNew) {
console.log(chalk.yellow(outdent`
ATTENTION: Package ${chalk.bold(pkj.name)} does not exist yet on NPM!
We will try to create it for you. Be aware to have @axa-ch as scope in your package.json!
Your current version defined in the package.json is ${chalk.bold(pkj.version)}.
`));
return;
}
} catch (error) {
throw error;
}
throw reason;
})
.then(() => {
console.log(chalk.cyan(outdent`
You are currently logged in as:
`));
console.log(chalk.yellow(whoami.trim()));
console.log(chalk.cyan(outdent`
Would you like to continue?
`));
console.log(chalk.yellow(outdent`
y: yes
n: no
`));
}),
]).then(() => {
// process.exit(0);
}).catch((reason) => {
console.log('foo');
console.error(reason);
process.exit(1);
});
const determineStable = () => {
console.log(chalk.cyan(outdent`
Do you want to release a stable version or unstable (beta postfixed)?
`));
console.log(chalk.yellow(outdent`
stable: for stable
unstable: for unstable
hotfix: for an urgent bug fix to be merged directly into master
`));
};
const reallyHotfix = () => {
console.log(chalk.red(outdent`
Have you merged all your urgent ${chalk.bold('hotfix/*')} branches into ${MASTER_TRUNK}?
Note: this has to be done by finger tips:)
`));
console.log(chalk.yellow(outdent`
y: yes
n: no
`));
};
const prerelease = (type) => {
console.log(chalk.cyan(outdent`
Ok, we will release a ${type} version!
Choose which version label you want to bump.
Remember:
${type === UNSTABLE ? 'BETA (prerelease) this increases the beta version of a patch. Recommended step!' : ''}
${type === UNSTABLE ? 'MAJOR BETA (premajor)' : 'MAJOR'} version when you make incompatible API changes,
${type === UNSTABLE ? 'MINOR BETA (preminor)' : 'MINOR'} version when you add functionality in a backwards-compatible manner, and
${type === UNSTABLE ? 'PATCH BETA (prepatch)' : 'PATCH'} version when you make backwards-compatible bug fixes.
Select:
`));
console.log(chalk.yellow(outdent`
${type === UNSTABLE ? 'beta: for beta release of current branch. Recommended' : ''}
major: for incompatible API changes
minor: new functionality in a backwards-compatible manner
patch: for backwards-compatible bug fixes
`));
};
const release = (type, version) => {
if (type === HOTFIX) {
console.log(chalk.cyan(outdent`
Ok, we will release a ${type} version!
I will do now the following:
1. build the dist folder
2. bump the desired version
3. publish to npm
4. ${chalk.red.bold(`Don't forget to merges your hotfix branches into ${DEVELOP_TRUNK} too`)}
Please confirm that you want to proceed
`));
} else {
console.log(chalk.cyan(outdent`
Ok, we will release a ${version} version!
I will do now the following:
1. pull the ${DEVELOP_TRUNK} branch
2. build the dist folder
3. bump the desired version
4. publish to npm
5. merge ${DEVELOP_TRUNK} into ${MASTER_TRUNK} and push
6. sync ${DEVELOP_TRUNK} with ${MASTER_TRUNK} again
Please confirm that you want to proceed
`));
}
console.log(chalk.yellow('\nproceed: to proceed with the above described steps. This operation cannot be undone!'));
};
const generalCleanupHandling = (exitcode) => {
execaSeries([
`git checkout ${DEVELOP_TRUNK}`,
`git branch -D ${RELEASE_TMP}`,
]).then(() => {
process.exit(exitcode);
}).catch((reason) => {
console.error(chalk.red(reason));
process.exit(1);
});
};
const confirmedRelease = (type, version) => {
if (type === STABLE && version === BETA) {
return;
}
const isHotfix = type === HOTFIX;
const TRUNK = isHotfix ? MASTER_TRUNK : DEVELOP_TRUNK;
const { scripts } = pkj;
const hasTestScript = scripts.test;
let releaseSteps = [
() => execaSeries([
`git checkout ${TRUNK} --quiet`,
'git pull',
`git checkout -b ${RELEASE_TMP} --quiet`,
]).then(() => {
console.log(chalk.cyan(outdent`
Step 1 complete...
`));
}),
() => execaSeries([
hasTestScript && 'npm run test',
'npm run build',
'git add ./dist ./docs',
'git commit -m"rebuild"',
]).then(() => {
console.log(chalk.cyan(outdent`
Step 2 complete...
`));
}),
() => execaSeries([
type === UNSTABLE ? `npm run bump-${version === BETA ? '' : `${version}-`}beta` : `npm run bump-${version}`,
]).then(() => {
console.log(chalk.cyan(outdent`
Step 3 complete...
`));
}),
() => execaSeries([
`npm publish ${version === BETA ? ' --tag beta' : ''}`,
`git checkout ${TRUNK} --quiet`,
`git merge --ff-only ${RELEASE_TMP}`,
'git push',
'git push --tags',
]).then(() => {
console.log(chalk.cyan(outdent`
Step 4 complete...
`));
}),
];
if (!isHotfix) {
releaseSteps = [
...releaseSteps,
() => execaSeries([
`git checkout ${MASTER_TRUNK} --quiet`,
`git merge --no-ff ${DEVELOP_TRUNK}`,
'git push',
'git push --tags',
]).then(() => {
console.log(chalk.cyan(outdent`
Step 5 complete...
`));
}),
() => execaSeries([
`git checkout ${DEVELOP_TRUNK} --quiet`,
`git merge --ff-only ${MASTER_TRUNK}`,
'git push',
'git push --tags',
]).then(() => {
console.log(chalk.cyan(outdent`
Step 6 complete! Publishing done successfully. Have fun!
`));
}),
];
}
promiseSeries(releaseSteps)
.then(() => {
generalCleanupHandling(0);
})
.catch((reason) => {
console.error(chalk.red(reason));
generalCleanupHandling(1);
});
};
let step = 0;
let releaseType = '';
let releaseVersion = '';
process.stdin.on('readable', () => {
const chunk = process.stdin.read();
if (chunk === null) {
return;
}
switch (chunk.trim()) {
case 'y':
if (step > 0) {
return;
}
console.log(chalk.cyan('\nOk, let\'s do it 😎 👍'));
if (releaseType === HOTFIX) {
prerelease(releaseType);
} else {
determineStable();
}
step++; // eslint-disable-line no-plusplus
break;
case 'n':
console.log('closing...');
process.exit(0);
break;
case STABLE:
if (step !== 1) {
return;
}
releaseType = STABLE;
prerelease(releaseType);
step++; // eslint-disable-line no-plusplus
break;
case UNSTABLE:
if (step !== 1) {
return;
}
releaseType = UNSTABLE;
prerelease(releaseType);
step++; // eslint-disable-line no-plusplus
break;
case HOTFIX:
if (step !== 1) {
return;
}
releaseType = HOTFIX;
reallyHotfix();
step++; // eslint-disable-line no-plusplus
break;
case MAJOR:
if (step !== 2) {
return;
}
releaseVersion = MAJOR;
release(releaseType, releaseVersion);
step++; // eslint-disable-line no-plusplus
break;
case MINOR:
if (step !== 2) {
return;
}
releaseVersion = MINOR;
release(releaseType, releaseVersion);
step++; // eslint-disable-line no-plusplus
break;
case PATCH:
if (step !== 2) {
return;
}
releaseVersion = PATCH;
release(releaseType, releaseVersion);
step++; // eslint-disable-line no-plusplus
break;
case BETA:
if (step !== 2) {
return;
}
releaseVersion = BETA;
release(releaseType, releaseVersion);
step++; // eslint-disable-line no-plusplus
break;
case 'proceed':
if (step !== 3) {
return;
}
confirmedRelease(releaseType, releaseVersion);
step++; // eslint-disable-line no-plusplus
break;
default:
console.log(chalk.cyan('Command not understood. Try again or press \'n\' to abort'));
break;
}
});
process.stdin.on('end', () => {
process.stdout.write('end');
});