-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathflags-config.ts
446 lines (428 loc) · 14.7 KB
/
flags-config.ts
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
435
436
437
438
439
440
441
442
443
444
445
446
import { HELP } from '../help/sections/manual';
import { ABOUT } from '../help/sections/about';
import { normalizeFlag, type CommandConfig, type FlagConfig } from './parse-args';
import { CHANNEL, SEMVER_VERSION, npmDistTagForChannelAndVersion } from './channel';
import { getGitState, getPublishedChannelInfo } from './git';
import chalk from 'chalk';
import semver from 'semver';
/**
* Like Pick but returns an object type instead of a union type.
*
* @internal
*/
type Subset<T, K extends keyof T> = {
[P in K]: T[P];
};
/**
* Like Typescript Pick but For Runtime.
*
* @internal
*/
export function pick<T extends Record<string, unknown>, K extends keyof T>(obj: T, keys: K[]): Subset<T, K> {
const result = {} as Subset<T, K>;
for (const key of keys) {
result[key] = obj[key];
}
return result;
}
/**
* Like Object.assign (is Object.assign) but ensures each arg and the result conform to T
*
* @internal
*/
export function merge<T>(...args: T[]): T {
return Object.assign({}, ...args);
}
export const publish_flags_config: FlagConfig = {
help: {
name: 'Help',
flag: 'help',
flag_aliases: ['h', 'm'],
flag_mispellings: [
'desc',
'describe',
'doc',
'docs',
'dsc',
'guide',
'halp',
'he',
'hel',
'hlp',
'man',
'mn',
'usage',
],
type: Boolean,
default_value: false,
description: 'Print this usage manual.',
examples: ['./publish/index.ts --help'],
},
channel: {
name: 'Channel',
flag: 'channel',
type: String,
default_value: async (options: Map<string, string | number | boolean | null>) => {
const gitState = await getGitState(options);
return gitState.expectedChannel;
},
validate: (value: unknown) => {
if (!['lts', 'release', 'beta', 'canary', 'lts-prev', 'release-prev'].includes(value as string)) {
throw new Error(`Channel must be one of lts, release, beta, canary, lts-prev, or release-prev. Got ${value}`);
}
},
description:
'EmberData always publishes to a "release channel".\nTypically this will be one of lts, release, beta, or canary.\nWhen publishing a new version of a non-current lts or non-current release, the channel should be "lts-prev" or "release-prev"',
examples: ['./publish/index.ts lts', './publish/index.ts publish lts', './publish/index.ts --channel=lts'],
positional: true,
positional_index: 0,
// required: true,
},
dry_run: {
name: 'Dry Run',
flag: 'dry_run',
flag_mispellings: ['dry'],
default_value: false,
description: 'Do not actually publish, just print what would be done',
type: Boolean,
examples: ['./publish/index.ts --channel=stable --dry_run'],
},
dangerously_force: {
name: 'Force Release',
flag: 'dangerously_force',
flag_mispellings: [],
default_value: false,
description: 'Ignore safety checks and attempt to create and publish a release anyway',
type: Boolean,
examples: ['./publish/index.ts --channel=stable --dangerously_force'],
},
tag: {
name: 'NPM Distribution Tag',
flag: 'tag',
flag_aliases: ['t'],
flag_mispellings: ['dist_tag'],
type: String,
description: '',
examples: [],
default_value: async (options: Map<string, string | number | boolean | null>) => {
const gitInfo = await getGitState(options);
return npmDistTagForChannelAndVersion(gitInfo.expectedChannel, gitInfo.rootVersion);
},
validate: async (value: unknown, options: Map<string, string | number | boolean | null>) => {
const channel = options.get('channel') as CHANNEL;
const gitInfo = await getGitState(options);
const expectedTag = npmDistTagForChannelAndVersion(channel, gitInfo.rootVersion);
if (value !== expectedTag) {
if (!options.get('dangerously_force')) {
throw new Error(
`Expected npm dist-tag ${expectedTag} for channel ${channel} on branch ${gitInfo.branch} with version ${gitInfo.rootVersion} but got ${value}`
);
} else {
console.log(
chalk.red(
`\t🚨 Expected npm dist-tag ${expectedTag} for channel ${channel} on branch ${
gitInfo.branch
} with version ${gitInfo.rootVersion} but got ${value}\n\t\t${chalk.yellow(
'⚠️ Continuing Due to use of --dangerously-force'
)}`
)
);
}
}
},
},
increment: {
name: 'Version Increment',
flag: 'increment',
flag_aliases: ['i', 'b'],
flag_mispellings: ['inc', 'bump', 'incr'],
description: 'kind of version bump to perform, if any.\nMust be one of "major", "minor", or "patch"',
type: String,
examples: [],
default_value: 'patch',
validate: (value: unknown) => {
if (!['major', 'minor', 'patch'].includes(value as string)) {
throw new Error(`the 'increment' option must be one of 'major', 'minor' or 'patch'`);
}
},
},
commit: {
name: 'Commit',
flag: 'commit',
flag_aliases: ['c'],
flag_mispellings: ['cm', 'comit', 'changelog', 'commit_changelog'],
description: 'Whether to commit the changes to the changelogs',
type: Boolean,
examples: [],
default_value: true,
},
// branch: {
// name: 'Update Local and Upstream Branch',
// flag: 'update_branch',
// flag_aliases: [],
// flag_mispellings: ['branch'],
// description:
// 'Whether to update the local and upstream branch according to the standard release channel flow. For release this will reset the branch to the current beta. For beta this will reset the branch to the current canary. For lts this will reset the branch to the current release. For lts-prev this is not a valid option.',
// type: Boolean,
// examples: [],
// default_value: false,
// },
from: {
name: 'From Version',
flag: 'from',
flag_aliases: ['v'],
flag_mispellings: ['ver'],
description: 'The version from which to increment and build a strategy',
type: String,
examples: [],
default_value: async (options: Map<string, string | number | boolean | null>) => {
const channel = options.get('channel') as CHANNEL;
if (channel === 'lts' || channel === 'release' || channel === 'beta' || channel === 'canary') {
const version = (await getPublishedChannelInfo())[channel === 'release' ? 'latest' : channel];
const currentVersion = (await getGitState(options)).rootVersion;
if (version !== currentVersion) {
return version;
}
return '';
}
return '';
},
validate: async (value: unknown) => {
if (typeof value !== 'string') {
throw new Error(`Expected a string but got ${value}`);
}
if (value.startsWith('v')) {
throw new Error(`Version passed to promote should not start with 'v'`);
}
if (semver.valid(value) === null) {
throw new Error(`Version passed to promote is not a valid semver version`);
}
},
},
upstream: {
name: 'Update Upstream Branch',
flag: 'upstream',
flag_aliases: ['u'],
flag_mispellings: ['upstraem', 'up'],
description: 'Whether to push the commits and tag upstream',
type: Boolean,
examples: [],
default_value: true,
},
pack: {
name: 'Pack Packages',
flag: 'pack',
flag_aliases: ['p'],
flag_mispellings: ['skip-pack'],
description: 'whether to pack tarballs for the public packages',
type: Boolean,
examples: [],
default_value: true,
},
publish: {
name: 'Publish Packages to NPM',
flag: 'publish',
flag_aliases: ['r'],
flag_mispellings: ['skip-publish', 'skip-release', 'release'],
description: 'whether to publish the packed tarballs to the npm registry',
type: Boolean,
examples: [],
default_value: true,
},
};
export const release_notes_flags_config: FlagConfig = merge(
pick(publish_flags_config, ['help', 'increment', 'dry_run', 'dangerously_force', 'tag', 'channel', 'upstream']),
{
commit: {
name: 'Commit',
flag: 'commit',
flag_aliases: ['c'],
flag_mispellings: ['cm', 'comit'],
description: 'Whether to commit the changes to the changelogs',
type: Boolean,
examples: [],
default_value: true,
},
from: {
name: 'From Version',
flag: 'from',
flag_aliases: ['v'],
flag_mispellings: ['ver', 'release', 'rel'],
description: 'The version from which to increment and build a strategy',
type: String,
examples: [],
default_value: async (options: Map<string, string | number | boolean | null>) => {
return (await getPublishedChannelInfo()).latest;
},
validate: async (value: unknown) => {
if (typeof value !== 'string') {
throw new Error(`Expected a string but got ${value}`);
}
if (value.startsWith('v')) {
throw new Error(`Version passed to promote should not start with 'v'`);
}
if (semver.valid(value) === null) {
throw new Error(`Version passed to promote is not a valid semver version`);
}
const versionInfo = semver.parse(value);
if (versionInfo?.prerelease?.length) {
throw new Error(`Version passed to promote cannot be prerelease version`);
}
},
},
}
);
export const promote_flags_config: FlagConfig = merge(
pick(publish_flags_config, ['help', 'dry_run', 'dangerously_force', 'upstream']),
{
version: {
name: 'Version',
flag: 'version',
flag_aliases: ['v'],
flag_mispellings: ['ver', 'release', 'rel'],
description: 'The version to promote to LTS',
type: String,
examples: [],
default_value: async (options: Map<string, string | number | boolean | null>) => {
return (await getPublishedChannelInfo()).latest;
},
validate: async (value: unknown) => {
if (typeof value !== 'string') {
throw new Error(`Expected a string but got ${value}`);
}
if (value.startsWith('v')) {
throw new Error(`Version passed to promote should not start with 'v'`);
}
if (semver.valid(value) === null) {
throw new Error(`Version passed to promote is not a valid semver version`);
}
const versionInfo = semver.parse(value);
if (versionInfo?.prerelease?.length) {
throw new Error(`Version passed to promote cannot be prerelease version`);
}
},
},
tag: {
name: 'NPM Distribution Tag',
flag: 'tag',
flag_aliases: ['t'],
flag_mispellings: ['dist_tag'],
type: String,
description: '',
examples: [],
default_value: async (options: Map<string, string | number | boolean | null>) => {
const version = options.get('version') as SEMVER_VERSION;
const existing = await getPublishedChannelInfo();
if (existing.latest === version) {
return 'lts';
} else {
return npmDistTagForChannelAndVersion('lts-prev', version);
}
},
validate: async (value: unknown, options: Map<string, string | number | boolean | null>) => {
let version = options.get('version') as SEMVER_VERSION;
const existing = await getPublishedChannelInfo();
if (!version) {
version = (await getPublishedChannelInfo()).latest;
}
if (value !== 'lts') {
// older lts channels should match lts-<major>-<minor>
if (typeof value !== 'string' || !value.startsWith('lts-')) {
throw new Error(`Expected a tag starting with "lts-" but got ${value}`);
}
const expected = npmDistTagForChannelAndVersion('lts-prev', version);
if (expected !== value) {
throw new Error(`Expected tag lts or ${expected} for version ${version} but got ${value}`);
}
}
if (existing[value] === version) {
throw new Error(`Version ${version} is already published to ${value}`);
}
const current = existing[value];
if (current && semver.lt(version, current)) {
throw new Error(`Version ${version} is less than the latest version ${current}`);
}
},
},
}
);
export const command_config: CommandConfig = {
help: {
name: 'Help',
cmd: 'help',
description: 'Output This Manual',
alt: Array.from(HELP),
example: '$ bun release help',
},
exec: {
name: 'Execute Command',
cmd: 'exec',
description:
'Executes another release command with the provided arguments, filtering out any args with undefined values.',
alt: [],
example: '$ bun release exec promote --version=5.3.0 --tag=lts',
},
about: {
name: 'About',
cmd: 'about',
description: 'Print Information About This Script',
alt: Array.from(ABOUT),
example: '$ bun release about',
},
release_notes: {
name: 'Release Notes',
cmd: 'release-notes',
alt: ['cl', 'changes', 'history', 'notes', 'releasenotes', 'changelog', 'log'],
description: `Generate release notes for the next release.`,
options: release_notes_flags_config,
example: '$ bun release cl',
},
latest_for: {
name: 'Latest For',
cmd: 'latest-for',
description: 'Print the latest version for a given channel',
alt: ['latest'],
example: '$ bun release latest-for beta',
},
promote: {
name: 'Promote to LTS',
cmd: 'promote',
description:
'Promote a prior release to LTS.\nThis will upate the dist-tags on npm without publishing any new versions or tarballs',
alt: ['retag', 'lts', 'lts-promote'],
options: promote_flags_config,
example: [
'$ bun release promote',
'$ bun release promote --version=5.3.0 --tag=lts',
'$ bun release promote 4.12.5 --tag=lts-4-12',
],
},
default: {
name: 'Publish',
cmd: 'publish',
default: true,
description:
'Publish a new version of EmberData to the specified channel.\nRequires a configured ye<<NODE_AUTH_TOKEN>> with npm access to all associated scopes and packages,\nor the ability to generate an OTP token for the same.',
options: publish_flags_config,
example: ['$ bun release', '$ bun release publish'],
},
};
export function getCommands() {
const keys = Object.keys(command_config);
const commands = new Map<string, string>();
keys.forEach((key) => {
const cmd = normalizeFlag(key);
commands.set(cmd, cmd);
commands.set(command_config[key].cmd, cmd);
if (command_config[cmd].alt) {
command_config[cmd].alt!.forEach((alt: string) => {
const alternate = normalizeFlag(alt);
if (commands.has(alternate) && commands.get(alternate) !== cmd) {
throw new Error(`Duplicate command alias ${alternate} for ${cmd} and ${commands.get(alternate)}`);
}
commands.set(alternate, cmd);
});
}
});
return commands;
}