@fairgarden/distribution 0.1.0-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (58) hide show
  1. package/Readme.md +94 -0
  2. package/dist/add-module.d.ts +58 -0
  3. package/dist/add-module.d.ts.map +1 -0
  4. package/dist/add-module.js +250 -0
  5. package/dist/add-module.js.map +1 -0
  6. package/dist/canary.d.ts +58 -0
  7. package/dist/canary.d.ts.map +1 -0
  8. package/dist/canary.js +117 -0
  9. package/dist/canary.js.map +1 -0
  10. package/dist/cli.d.ts +3 -0
  11. package/dist/cli.d.ts.map +1 -0
  12. package/dist/cli.js +747 -0
  13. package/dist/cli.js.map +1 -0
  14. package/dist/config-edit.d.ts +18 -0
  15. package/dist/config-edit.d.ts.map +1 -0
  16. package/dist/config-edit.js +166 -0
  17. package/dist/config-edit.js.map +1 -0
  18. package/dist/extends.d.ts +59 -0
  19. package/dist/extends.d.ts.map +1 -0
  20. package/dist/extends.js +163 -0
  21. package/dist/extends.js.map +1 -0
  22. package/dist/extract.d.ts +31 -0
  23. package/dist/extract.d.ts.map +1 -0
  24. package/dist/extract.js +177 -0
  25. package/dist/extract.js.map +1 -0
  26. package/dist/git-url.d.ts +19 -0
  27. package/dist/git-url.d.ts.map +1 -0
  28. package/dist/git-url.js +55 -0
  29. package/dist/git-url.js.map +1 -0
  30. package/dist/index.d.ts +12 -0
  31. package/dist/index.d.ts.map +1 -0
  32. package/dist/index.js +7 -0
  33. package/dist/index.js.map +1 -0
  34. package/dist/overrides.d.ts +32 -0
  35. package/dist/overrides.d.ts.map +1 -0
  36. package/dist/overrides.js +81 -0
  37. package/dist/overrides.js.map +1 -0
  38. package/dist/readme.d.ts +58 -0
  39. package/dist/readme.d.ts.map +1 -0
  40. package/dist/readme.js +299 -0
  41. package/dist/readme.js.map +1 -0
  42. package/dist/release.d.ts +140 -0
  43. package/dist/release.d.ts.map +1 -0
  44. package/dist/release.js +409 -0
  45. package/dist/release.js.map +1 -0
  46. package/dist/scaffold.d.ts +46 -0
  47. package/dist/scaffold.d.ts.map +1 -0
  48. package/dist/scaffold.js +389 -0
  49. package/dist/scaffold.js.map +1 -0
  50. package/dist/submodules.d.ts +95 -0
  51. package/dist/submodules.d.ts.map +1 -0
  52. package/dist/submodules.js +225 -0
  53. package/dist/submodules.js.map +1 -0
  54. package/dist/workflows.d.ts +57 -0
  55. package/dist/workflows.d.ts.map +1 -0
  56. package/dist/workflows.js +358 -0
  57. package/dist/workflows.js.map +1 -0
  58. package/package.json +51 -0
