pgflow 0.0.1-prealpha.0 → 0.0.12

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 (125) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/LICENSE.md +660 -0
  3. package/eslint.config.cjs +3 -0
  4. package/package.json +13 -6
  5. package/project.json +40 -0
  6. package/src/commands/install/copy-migrations.ts +101 -0
  7. package/src/commands/install/index.ts +45 -0
  8. package/src/commands/install/supabase-path-prompt.ts +28 -0
  9. package/src/commands/install/update-config-toml.ts +130 -0
  10. package/src/index.ts +76 -0
  11. package/src/types.d.ts +1 -0
  12. package/tsconfig.json +20 -0
  13. package/tsconfig.lib.json +19 -0
  14. package/tsconfig.spec.json +33 -0
  15. package/vite.config.ts +25 -0
  16. package/dist/node_modules/.package-lock.json +0 -129
  17. package/dist/node_modules/@commander-js/extra-typings/LICENSE +0 -21
  18. package/dist/node_modules/@commander-js/extra-typings/README.md +0 -66
  19. package/dist/node_modules/@commander-js/extra-typings/esm.d.mts +0 -2
  20. package/dist/node_modules/@commander-js/extra-typings/esm.mjs +0 -16
  21. package/dist/node_modules/@commander-js/extra-typings/index.d.ts +0 -1467
  22. package/dist/node_modules/@commander-js/extra-typings/index.js +0 -25
  23. package/dist/node_modules/@commander-js/extra-typings/package.json +0 -63
  24. package/dist/node_modules/@esbuild/linux-x64/README.md +0 -3
  25. package/dist/node_modules/@esbuild/linux-x64/bin/esbuild +0 -0
  26. package/dist/node_modules/@esbuild/linux-x64/package.json +0 -20
  27. package/dist/node_modules/commander/LICENSE +0 -22
  28. package/dist/node_modules/commander/Readme.md +0 -1149
  29. package/dist/node_modules/commander/esm.mjs +0 -16
  30. package/dist/node_modules/commander/index.js +0 -24
  31. package/dist/node_modules/commander/lib/argument.js +0 -149
  32. package/dist/node_modules/commander/lib/command.js +0 -2662
  33. package/dist/node_modules/commander/lib/error.js +0 -39
  34. package/dist/node_modules/commander/lib/help.js +0 -709
  35. package/dist/node_modules/commander/lib/option.js +0 -367
  36. package/dist/node_modules/commander/lib/suggestSimilar.js +0 -101
  37. package/dist/node_modules/commander/package-support.json +0 -16
  38. package/dist/node_modules/commander/package.json +0 -82
  39. package/dist/node_modules/commander/typings/esm.d.mts +0 -3
  40. package/dist/node_modules/commander/typings/index.d.ts +0 -1045
  41. package/dist/node_modules/esbuild/LICENSE.md +0 -21
  42. package/dist/node_modules/esbuild/README.md +0 -3
  43. package/dist/node_modules/esbuild/bin/esbuild +0 -0
  44. package/dist/node_modules/esbuild/install.js +0 -287
  45. package/dist/node_modules/esbuild/lib/main.d.ts +0 -705
  46. package/dist/node_modules/esbuild/lib/main.js +0 -2251
  47. package/dist/node_modules/esbuild/package.json +0 -48
  48. package/dist/node_modules/get-tsconfig/LICENSE +0 -21
  49. package/dist/node_modules/get-tsconfig/README.md +0 -235
  50. package/dist/node_modules/get-tsconfig/dist/index.cjs +0 -7
  51. package/dist/node_modules/get-tsconfig/dist/index.d.cts +0 -1439
  52. package/dist/node_modules/get-tsconfig/dist/index.d.mts +0 -1439
  53. package/dist/node_modules/get-tsconfig/dist/index.mjs +0 -7
  54. package/dist/node_modules/get-tsconfig/package.json +0 -46
  55. package/dist/node_modules/resolve-pkg-maps/LICENSE +0 -21
  56. package/dist/node_modules/resolve-pkg-maps/README.md +0 -216
  57. package/dist/node_modules/resolve-pkg-maps/dist/index.cjs +0 -1
  58. package/dist/node_modules/resolve-pkg-maps/dist/index.d.cts +0 -11
  59. package/dist/node_modules/resolve-pkg-maps/dist/index.d.mts +0 -11
  60. package/dist/node_modules/resolve-pkg-maps/dist/index.mjs +0 -1
  61. package/dist/node_modules/resolve-pkg-maps/package.json +0 -42
  62. package/dist/node_modules/tsx/LICENSE +0 -21
  63. package/dist/node_modules/tsx/README.md +0 -32
  64. package/dist/node_modules/tsx/dist/cjs/api/index.cjs +0 -1
  65. package/dist/node_modules/tsx/dist/cjs/api/index.d.cts +0 -35
  66. package/dist/node_modules/tsx/dist/cjs/api/index.d.mts +0 -35
  67. package/dist/node_modules/tsx/dist/cjs/api/index.mjs +0 -1
  68. package/dist/node_modules/tsx/dist/cjs/index.cjs +0 -1
  69. package/dist/node_modules/tsx/dist/cjs/index.mjs +0 -1
  70. package/dist/node_modules/tsx/dist/cli.cjs +0 -54
  71. package/dist/node_modules/tsx/dist/cli.mjs +0 -55
  72. package/dist/node_modules/tsx/dist/client-BQVF1NaW.mjs +0 -1
  73. package/dist/node_modules/tsx/dist/client-D6NvIMSC.cjs +0 -1
  74. package/dist/node_modules/tsx/dist/esm/api/index.cjs +0 -1
  75. package/dist/node_modules/tsx/dist/esm/api/index.d.cts +0 -35
  76. package/dist/node_modules/tsx/dist/esm/api/index.d.mts +0 -35
  77. package/dist/node_modules/tsx/dist/esm/api/index.mjs +0 -1
  78. package/dist/node_modules/tsx/dist/esm/index.cjs +0 -2
  79. package/dist/node_modules/tsx/dist/esm/index.mjs +0 -2
  80. package/dist/node_modules/tsx/dist/get-pipe-path-BHW2eJdv.mjs +0 -1
  81. package/dist/node_modules/tsx/dist/get-pipe-path-BoR10qr8.cjs +0 -1
  82. package/dist/node_modules/tsx/dist/index-B4SIRlEU.cjs +0 -16
  83. package/dist/node_modules/tsx/dist/index-DlKgSVBb.mjs +0 -16
  84. package/dist/node_modules/tsx/dist/loader.cjs +0 -1
  85. package/dist/node_modules/tsx/dist/loader.mjs +0 -1
  86. package/dist/node_modules/tsx/dist/node-features-_8ZFwP_x.mjs +0 -1
  87. package/dist/node_modules/tsx/dist/node-features-roYmp9jK.cjs +0 -1
  88. package/dist/node_modules/tsx/dist/package--YiorJOq.mjs +0 -1
  89. package/dist/node_modules/tsx/dist/package-BUuKnQXZ.cjs +0 -1
  90. package/dist/node_modules/tsx/dist/patch-repl.cjs +0 -1
  91. package/dist/node_modules/tsx/dist/patch-repl.mjs +0 -1
  92. package/dist/node_modules/tsx/dist/preflight.cjs +0 -1
  93. package/dist/node_modules/tsx/dist/preflight.mjs +0 -1
  94. package/dist/node_modules/tsx/dist/register-C3TE0KFF.mjs +0 -3
  95. package/dist/node_modules/tsx/dist/register-CVZT-Tul.cjs +0 -1
  96. package/dist/node_modules/tsx/dist/register-DCnOAxY2.cjs +0 -3
  97. package/dist/node_modules/tsx/dist/register-RyGUjI6j.mjs +0 -1
  98. package/dist/node_modules/tsx/dist/repl.cjs +0 -3
  99. package/dist/node_modules/tsx/dist/repl.mjs +0 -3
  100. package/dist/node_modules/tsx/dist/require-BnyFnlmk.mjs +0 -1
  101. package/dist/node_modules/tsx/dist/require-DBpSESet.cjs +0 -1
  102. package/dist/node_modules/tsx/dist/suppress-warnings.cjs +0 -1
  103. package/dist/node_modules/tsx/dist/suppress-warnings.mjs +0 -1
  104. package/dist/node_modules/tsx/dist/temporary-directory-B83uKxJF.cjs +0 -1
  105. package/dist/node_modules/tsx/dist/temporary-directory-CwHp0_NW.mjs +0 -1
  106. package/dist/node_modules/tsx/dist/types-Cxp8y2TL.d.ts +0 -5
  107. package/dist/node_modules/tsx/package.json +0 -68
  108. package/dist/package.json +0 -27
  109. package/dist/sql/000000_schema.sql +0 -150
  110. package/dist/sql/000005_create_flow.sql +0 -29
  111. package/dist/sql/000010_add_step.sql +0 -48
  112. package/dist/sql/000015_start_ready_steps.sql +0 -45
  113. package/dist/sql/000020_start_flow.sql +0 -46
  114. package/dist/sql/000030_read_with_poll_backport.sql +0 -70
  115. package/dist/sql/000040_poll_for_tasks.sql +0 -100
  116. package/dist/sql/000045_maybe_complete_run.sql +0 -30
  117. package/dist/sql/000050_complete_task.sql +0 -98
  118. package/dist/sql/000055_calculate_retry_delay.sql +0 -11
  119. package/dist/sql/000060_fail_task.sql +0 -124
  120. package/dist/sql/000_edge_worker_initial.sql +0 -86
  121. package/dist/src/commands/install.d.ts +0 -4
  122. package/dist/src/commands/install.d.ts.map +0 -1
  123. package/dist/src/index.d.ts +0 -2
  124. package/dist/src/index.d.ts.map +0 -1
  125. package/dist/tsconfig.lib.tsbuildinfo +0 -1
