forked from angular/material
-
Notifications
You must be signed in to change notification settings - Fork 0
/
release.js
418 lines (372 loc) · 12.6 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
(function () {
'use strict';
var colors = require('colors');
var strip = require('cli-color/strip');
var fs = require('fs');
var prompt = require('prompt-sync');
var child_process = require('child_process');
var pkg = require('./package.json');
var oldVersion = pkg.version;
var abortCmds = [ 'git reset --hard', 'git checkout staging', 'rm abort push' ];
var pushCmds = [ 'rm abort push' ];
var cleanupCmds = [];
var defaultOptions = { encoding: 'utf-8' };
var origin = 'https://github.com/angular/material.git';
var lineWidth = 80;
var lastMajorVer = JSON.parse(exec('curl https://material.angularjs.org/docs.json')).latest;
var newVersion;
var dryRun;
header();
write('Is this a dry-run? {{"[yes/no]".cyan}} ');
dryRun = prompt() !== 'no';
if (dryRun) {
write('What would you like the old version to be? (default: {{oldVersion.cyan}}) ');
oldVersion = prompt() || oldVersion;
build();
} else if (validate()) {
build();
}
function build () {
newVersion = getNewVersion();
line();
checkoutVersionBranch();
updateVersion();
createChangelog();
commitChanges();
tagRelease();
cloneRepo('bower-material');
updateBowerVersion();
cloneRepo('code.material.angularjs.org');
updateSite();
updateMaster();
writeScript('abort', abortCmds.concat(cleanupCmds));
if (!dryRun) writeScript('push', pushCmds.concat(cleanupCmds));
line();
log('Your repo is ready to be pushed.');
log('Please look over {{"CHANGELOG.md".cyan}} and make any changes.');
log('When you are ready, please run "{{"./push".cyan}}" to finish the process.');
log('If you would like to cancel this release, please run "./abort"');
}
//-- utility methods
function validate () {
if (exec('npm whoami') !== 'angularcore') {
err('You must be authenticated with npm as "angularcore" to perform a release.');
} else if (exec('git rev-parse --abbrev-ref HEAD') !== 'staging') {
err('Releases can only performed from "staging" at this time.');
} else {
return true;
}
function err (msg) {
var str = 'Error: ' + msg;
log(str.red);
}
}
function checkoutVersionBranch () {
exec('git checkout -q -b release/{{newVersion}}');
abortCmds.push('git branch -D release/{{newVersion}}');
}
function updateVersion () {
start('Updating {{"package.json".cyan}} version from {{oldVersion.cyan}} to {{newVersion.cyan}}...');
pkg.version = newVersion;
fs.writeFileSync('./package.json', JSON.stringify(pkg, null, 2));
done();
abortCmds.push('git checkout package.json');
pushCmds.push('git add package.json');
}
function createChangelog () {
start('Generating changelog from {{oldVersion.cyan}} to {{newVersion.cyan}}...');
exec([
'git fetch --tags',
'git checkout v{{lastMajorVer}} -- CHANGELOG.md',
'gulp changelog --sha=$(git merge-base v{{lastMajorVer}} HEAD)'
]);
done();
abortCmds.push('git checkout CHANGELOG.md');
pushCmds.push('git add CHANGELOG.md');
}
function clear () {
write("\u001b[2J\u001b[0;0H");
}
function getNewVersion () {
header();
var options = getVersionOptions(oldVersion), key, type, version;
log('The current version is {{oldVersion.cyan}}.');
log('');
log('What should the next version be?');
for (key in options) { log((+key + 1) + ') ' + options[ key ].cyan); }
log('');
write('Please select a new version: ');
type = prompt();
if (options[ type - 1 ]) version = options[ type - 1 ];
else if (type.match(/^\d+\.\d+\.\d+(-rc\d+)?$/)) version = type;
else throw new Error('Your entry was invalid.');
log('');
log('The new version will be ' + version.cyan + '.');
write('Is this correct? {{"[yes/no]".cyan}} ');
return prompt() === 'yes' ? version : getNewVersion();
function getVersionOptions (version) {
return version.match(/-rc\d+$/)
? [ increment(version, 'rc'),
increment(version, 'minor') ]
: [ increment(version, 'patch'),
increment(version, 'minor'),
increment(version, 'major') ].map(addRC);
function increment (versionString, type) {
var version = parseVersion(versionString);
if (version.rc) {
switch (type) {
case 'minor':
version.rc = 0;
break;
case 'rc':
version.rc++;
break;
}
} else {
version[ type ]++;
//-- reset any version numbers lower than the one changed
switch (type) {
case 'major':
version.minor = 0;
case 'minor':
version.patch = 0;
case 'patch':
version.rc = 0;
}
}
return getVersionString(version);
function parseVersion (version) {
var parts = version.split(/\.|\-rc/g);
return {
string: version,
major: parts[ 0 ],
minor: parts[ 1 ],
patch: parts[ 2 ],
rc: parts[ 3 ] || 0
};
}
function getVersionString (version) {
var str = version.major + '.' + version.minor + '.' + version.patch;
if (version.rc) str += '-rc' + version.rc;
return str;
}
}
function addRC (str) {
return str + '-rc1';
}
}
}
function tagRelease () {
pushCmds.push(
'git tag v{{newVersion}}',
'git push {{origin}} HEAD',
'git push --tags'
);
}
function commitChanges () {
start('Committing changes...');
exec('git commit -am "release: version {{newVersion}}"');
done();
pushCmds.push('git commit --amend --no-edit');
}
function cloneRepo (repo) {
start('Cloning ' + repo.cyan + ' from Github...');
exec('rm -rf ' + repo);
exec('git clone https://github.com/angular/' + repo + '.git --depth=1');
done();
cleanupCmds.push('rm -rf ' + repo);
}
function fill (str) {
return str.replace(/\{\{[^\}]+\}\}/g, function (match) {
return eval(match.substr(2, match.length - 4));
});
}
function writeScript (name, cmds) {
fs.writeFileSync(name, '#!/usr/bin/env bash\n\n' + fill(cmds.join('\n')));
exec('chmod +x ' + name);
}
function updateBowerVersion () {
start('Updating bower version...');
var options = { cwd: './bower-material' },
bower = require(options.cwd + '/bower.json'),
pkg = require(options.cwd + '/package.json');
//-- update versions in config files
bower.version = pkg.version = newVersion;
fs.writeFileSync(options.cwd + '/package.json', JSON.stringify(pkg, null, 2));
fs.writeFileSync(options.cwd + '/bower.json', JSON.stringify(bower, null, 2));
done();
start('Building bower files...');
//-- build files for bower
exec([
'rm -rf dist',
'gulp build',
'gulp build-all-modules --mode=default',
'gulp build-all-modules --mode=closure',
'rm -rf dist/demos',
]);
done();
start('Copy files into bower repo...');
//-- copy files over to bower repo
exec([
'cp -Rf ../dist/* ./',
'git add -A',
'git commit -m "release: version {{newVersion}}"',
'rm -rf ../dist'
], options);
done();
//-- add steps to push script
pushCmds.push(
comment('push to bower (master and tag) and publish to npm'),
'cd ' + options.cwd,
'cp ../CHANGELOG.md .',
'git add CHANGELOG.md',
'git commit --amend --no-edit',
'git tag -f v{{newVersion}}',
'git pull --rebase',
'git push',
'git push --tags',
( newVersion.indexOf('rc') < 0 ? 'npm publish' : '# skipped npm publish due to RC version' ),
'cd ..'
);
}
function updateSite () {
start('Adding new version of the docs site...');
var options = { cwd: './code.material.angularjs.org' };
writeDocsJson();
//-- build files for bower
exec([
'rm -rf dist',
'gulp docs',
'sed -i \'\' \'s,http:\\/\\/localhost:8080\\/angular-material,https:\\/\\/gitcdn.xyz/repo/angular/bower-material/v{{newVersion}}/angular-material,g\' dist/docs/docs.js',
'sed -i \'\' \'s,base\ href=\\",base\ href=\\"/{{newVersion}},g\' dist/docs/index.html'
]);
//-- copy files over to site repo
exec([
'rm -rf ./*-rc*',
'cp -Rf ../dist/docs {{newVersion}}',
( newVersion.indexOf('rc') < 0 ? 'rm -rf latest && cp -Rf ../dist/docs latest' : '# skipped latest because this is a release candidate' ),
'git add -A',
'git commit -m "release: version {{newVersion}}"',
'rm -rf ../dist'
], options);
//-- update firebase.json file
writeFirebaseJson();
done();
//-- add steps to push script
pushCmds.push(
comment('push the site'),
'cd ' + options.cwd,
'git pull --rebase',
'git push',
'cd ..'
);
function writeFirebaseJson () {
fs.writeFileSync(options.cwd + '/firebase.json', getFirebaseJson());
function getFirebaseJson () {
var json = require(options.cwd + '/firebase.json');
json.rewrites = json.rewrites || [];
switch (json.rewrites.length) {
case 0:
json.rewrites.push(getRewrite('HEAD'));
case 1:
json.rewrites.push(getRewrite('latest'));
default:
json.rewrites.push(getRewrite(newVersion));
}
return JSON.stringify(json, null, 2);
function getRewrite (str) {
return {
source: '/' + str + '/**/!(*.@(js|html|css|json|svg|png|jpg|jpeg))',
destination: '/' + str + '/index.html'
};
}
}
}
function writeDocsJson () {
var config = require(options.cwd + '/docs.json');
config.versions = config.versions.filter(function (version) {
return version.indexOf('rc') < 0;
});
config.versions.unshift(newVersion);
//-- only set to default if not a release candidate
if (newVersion.indexOf('rc') < 0) config.latest = newVersion;
fs.writeFileSync(options.cwd + '/docs.json', JSON.stringify(config, null, 2));
}
}
function updateMaster () {
pushCmds.push(
comment('update package.json in master'),
'git co master',
'git pull --rebase {{origin}} master',
'git checkout release/{{newVersion}} -- CHANGELOG.md',
'node -e "' + stringifyFunction(buildCommand) + '"',
'git add CHANGELOG.md',
'git add package.json',
'git commit -m "update version number in package.json to {{newVersion}}"',
'git push'
);
function buildCommand () {
require('fs').writeFileSync('package.json', JSON.stringify(getUpdatedJson(), null, 2));
function getUpdatedJson () {
var json = require('./package.json');
json.version = '{{newVersion}}';
return json;
}
}
function stringifyFunction (method) {
return method
.toString()
.split('\n')
.slice(1, -1)
.map(function (line) { return line.trim(); })
.join(' ')
.replace(/"/g, '\\"');
}
}
function header () {
clear();
line();
log(center('Angular Material Release'));
line();
}
function center (msg) {
msg = ' ' + msg.trim() + ' ';
var length = msg.length;
var spaces = Math.floor((lineWidth - length) / 2);
return Array(spaces + 1).join('-') + msg.green + Array(lineWidth - msg.length - spaces + 1).join('-');
}
function done () {
log('done'.green);
}
function exec (cmd, userOptions) {
if (cmd instanceof Array) {
return cmd.map(function (cmd) { return exec(cmd, userOptions); });
}
try {
var options = Object.create(defaultOptions);
for (var key in userOptions) options[ key ] = userOptions[ key ];
return child_process.execSync(fill(cmd) + ' 2> /dev/null', options).trim();
} catch (err) {
return err;
}
}
function comment (msg) {
return '\n# ' + msg + '\n';
}
function start (msg) {
var parsedMsg = fill(msg),
msgLength = strip(parsedMsg).length,
diff = lineWidth - 4 - msgLength;
write(parsedMsg + Array(diff + 1).join(' '));
}
function log (msg) {
msg = msg || '';
console.log(fill(msg));
}
function write (msg) {
process.stdout.write(fill(msg));
}
function line () {
log(Array(lineWidth + 1).join('-'));
}
})();