forked from live-codes/livecodes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart-release.mjs
More file actions
293 lines (268 loc) · 8.28 KB
/
Copy pathstart-release.mjs
File metadata and controls
293 lines (268 loc) · 8.28 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
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
import { confirm, input, select } from '@inquirer/prompts';
import conventionalChangelog from 'conventional-changelog';
import { execSync } from 'child_process';
import fs from 'fs';
import { createRequire } from 'module';
import prettier from 'prettier';
const require = createRequire(import.meta.url);
const appPkgPath = '../package.json';
const sdkPkgPath = '../src/sdk/package.sdk.json';
const changelogPath = '../CHANGELOG.md';
const appPkg = require(appPkgPath);
const originalAppVersion = appPkg.appVersion;
const sdkPkg = require(sdkPkgPath);
const originalSDKVersion = sdkPkg.version;
const prettierConfig = appPkg.prettier;
let releaseTarget;
const stringify = async (obj) => {
const str = JSON.stringify(obj, null, 2) + '\n';
const formatted = await prettier.format(str, {
parser: 'json',
...prettierConfig,
});
return formatted;
};
const confirmCancel = async (continueFn) => {
if (await confirm({ message: 'Do you want to cancel release and discard all changes?' })) {
execSync(`git reset --hard`);
console.log('Release cancelled!');
process.exit(1);
}
return continueFn();
};
const checkIsDevelop = () => {
const gitBranch = execSync('git rev-parse --abbrev-ref HEAD').toString().replace(/\n/g, '');
if (gitBranch !== 'develop') {
console.log('A release can only be started from the branch: develop');
process.exit(1);
}
};
const checkIsClean = () => {
const gitStatus = execSync('git status -s').toString().replace(/\n/g, '').trim();
if (gitStatus) {
console.log('Please commit changes before starting a release.');
process.exit(1);
}
};
const performChecks = async () => {
checkIsDevelop();
checkIsClean();
};
const selectReleaseTarget = async () => {
releaseTarget = await select({
message: 'Create a release for:',
choices: [
{
name: 'App',
value: 'app',
},
{
name: 'SDK',
value: 'sdk',
},
{
name: 'Cancel',
value: 'cancel',
},
],
});
if (releaseTarget === 'cancel') {
return confirmCancel(selectReleaseTarget);
}
};
const bumpSDKVersion = (oldSDKVersion, sdkBump) => {
if (!sdkBump) return;
let [major, minor, patch] = oldSDKVersion.split('.');
if (sdkBump === 'major') {
major = String(Number(major) + 1);
minor = '0';
patch = '0';
}
if (sdkBump === 'minor') {
minor = String(Number(minor) + 1);
patch = '0';
}
if (sdkBump === 'patch') {
patch = String(Number(patch) + 1);
}
return `${major}.${minor}.${patch}`;
};
const specifyAppVersion = () =>
input({
message: 'Please specify the new App version:',
validate(value) {
const version = value.startsWith('v') ? value.slice(1) : value;
if (isNaN(Number(version))) return false;
return Number(version) > Number(originalAppVersion);
},
});
const specifySDKVersion = () =>
input({
message: 'Please specify the new SDK version:',
validate(value) {
const version = value.startsWith('v')
? value.slice(1)
: value.startsWith('sdk-v')
? value.slice(5)
: value;
const parts = version.split('.');
if (parts.length !== 3) return false;
for (const part of parts) {
if (isNaN(Number(part))) return false;
}
const originalVersionParts = originalSDKVersion.split('.');
if (Number(parts[0]) > Number(originalVersionParts[0])) return true;
if (Number(parts[1]) > Number(originalVersionParts[1])) return true;
if (Number(parts[2]) > Number(originalVersionParts[2])) return true;
return false;
},
});
const getAppBump = async () => {
const suggestedBump = String(Number(originalAppVersion) + 1);
const bump = await select({
message: `App version upgrade: (current: ${originalAppVersion})`,
default: suggestedBump,
choices: [
{
name: suggestedBump,
value: suggestedBump,
},
{
name: 'Specify version',
value: 'specify',
},
{
name: 'Cancel',
value: 'cancel',
},
],
});
if (bump === 'cancel') {
return confirmCancel(getAppBump);
}
return bump;
};
const getSDKBump = async (releaseNotes) => {
const suggestedBump = releaseNotes.includes('### BREAKING CHANGES')
? 'major'
: releaseNotes.includes('### Features')
? 'minor'
: 'patch';
const hint =
suggestedBump === 'major'
? ' (has breaking changes!)'
: suggestedBump === 'minor'
? ' (includes new feature(s))'
: '';
const bump = await select({
message: `Library version upgrade:${hint}`,
default: suggestedBump,
choices: [
{
name: 'Major',
value: 'major',
},
{
name: 'Minor',
value: 'minor',
},
{
name: 'Patch',
value: 'patch',
},
{
name: 'Specify version',
value: 'specify',
},
{
name: 'Cancel',
value: 'cancel',
},
],
});
if (bump === 'cancel') {
return confirmCancel(() => getSDKBump(releaseNotes));
}
return bump;
};
const changeAppVersion = async (releaseNotes) => {
const bump = await getAppBump();
const selectedVersion = bump === 'specify' ? await specifyAppVersion() : bump;
const version = selectedVersion?.startsWith('v') ? selectedVersion.slice(1) : selectedVersion;
const versionName = 'v' + version;
appPkg.appVersion = version;
if (!(await confirm({ message: `Creating App version: ${versionName}\nProceed?` }))) {
return confirmCancel(() => changeAppVersion(releaseNotes));
}
fs.writeFileSync(new URL(appPkgPath, import.meta.url), await stringify(appPkg), 'utf8');
return releaseNotes;
};
const changeSDKVersion = async (releaseNotes) => {
const bump = await getSDKBump(releaseNotes);
const selectedVersion =
bump === 'specify' ? await specifySDKVersion() : bumpSDKVersion(originalSDKVersion, bump);
const version = selectedVersion?.startsWith('v')
? selectedVersion.slice(1)
: selectedVersion?.startsWith('sdk-v')
? selectedVersion.slice(5)
: selectedVersion;
const versionName = 'sdk-v' + version;
sdkPkg.version = version;
if (!(await confirm({ message: `Creating SDK version: ${versionName}\nProceed?` }))) {
return confirmCancel(() => changeSDKVersion(releaseNotes));
}
fs.writeFileSync(new URL(sdkPkgPath, import.meta.url), await stringify(sdkPkg), 'utf8');
return releaseNotes;
};
const changeVersion = async (releaseNotes) =>
releaseTarget === 'app' ? changeAppVersion(releaseNotes) : changeSDKVersion(releaseNotes);
const streamToString = (stream) => {
const chunks = [];
return new Promise((resolve, reject) => {
stream.on('data', (chunk) => chunks.push(Buffer.from(chunk)));
stream.on('error', (err) => reject(err));
stream.on('end', () => resolve(Buffer.concat(chunks).toString('utf8')));
});
};
const getReleaseNotes = async () =>
streamToString(
conventionalChangelog({
preset: 'angular',
}),
);
const writeChangelog = async (releaseNotes) => {
const version = releaseTarget === 'sdk' ? 'sdk-v' + sdkPkg.version : 'v' + appPkg.appVersion;
const changelog = fs.readFileSync(new URL(changelogPath, import.meta.url), 'utf8');
const changelogSeparator = '\n---';
const [changelogHeader, ...prevLogs] = changelog.split(changelogSeparator);
const releaseChangelog =
'\n\n#' + releaseNotes.replace('[0.0.0]', `[${version}]`).replace('v0.0.0', `${version}`);
const newChangelog = [changelogHeader, releaseChangelog, ...prevLogs].join(changelogSeparator);
fs.writeFileSync(new URL(changelogPath, import.meta.url), newChangelog, 'utf8');
const waitForApproval = async () => {
if (!(await confirm({ message: `Change log added to ./CHANGELOG.md\nProceed?` }))) {
return confirmCancel(waitForApproval);
}
return version;
};
return waitForApproval();
};
const pushReleaseBranch = (version) => {
if (!version) {
console.log('Invalid version. Aborting.');
process.exit(1);
}
const branchName = 'releases/' + version;
execSync(`git checkout -b ${branchName}`);
execSync(`git add -A && git commit -m "release: ${version}"`);
execSync(`git push -u origin ${branchName}`);
};
const run = async () => {
performChecks()
.then(selectReleaseTarget)
.then(getReleaseNotes)
.then(changeVersion)
.then(writeChangelog)
.then(pushReleaseBranch);
};
run();