@@ -1,709 +0,0 @@
1
- const { humanReadableArgName } = require('./argument.js');
2
-
3
- /**
4
- * TypeScript import types for JSDoc, used by Visual Studio Code IntelliSense and `npm run typescript-checkJS`
5
- * https://www.typescriptlang.org/docs/handbook/jsdoc-supported-types.html#import-types
6
- * @typedef { import("./argument.js").Argument } Argument
7
- * @typedef { import("./command.js").Command } Command
8
- * @typedef { import("./option.js").Option } Option
9
- */
10
-
11
- // Although this is a class, methods are static in style to allow override using subclass or just functions.
12
- class Help {
13
- constructor() {
14
- this.helpWidth = undefined;
15
- this.minWidthToWrap = 40;
16
- this.sortSubcommands = false;
17
- this.sortOptions = false;
18
- this.showGlobalOptions = false;
19
- }
20
-
21
- /**
22
- * prepareContext is called by Commander after applying overrides from `Command.configureHelp()`
23
- * and just before calling `formatHelp()`.
24
- *
25
- * Commander just uses the helpWidth and the rest is provided for optional use by more complex subclasses.
26
- *
27
- * @param {{ error?: boolean, helpWidth?: number, outputHasColors?: boolean }} contextOptions
28
- */
29
- prepareContext(contextOptions) {
30
- this.helpWidth = this.helpWidth ?? contextOptions.helpWidth ?? 80;
31
- }
32
-
33
- /**
34
- * Get an array of the visible subcommands. Includes a placeholder for the implicit help command, if there is one.
35
- *
36
- * @param {Command} cmd
37
- * @returns {Command[]}
38
- */
39
-
40
- visibleCommands(cmd) {
41
- const visibleCommands = cmd.commands.filter((cmd) => !cmd._hidden);
42
- const helpCommand = cmd._getHelpCommand();
43
- if (helpCommand && !helpCommand._hidden) {
44
- visibleCommands.push(helpCommand);
45
- }
46
- if (this.sortSubcommands) {
47
- visibleCommands.sort((a, b) => {
48
- // @ts-ignore: because overloaded return type
49
- return a.name().localeCompare(b.name());
50
- });
51
- }
52
- return visibleCommands;
53
- }
54
-
55
- /**
56
- * Compare options for sort.
57
- *
58
- * @param {Option} a
59
- * @param {Option} b
60
- * @returns {number}
61
- */
62
- compareOptions(a, b) {
63
- const getSortKey = (option) => {
64
- // WYSIWYG for order displayed in help. Short used for comparison if present. No special handling for negated.
65
- return option.short
66
- ? option.short.replace(/^-/, '')
67
- : option.long.replace(/^--/, '');
68
- };
69
- return getSortKey(a).localeCompare(getSortKey(b));
70
- }
71
-
72
- /**
73
- * Get an array of the visible options. Includes a placeholder for the implicit help option, if there is one.
74
- *
75
- * @param {Command} cmd
76
- * @returns {Option[]}
77
- */
78
-
79
- visibleOptions(cmd) {
80
- const visibleOptions = cmd.options.filter((option) => !option.hidden);
81
- // Built-in help option.
82
- const helpOption = cmd._getHelpOption();
83
- if (helpOption && !helpOption.hidden) {
84
- // Automatically hide conflicting flags. Bit dubious but a historical behaviour that is convenient for single-command programs.
85
- const removeShort = helpOption.short && cmd._findOption(helpOption.short);
86
- const removeLong = helpOption.long && cmd._findOption(helpOption.long);
87
- if (!removeShort && !removeLong) {
88
- visibleOptions.push(helpOption); // no changes needed
89
- } else if (helpOption.long && !removeLong) {
90
- visibleOptions.push(
91
- cmd.createOption(helpOption.long, helpOption.description),
92
- );
93
- } else if (helpOption.short && !removeShort) {
94
- visibleOptions.push(
95
- cmd.createOption(helpOption.short, helpOption.description),
96
- );
97
- }
98
- }
99
- if (this.sortOptions) {
100
- visibleOptions.sort(this.compareOptions);
101
- }
102
- return visibleOptions;
103
- }
104
-
105
- /**
106
- * Get an array of the visible global options. (Not including help.)
107
- *
108
- * @param {Command} cmd
109
- * @returns {Option[]}
110
- */
111
-
112
- visibleGlobalOptions(cmd) {
113
- if (!this.showGlobalOptions) return [];
114
-
115
- const globalOptions = [];
116
- for (
117
- let ancestorCmd = cmd.parent;
118
- ancestorCmd;
119
- ancestorCmd = ancestorCmd.parent
120
- ) {
121
- const visibleOptions = ancestorCmd.options.filter(
122
- (option) => !option.hidden,
123
- );
124
- globalOptions.push(...visibleOptions);
125
- }
126
- if (this.sortOptions) {
127
- globalOptions.sort(this.compareOptions);
128
- }
129
- return globalOptions;
130
- }
131
-
132
- /**
133
- * Get an array of the arguments if any have a description.
134
- *
135
- * @param {Command} cmd
136
- * @returns {Argument[]}
137
- */
138
-
139
- visibleArguments(cmd) {
140
- // Side effect! Apply the legacy descriptions before the arguments are displayed.
141
- if (cmd._argsDescription) {
142
- cmd.registeredArguments.forEach((argument) => {
143
- argument.description =
144
- argument.description || cmd._argsDescription[argument.name()] || '';
145
- });
146
- }
147
-
148
- // If there are any arguments with a description then return all the arguments.
149
- if (cmd.registeredArguments.find((argument) => argument.description)) {
150
- return cmd.registeredArguments;
151
- }
152
- return [];
153
- }
154
-
155
- /**
156
- * Get the command term to show in the list of subcommands.
157
- *
158
- * @param {Command} cmd
159
- * @returns {string}
160
- */
161
-
162
- subcommandTerm(cmd) {
163
- // Legacy. Ignores custom usage string, and nested commands.
164
- const args = cmd.registeredArguments
165
- .map((arg) => humanReadableArgName(arg))
166
- .join(' ');
167
- return (
168
- cmd._name +
169
- (cmd._aliases[0] ? '|' + cmd._aliases[0] : '') +
170
- (cmd.options.length ? ' [options]' : '') + // simplistic check for non-help option
171
- (args ? ' ' + args : '')
172
- );
173
- }
174
-
175
- /**
176
- * Get the option term to show in the list of options.
177
- *
178
- * @param {Option} option
179
- * @returns {string}
180
- */
181
-
182
- optionTerm(option) {
183
- return option.flags;
184
- }
185
-
186
- /**
187
- * Get the argument term to show in the list of arguments.
188
- *
189
- * @param {Argument} argument
190
- * @returns {string}
191
- */
192
-
193
- argumentTerm(argument) {
194
- return argument.name();
195
- }
196
-
197
- /**
198
- * Get the longest command term length.
199
- *
200
- * @param {Command} cmd
201
- * @param {Help} helper
202
- * @returns {number}
203
- */
204
-
205
- longestSubcommandTermLength(cmd, helper) {
206
- return helper.visibleCommands(cmd).reduce((max, command) => {
207
- return Math.max(
208
- max,
209
- this.displayWidth(
210
- helper.styleSubcommandTerm(helper.subcommandTerm(command)),
211
- ),
212
- );
213
- }, 0);
214
- }
215
-
216
- /**
217
- * Get the longest option term length.
218
- *
219
- * @param {Command} cmd
220
- * @param {Help} helper
221
- * @returns {number}
222
- */
223
-
224
- longestOptionTermLength(cmd, helper) {
225
- return helper.visibleOptions(cmd).reduce((max, option) => {
226
- return Math.max(
227
- max,
228
- this.displayWidth(helper.styleOptionTerm(helper.optionTerm(option))),
229
- );
230
- }, 0);
231
- }
232
-
233
- /**
234
- * Get the longest global option term length.
235
- *
236
- * @param {Command} cmd
237
- * @param {Help} helper
238
- * @returns {number}
239
- */
240
-
241
- longestGlobalOptionTermLength(cmd, helper) {
242
- return helper.visibleGlobalOptions(cmd).reduce((max, option) => {
243
- return Math.max(
244
- max,
245
- this.displayWidth(helper.styleOptionTerm(helper.optionTerm(option))),
246
- );
247
- }, 0);
248
- }
249
-
250
- /**
251
- * Get the longest argument term length.
252
- *
253
- * @param {Command} cmd
254
- * @param {Help} helper
255
- * @returns {number}
256
- */
257
-
258
- longestArgumentTermLength(cmd, helper) {
259
- return helper.visibleArguments(cmd).reduce((max, argument) => {
260
- return Math.max(
261
- max,
262
- this.displayWidth(
263
- helper.styleArgumentTerm(helper.argumentTerm(argument)),
264
- ),
265
- );
266
- }, 0);
267
- }
268
-
269
- /**
270
- * Get the command usage to be displayed at the top of the built-in help.
271
- *
272
- * @param {Command} cmd
273
- * @returns {string}
274
- */
275
-
276
- commandUsage(cmd) {
277
- // Usage
278
- let cmdName = cmd._name;
279
- if (cmd._aliases[0]) {
280
- cmdName = cmdName + '|' + cmd._aliases[0];
281
- }
282
- let ancestorCmdNames = '';
283
- for (
284
- let ancestorCmd = cmd.parent;
285
- ancestorCmd;
286
- ancestorCmd = ancestorCmd.parent
287
- ) {
288
- ancestorCmdNames = ancestorCmd.name() + ' ' + ancestorCmdNames;
289
- }
290
- return ancestorCmdNames + cmdName + ' ' + cmd.usage();
291
- }
292
-
293
- /**
294
- * Get the description for the command.
295
- *
296
- * @param {Command} cmd
297
- * @returns {string}
298
- */
299
-
300
- commandDescription(cmd) {
301
- // @ts-ignore: because overloaded return type
302
- return cmd.description();
303
- }
304
-
305
- /**
306
- * Get the subcommand summary to show in the list of subcommands.
307
- * (Fallback to description for backwards compatibility.)
308
- *
309
- * @param {Command} cmd
310
- * @returns {string}
311
- */
312
-
313
- subcommandDescription(cmd) {
314
- // @ts-ignore: because overloaded return type
315
- return cmd.summary() || cmd.description();
316
- }
317
-
318
- /**
319
- * Get the option description to show in the list of options.
320
- *
321
- * @param {Option} option
322
- * @return {string}
323
- */
324
-
325
- optionDescription(option) {
326
- const extraInfo = [];
327
-
328
- if (option.argChoices) {
329
- extraInfo.push(
330
- // use stringify to match the display of the default value
331
- `choices: ${option.argChoices.map((choice) => JSON.stringify(choice)).join(', ')}`,
332
- );
333
- }
334
- if (option.defaultValue !== undefined) {
335
- // default for boolean and negated more for programmer than end user,
336
- // but show true/false for boolean option as may be for hand-rolled env or config processing.
337
- const showDefault =
338
- option.required ||
339
- option.optional ||
340
- (option.isBoolean() && typeof option.defaultValue === 'boolean');
341
- if (showDefault) {
342
- extraInfo.push(
343
- `default: ${option.defaultValueDescription || JSON.stringify(option.defaultValue)}`,
344
- );
345
- }
346
- }
347
- // preset for boolean and negated are more for programmer than end user
348
- if (option.presetArg !== undefined && option.optional) {
349
- extraInfo.push(`preset: ${JSON.stringify(option.presetArg)}`);
350
- }
351
- if (option.envVar !== undefined) {
352
- extraInfo.push(`env: ${option.envVar}`);
353
- }
354
- if (extraInfo.length > 0) {
355
- return `${option.description} (${extraInfo.join(', ')})`;
356
- }
357
-
358
- return option.description;
359
- }
360
-
361
- /**
362
- * Get the argument description to show in the list of arguments.
363
- *
364
- * @param {Argument} argument
365
- * @return {string}
366
- */
367
-
368
- argumentDescription(argument) {
369
- const extraInfo = [];
370
- if (argument.argChoices) {
371
- extraInfo.push(
372
- // use stringify to match the display of the default value
373
- `choices: ${argument.argChoices.map((choice) => JSON.stringify(choice)).join(', ')}`,
374
- );
375
- }
376
- if (argument.defaultValue !== undefined) {
377
- extraInfo.push(
378
- `default: ${argument.defaultValueDescription || JSON.stringify(argument.defaultValue)}`,
379
- );
380
- }
381
- if (extraInfo.length > 0) {
382
- const extraDescription = `(${extraInfo.join(', ')})`;
383
- if (argument.description) {
384
- return `${argument.description} ${extraDescription}`;
385
- }
386
- return extraDescription;
387
- }
388
- return argument.description;
389
- }
390
-
391
- /**
392
- * Generate the built-in help text.
393
- *
394
- * @param {Command} cmd
395
- * @param {Help} helper
396
- * @returns {string}
397
- */
398
-
399
- formatHelp(cmd, helper) {
400
- const termWidth = helper.padWidth(cmd, helper);
401
- const helpWidth = helper.helpWidth ?? 80; // in case prepareContext() was not called
402
-
403
- function callFormatItem(term, description) {
404
- return helper.formatItem(term, termWidth, description, helper);
405
- }
406
-
407
- // Usage
408
- let output = [
409
- `${helper.styleTitle('Usage:')} ${helper.styleUsage(helper.commandUsage(cmd))}`,
410
- '',
411
- ];
412
-
413
- // Description
414
- const commandDescription = helper.commandDescription(cmd);
415
- if (commandDescription.length > 0) {
416
- output = output.concat([
417
- helper.boxWrap(
418
- helper.styleCommandDescription(commandDescription),
419
- helpWidth,
420
- ),
421
- '',
422
- ]);
423
- }
424
-
425
- // Arguments
426
- const argumentList = helper.visibleArguments(cmd).map((argument) => {
427
- return callFormatItem(
428
- helper.styleArgumentTerm(helper.argumentTerm(argument)),
429
- helper.styleArgumentDescription(helper.argumentDescription(argument)),
430
- );
431
- });
432
- if (argumentList.length > 0) {
433
- output = output.concat([
434
- helper.styleTitle('Arguments:'),
435
- ...argumentList,
436
- '',
437
- ]);
438
- }
439
-
440
- // Options
441
- const optionList = helper.visibleOptions(cmd).map((option) => {
442
- return callFormatItem(
443
- helper.styleOptionTerm(helper.optionTerm(option)),
444
- helper.styleOptionDescription(helper.optionDescription(option)),
445
- );
446
- });
447
- if (optionList.length > 0) {
448
- output = output.concat([
449
- helper.styleTitle('Options:'),
450
- ...optionList,
451
- '',
452
- ]);
453
- }
454
-
455
- if (helper.showGlobalOptions) {
456
- const globalOptionList = helper
457
- .visibleGlobalOptions(cmd)
458
- .map((option) => {
459
- return callFormatItem(
460
- helper.styleOptionTerm(helper.optionTerm(option)),
461
- helper.styleOptionDescription(helper.optionDescription(option)),
462
- );
463
- });
464
- if (globalOptionList.length > 0) {
465
- output = output.concat([
466
- helper.styleTitle('Global Options:'),
467
- ...globalOptionList,
468
- '',
469
- ]);
470
- }
471
- }
472
-
473
- // Commands
474
- const commandList = helper.visibleCommands(cmd).map((cmd) => {
475
- return callFormatItem(
476
- helper.styleSubcommandTerm(helper.subcommandTerm(cmd)),
477
- helper.styleSubcommandDescription(helper.subcommandDescription(cmd)),
478
- );
479
- });
480
- if (commandList.length > 0) {
481
- output = output.concat([
482
- helper.styleTitle('Commands:'),
483
- ...commandList,
484
- '',
485
- ]);
486
- }
487
-
488
- return output.join('\n');
489
- }
490
-
491
- /**
492
- * Return display width of string, ignoring ANSI escape sequences. Used in padding and wrapping calculations.
493
- *
494
- * @param {string} str
495
- * @returns {number}
496
- */
497
- displayWidth(str) {
498
- return stripColor(str).length;
499
- }
500
-
501
- /**
502
- * Style the title for displaying in the help. Called with 'Usage:', 'Options:', etc.
503
- *
504
- * @param {string} str
505
- * @returns {string}
506
- */
507
- styleTitle(str) {
508
- return str;
509
- }
510
-
511
- styleUsage(str) {
512
- // Usage has lots of parts the user might like to color separately! Assume default usage string which is formed like:
513
- // command subcommand [options] [command] <foo> [bar]
514
- return str
515
- .split(' ')
516
- .map((word) => {
517
- if (word === '[options]') return this.styleOptionText(word);
518
- if (word === '[command]') return this.styleSubcommandText(word);
519
- if (word[0] === '[' || word[0] === '<')
520
- return this.styleArgumentText(word);
521
- return this.styleCommandText(word); // Restrict to initial words?
522
- })
523
- .join(' ');
524
- }
525
- styleCommandDescription(str) {
526
- return this.styleDescriptionText(str);
527
- }
528
- styleOptionDescription(str) {
529
- return this.styleDescriptionText(str);
530
- }
531
- styleSubcommandDescription(str) {
532
- return this.styleDescriptionText(str);
533
- }
534
- styleArgumentDescription(str) {
535
- return this.styleDescriptionText(str);
536
- }
537
- styleDescriptionText(str) {
538
- return str;
539
- }
540
- styleOptionTerm(str) {
541
- return this.styleOptionText(str);
542
- }
543
- styleSubcommandTerm(str) {
544
- // This is very like usage with lots of parts! Assume default string which is formed like:
545
- // subcommand [options] <foo> [bar]
546
- return str
547
- .split(' ')
548
- .map((word) => {
549
- if (word === '[options]') return this.styleOptionText(word);
550
- if (word[0] === '[' || word[0] === '<')
551
- return this.styleArgumentText(word);
552
- return this.styleSubcommandText(word); // Restrict to initial words?
553
- })
554
- .join(' ');
555
- }
556
- styleArgumentTerm(str) {
557
- return this.styleArgumentText(str);
558
- }
559
- styleOptionText(str) {
560
- return str;
561
- }
562
- styleArgumentText(str) {
563
- return str;
564
- }
565
- styleSubcommandText(str) {
566
- return str;
567
- }
568
- styleCommandText(str) {
569
- return str;
570
- }
571
-
572
- /**
573
- * Calculate the pad width from the maximum term length.
574
- *
575
- * @param {Command} cmd
576
- * @param {Help} helper
577
- * @returns {number}
578
- */
579
-
580
- padWidth(cmd, helper) {
581
- return Math.max(
582
- helper.longestOptionTermLength(cmd, helper),
583
- helper.longestGlobalOptionTermLength(cmd, helper),
584
- helper.longestSubcommandTermLength(cmd, helper),
585
- helper.longestArgumentTermLength(cmd, helper),
586
- );
587
- }
588
-
589
- /**
590
- * Detect manually wrapped and indented strings by checking for line break followed by whitespace.
591
- *
592
- * @param {string} str
593
- * @returns {boolean}
594
- */
595
- preformatted(str) {
596
- return /\n[^\S\r\n]/.test(str);
597
- }
598
-
599
- /**
600
- * Format the "item", which consists of a term and description. Pad the term and wrap the description, indenting the following lines.
601
- *
602
- * So "TTT", 5, "DDD DDDD DD DDD" might be formatted for this.helpWidth=17 like so:
603
- * TTT DDD DDDD
604
- * DD DDD
605
- *
606
- * @param {string} term
607
- * @param {number} termWidth
608
- * @param {string} description
609
- * @param {Help} helper
610
- * @returns {string}
611
- */
612
- formatItem(term, termWidth, description, helper) {
613
- const itemIndent = 2;
614
- const itemIndentStr = ' '.repeat(itemIndent);
615
- if (!description) return itemIndentStr + term;
616
-
617
- // Pad the term out to a consistent width, so descriptions are aligned.
618
- const paddedTerm = term.padEnd(
619
- termWidth + term.length - helper.displayWidth(term),
620
- );
621
-
622
- // Format the description.
623
- const spacerWidth = 2; // between term and description
624
- const helpWidth = this.helpWidth ?? 80; // in case prepareContext() was not called
625
- const remainingWidth = helpWidth - termWidth - spacerWidth - itemIndent;
626
- let formattedDescription;
627
- if (
628
- remainingWidth < this.minWidthToWrap ||
629
- helper.preformatted(description)
630
- ) {
631
- formattedDescription = description;
632
- } else {
633
- const wrappedDescription = helper.boxWrap(description, remainingWidth);
634
- formattedDescription = wrappedDescription.replace(
635
- /\n/g,
636
- '\n' + ' '.repeat(termWidth + spacerWidth),
637
- );
638
- }
639
-
640
- // Construct and overall indent.
641
- return (
642
- itemIndentStr +
643
- paddedTerm +
644
- ' '.repeat(spacerWidth) +
645
- formattedDescription.replace(/\n/g, `\n${itemIndentStr}`)
646
- );
647
- }
648
-
649
- /**
650
- * Wrap a string at whitespace, preserving existing line breaks.
651
- * Wrapping is skipped if the width is less than `minWidthToWrap`.
652
- *
653
- * @param {string} str
654
- * @param {number} width
655
- * @returns {string}
656
- */
657
- boxWrap(str, width) {
658
- if (width < this.minWidthToWrap) return str;
659
-
660
- const rawLines = str.split(/\r\n|\n/);
661
- // split up text by whitespace
662
- const chunkPattern = /[\s]*[^\s]+/g;
663
- const wrappedLines = [];
664
- rawLines.forEach((line) => {
665
- const chunks = line.match(chunkPattern);
666
- if (chunks === null) {
667
- wrappedLines.push('');
668
- return;
669
- }
670
-
671
- let sumChunks = [chunks.shift()];
672
- let sumWidth = this.displayWidth(sumChunks[0]);
673
- chunks.forEach((chunk) => {
674
- const visibleWidth = this.displayWidth(chunk);
675
- // Accumulate chunks while they fit into width.
676
- if (sumWidth + visibleWidth <= width) {
677
- sumChunks.push(chunk);
678
- sumWidth += visibleWidth;
679
- return;
680
- }
681
- wrappedLines.push(sumChunks.join(''));
682
-
683
- const nextChunk = chunk.trimStart(); // trim space at line break
684
- sumChunks = [nextChunk];
685
- sumWidth = this.displayWidth(nextChunk);
686
- });
687
- wrappedLines.push(sumChunks.join(''));
688
- });
689
-
690
- return wrappedLines.join('\n');
691
- }
692
- }
693
-
694
- /**
695
- * Strip style ANSI escape sequences from the string. In particular, SGR (Select Graphic Rendition) codes.
696
- *
697
- * @param {string} str
698
- * @returns {string}
699
- * @package
700
- */
701
-
702
- function stripColor(str) {
703
- // eslint-disable-next-line no-control-regex
704
- const sgrPattern = /\x1b\[\d*(;\d*)*m/g;
705
- return str.replace(sgrPattern, '');
706
- }
707
-
708
- exports.Help = Help;
709
- exports.stripColor = stripColor;