forked from angular/material
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrelease.js
More file actions
160 lines (138 loc) · 4.83 KB
/
release.js
File metadata and controls
160 lines (138 loc) · 4.83 KB
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
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 newVersion = getNewVersion();
var reset = [ 'git co master', 'rm reset' ];
console.log('\n--------\n');
//-- do stuff
checkoutVersionBranch();
updateVersion();
createChangelog();
commitChanges();
tagRelease();
cloneBower();
removeBower();
writeResetScript();
console.log('\n--------\n');
console.log('Your repo is ready to be pushed.');
console.log('Please look over CHANGELOG.md and amend-commit any changes.');
//-- utility methods
function checkoutVersionBranch () {
child_process.execSync(fill('git co -b v{{newVersion}}'));
reset.push(fill('git br -D v{{newVersion}}'));
}
function updateVersion () {
process.stdout.write(fill('Updating package.json version from {{oldVersion}} to {{newVersion}}...'));
pkg.version = newVersion;
fs.writeFileSync('./package.json', JSON.stringify(pkg, null, 2));
console.log('done.');
reset.push('git co package.json');
}
function createChangelog () {
process.stdout.write(fill('Generating changelog from v{{oldVersion}} to v{{newVersion}}...'));
var cmd = fill('gulp changelog --sha=$(git merge-base v{{oldVersion}} HEAD)');
child_process.execSync(cmd);
console.log('done.');
reset.push('git co CHANGELOG.md');
}
function clear () {
process.stdout.write("\u001b[2J\u001b[0;0H");
}
function getNewVersion () {
clear();
var options = getVersionOptions(oldVersion), key, type, version;
console.log(fill('The current version is {{oldVersion}}.'));
console.log('');
console.log('What type of release is this?');
for (key in options) { console.log((+key + 1) + ') ' + options[key]); }
console.log('');
process.stdout.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.');
if (version.indexOf('rc') < 0) {
console.log('');
process.stdout.write('Is this a release candidate? [yes/no] ');
if (prompt() === 'yes') version += '-rc1';
}
console.log('');
process.stdout.write('The new version will be ' + version + '. Is this correct? [yes/no] ');
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') ];
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 tagRelease () {
process.stdout.write('Tagging release...');
child_process.execSync(fill('git tag v{{newVersion}}'));
console.log('done.');
reset.push(fill('git tag -d v{{newVersion}}'));
}
function commitChanges () {
process.stdout.write('Committing changes...');
child_process.execSync(fill('git commit -am "release: version {{newVersion}}"'));
console.log('done.');
}
function removeBower () {
process.stdout.write('Removing bower-material...');
child_process.execSync('rm -rf bower-material');
console.log('done.');
}
function cloneBower () {
process.stdout.write('Cloning bower-material from Github...');
child_process.execSync('git clone https://github.com/angular/bower-material.git --depth=1');
console.log('done.');
}
function fill(str) {
return str.replace(/\{\{[^\}]+\}\}/g, function (match) {
return eval(match.substr(2, match.length - 4));
});
}
function writeResetScript () {
if (fs.existsSync('reset')) {
reset.unshift.apply(reset, fs.readFileSync('reset', { encoding: 'ascii' }).split('\n'));
}
fs.writeFileSync('reset', removeDuplicates(reset).join('\n'));
child_process.execSync('chmod +x reset');
}
function removeDuplicates (arr) {
return arr.reduce(function (arr, cmd) {
if (arr.indexOf(cmd) < 0) arr.push(cmd);
return arr;
}, []);
}