@travetto/cli 4.0.0-rc.6 → 4.0.0-rc.8

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@travetto/cli",
3
- "version": "4.0.0-rc.6",
3
+ "version": "4.0.0-rc.8",
4
4
  "description": "CLI infrastructure for Travetto framework",
5
5
  "keywords": [
6
6
  "cli",
@@ -29,8 +29,8 @@
29
29
  "directory": "module/cli"
30
30
  },
31
31
  "dependencies": {
32
- "@travetto/schema": "^4.0.0-rc.6",
33
- "@travetto/terminal": "^4.0.0-rc.6"
32
+ "@travetto/schema": "^4.0.0-rc.8",
33
+ "@travetto/terminal": "^4.0.0-rc.8"
34
34
  },
35
35
  "travetto": {
36
36
  "displayName": "Command Line Interface",
package/src/error.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { PackageUtil, RuntimeContext } from '@travetto/manifest';
2
2
  import { cliTpl } from './color';
3
- import { CliValidationError } from './types';
3
+ import { CliValidationError, CliCommandShape } from './types';
4
4
 
5
5
  const COMMAND_PACKAGE = [
6
6
  [/^test(:watch)?$/, 'test', false],
@@ -50,9 +50,11 @@ ${{ identifier: install }}
50
50
  */
51
51
  export class CliValidationResultError extends Error {
52
52
  errors: CliValidationError[];
53
+ command: CliCommandShape;
53
54
 
54
- constructor(errors: CliValidationError[]) {
55
+ constructor(command: CliCommandShape, errors: CliValidationError[]) {
55
56
  super('');
57
+ this.command = command;
56
58
  this.errors = errors;
57
59
  }
58
60
  }
package/src/execute.ts CHANGED
@@ -1,7 +1,6 @@
1
1
  import { ConsoleManager, Env, ShutdownManager } from '@travetto/base';
2
2
 
3
3
  import { HelpUtil } from './help';
4
- import { CliCommandShape } from './types';
5
4
  import { CliCommandRegistry } from './registry';
6
5
  import { CliCommandSchemaUtil } from './schema';
7
6
  import { CliUnknownCommandError, CliValidationResultError } from './error';
@@ -13,51 +12,44 @@ import { CliUtil } from './util';
13
12
  */
14
13
  export class ExecutionManager {
15
14
 
16
- /** Prepare command for execution */
17
- static async #prepareAndBind(cmd: CliCommandShape, args: string[]): Promise<unknown[]> {
18
- const schema = await CliCommandSchemaUtil.getSchema(cmd);
19
- args = await CliParseUtil.expandArgs(schema, args);
20
- cmd._parsed = await CliParseUtil.parse(schema, args);
21
-
22
- await cmd.preBind?.();
23
- try {
24
- const known = await CliCommandSchemaUtil.bindInput(cmd, cmd._parsed);
25
-
26
- await cmd.preValidate?.();
27
- await CliCommandSchemaUtil.validate(cmd, known);
28
-
29
- await cmd._cfg!.preMain?.(cmd);
30
- await cmd.preMain?.();
31
-
32
- return known;
33
- } catch (err) {
34
- if (err instanceof CliValidationResultError) {
35
- console.error!(await HelpUtil.renderValidationError(cmd, err));
36
- console.error!(await HelpUtil.renderCommandHelp(cmd));
37
- process.exit(1);
15
+ /** Error handler */
16
+ static async #onError(err: unknown): Promise<void> {
17
+ process.exitCode ??= 1;
18
+ if (err instanceof CliValidationResultError) {
19
+ console.error!(await HelpUtil.renderValidationError(err));
20
+ console.error!(await HelpUtil.renderCommandHelp(err.command));
21
+ } else if (err instanceof CliUnknownCommandError) {
22
+ if (err.help) {
23
+ console.error!(err.help);
38
24
  } else {
39
- throw err;
25
+ console.error!(err.defaultMessage, '\n');
26
+ console.error!(await HelpUtil.renderAllHelp(''));
40
27
  }
28
+ } else {
29
+ console.error!(err);
41
30
  }
31
+ console.error!();
42
32
  }
43
33
 
44
- /** Fetch a single command */
45
- static async #getCommand(cmd: string): Promise<CliCommandShape> {
46
- try {
47
- return await CliCommandRegistry.getInstance(cmd, true);
48
- } catch (err) {
49
- if (err instanceof CliUnknownCommandError) {
50
- if (err.help) {
51
- console.error!(err.help);
52
- } else {
53
- console.error!(err.defaultMessage, '\n');
54
- console.error!(await HelpUtil.renderAllHelp(''));
55
- }
56
- process.exit(1);
57
- } else {
58
- throw err;
59
- }
60
- }
34
+ /** Run command */
35
+ static async #runCommand(cmd: string, args: string[]): Promise<void> {
36
+ const command = await CliCommandRegistry.getInstance(cmd, true);
37
+ const schema = await CliCommandSchemaUtil.getSchema(command);
38
+ const fullArgs = await CliParseUtil.expandArgs(schema, args);
39
+ const state = command._parsed = await CliParseUtil.parse(schema, fullArgs);
40
+
41
+ await command.preBind?.();
42
+ const boundArgs = await CliCommandSchemaUtil.bindInput(command, state);
43
+
44
+ await command.preValidate?.();
45
+ await CliCommandSchemaUtil.validate(command, boundArgs);
46
+
47
+ await command._cfg!.preMain?.(command);
48
+ await command.preMain?.();
49
+
50
+ ConsoleManager.debug(Env.debug);
51
+ const result = await command.main(...boundArgs);
52
+ await CliUtil.listenForResponse(result);
61
53
  }
62
54
 
63
55
  /**
@@ -67,29 +59,17 @@ export class ExecutionManager {
67
59
  static async run(argv: string[]): Promise<void> {
68
60
  try {
69
61
  const { cmd, args, help } = CliParseUtil.getArgs(argv);
70
-
71
62
  if (!cmd) {
72
63
  console.info!(await HelpUtil.renderAllHelp());
73
- return;
74
- }
75
-
76
- const command = await this.#getCommand(cmd);
77
-
78
- if (help) {
79
- console.log!(await HelpUtil.renderCommandHelp(command));
80
- return;
64
+ } else if (help) {
65
+ console.log!(await HelpUtil.renderCommandHelp(cmd));
81
66
  } else {
82
- const known = await this.#prepareAndBind(command, args);
83
- ConsoleManager.debug(Env.debug);
84
- const result = await command.main(...known);
85
- await CliUtil.listenForResponse(result);
67
+ await this.#runCommand(cmd, args);
86
68
  }
87
69
  } catch (err) {
88
- process.exitCode ??= 1;
89
- console.error!(err);
90
- console.error!();
70
+ await this.#onError(err);
91
71
  } finally {
92
- await ShutdownManager.gracefulShutdown(process.exitCode);
72
+ await ShutdownManager.gracefulShutdown();
93
73
  }
94
74
  }
95
75
  }
package/src/help.ts CHANGED
@@ -23,7 +23,8 @@ export class HelpUtil {
23
23
  * Render command-specific help
24
24
  * @param command
25
25
  */
26
- static async renderCommandHelp(command: CliCommandShape): Promise<string> {
26
+ static async renderCommandHelp(cmd: CliCommandShape | string): Promise<string> {
27
+ const command = typeof cmd === 'string' ? await CliCommandRegistry.getInstance(cmd, true) : cmd;
27
28
  const commandName = CliCommandRegistry.getName(command);
28
29
 
29
30
  await command.preHelp?.();
@@ -130,7 +131,7 @@ export class HelpUtil {
130
131
  /**
131
132
  * Render validation error to a string
132
133
  */
133
- static renderValidationError(cmd: CliCommandShape, err: CliValidationResultError): string {
134
+ static renderValidationError(err: CliValidationResultError): string {
134
135
  return [
135
136
  cliTpl`${{ failure: 'Execution failed' }}:`,
136
137
  ...err.errors.map(e => e.source && e.source !== 'custom' ?
package/src/schema.ts CHANGED
@@ -171,7 +171,7 @@ export class CliCommandSchemaUtil {
171
171
  async (): Promise<void> => {
172
172
  const res = await cmd.validate?.(...args);
173
173
  if (res) {
174
- throw new CliValidationResultError(Array.isArray(res) ? res : [res]);
174
+ throw new CliValidationResultError(cmd, Array.isArray(res) ? res : [res]);
175
175
  }
176
176
  },
177
177
  ];
@@ -187,7 +187,7 @@ export class CliCommandSchemaUtil {
187
187
 
188
188
  const errors = (await Promise.all(results)).flatMap(x => (x ?? []));
189
189
  if (errors.length) {
190
- throw new CliValidationResultError(errors);
190
+ throw new CliValidationResultError(cmd, errors);
191
191
  }
192
192
  return cmd;
193
193
  }
package/src/util.ts CHANGED
@@ -28,7 +28,10 @@ export class CliUtil {
28
28
  /**
29
29
  * Run a command as restartable, linking into self
30
30
  */
31
- static runWithRestart<T extends CliCommandShapeFields & CliCommandShape>(cmd: T): Promise<unknown> | undefined {
31
+ static runWithRestart<T extends CliCommandShapeFields & CliCommandShape>(cmd: T, ipc?: boolean): Promise<unknown> | undefined {
32
+ if (ipc && process.connected) {
33
+ ExecUtil.exitOnDisconnect();
34
+ }
32
35
  if (Env.TRV_CAN_RESTART.isFalse || !(cmd.canRestart ?? !Env.production)) {
33
36
  Env.TRV_CAN_RESTART.clear();
34
37
  return;
@@ -38,7 +41,7 @@ export class CliUtil {
38
41
  ...process.env,
39
42
  ...Env.TRV_CAN_RESTART.export(false)
40
43
  },
41
- stdio: [0, 1, 2, 'ipc']
44
+ stdio: [0, 1, 2, ipc ? 'ipc' : undefined]
42
45
  }));
43
46
  }
44
47