-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·273 lines (219 loc) · 7.65 KB
/
index.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
#!/usr/bin/env node
const childProcess = require('child_process');
const readline = require('readline');
const fs = require('fs');
const chalk = require('chalk');
const BufferList = require('bl');
const {
exit
} = require('process');
const editJsonFile = require("edit-json-file");
let [, , ...args] = process.argv;
const pkg = fs.readFileSync(`${__dirname}/package.json`, 'utf8');
const package = JSON.parse(pkg);
let loadedFile = fs.readFileSync('./.grelease', 'utf8');
const config = JSON.parse(loadedFile);
let releaseBranch = '';
async function execute(command, ags) {
const child = childProcess.spawn(command, ags);
const stdout = child.stdout ? new BufferList() : '';
const stderr = child.stderr ? new BufferList() : '';
if (child.stdout) {
child.stdout.on('data', data => {
stdout.append(data);
})
}
if (child.stderr) {
child.stderr.on('data', data => {
stderr.append(data);
})
}
const promise = new Promise((resolve, reject) => {
child.on('error', reject);
child.on('exit', code => {
if (code === 0) {
resolve(stdout);
} else {
const err = new Error(`child exited with code ${code}`)
err.code = code;
err.stderr = stderr;
err.stdout = stdout;
reject(err)
}
})
})
promise.child = child;
return promise
}
async function checkCleanWorkingTree() {
console.log(chalk.green('- Checking local repository for uncommited changes'));
let res = await execute('git', ['status']);
if (!res.toString().includes('nothing to commit, working tree clean')) {
throw new Error('Working tree is not clean. Please commit and push your changes.')
}
}
async function deleteLocalTag(tag) {
await execute("git", ["tag", "-d", tag]);
}
async function pullRemote() {
console.log(chalk.green('- Pulling from remote origin'));
await execute('git', ['fetch']);
await execute('git', ['pull', 'origin', '-a']);
}
async function readLocalTags() {
console.log(chalk.green('- Deleting local tags'));
const output = await execute("git", ["tag", "-l"]);
let tags = output.toString().split(/(?:\r\n|\r|\n)/g);
for (let tag of tags) {
if (tag !== '') {
await deleteLocalTag(tag);
}
}
}
async function checkNewTag(tag) {
if (tag !== '') {
const output = await execute("git", ["tag", "-l"]);
let tags = output.toString().split(/(?:\r\n|\r|\n)/g);
return !tags.includes(tag);
} else {
return false;
}
}
async function readNewTag(newAttempt = false) {
return new Promise(async (resolve, _) => {
const output = await execute("git", ["tag", "-l"]);
let tags = output.toString().split(/(?:\r\n|\r|\n)/g);
tags.splice(tags.length - 1, 1);
let latestRelease = '---'
if (tags.length > 0) {
latestRelease = tags[tags.length - 1]
}
let rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question(newAttempt ? 'Version already exists. New version? ' : `Which version do you want to release? (Last release: ${latestRelease}) `, async (tag) => {
if (await checkNewTag(tag)) {
resolve(tag);
rl.close();
} else {
rl.close();
resolve(await readNewTag(true));
}
});
});
}
async function checkDevBranch() {
return new Promise(async (resolve, _) => {
let currentBranch = (await execute('git', ['symbolic-ref', 'HEAD'])).toString().replace('refs/heads/', '').replace('\n', '');
if (currentBranch !== config.devBranch && currentBranch !== releaseBranch && config.masterBranch !== currentBranch) {
console.log(`You are not in the development branch ('${config.devBranch}'). You are in '${currentBranch}'.`);
rl.question("Do you want to checkout the development branch and merge the current branch? (y/n) ", async (answer) => {
if (answer === 'y') {
await execute('git', ['checkout', config.devBranch]);
await execute('git', ['pull']);
await execute('git', ['merge', '--no-ff', currentBranch]);
resolve();
rl.close();
} else {
console.log('Okay, then I will stop here.');
exit(0);
}
});
} else {
resolve();
}
});
}
async function doManualChanges() {
return new Promise((resolve, _) => {
let rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question("You can apply changes to the files now. Press Enter when you are ready to release ... ", async (tag) => {
resolve();
rl.close();
});
})
}
async function switchToReleaseBranch() {
console.log(chalk.green(`- Checking out to release branch ('${releaseBranch}') `));
if (releaseBranch === '') throw new Error('Releasebranch name can not be empty.');
let branches = (await execute('git', ['branch', '-l'])).toString().replace('*', '').replace(/^\s+|\s+$/gm, '').split(/(?:\r\n|\r|\n)/g);
if (branches.includes(releaseBranch)) {
await execute('git', ['checkout', releaseBranch]);
} else {
await execute('git', ['checkout', '-b', releaseBranch]);
}
console.log(chalk.green(`- Merging '${config.devBranch}' into '${releaseBranch}' `));
await execute('git', ['merge', '--no-ff', config.devBranch]);
}
async function bumpPackageVersion(tag) {
console.log(chalk.green('- Bumping version '));
for (let file of config.packages) {
let package = editJsonFile(file);
package.set('version', tag);
package.save();
}
await doManualChanges();
console.log(chalk.green('- Commiting new version '));
await execute('git', ['add', '.']);
await execute('git', ['commit', '-am', `Bump version to ${tag}.`]);
console.log(chalk.green('- Pushing new version '));
await execute('git', ['push', '--set-upstream', 'origin', releaseBranch]);
}
async function mergeReleaseInMaster() {
console.log(chalk.green(`- Merging ${releaseBranch} into master `));
await execute('git', ['checkout', config.masterBranch]);
await execute('git', ['fetch']);
await execute('git', ['pull']);
await execute('git', ['merge', '--no-ff', releaseBranch]);
await execute('git', ['push']);
}
async function createTag(tag) {
console.log(chalk.green(`- Tagging the release `));
await execute('git', ['tag', '-a', tag, '-m', `Release of ${tag}`]);
await execute('git', ['push', 'origin', tag]);
}
async function switchToDevelopBranch() {
console.log(chalk.green('- Switching back to development branch '));
await execute('git', ['checkout', config.devBranch]);
await pullRemote();
console.log(chalk.green(`- Merging '${config.masterBranch}' into '${config.devBranch}' \n`));
await execute('git', ['merge', '--no-ff', `origin/${config.masterBranch}`]);
await execute('git', ['push']);
}
async function main() {
try {
process.stdout.write('\033c');
if (args.includes('-v')) {
console.log(package.version);
exit(0);
}
await execute('clear');
console.log(chalk.bgGreen(`##### Welcome to gRelease! (${package.version}) ##### \n`));
await checkCleanWorkingTree();
await readLocalTags();
await pullRemote();
let newTag = await readNewTag();
releaseBranch = `release/${newTag}`;
await checkDevBranch();
await switchToReleaseBranch();
await bumpPackageVersion(newTag);
await mergeReleaseInMaster();
await createTag(newTag);
await switchToDevelopBranch();
console.log(chalk.bgGreen(`##### Release ${newTag} is ready. ##### \n`));
} catch (e) {
console.log('');
console.log(chalk.bgRed('ERROR'));
console.log(chalk.bgRed(e.message));
if (args.includes('--verbose')) {
console.log(e);
if (e.stdout) console.log(e.stdout.toString());
if (e.stderr) console.log(e.stderr.toString());
}
}
}
main();