open-tag 0.0.5 → 0.1.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.
@@ -1,39 +1,88 @@
1
1
  #!/usr/bin/env node
2
- import { B as resolveOpenTagHomeLayout, I as presentCommand, L as toCommandError, M as CommandError, N as EXIT_CODES, O as CommanderError, P as commandExitCode, R as configureClientLoggerForService, S as resolveChannelEnvironment, t as createProgram, z as resolveOpenTagHome } from "../program-CpPCcBDJ.mjs";
3
- //#region src/cli/index.ts
4
- const json = process.argv.includes("--json");
5
- try {
6
- const environment = resolveChannelEnvironment(process.env);
7
- const home = resolveOpenTagHome(environment);
8
- configureClientLoggerForService(resolveOpenTagHomeLayout(home).logs);
9
- await createProgram({ json }).parseAsync(process.argv);
10
- } catch (error) {
11
- if (error instanceof CommanderError) {
12
- if (error.exitCode !== 0 && json) {
13
- const commandError = new CommandError({
14
- code: "USAGE_ERROR",
15
- category: "validation",
16
- retryability: "never",
17
- phase: "validation"
18
- }, error.message);
19
- presentCommand({
20
- ok: false,
21
- error: commandError,
22
- exitCode: EXIT_CODES.usage
23
- }, { json: true });
24
- }
25
- process.exit(error.exitCode === 0 ? 0 : EXIT_CODES.usage);
26
- } else {
2
+ import { B as presentCommand, F as CommandError, H as configureClientLoggerForService, I as EXIT_CODES, L as commandExitCode, U as resolveOpenTagHome, V as toCommandError, W as resolveOpenTagHomeLayout, a as installCliProcessErrorReporting, j as CommanderError, k as resolveChannelEnvironment, o as reportCommandFailure, s as resolveCommandPath, t as createProgram, z as observeCommandFailures } from "../program-CXagchrn.mjs";
3
+ //#region src/cli/run.ts
4
+ /**
5
+ * The CLI entry point's whole behaviour, so it can run under test with a real program.
6
+ *
7
+ * A failure is reported from exactly one of two places: the shared execution path, which handles
8
+ * most command failures and returns an exit code, or the catch below, which sees what escaped it.
9
+ * `reportCommandFailure` keeps the two from reporting the same thrown value twice.
10
+ */
11
+ async function runCli(options) {
12
+ const argv = [...options.argv];
13
+ const env = options.env ?? process.env;
14
+ const json = argv.includes("--json");
15
+ const report = options.report ?? reportCommandFailure;
16
+ const state = {};
17
+ try {
18
+ await runProgram(options, argv, env, report, state);
19
+ return EXIT_CODES.success;
20
+ } catch (error) {
21
+ if (error instanceof CommanderError) return exitForUsage(error, json, options.exit);
27
22
  const commandError = toCommandError(error);
28
- const result = {
23
+ const exitCode = presentCommand({
29
24
  ok: false,
30
25
  error: commandError,
31
26
  exitCode: commandExitCode(commandError)
32
- };
33
- process.exitCode = presentCommand(result, { json });
27
+ }, { json });
28
+ await report(error, commandError, {
29
+ command: state.command,
30
+ environment: env
31
+ });
32
+ return exitCode;
33
+ } finally {
34
+ state.detachObserver?.();
35
+ }
36
+ }
37
+ async function runProgram(options, argv, env, report, state) {
38
+ const program = options.program ?? createProgram({ json: argv.includes("--json") });
39
+ state.command = resolveCommandPath(program, argv.slice(2));
40
+ const environment = resolveChannelEnvironment(env);
41
+ const home = resolveOpenTagHome(environment);
42
+ configureClientLoggerForService(resolveOpenTagHomeLayout(home).logs);
43
+ const context = {
44
+ command: state.command,
45
+ environment: env,
46
+ home
47
+ };
48
+ if (options.processHandlers !== false) installCliProcessErrorReporting(context);
49
+ state.detachObserver = observeCommandFailures((error, commandError) => {
50
+ report(error, commandError, context);
51
+ });
52
+ await program.parseAsync(argv);
53
+ }
54
+ /**
55
+ * Commander usage errors (unknown options, missing required options or arguments) are
56
+ * input-validation failures: they exit 2, and a --json caller receives the same failure envelope
57
+ * as any other command instead of human help text.
58
+ */
59
+ function exitForUsage(error, json, exit = (code) => process.exit(code)) {
60
+ if (error.exitCode !== 0 && json) {
61
+ const commandError = new CommandError({
62
+ code: "USAGE_ERROR",
63
+ category: "validation",
64
+ retryability: "never",
65
+ phase: "validation"
66
+ }, error.message);
67
+ presentCommand({
68
+ ok: false,
69
+ error: commandError,
70
+ exitCode: EXIT_CODES.usage
71
+ }, { json: true });
34
72
  }
73
+ const code = error.exitCode === 0 ? 0 : EXIT_CODES.usage;
74
+ exit(code);
75
+ return code;
35
76
  }
36
77
  //#endregion
78
+ //#region src/cli/index.ts
79
+ process.setSourceMapsEnabled(true);
80
+ const exitCode = await runCli({
81
+ argv: process.argv,
82
+ env: process.env
83
+ });
84
+ if (exitCode !== 0) process.exitCode = exitCode;
85
+ //#endregion
37
86
  export {};
38
87
 
39
88
  //# sourceMappingURL=index.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","names":[],"sources":["../../src/cli/index.ts"],"sourcesContent":["#!/usr/bin/env node\n\nimport { configureClientLoggerForService, resolveOpenTagHome, resolveOpenTagHomeLayout } from \"@opentag/client\";\nimport { CommanderError } from \"commander\";\nimport { resolveChannelEnvironment } from \"../core/channel/environment.js\";\nimport {\n CommandError,\n type CommandResult,\n commandExitCode,\n EXIT_CODES,\n presentCommand,\n toCommandError,\n} from \"../core/command/policy.js\";\nimport { createProgram } from \"./program.js\";\n\nconst json = process.argv.includes(\"--json\");\ntry {\n const environment = resolveChannelEnvironment(process.env);\n const home = resolveOpenTagHome(environment);\n configureClientLoggerForService(resolveOpenTagHomeLayout(home).logs);\n await createProgram({ json }).parseAsync(process.argv);\n} catch (error) {\n // Commander usage errors (unknown options, missing required options or arguments) are\n // input-validation failures: they exit 2, and a --json caller receives the same failure\n // envelope as any other command instead of human help text.\n if (error instanceof CommanderError) {\n if (error.exitCode !== 0 && json) {\n const commandError = new CommandError(\n { code: \"USAGE_ERROR\", category: \"validation\", retryability: \"never\", phase: \"validation\" },\n error.message,\n );\n presentCommand({ ok: false, error: commandError, exitCode: EXIT_CODES.usage }, { json: true });\n }\n process.exit(error.exitCode === 0 ? 0 : EXIT_CODES.usage);\n } else {\n const commandError = toCommandError(error);\n const result: CommandResult<never> = {\n ok: false,\n error: commandError,\n exitCode: commandExitCode(commandError),\n };\n process.exitCode = presentCommand(result, { json });\n }\n}\n"],"mappings":";;;AAeA,MAAM,OAAO,QAAQ,KAAK,SAAS,QAAQ;AAC3C,IAAI;CACF,MAAM,cAAc,0BAA0B,QAAQ,GAAG;CACzD,MAAM,OAAO,mBAAmB,WAAW;CAC3C,gCAAgC,yBAAyB,IAAI,CAAC,CAAC,IAAI;CACnE,MAAM,cAAc,EAAE,KAAK,CAAC,CAAC,CAAC,WAAW,QAAQ,IAAI;AACvD,SAAS,OAAO;CAId,IAAI,iBAAiB,gBAAgB;EACnC,IAAI,MAAM,aAAa,KAAK,MAAM;GAChC,MAAM,eAAe,IAAI,aACvB;IAAE,MAAM;IAAe,UAAU;IAAc,cAAc;IAAS,OAAO;GAAa,GAC1F,MAAM,OACR;GACA,eAAe;IAAE,IAAI;IAAO,OAAO;IAAc,UAAU,WAAW;GAAM,GAAG,EAAE,MAAM,KAAK,CAAC;EAC/F;EACA,QAAQ,KAAK,MAAM,aAAa,IAAI,IAAI,WAAW,KAAK;CAC1D,OAAO;EACL,MAAM,eAAe,eAAe,KAAK;EACzC,MAAM,SAA+B;GACnC,IAAI;GACJ,OAAO;GACP,UAAU,gBAAgB,YAAY;EACxC;EACA,QAAQ,WAAW,eAAe,QAAQ,EAAE,KAAK,CAAC;CACpD;AACF"}
1
+ {"version":3,"file":"index.mjs","names":[],"sources":["../../src/cli/run.ts","../../src/cli/index.ts"],"sourcesContent":["import { configureClientLoggerForService, resolveOpenTagHome, resolveOpenTagHomeLayout } from \"@opentag/client\";\nimport { type Command, CommanderError } from \"commander\";\nimport { resolveChannelEnvironment } from \"../core/channel/environment.js\";\nimport {\n CommandError,\n commandExitCode,\n EXIT_CODES,\n observeCommandFailures,\n presentCommand,\n toCommandError,\n} from \"../core/command/policy.js\";\nimport {\n installCliProcessErrorReporting,\n reportCommandFailure,\n resolveCommandPath,\n} from \"../core/diagnostics/error-reporting.js\";\nimport { createProgram } from \"./program.js\";\n\nexport interface RunCliOptions {\n argv: readonly string[];\n env?: NodeJS.ProcessEnv;\n /** The program to run; the real one by default. Tests register a throwing command on it. */\n program?: Command;\n /** Relays one failure; `reportCommandFailure` by default. */\n report?: typeof reportCommandFailure;\n /** Ends the process for a Commander usage or help exit; `process.exit` by default. */\n exit?: (code: number) => void;\n /** Whether to install the process-level uncaught-failure handlers; on by default. */\n processHandlers?: boolean;\n}\n\ninterface RunState {\n command?: string;\n detachObserver?: () => void;\n}\n\n/**\n * The CLI entry point's whole behaviour, so it can run under test with a real program.\n *\n * A failure is reported from exactly one of two places: the shared execution path, which handles\n * most command failures and returns an exit code, or the catch below, which sees what escaped it.\n * `reportCommandFailure` keeps the two from reporting the same thrown value twice.\n */\nexport async function runCli(options: RunCliOptions): Promise<number> {\n const argv = [...options.argv];\n const env = options.env ?? process.env;\n const json = argv.includes(\"--json\");\n const report = options.report ?? reportCommandFailure;\n const state: RunState = {};\n try {\n await runProgram(options, argv, env, report, state);\n return EXIT_CODES.success;\n } catch (error) {\n if (error instanceof CommanderError) return exitForUsage(error, json, options.exit);\n const commandError = toCommandError(error);\n const exitCode = presentCommand(\n { ok: false, error: commandError, exitCode: commandExitCode(commandError) },\n { json },\n );\n // After the failure is presented, so reporting can only ever delay the exit, never the answer.\n await report(error, commandError, { command: state.command, environment: env });\n return exitCode;\n } finally {\n state.detachObserver?.();\n }\n}\n\nasync function runProgram(\n options: RunCliOptions,\n argv: string[],\n env: NodeJS.ProcessEnv,\n report: typeof reportCommandFailure,\n state: RunState,\n): Promise<void> {\n const program = options.program ?? createProgram({ json: argv.includes(\"--json\") });\n // The command path only, never its arguments: those are the user's and may carry secrets.\n state.command = resolveCommandPath(program, argv.slice(2));\n const environment = resolveChannelEnvironment(env);\n const home = resolveOpenTagHome(environment);\n configureClientLoggerForService(resolveOpenTagHomeLayout(home).logs);\n const context = { command: state.command, environment: env, home };\n if (options.processHandlers !== false) installCliProcessErrorReporting(context);\n state.detachObserver = observeCommandFailures((error, commandError) => {\n void report(error, commandError, context);\n });\n await program.parseAsync(argv);\n}\n\n/**\n * Commander usage errors (unknown options, missing required options or arguments) are\n * input-validation failures: they exit 2, and a --json caller receives the same failure envelope\n * as any other command instead of human help text.\n */\nfunction exitForUsage(\n error: CommanderError,\n json: boolean,\n exit: (code: number) => void = (code) => process.exit(code),\n): number {\n if (error.exitCode !== 0 && json) {\n const commandError = new CommandError(\n { code: \"USAGE_ERROR\", category: \"validation\", retryability: \"never\", phase: \"validation\" },\n error.message,\n );\n presentCommand({ ok: false, error: commandError, exitCode: EXIT_CODES.usage }, { json: true });\n }\n const code = error.exitCode === 0 ? 0 : EXIT_CODES.usage;\n exit(code);\n return code;\n}\n","#!/usr/bin/env node\n\nimport { runCli } from \"./run.js\";\n\n/*\n * The CLI ships as one bundle, so an untranslated stack names `dist/cli/index.mjs` and a line number\n * in it, which tells a reader of an error report nothing about the source it came from. Source maps\n * are consulted when a stack is captured, so asking for them here covers everything this process can\n * go on to throw. `--enable-source-maps` would do the same, but a `#!/usr/bin/env node` shebang\n * cannot portably carry a flag.\n */\nprocess.setSourceMapsEnabled(true);\n\nconst exitCode = await runCli({ argv: process.argv, env: process.env });\nif (exitCode !== 0) process.exitCode = exitCode;\n"],"mappings":";;;;;;;;;;AA2CA,eAAsB,OAAO,SAAyC;CACpE,MAAM,OAAO,CAAC,GAAG,QAAQ,IAAI;CAC7B,MAAM,MAAM,QAAQ,OAAO,QAAQ;CACnC,MAAM,OAAO,KAAK,SAAS,QAAQ;CACnC,MAAM,SAAS,QAAQ,UAAU;CACjC,MAAM,QAAkB,CAAC;CACzB,IAAI;EACF,MAAM,WAAW,SAAS,MAAM,KAAK,QAAQ,KAAK;EAClD,OAAO,WAAW;CACpB,SAAS,OAAO;EACd,IAAI,iBAAiB,gBAAgB,OAAO,aAAa,OAAO,MAAM,QAAQ,IAAI;EAClF,MAAM,eAAe,eAAe,KAAK;EACzC,MAAM,WAAW,eACf;GAAE,IAAI;GAAO,OAAO;GAAc,UAAU,gBAAgB,YAAY;EAAE,GAC1E,EAAE,KAAK,CACT;EAEA,MAAM,OAAO,OAAO,cAAc;GAAE,SAAS,MAAM;GAAS,aAAa;EAAI,CAAC;EAC9E,OAAO;CACT,UAAU;EACR,MAAM,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