package/dist/cli.js ADDED
@@ -0,0 +1,747 @@
1
+ #!/usr/bin/env node
2
+ import path from 'node:path';
3
+ import { execFileSync } from 'node:child_process';
4
+ import { mkdir } from 'node:fs/promises';
5
+ import { addModule } from "./add-module.js";
6
+ import { extractModule } from "./extract.js";
7
+ import { writeReadmes } from "./readme.js";
8
+ import { writeWorkflows } from "./workflows.js";
9
+ import { writeOverrides } from "./overrides.js";
10
+ import { checkReleasable, planRelease, readRelease, release, reportToActions as reportRelease, startPrerelease, } from "./release.js";
11
+ import { reportToActions as reportCanary, stampCanary } from "./canary.js";
12
+ import { describeRemote, isSsh, toHttps } from "./git-url.js";
13
+ import { distributionRepo, isEmpty, moduleRepo, monolithRepo, write, } from "./scaffold.js";
14
+ import { describeViolations, inspectExtends } from "./extends.js";
15
+ import { inspect, isPublic, moveTo, repositoryRoot, setUrl, submodules, target, } from "./submodules.js";
16
+ // Piping into `head` closes stdout early; that is ordinary use, not a failure.
17
+ for (const stream of [process.stdout, process.stderr]) {
18
+ stream.on('error', (error) => {
19
+ if (error.code === 'EPIPE')
20
+ process.exit(0);
21
+ throw error;
22
+ });
23
+ }
24
+ const USAGE = `Usage: fg-dist <command> [options]
25
+
26
+ Commands:
27
+ init <kind> [dir] Scaffold a repository; kind is "distribution", "monolith" or "module"
28
+ add-module <url> Add a module repository as a submodule and link it
29
+ extract <path> Turn a directory here into its own repository and submodule
30
+ use-https [name...] Rewrite submodule urls from ssh to https
31
+ readme Record versions in the readmes; run in a distribution or a module
32
+ workflows Give every module a publishing workflow
33
+ overrides Point the workspace at this distribution's own checkouts
34
+ canary Stamp a canary version into the manifest, for CI
35
+ release Move a module on from the release it just published
36
+ prerelease Start the next line on a branch, leaving main where it is
37
+ check Fail when this ships anything older than what it extends
38
+ sync Report which modules have newer versions available
39
+ bump [name...] Move modules to their newest non-major version
40
+
41
+ Options:
42
+ --cwd <dir> Repository directory (default: the working directory)
43
+ --major Allow major upgrades (bump); the next major (release, prerelease)
44
+ --minor Release the next minor, or branch one (release, prerelease)
45
+ --patch Release the next patch, staying on this line (release)
46
+ --id <name> Prerelease identifier: alpha, beta, rc (release, prerelease)
47
+ --no-fetch Use the refs already fetched (sync, bump)
48
+ --dry-run Report what would change without changing it
49
+ --check Verify instead of writing (readme, release, overrides)
50
+ --dry-run does the same for workflows, readme and overrides
51
+ --name <name> Package name for "init", mount name for add-module
52
+ --at <dir> Where add-module puts the submodule (default: apps/<name>)
53
+ --no-git Skip "git init" when scaffolding
54
+ --url <git-url> Remote the scaffolded repository will live at (init)
55
+ --extends <name> Distribution the scaffolded one extends (init distribution)
56
+ --separate Scaffold a distribution whose apps deploy separately
57
+ --ssh Record a submodule's URL as given, without rewriting it
58
+ --no-verify Skip checking whether the https urls can be cloned
59
+ --no-tag Do not tag the extracted module with its declared version
60
+ --force Overwrite workflows that are already there (workflows)
61
+ --no-push Leave the release branches local, and open no pull request
62
+ `;
63
+ const parseArgs = (argv) => {
64
+ const args = {
65
+ command: undefined,
66
+ names: [],
67
+ cwd: process.cwd(),
68
+ check: false,
69
+ major: false,
70
+ fetch: true,
71
+ dryRun: false,
72
+ name: undefined,
73
+ at: undefined,
74
+ git: true,
75
+ url: undefined,
76
+ extends: undefined,
77
+ separate: false,
78
+ ssh: false,
79
+ verify: true,
80
+ tag: true,
81
+ force: false,
82
+ push: true,
83
+ bump: undefined,
84
+ id: undefined,
85
+ };
86
+ for (let index = 0; index < argv.length; index += 1) {
87
+ const arg = argv[index];
88
+ if (arg === '--cwd') {
89
+ args.cwd = path.resolve(argv[++index] ?? '.');
90
+ }
91
+ else if (arg === '--check') {
92
+ args.check = true;
93
+ }
94
+ else if (arg === '--major') {
95
+ args.major = true;
96
+ args.bump = 'major';
97
+ }
98
+ else if (arg === '--minor') {
99
+ args.bump = 'minor';
100
+ }
101
+ else if (arg === '--patch') {
102
+ args.bump = 'patch';
103
+ }
104
+ else if (arg === '--id') {
105
+ args.id = argv[++index];
106
+ }
107
+ else if (arg === '--no-fetch') {
108
+ args.fetch = false;
109
+ }
110
+ else if (arg === '--dry-run') {
111
+ args.dryRun = true;
112
+ }
113
+ else if (arg === '--name') {
114
+ args.name = argv[++index];
115
+ }
116
+ else if (arg === '--at') {
117
+ args.at = argv[++index];
118
+ }
119
+ else if (arg === '--no-git') {
120
+ args.git = false;
121
+ }
122
+ else if (arg === '--url') {
123
+ args.url = argv[++index];
124
+ }
125
+ else if (arg === '--extends') {
126
+ args.extends = argv[++index];
127
+ }
128
+ else if (arg === '--separate') {
129
+ args.separate = true;
130
+ }
131
+ else if (arg === '--ssh') {
132
+ args.ssh = true;
133
+ }
134
+ else if (arg === '--no-verify') {
135
+ args.verify = false;
136
+ }
137
+ else if (arg === '--no-tag') {
138
+ args.tag = false;
139
+ }
140
+ else if (arg === '--force') {
141
+ args.force = true;
142
+ }
143
+ else if (arg === '--no-push') {
144
+ args.push = false;
145
+ }
146
+ else if (!arg.startsWith('-')) {
147
+ if (args.command)
148
+ args.names.push(arg);
149
+ else
150
+ args.command = arg;
151
+ }
152
+ else {
153
+ throw new Error(`Unrecognized argument: ${arg}`);
154
+ }
155
+ }
156
+ return args;
157
+ };
158
+ /** One line per module, aligned, saying what is available. */
159
+ const describeState = (state) => {
160
+ const { current, available, untagged, dirty } = state;
161
+ const parts = [];
162
+ const at = current ? (state.exact ? current : `${current}+`) : 'untagged';
163
+ parts.push(at);
164
+ if (available.length > 0) {
165
+ const kinds = ['patch', 'minor', 'major']
166
+ .filter((kind) => state.upgrades[kind])
167
+ .map((kind) => `${state.upgrades[kind]} (${kind})`);
168
+ parts.push(`-> ${kinds.join(', ')}`);
169
+ }
170
+ else if (current) {
171
+ parts.push('up to date');
172
+ }
173
+ else {
174
+ parts.push('no version tags');
175
+ }
176
+ if (untagged > 0)
177
+ parts.push(`${untagged} untagged commit${untagged === 1 ? '' : 's'}`);
178
+ if (dirty)
179
+ parts.push('uncommitted changes');
180
+ if (state.fetched === false)
181
+ parts.push('could not fetch');
182
+ if (isSsh(state.submodule.url))
183
+ parts.push('ssh url');
184
+ return parts.join(' ');
185
+ };
186
+ /** Submodules of the repository holding `cwd`, optionally filtered by name. */
187
+ const modulesFor = (cwd, names) => {
188
+ const root = repositoryRoot(cwd);
189
+ if (!root)
190
+ throw new Error(`${cwd} is not inside a git repository.`);
191
+ const all = submodules(root);
192
+ if (all.length === 0) {
193
+ throw new Error(`${root} has no submodules to sync.`);
194
+ }
195
+ if (names.length === 0)
196
+ return { root, modules: all };
197
+ const modules = all.filter((module) => names.includes(module.name) || names.includes(module.relativePath));
198
+ const missing = names.filter((name) => !all.some((m) => m.name === name || m.relativePath === name));
199
+ if (missing.length > 0) {
200
+ throw new Error(`No such submodule: ${missing.join(', ')}. ` +
201
+ `Known: ${all.map((m) => m.name).join(', ')}.`);
202
+ }
203
+ return { root, modules };
204
+ };
205
+ const pad = (values) => values.reduce((widest, value) => Math.max(widest, value.length), 0);
206
+ const main = async () => {
207
+ const args = parseArgs(process.argv.slice(2));
208
+ if (!args.command || args.command === 'help') {
209
+ process.stdout.write(USAGE);
210
+ return args.command ? 0 : 1;
211
+ }
212
+ if (args.command === 'init') {
213
+ const [kind, where] = args.names;
214
+ if (kind !== 'distribution' && kind !== 'monolith' && kind !== 'module') {
215
+ process.stderr.write('init needs a kind: "distribution", "monolith" or "module".\n');
216
+ return 1;
217
+ }
218
+ const target = path.resolve(args.cwd, where ?? '.');
219
+ await mkdir(target, { recursive: true });
220
+ if (!(await isEmpty(target))) {
221
+ process.stderr.write(`${target} is not empty; refusing to scaffold over it.\n`);
222
+ return 1;
223
+ }
224
+ const name = args.name ?? path.basename(target);
225
+ // A module is consumed as a submodule, so record the URL it will be
226
+ // cloned from in the form a keyless clone can use.
227
+ const origin = args.url ? (args.ssh ? args.url : toHttps(args.url)) : undefined;
228
+ const files = kind === 'distribution'
229
+ ? distributionRepo(name, origin, args.extends, { monolith: !args.separate })
230
+ : kind === 'monolith'
231
+ ? monolithRepo(name, origin)
232
+ : moduleRepo(name, origin);
233
+ const written = await write(target, files);
234
+ if (args.git) {
235
+ try {
236
+ execFileSync('git', ['init', '--quiet'], { cwd: target, stdio: 'ignore' });
237
+ if (origin) {
238
+ execFileSync('git', ['remote', 'add', 'origin', origin], {
239
+ cwd: target,
240
+ stdio: 'ignore',
241
+ });
242
+ }
243
+ }
244
+ catch {
245
+ process.stderr.write('Could not run git init; scaffolding is still written.\n');
246
+ }
247
+ }
248
+ for (const file of written)
249
+ process.stdout.write(` ${file}\n`);
250
+ process.stdout.write(`\n${kind} scaffolded in ${target}.\n`);
251
+ if (origin) {
252
+ process.stdout.write(`origin is ${origin}\n`);
253
+ if (args.url && origin !== args.url) {
254
+ process.stdout.write(`(rewritten from ${args.url}; a submodule is cloned without an SSH key)\n`);
255
+ }
256
+ }
257
+ else {
258
+ process.stdout.write('No remote set. Pass --url so a monolith can add this as a submodule.\n');
259
+ }
260
+ process.stdout.write(kind === 'module'
261
+ ? 'Next: pnpm install, then commit and push so a distribution can add it.\n'
262
+ : 'Next: pnpm install, then `fg-dist add-module <url>` to ship a module.\n');
263
+ return 0;
264
+ }
265
+ if (args.command === 'add-module') {
266
+ const [url] = args.names;
267
+ if (!url) {
268
+ process.stderr.write('add-module needs a repository url.\n');
269
+ return 1;
270
+ }
271
+ const result = await addModule(args.cwd, url, {
272
+ at: args.at,
273
+ mount: args.name,
274
+ ssh: args.ssh,
275
+ });
276
+ process.stdout.write(`Added ${result.relativePath} from ${result.url}\n`);
277
+ if (result.packageName && result.version) {
278
+ process.stdout.write(`Shipping ${result.packageName}@${result.version}, as the checkout declares it\n`);
279
+ }
280
+ if (result.rewritten) {
281
+ process.stdout.write(`(rewritten from ${url}; a submodule is cloned without an SSH key)\n`);
282
+ }
283
+ else if (args.ssh && isSsh(result.url)) {
284
+ process.stdout.write(`(recorded as SSH, so only clones with a key for ${describeRemote(result.url)} can build this)\n`);
285
+ }
286
+ if (result.linked && result.monolithPackageJson) {
287
+ process.stdout.write(`Linked through the workspace in ` +
288
+ `${path.relative(args.cwd, result.monolithPackageJson)}\n`);
289
+ }
290
+ else if (!result.packageName) {
291
+ process.stdout.write('It has no package.json, so nothing was linked.\n');
292
+ }
293
+ if (result.overrides) {
294
+ process.stdout.write('Workspace overrides updated, so this resolves from the tree rather than npm\n');
295
+ }
296
+ else if (result.overridesError) {
297
+ process.stderr.write(`\nThe module is added, but the workspace overrides were not updated:\n` +
298
+ ` ${result.overridesError}\n` +
299
+ 'Run `fg-dist overrides` once that is sorted.\n');
300
+ }
301
+ if (result.mounted && result.monolithConfig) {
302
+ process.stdout.write(`Mounted at /${result.mount} in ${path.relative(args.cwd, result.monolithConfig)}\n`);
303
+ }
304
+ else if (result.mountError && result.monolithPackageJson) {
305
+ process.stderr.write(`\nCould not mount it: ${result.mountError}\n` +
306
+ `Add this to the monolith's Next config yourself:\n` +
307
+ ` ${result.mount}: '${result.packageName ?? result.relativePath}',\n`);
308
+ }
309
+ else if (!result.isApp) {
310
+ // A package is shared by the apps, not served at a path of its own.
311
+ process.stdout.write('It has no routes, so it is a package the apps depend on rather than one to mount.\n');
312
+ }
313
+ else if (!result.monolithPackageJson) {
314
+ // A distribution too complex to serve from one deployment has no
315
+ // monolith; each app under apps/ is deployed on its own.
316
+ process.stdout.write('No monolith app here, so it is shipped but not mounted. Deploy it on its own.\n');
317
+ }
318
+ else if (result.monolithConfig) {
319
+ process.stdout.write(`Already mounted at /${result.mount}.\n`);
320
+ }
321
+ process.stdout.write(result.monolithPackageJson
322
+ ? '\nRun pnpm install and `fg-monolith merge-package-json`.\n'
323
+ : '\nRun pnpm install.\n');
324
+ return 0;
325
+ }
326
+ if (args.command === 'readme') {
327
+ const root = repositoryRoot(args.cwd);
328
+ if (!root)
329
+ throw new Error(`${args.cwd} is not inside a git repository.`);
330
+ const { updates, modules, kind } = await writeReadmes(root, {
331
+ check: args.check || args.dryRun,
332
+ });
333
+ const changed = updates.filter((update) => update.changed);
334
+ for (const module of modules) {
335
+ process.stdout.write(`${module.relativePath} ${module.name}@${module.version}\n`);
336
+ }
337
+ if (changed.length === 0) {
338
+ process.stdout.write(kind === 'module'
339
+ ? 'This module\'s readme already states its version.\n'
340
+ : '\nEvery readme already states its version.\n');
341
+ return 0;
342
+ }
343
+ if (args.check) {
344
+ process.stderr.write(`\n${changed.length} readme(s) are out of date:\n${changed
345
+ .map((update) => ` ${path.relative(root, update.file)}`)
346
+ .join('\n')}\nRun \`fg-dist readme\` to update them.\n`);
347
+ return 1;
348
+ }
349
+ process.stdout.write(`\nUpdated:\n${changed
350
+ .map((update) => ` ${path.relative(root, update.file)}`)
351
+ .join('\n')}\n`);
352
+ return 0;
353
+ }
354
+ if (args.command === 'workflows') {
355
+ const root = repositoryRoot(args.cwd);
356
+ if (!root)
357
+ throw new Error(`${args.cwd} is not inside a git repository.`);
358
+ const updates = await writeWorkflows(root, {
359
+ force: args.force,
360
+ dryRun: args.dryRun,
361
+ });
362
+ if (updates.length === 0) {
363
+ process.stdout.write('No modules here. Run this in a distribution, which is where the modules are.\n');
364
+ return 0;
365
+ }
366
+ const written = updates.filter((update) => !update.skipped);
367
+ for (const update of updates) {
368
+ process.stdout.write(update.skipped
369
+ ? `${update.relativePath} has one already\n`
370
+ : `${update.relativePath} ${update.files.join(', ')}\n`);
371
+ }
372
+ if (written.length === 0) {
373
+ process.stdout.write('\nEvery module already publishes. Pass --force to overwrite the workflows.\n');
374
+ return 0;
375
+ }
376
+ const unprivated = written.filter((update) => update.unprivated);
377
+ const missingFiles = written.filter((update) => update.missingFiles);
378
+ if (args.dryRun) {
379
+ process.stdout.write(`\n${written.length} module(s) would be written. Nothing changed.\n`);
380
+ return 0;
381
+ }
382
+ if (unprivated.length > 0) {
383
+ process.stdout.write(`\nTook "private": true off ${unprivated.map((u) => u.relativePath).join(', ')} — ` +
384
+ 'a private package cannot publish.\n');
385
+ }
386
+ const missingAccess = written.filter((update) => update.missingAccess);
387
+ if (missingAccess.length > 0) {
388
+ process.stdout.write(`\nNo "publishConfig.access" in ${missingAccess.map((u) => u.relativePath).join(', ')}.\n` +
389
+ 'Not set here, because npm treats an explicit value as an instruction to\n' +
390
+ 'change an existing package\'s visibility. Say which it is yourself:\n' +
391
+ ' "publishConfig": { "access": "public" } — and provenance is generated\n' +
392
+ ' "publishConfig": { "access": "restricted" } — and it is not\n');
393
+ }
394
+ if (missingFiles.length > 0) {
395
+ process.stdout.write(`\nNo "files" list in ${missingFiles.map((u) => u.relativePath).join(', ')}.\n` +
396
+ 'npm will ship whatever is in the directory. Say what belongs in the tarball:\n' +
397
+ ' a module consumed as source ships "app", "lib", "public";\n' +
398
+ ' one that builds ships "dist".\n');
399
+ }
400
+ process.stdout.write(`\nWrote a workflow into ${written.length} module(s).\n` +
401
+ 'Each one is a separate repository, so commit and push them individually.\n' +
402
+ 'npm trusted publishing has to be configured per package before the first run:\n' +
403
+ ' publish once from a workstation, then point the trusted publisher at\n' +
404
+ ' .github/workflows/publish.yml in that module\'s repository.\n');
405
+ return 0;
406
+ }
407
+ if (args.command === 'overrides') {
408
+ const root = repositoryRoot(args.cwd);
409
+ if (!root)
410
+ throw new Error(`${args.cwd} is not inside a git repository.`);
411
+ const { file, names, changed, missing } = await writeOverrides(root, {
412
+ check: args.check || args.dryRun,
413
+ });
414
+ if (missing) {
415
+ process.stderr.write(`There is no ${path.relative(root, file) || 'pnpm-workspace.yaml'} here. ` +
416
+ 'A distribution needs one before its modules can be overridden.\n');
417
+ return 1;
418
+ }
419
+ if (names.length === 0) {
420
+ process.stdout.write('No modules here. Run this in a distribution, which is what does the overriding.\n');
421
+ return 0;
422
+ }
423
+ for (const name of names)
424
+ process.stdout.write(` ${name}\n`);
425
+ if (!changed) {
426
+ process.stdout.write(`\n${path.relative(root, file)} already covers every module.\n`);
427
+ return 0;
428
+ }
429
+ if (args.dryRun) {
430
+ process.stdout.write(`\n${path.relative(root, file)} would be updated.\n`);
431
+ return 0;
432
+ }
433
+ if (args.check) {
434
+ process.stderr.write(`\n${path.relative(root, file)} does not match the modules here.\n` +
435
+ 'Run `fg-dist overrides` to update it.\n');
436
+ return 1;
437
+ }
438
+ process.stdout.write(`\nUpdated ${path.relative(root, file)}. Run \`pnpm install\` to take it up.\n`);
439
+ return 0;
440
+ }
441
+ if (args.command === 'canary') {
442
+ const root = repositoryRoot(args.cwd) ?? args.cwd;
443
+ const result = await stampCanary(root, { dryRun: args.dryRun });
444
+ await reportCanary(result);
445
+ process.stdout.write(result.skip
446
+ ? `${result.name}@${result.version} is already the canary for ${result.sha}.\n`
447
+ : `${args.dryRun ? 'Would stamp' : 'Stamped'} ${result.name}@${result.version}\n`);
448
+ return 0;
449
+ }
450
+ if (args.command === 'release') {
451
+ const root = repositoryRoot(args.cwd);
452
+ if (!root)
453
+ throw new Error(`${args.cwd} is not inside a git repository.`);
454
+ if (args.check) {
455
+ const releasable = await checkReleasable(root);
456
+ await reportRelease(releasable);
457
+ process.stdout.write(`${releasable.name}@${releasable.version} is not on npm; ready to release as ` +
458
+ `${releasable.tag}.\n`);
459
+ return 0;
460
+ }
461
+ // Which way main moves on is a judgement about what changed, so it is asked
462
+ // for rather than guessed at. Showing all three is the cheapest way to ask.
463
+ if (!args.bump) {
464
+ const { released, tagged, base } = await readRelease(root, { bump: 'minor' });
465
+ if (!released.includes('-')) {
466
+ process.stdout.write(`Released ${released}, ${tagged ? `tagged ${base}` : 'which is not tagged here'}\n\n` +
467
+ 'Which way does main move on?\n\n');
468
+ for (const bump of ['patch', 'minor', 'major']) {
469
+ const plan = planRelease(released, { bump });
470
+ process.stdout.write(` --${bump.padEnd(6)} ${plan.next.version.padEnd(8)} ` +
471
+ (plan.maintenance
472
+ ? `${plan.maintenance.branch} keeps ${released.split('.').slice(0, 2).join('.')}.x\n`
473
+ : 'stays on this line; nothing branches off\n'));
474
+ }
475
+ process.stdout.write(`\nOr \`fg-dist prerelease --major\` to start the next line beside main, ` +
476
+ 'without\nmoving main off this one.\n');
477
+ return 1;
478
+ }
479
+ }
480
+ const outcome = await release(root, {
481
+ bump: args.bump,
482
+ id: args.id,
483
+ dryRun: args.dryRun,
484
+ push: args.push,
485
+ });
486
+ const { plan } = outcome;
487
+ process.stdout.write(`Released ${plan.released}, ` +
488
+ `${plan.tagged ? `tagged ${plan.base}` : 'which is not tagged here'}\n\n`);
489
+ const width = pad([plan.maintenance?.branch ?? '', plan.next.branch]);
490
+ if (plan.maintenance) {
491
+ process.stdout.write(` ${plan.maintenance.branch.padEnd(width)} ${plan.maintenance.version} ` +
492
+ `cut from ${plan.base}, where ${plan.released} is maintained\n`);
493
+ }
494
+ process.stdout.write(` ${plan.next.branch.padEnd(width)} ${plan.next.version} ` +
495
+ `opened against ${outcome.base}\n`);
496
+ if (args.dryRun) {
497
+ process.stdout.write('\nNothing written. Drop --dry-run to do it.\n');
498
+ return 0;
499
+ }
500
+ // Only worth saying when something was cut from the tag. A prerelease has
501
+ // no maintenance branch, so there is nothing the missing tag affected.
502
+ if (!plan.tagged && plan.maintenance) {
503
+ process.stdout.write(`\nThere is no v${plan.released} tag here, so ${plan.maintenance.branch} was cut ` +
504
+ `from HEAD.\nIf the release workflow has not run yet, run it first: the tag is ` +
505
+ `what\nseparates the released commit from whatever ${outcome.base} has done since.\n`);
506
+ }
507
+ if (!outcome.pushed) {
508
+ process.stdout.write('\nLeft local. Push them and open the pull request yourself.\n');
509
+ return 0;
510
+ }
511
+ process.stdout.write(outcome.pullRequest
512
+ ? `\n${outcome.pullRequest}\n`
513
+ : '\nPushed. `gh` is not here, so open the pull request yourself.\n');
514
+ return 0;
515
+ }
516
+ if (args.command === 'prerelease') {
517
+ const root = repositoryRoot(args.cwd);
518
+ if (!root)
519
+ throw new Error(`${args.cwd} is not inside a git repository.`);
520
+ if (args.bump !== 'major' && args.bump !== 'minor') {
521
+ process.stderr.write('prerelease needs --major or --minor: which line is being started.\n');
522
+ return 1;
523
+ }
524
+ const { plan, created, pushed } = await startPrerelease(root, {
525
+ bump: args.bump,
526
+ id: args.id,
527
+ dryRun: args.dryRun,
528
+ push: args.push,
529
+ });
530
+ process.stdout.write(` ${plan.branch} ${plan.version} cut from ${plan.from}, which main keeps\n`);
531
+ if (args.dryRun) {
532
+ process.stdout.write('\nNothing written. Drop --dry-run to do it.\n');
533
+ return 0;
534
+ }
535
+ if (!created)
536
+ return 1;
537
+ process.stdout.write(pushed
538
+ ? `\nPushed. Release from it with a dist tag of its own — \`next\`, say — so\n` +
539
+ '`latest` goes on meaning the line main is shipping.\n'
540
+ : '\nLeft local. Push it when you are ready.\n');
541
+ return 0;
542
+ }
543
+ if (args.command === 'check') {
544
+ const root = repositoryRoot(args.cwd);
545
+ if (!root)
546
+ throw new Error(`${args.cwd} is not inside a git repository.`);
547
+ const report = inspectExtends(root);
548
+ if (!report.distribution.extends) {
549
+ process.stdout.write(`${report.distribution.name} extends nothing, so there is no floor to check.\n`);
550
+ return 0;
551
+ }
552
+ if (report.unresolved) {
553
+ process.stderr.write(`${report.distribution.name} extends ${report.unresolved}, which is not ` +
554
+ 'installed, so nothing could be compared against it.\n');
555
+ return 1;
556
+ }
557
+ if (report.violations.length > 0) {
558
+ process.stderr.write(`${describeViolations(report)}\n`);
559
+ return 1;
560
+ }
561
+ process.stdout.write(`${report.distribution.name}@${report.distribution.version} ships nothing older ` +
562
+ `than ${report.parent?.name}@${report.parent?.version}.\n`);
563
+ return 0;
564
+ }
565
+ if (args.command === 'extract') {
566
+ const [target] = args.names;
567
+ if (!target) {
568
+ process.stderr.write('extract needs the directory to turn into a module.\n');
569
+ return 1;
570
+ }
571
+ if (!args.url) {
572
+ process.stderr.write('extract needs --url, the repository the module will live at.\n');
573
+ return 1;
574
+ }
575
+ const result = await extractModule(args.cwd, target, {
576
+ url: args.url,
577
+ ssh: args.ssh,
578
+ tag: args.tag,
579
+ });
580
+ process.stdout.write(`${result.relativePath} is now ${result.initialised ? 'a repository' : 'its own repository'}` +
581
+ ` and a submodule of this one\n`);
582
+ if (result.packageName && result.version) {
583
+ process.stdout.write(`Shipping ${result.packageName}@${result.version}\n`);
584
+ }
585
+ if (result.tagged) {
586
+ process.stdout.write(`Tagged ${result.tagged}, so sync can track it\n` +
587
+ ` That tag is what the first release would have created, so release it\n` +
588
+ ` with the next version rather than this one — or \`--no-tag\` next time.\n`);
589
+ }
590
+ if (result.overridesError) {
591
+ process.stderr.write(`\nThe module is extracted, but the workspace overrides were not updated:\n` +
592
+ ` ${result.overridesError}\n` +
593
+ 'Run `fg-dist overrides` once that is sorted.\n');
594
+ }
595
+ process.stdout.write(`origin is ${result.url}\n`);
596
+ if (result.rewritten) {
597
+ process.stdout.write(`(rewritten from ${args.url}; a submodule is cloned without an SSH key)\n`);
598
+ }
599
+ // Nothing was pushed: the submodule points at a remote that may not exist
600
+ // yet, which is fine here and fatal on anyone else's clone.
601
+ process.stdout.write(`\nPush it before anyone else clones this:\n` +
602
+ ` git -C ${result.relativePath} push -u origin main --tags\n` +
603
+ 'Then commit the new submodule here.\n');
604
+ return 0;
605
+ }
606
+ if (args.command === 'use-https') {
607
+ const { root, modules } = modulesFor(args.cwd, args.names);
608
+ const ssh = modules.filter((module) => isSsh(module.url));
609
+ if (ssh.length === 0) {
610
+ process.stdout.write('Every submodule url is already https.\n');
611
+ return 0;
612
+ }
613
+ for (const module of ssh) {
614
+ const https = toHttps(module.url);
615
+ if (https === module.url) {
616
+ process.stderr.write(`${module.relativePath} no https form for ${module.url}\n`);
617
+ continue;
618
+ }
619
+ if (!args.dryRun)
620
+ setUrl(root, module, https);
621
+ process.stdout.write(`${module.relativePath} ${module.url} -> ${https}\n`);
622
+ }
623
+ if (args.dryRun) {
624
+ process.stdout.write('\nDry run; .gitmodules was not changed.\n');
625
+ return 0;
626
+ }
627
+ process.stdout.write('\n.gitmodules updated. Commit it to record the new urls.\n');
628
+ // https is necessary but not sufficient: a keyless clone also needs the
629
+ // repository to be readable without credentials.
630
+ if (!args.verify)
631
+ return 0;
632
+ process.stdout.write('\nChecking whether each can be cloned without credentials:\n');
633
+ const privateOnes = [];
634
+ for (const module of ssh) {
635
+ const https = toHttps(module.url);
636
+ const reachable = isPublic(https);
637
+ if (!reachable)
638
+ privateOnes.push(module.relativePath);
639
+ process.stdout.write(` ${module.relativePath} ${reachable ? 'public' : 'not readable'}\n`);
640
+ }
641
+ if (privateOnes.length > 0) {
642
+ process.stdout.write(`\n${privateOnes.length} of these cannot be read anonymously, so a build ` +
643
+ 'host still cannot clone them. Make them public, or expect the build to fail.\n');
644
+ return 1;
645
+ }
646
+ process.stdout.write('\nAll readable anonymously.\n');
647
+ return 0;
648
+ }
649
+ if (args.command === 'sync') {
650
+ const { root, modules } = modulesFor(args.cwd, args.names);
651
+ const states = modules.map((module) => inspect(module, { fetch: args.fetch }));
652
+ const width = pad(states.map((state) => state.submodule.relativePath));
653
+ for (const state of states) {
654
+ process.stdout.write(`${state.submodule.relativePath.padEnd(width)} ${describeState(state)}\n`);
655
+ }
656
+ // .gitmodules is committed, so an SSH URL is what a build host will try.
657
+ const ssh = states.filter((state) => isSsh(state.submodule.url));
658
+ if (ssh.length > 0) {
659
+ process.stdout.write(`\n${ssh.length} module(s) are recorded with an ssh url, which a clone ` +
660
+ 'without a key cannot use:\n');
661
+ for (const state of ssh) {
662
+ process.stdout.write(` ${state.submodule.relativePath} ${state.submodule.url}` +
663
+ ` -> ${toHttps(state.submodule.url)}\n`);
664
+ }
665
+ process.stdout.write('Run `fg-dist use-https` to rewrite them. They also have to be ' +
666
+ 'readable without credentials, which means public.\n');
667
+ }
668
+ // An extension may move ahead of what it extends, never behind it.
669
+ const extendsReport = inspectExtends(root);
670
+ if (extendsReport.violations.length > 0) {
671
+ process.stdout.write(`\n${describeViolations(extendsReport)}\n`);
672
+ }
673
+ else if (extendsReport.unresolved) {
674
+ process.stdout.write(`\nThis extends ${extendsReport.unresolved}, which is not installed, ` +
675
+ 'so nothing could be compared against it.\n');
676
+ }
677
+ else if (extendsReport.parent) {
678
+ process.stdout.write(`\nUp to date with ${extendsReport.parent.name}@${extendsReport.parent.version}, ` +
679
+ 'which it extends.\n');
680
+ }
681
+ const upgradable = states.filter((state) => target(state, { major: true }));
682
+ const untagged = states.filter((state) => state.current === undefined);
683
+ if (upgradable.length === 0) {
684
+ process.stdout.write(untagged.length === states.length
685
+ ? '\nNo module carries a version tag, so there is nothing to compare. ' +
686
+ 'Tag a release in each module to track it here.\n'
687
+ : '\nEverything is on its newest version.\n');
688
+ if (untagged.length > 0 && untagged.length < states.length) {
689
+ process.stdout.write(`${untagged.length} module(s) carry no version tag and were not compared.\n`);
690
+ }
691
+ return 0;
692
+ }
693
+ // A major is only held back when it is newer than the non-major target.
694
+ const held = upgradable.filter((state) => state.upgrades.major !== undefined);
695
+ process.stdout.write(`\n${upgradable.length} module(s) have newer versions. `);
696
+ process.stdout.write('Run `fg-dist bump` to take them');
697
+ process.stdout.write(held.length > 0
698
+ ? `, or \`fg-dist bump --major\` to include the ${held.length} major upgrade(s).\n`
699
+ : '.\n');
700
+ return 0;
701
+ }
702
+ if (args.command === 'bump') {
703
+ const { modules } = modulesFor(args.cwd, args.names);
704
+ const states = modules.map((module) => inspect(module, { fetch: args.fetch }));
705
+ const moves = states
706
+ .map((state) => ({ state, to: target(state, { major: args.major }) }))
707
+ .filter((move) => move.to !== undefined);
708
+ if (moves.length === 0) {
709
+ process.stdout.write('Nothing to bump.\n');
710
+ const held = states.filter((state) => target(state, { major: true }));
711
+ if (held.length > 0 && !args.major) {
712
+ process.stdout.write(`${held.length} module(s) have a major upgrade available; pass --major to take it.\n`);
713
+ }
714
+ return 0;
715
+ }
716
+ // Moving a dirty submodule would fail mid-checkout or discard work.
717
+ const dirty = moves.filter((move) => move.state.dirty);
718
+ if (dirty.length > 0) {
719
+ process.stderr.write(`Refusing to bump, these have uncommitted changes:\n${dirty
720
+ .map((move) => ` ${move.state.submodule.relativePath}`)
721
+ .join('\n')}\n`);
722
+ return 1;
723
+ }
724
+ for (const { state, to } of moves) {
725
+ const from = state.current ?? state.head.slice(0, 7);
726
+ if (args.dryRun) {
727
+ process.stdout.write(`${state.submodule.relativePath} ${from} -> ${to}\n`);
728
+ continue;
729
+ }
730
+ moveTo(state.submodule, to);
731
+ process.stdout.write(`${state.submodule.relativePath} ${from} -> ${to}\n`);
732
+ }
733
+ process.stdout.write(args.dryRun
734
+ ? '\nDry run; nothing moved.\n'
735
+ : '\nSubmodule pointers moved. Commit them to record the new versions.\n');
736
+ return 0;
737
+ }
738
+ process.stderr.write(`Unknown command: ${args.command}\n\n${USAGE}`);
739
+ return 1;
740
+ };
741
+ main().then((code) => {
742
+ process.exitCode = code;
743
+ }, (error) => {
744
+ process.stderr.write(`${error instanceof Error ? error.message : String(error)}\n`);
745
+ process.exitCode = 1;
746
+ });
747
+ //# sourceMappingURL=cli.js.map