@jakubmazanec/cli 1.0.0-unstable.67da34992 → 1.0.0-unstable.a43a6cfaf
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/build/_virtual/_rolldown/runtime.js +12 -0
- package/build/constants.js +6 -4
- package/build/constants.js.map +1 -7
- package/build/main.js +17 -3
- package/build/program/Command.js +55 -70
- package/build/program/Command.js.map +1 -7
- package/build/program/Program.js +153 -187
- package/build/program/Program.js.map +1 -7
- package/build/program/ProgramError.js +8 -4
- package/build/program/ProgramError.js.map +1 -7
- package/build/program/getProgramDetail.js +21 -23
- package/build/program/getProgramDetail.js.map +1 -7
- package/build/ui/Failure.js +60 -16
- package/build/ui/Failure.js.map +1 -7
- package/build/ui/Header.js +17 -16
- package/build/ui/Header.js.map +1 -7
- package/build/ui/Help.js +47 -34
- package/build/ui/Help.js.map +1 -7
- package/build/ui/Spinner.js +34 -18
- package/build/ui/Spinner.js.map +1 -7
- package/build/ui/colors.js +26 -11
- package/build/ui/colors.js.map +1 -7
- package/build/ui/form/FormRow.js +41 -71
- package/build/ui/form/FormRow.js.map +1 -7
- package/build/ui/form/SelectInput.js +90 -82
- package/build/ui/form/SelectInput.js.map +1 -7
- package/build/ui/form/TextInput.js +114 -125
- package/build/ui/form/TextInput.js.map +1 -7
- package/build/ui/form/createForm.js +56 -69
- package/build/ui/form/createForm.js.map +1 -7
- package/build/ui/getErrorDetail.js +46 -61
- package/build/ui/getErrorDetail.js.map +1 -7
- package/build/ui/icons.js +26 -11
- package/build/ui/icons.js.map +1 -7
- package/build/ui/internals/Wrapper.js +45 -43
- package/build/ui/internals/Wrapper.js.map +1 -7
- package/build/ui/internals/programContext.js +8 -2
- package/build/ui/internals/programContext.js.map +1 -7
- package/build/ui/internals/rotate.js +14 -4
- package/build/ui/internals/rotate.js.map +1 -7
- package/build/ui/useProgram.js +21 -15
- package/build/ui/useProgram.js.map +1 -7
- package/package.json +9 -9
- package/source/ui/form/TextInput.tsx +1 -0
- package/build/main.js.map +0 -7
- package/build/program/CommandAction.js +0 -1
- package/build/program/CommandAction.js.map +0 -7
- package/build/program/CommandArguments.js +0 -1
- package/build/program/CommandArguments.js.map +0 -7
- package/build/program/CommandOptions.js +0 -1
- package/build/program/CommandOptions.js.map +0 -7
- package/build/program/ProgramCreateOptions.js +0 -1
- package/build/program/ProgramCreateOptions.js.map +0 -7
- package/build/program/ProgramDetail.js +0 -1
- package/build/program/ProgramDetail.js.map +0 -7
- package/build/program/ProgramOptions.js +0 -1
- package/build/program/ProgramOptions.js.map +0 -7
- package/build/program/ProgramStreams.js +0 -1
- package/build/program/ProgramStreams.js.map +0 -7
- package/build/program.js +0 -5
- package/build/program.js.map +0 -7
- package/build/ui/ErrorDetail.js +0 -1
- package/build/ui/ErrorDetail.js.map +0 -7
- package/build/ui/form/Form.js +0 -1
- package/build/ui/form/Form.js.map +0 -7
- package/build/ui/form/FormConfig.js +0 -1
- package/build/ui/form/FormConfig.js.map +0 -7
- package/build/ui/form/FormProps.js +0 -1
- package/build/ui/form/FormProps.js.map +0 -7
- package/build/ui/form/FormResult.js +0 -1
- package/build/ui/form/FormResult.js.map +0 -7
- package/build/ui/form/FormRowConfig.js +0 -1
- package/build/ui/form/FormRowConfig.js.map +0 -7
- package/build/ui/form/FormRowsConfig.js +0 -1
- package/build/ui/form/FormRowsConfig.js.map +0 -7
- package/build/ui/form/FormValue.js +0 -1
- package/build/ui/form/FormValue.js.map +0 -7
- package/build/ui/form/FormValues.js +0 -1
- package/build/ui/form/FormValues.js.map +0 -7
- package/build/ui/form/SelectInputItem.js +0 -1
- package/build/ui/form/SelectInputItem.js.map +0 -7
- package/build/ui/form.js +0 -5
- package/build/ui/form.js.map +0 -7
- package/build/ui/internals.js +0 -4
- package/build/ui/internals.js.map +0 -7
- package/build/ui.js +0 -10
- package/build/ui.js.map +0 -7
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// \0rolldown/runtime.js
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __exportAll = (all, no_symbols) => {
|
|
4
|
+
let target = {};
|
|
5
|
+
for (var name in all) __defProp(target, name, {
|
|
6
|
+
get: all[name],
|
|
7
|
+
enumerable: true
|
|
8
|
+
});
|
|
9
|
+
if (!no_symbols) __defProp(target, Symbol.toStringTag, { value: "Module" });
|
|
10
|
+
return target;
|
|
11
|
+
};
|
|
12
|
+
export { __exportAll };
|
package/build/constants.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import path from "node:path";
|
|
2
|
+
// source/constants.ts
|
|
2
3
|
let directory = import.meta.dirname;
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
export
|
|
6
|
-
|
|
4
|
+
const PACKAGE_PATH = path.resolve(directory, "..");
|
|
5
|
+
const CACHE_PATH = path.resolve(PACKAGE_PATH, "node_modules/.cache");
|
|
6
|
+
export { CACHE_PATH, PACKAGE_PATH };
|
|
7
|
+
|
|
8
|
+
//# sourceMappingURL=constants.js.map
|
package/build/constants.js.map
CHANGED
|
@@ -1,7 +1 @@
|
|
|
1
|
-
{
|
|
2
|
-
"version": 3,
|
|
3
|
-
"sources": ["../source/constants.ts"],
|
|
4
|
-
"sourcesContent": ["import path from 'node:path';\n\nlet directory = import.meta.dirname;\n\nexport const PACKAGE_PATH = path.resolve(directory, '..');\nexport const CACHE_PATH = path.resolve(PACKAGE_PATH, 'node_modules/.cache');\n\nexport const DELIMITER = '$ ';\n"],
|
|
5
|
-
"mappings": "AAAA,OAAO,UAAU;AAEjB,IAAI,YAAY,YAAY;AAErB,aAAM,eAAe,KAAK,QAAQ,WAAW,IAAI;AACjD,aAAM,aAAa,KAAK,QAAQ,cAAc,qBAAqB;AAEnE,aAAM,YAAY;",
|
|
6
|
-
"names": []
|
|
7
|
-
}
|
|
1
|
+
{"version":3,"file":"constants.js","names":[],"sources":["../source/constants.ts"],"sourcesContent":["import path from 'node:path';\n\nlet directory = import.meta.dirname;\n\nexport const PACKAGE_PATH = path.resolve(directory, '..');\nexport const CACHE_PATH = path.resolve(PACKAGE_PATH, 'node_modules/.cache');\n\nexport const DELIMITER = '$ ';\n"],"mappings":";;AAEA,IAAI,YAAY,YAAY;AAE5B,MAAa,eAAe,KAAK,QAAQ,WAAW,IAAI;AACxD,MAAa,aAAa,KAAK,QAAQ,cAAc,qBAAqB"}
|
package/build/main.js
CHANGED
|
@@ -1,3 +1,17 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import { Command } from "./program/Command.js";
|
|
2
|
+
import { getProgramDetail } from "./program/getProgramDetail.js";
|
|
3
|
+
import { colors_exports } from "./ui/colors.js";
|
|
4
|
+
import { getErrorDetail } from "./ui/getErrorDetail.js";
|
|
5
|
+
import { Spinner } from "./ui/Spinner.js";
|
|
6
|
+
import { Header } from "./ui/Header.js";
|
|
7
|
+
import { icons_exports } from "./ui/icons.js";
|
|
8
|
+
import { Failure } from "./ui/Failure.js";
|
|
9
|
+
import { SelectInput } from "./ui/form/SelectInput.js";
|
|
10
|
+
import { TextInput } from "./ui/form/TextInput.js";
|
|
11
|
+
import { FormRow } from "./ui/form/FormRow.js";
|
|
12
|
+
import { createForm } from "./ui/form/createForm.js";
|
|
13
|
+
import { Help } from "./ui/Help.js";
|
|
14
|
+
import { useProgram } from "./ui/useProgram.js";
|
|
15
|
+
import { ProgramError } from "./program/ProgramError.js";
|
|
16
|
+
import { Program } from "./program/Program.js";
|
|
17
|
+
export { Command, Failure, FormRow, Header, Help, Program, ProgramError, SelectInput, Spinner, TextInput, colors_exports as colors, createForm, getErrorDetail, getProgramDetail, icons_exports as icons, useProgram };
|
package/build/program/Command.js
CHANGED
|
@@ -1,70 +1,55 @@
|
|
|
1
|
-
import {
|
|
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
|
-
commands: [this.name],
|
|
57
|
-
parameters: this.parameters ?? void 0,
|
|
58
|
-
options: this.options ?? void 0,
|
|
59
|
-
allowUnknownOptions: this.allowUnknownOptions,
|
|
60
|
-
allowUnknownParameters: this.allowUnknownParameters
|
|
61
|
-
});
|
|
62
|
-
}
|
|
63
|
-
/**
|
|
64
|
-
* Executes command action with provided parsed arguments and {@link Program} instance.
|
|
65
|
-
*/
|
|
66
|
-
async run(parsedArgs, program) {
|
|
67
|
-
await this.action(parsedArgs, program);
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
//# sourceMappingURL=Command.js.map
|
|
1
|
+
import { parseArguments } from "@jakubmazanec/args";
|
|
2
|
+
// source/program/Command.ts
|
|
3
|
+
/**
|
|
4
|
+
* Command encapsulates piece of a CLI's logic and is executed when {@link Program} instance parses
|
|
5
|
+
* that command from the command line arguments.
|
|
6
|
+
*/
|
|
7
|
+
var Command = class {
|
|
8
|
+
allowUnknownOptions;
|
|
9
|
+
allowUnknownParameters;
|
|
10
|
+
description;
|
|
11
|
+
name;
|
|
12
|
+
options;
|
|
13
|
+
parameters;
|
|
14
|
+
action;
|
|
15
|
+
/**
|
|
16
|
+
* @param name Command name, ie. space-separated words consisting only of letters, numbers (but not at the start of the word) or dashes.
|
|
17
|
+
* @param action Function that runs when command is executed.
|
|
18
|
+
* @param options Additionl command options.
|
|
19
|
+
*/
|
|
20
|
+
constructor(name, action, { description, parameters, options, allowUnknownOptions, allowUnknownParameters }) {
|
|
21
|
+
this.name = name;
|
|
22
|
+
this.action = action;
|
|
23
|
+
if (description) this.description = description;
|
|
24
|
+
if (parameters) this.parameters = parameters;
|
|
25
|
+
else this.parameters = null;
|
|
26
|
+
if (options) this.options = options;
|
|
27
|
+
else this.options = null;
|
|
28
|
+
if (allowUnknownOptions) this.allowUnknownOptions = allowUnknownOptions;
|
|
29
|
+
else this.allowUnknownOptions = false;
|
|
30
|
+
if (allowUnknownParameters) this.allowUnknownParameters = allowUnknownParameters;
|
|
31
|
+
else this.allowUnknownParameters = false;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Parses a list of command line arguments into an arguments object. Parser is pre-configured
|
|
35
|
+
* based on this command.
|
|
36
|
+
*/
|
|
37
|
+
parseArguments(argv) {
|
|
38
|
+
return parseArguments(argv, {
|
|
39
|
+
commands: [this.name],
|
|
40
|
+
parameters: this.parameters ?? void 0,
|
|
41
|
+
options: this.options ?? void 0,
|
|
42
|
+
allowUnknownOptions: this.allowUnknownOptions,
|
|
43
|
+
allowUnknownParameters: this.allowUnknownParameters
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Executes command action with provided parsed arguments and {@link Program} instance.
|
|
48
|
+
*/
|
|
49
|
+
async run(parsedArgs, program) {
|
|
50
|
+
await this.action(parsedArgs, program);
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
export { Command };
|
|
54
|
+
|
|
55
|
+
//# sourceMappingURL=Command.js.map
|
|
@@ -1,7 +1 @@
|
|
|
1
|
-
{
|
|
2
|
-
"version": 3,
|
|
3
|
-
"sources": ["../../source/program/Command.ts"],
|
|
4
|
-
"sourcesContent": ["import {\n type Arguments,\n type Argv,\n type OptionsConfig,\n type ParametersConfig,\n parseArguments,\n} from '@jakubmazanec/args';\n\nimport {type CommandAction} from './CommandAction.js';\nimport {type CommandOptions} from './CommandOptions.js';\nimport {type Program} from './Program.js';\n\n/**\n * Command encapsulates piece of a CLI's logic and is executed when {@link Program} instance parses\n * that command from the command line arguments.\n */\nexport class Command<\n A extends string,\n const B extends ParametersConfig | undefined = undefined,\n const C extends OptionsConfig | undefined = undefined,\n D extends boolean = false,\n E extends boolean = false,\n> {\n readonly allowUnknownOptions: D;\n readonly allowUnknownParameters: E;\n readonly description?: string;\n readonly name: A;\n readonly options: C extends OptionsConfig ? C : null;\n readonly parameters: B extends ParametersConfig ? B : null;\n private readonly action: CommandAction<A, B, C, D, E>;\n\n /**\n * @param name Command name, ie. space-separated words consisting only of letters, numbers (but not at the start of the word) or dashes.\n * @param action Function that runs when command is executed.\n * @param options Additionl command options.\n */\n constructor(\n name: A,\n action: CommandAction<A, B, C, D, E>,\n {\n description,\n parameters,\n options,\n allowUnknownOptions,\n allowUnknownParameters,\n }: CommandOptions<NonNullable<B>, NonNullable<C>, D, E>,\n ) {\n this.name = name;\n this.action = action;\n\n if (description) {\n this.description = description;\n }\n\n if (parameters) {\n this.parameters = parameters as B extends ParametersConfig ? B : null;\n } else {\n this.parameters = null as B extends ParametersConfig ? B : null;\n }\n\n if (options) {\n this.options = options as C extends OptionsConfig ? C : null;\n } else {\n this.options = null as C extends OptionsConfig ? C : null;\n }\n\n if (allowUnknownOptions) {\n this.allowUnknownOptions = allowUnknownOptions;\n } else {\n this.allowUnknownOptions = false as D;\n }\n\n if (allowUnknownParameters) {\n this.allowUnknownParameters = allowUnknownParameters;\n } else {\n this.allowUnknownParameters = false as E;\n }\n }\n\n /**\n * Parses a list of command line arguments into an arguments object. Parser is pre-configured\n * based on this command.\n */\n parseArguments(argv: Argv): Arguments<{\n commands: readonly [A];\n parameters: B;\n options: C;\n allowUnknownOptions: D;\n allowUnknownParameters: E;\n }> {\n return parseArguments(argv, {\n commands: [this.name],\n parameters: this.parameters ?? undefined,\n options: this.options ?? undefined,\n allowUnknownOptions: this.allowUnknownOptions,\n allowUnknownParameters: this.allowUnknownParameters,\n } as {\n commands: readonly [A];\n parameters: B;\n options: C;\n allowUnknownOptions: D;\n allowUnknownParameters: E;\n });\n }\n\n /**\n * Executes command action with provided parsed arguments and {@link Program} instance.\n */\n async run(\n parsedArgs: Arguments<{\n commands: readonly [A];\n parameters: B;\n options: C;\n allowUnknownOptions: D;\n allowUnknownParameters: E;\n }>,\n program: Program,\n ): Promise<void> {\n await this.action(parsedArgs, program);\n }\n}\n"],
|
|
5
|
-
"mappings": "AAAA;AAAA,EAKE;AAAA,OACK;AAUA,aAAM,QAMX;AAAA,EACS;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACQ;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOjB,YACE,MACA,QACA;AAAA,IACE;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF,GACA;AACA,SAAK,OAAO;AACZ,SAAK,SAAS;AAEd,QAAI,aAAa;AACf,WAAK,cAAc;AAAA,IACrB;AAEA,QAAI,YAAY;AACd,WAAK,aAAa;AAAA,IACpB,OAAO;AACL,WAAK,aAAa;AAAA,IACpB;AAEA,QAAI,SAAS;AACX,WAAK,UAAU;AAAA,IACjB,OAAO;AACL,WAAK,UAAU;AAAA,IACjB;AAEA,QAAI,qBAAqB;AACvB,WAAK,sBAAsB;AAAA,IAC7B,OAAO;AACL,WAAK,sBAAsB;AAAA,IAC7B;AAEA,QAAI,wBAAwB;AAC1B,WAAK,yBAAyB;AAAA,IAChC,OAAO;AACL,WAAK,yBAAyB;AAAA,IAChC;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,eAAe,MAMZ;AACD,WAAO,eAAe,MAAM;AAAA,MAC1B,UAAU,CAAC,KAAK,IAAI;AAAA,MACpB,YAAY,KAAK,cAAc;AAAA,MAC/B,SAAS,KAAK,WAAW;AAAA,MACzB,qBAAqB,KAAK;AAAA,MAC1B,wBAAwB,KAAK;AAAA,IAC/B,CAMC;AAAA,EACH;AAAA;AAAA;AAAA;AAAA,EAKA,MAAM,IACJ,YAOA,SACe;AACf,UAAM,KAAK,OAAO,YAAY,OAAO;AAAA,EACvC;AACF;",
|
|
6
|
-
"names": []
|
|
7
|
-
}
|
|
1
|
+
{"version":3,"file":"Command.js","names":[],"sources":["../../source/program/Command.ts"],"sourcesContent":["import {\n type Arguments,\n type Argv,\n type OptionsConfig,\n type ParametersConfig,\n parseArguments,\n} from '@jakubmazanec/args';\n\nimport {type CommandAction} from './CommandAction.js';\nimport {type CommandOptions} from './CommandOptions.js';\nimport {type Program} from './Program.js';\n\n/**\n * Command encapsulates piece of a CLI's logic and is executed when {@link Program} instance parses\n * that command from the command line arguments.\n */\nexport class Command<\n A extends string,\n const B extends ParametersConfig | undefined = undefined,\n const C extends OptionsConfig | undefined = undefined,\n D extends boolean = false,\n E extends boolean = false,\n> {\n readonly allowUnknownOptions: D;\n readonly allowUnknownParameters: E;\n readonly description?: string;\n readonly name: A;\n readonly options: C extends OptionsConfig ? C : null;\n readonly parameters: B extends ParametersConfig ? B : null;\n private readonly action: CommandAction<A, B, C, D, E>;\n\n /**\n * @param name Command name, ie. space-separated words consisting only of letters, numbers (but not at the start of the word) or dashes.\n * @param action Function that runs when command is executed.\n * @param options Additionl command options.\n */\n constructor(\n name: A,\n action: CommandAction<A, B, C, D, E>,\n {\n description,\n parameters,\n options,\n allowUnknownOptions,\n allowUnknownParameters,\n }: CommandOptions<NonNullable<B>, NonNullable<C>, D, E>,\n ) {\n this.name = name;\n this.action = action;\n\n if (description) {\n this.description = description;\n }\n\n if (parameters) {\n this.parameters = parameters as B extends ParametersConfig ? B : null;\n } else {\n this.parameters = null as B extends ParametersConfig ? B : null;\n }\n\n if (options) {\n this.options = options as C extends OptionsConfig ? C : null;\n } else {\n this.options = null as C extends OptionsConfig ? C : null;\n }\n\n if (allowUnknownOptions) {\n this.allowUnknownOptions = allowUnknownOptions;\n } else {\n this.allowUnknownOptions = false as D;\n }\n\n if (allowUnknownParameters) {\n this.allowUnknownParameters = allowUnknownParameters;\n } else {\n this.allowUnknownParameters = false as E;\n }\n }\n\n /**\n * Parses a list of command line arguments into an arguments object. Parser is pre-configured\n * based on this command.\n */\n parseArguments(argv: Argv): Arguments<{\n commands: readonly [A];\n parameters: B;\n options: C;\n allowUnknownOptions: D;\n allowUnknownParameters: E;\n }> {\n return parseArguments(argv, {\n commands: [this.name],\n parameters: this.parameters ?? undefined,\n options: this.options ?? undefined,\n allowUnknownOptions: this.allowUnknownOptions,\n allowUnknownParameters: this.allowUnknownParameters,\n } as {\n commands: readonly [A];\n parameters: B;\n options: C;\n allowUnknownOptions: D;\n allowUnknownParameters: E;\n });\n }\n\n /**\n * Executes command action with provided parsed arguments and {@link Program} instance.\n */\n async run(\n parsedArgs: Arguments<{\n commands: readonly [A];\n parameters: B;\n options: C;\n allowUnknownOptions: D;\n allowUnknownParameters: E;\n }>,\n program: Program,\n ): Promise<void> {\n await this.action(parsedArgs, program);\n }\n}\n"],"mappings":";;;;;;AAgBA,IAAa,UAAb,MAME;CACA;CACA;CACA;CACA;CACA;CACA;CACA;;;;;;CAOA,YACE,MACA,QACA,EACE,aACA,YACA,SACA,qBACA,0BAEF;EACA,KAAK,OAAO;EACZ,KAAK,SAAS;EAEd,IAAI,aACF,KAAK,cAAc;EAGrB,IAAI,YACF,KAAK,aAAa;OAElB,KAAK,aAAa;EAGpB,IAAI,SACF,KAAK,UAAU;OAEf,KAAK,UAAU;EAGjB,IAAI,qBACF,KAAK,sBAAsB;OAE3B,KAAK,sBAAsB;EAG7B,IAAI,wBACF,KAAK,yBAAyB;OAE9B,KAAK,yBAAyB;CAElC;;;;;CAMA,eAAe,MAMZ;EACD,OAAO,eAAe,MAAM;GAC1B,UAAU,CAAC,KAAK,IAAI;GACpB,YAAY,KAAK,cAAc,KAAA;GAC/B,SAAS,KAAK,WAAW,KAAA;GACzB,qBAAqB,KAAK;GAC1B,wBAAwB,KAAK;EAC/B,CAMC;CACH;;;;CAKA,MAAM,IACJ,YAOA,SACe;EACf,MAAM,KAAK,OAAO,YAAY,OAAO;CACvC;AACF"}
|
package/build/program/Program.js
CHANGED
|
@@ -1,190 +1,156 @@
|
|
|
1
|
+
import { CACHE_PATH } from "../constants.js";
|
|
2
|
+
import { Failure } from "../ui/Failure.js";
|
|
3
|
+
import { Help } from "../ui/Help.js";
|
|
4
|
+
import { Wrapper } from "../ui/internals/Wrapper.js";
|
|
5
|
+
import { ProgramError } from "./ProgramError.js";
|
|
1
6
|
import fs from "fs-extra";
|
|
2
7
|
import fetchLatestVersion from "latest-version";
|
|
3
8
|
import path from "node:path";
|
|
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
|
-
if (parsedArgs.errors.length) {
|
|
153
|
-
let { createElement } = await import("react");
|
|
154
|
-
for (let error of parsedArgs.errors) {
|
|
155
|
-
process.exitCode = 1;
|
|
156
|
-
await this.renderElement(
|
|
157
|
-
createElement(this.ErrorBoundaryFallbackComponent, {
|
|
158
|
-
error,
|
|
159
|
-
resetErrorBoundary: () => {
|
|
160
|
-
}
|
|
161
|
-
})
|
|
162
|
-
);
|
|
163
|
-
}
|
|
164
|
-
} else {
|
|
165
|
-
try {
|
|
166
|
-
await command.run(parsedArgs, this);
|
|
167
|
-
} catch (error) {
|
|
168
|
-
let { createElement } = await import("react");
|
|
169
|
-
if (error instanceof Error) {
|
|
170
|
-
process.exitCode = 1;
|
|
171
|
-
await this.renderElement(
|
|
172
|
-
createElement(this.ErrorBoundaryFallbackComponent, {
|
|
173
|
-
error,
|
|
174
|
-
resetErrorBoundary: () => {
|
|
175
|
-
}
|
|
176
|
-
})
|
|
177
|
-
);
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
break;
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
if (!hasCommandRun) {
|
|
185
|
-
let { createElement } = await import("react");
|
|
186
|
-
await this.renderElement(createElement(Help, { program: this }));
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
//# sourceMappingURL=Program.js.map
|
|
9
|
+
// source/program/Program.ts
|
|
10
|
+
/**
|
|
11
|
+
* Program manages and executes {@link Command} instances attached to it based on parsing command
|
|
12
|
+
* line arguments.
|
|
13
|
+
*/
|
|
14
|
+
var Program = class {
|
|
15
|
+
/** Program executable name. */
|
|
16
|
+
bin;
|
|
17
|
+
/** Program commands */
|
|
18
|
+
commands = [];
|
|
19
|
+
/** Program description. */
|
|
20
|
+
description;
|
|
21
|
+
/** More readable program name. */
|
|
22
|
+
displayName;
|
|
23
|
+
/** Custom error boundary fallback component */
|
|
24
|
+
ErrorBoundaryFallbackComponent;
|
|
25
|
+
isRendering = false;
|
|
26
|
+
/** Program latest version. */
|
|
27
|
+
latestVersion;
|
|
28
|
+
/** Program name. */
|
|
29
|
+
name;
|
|
30
|
+
streams = {
|
|
31
|
+
stderr: process.stderr,
|
|
32
|
+
stdin: process.stdin,
|
|
33
|
+
stdout: process.stdout
|
|
34
|
+
};
|
|
35
|
+
/** Program version. */
|
|
36
|
+
version;
|
|
37
|
+
constructor({ name, displayName, version, latestVersion, description, bin, ErrorBoundaryFallbackComponent }) {
|
|
38
|
+
if (name) this.name = name;
|
|
39
|
+
if (displayName) this.displayName = displayName;
|
|
40
|
+
if (version) this.version = version;
|
|
41
|
+
if (latestVersion) this.latestVersion = latestVersion;
|
|
42
|
+
if (description) this.description = description;
|
|
43
|
+
if (bin) this.bin = bin;
|
|
44
|
+
if (ErrorBoundaryFallbackComponent) this.ErrorBoundaryFallbackComponent = ErrorBoundaryFallbackComponent;
|
|
45
|
+
else this.ErrorBoundaryFallbackComponent = Failure;
|
|
46
|
+
}
|
|
47
|
+
static async create({ name, version, description, bin, ErrorBoundaryFallbackComponent, checkForUpdate }) {
|
|
48
|
+
let latestVersion;
|
|
49
|
+
if (checkForUpdate && name) {
|
|
50
|
+
let cachePath = path.join(CACHE_PATH, name, "latest-version.json");
|
|
51
|
+
let cachedLatestVersion;
|
|
52
|
+
try {
|
|
53
|
+
cachedLatestVersion = await fs.readJson(cachePath);
|
|
54
|
+
} catch {}
|
|
55
|
+
if (!cachedLatestVersion || Date.now() - cachedLatestVersion.datetime > 864e4) {
|
|
56
|
+
latestVersion = await fetchLatestVersion(name);
|
|
57
|
+
await fs.ensureFile(cachePath);
|
|
58
|
+
await fs.writeJson(cachePath, {
|
|
59
|
+
value: latestVersion,
|
|
60
|
+
datetime: Date.now()
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
return new this({
|
|
65
|
+
name,
|
|
66
|
+
version,
|
|
67
|
+
latestVersion,
|
|
68
|
+
description,
|
|
69
|
+
bin,
|
|
70
|
+
ErrorBoundaryFallbackComponent
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Adds command to the program.
|
|
75
|
+
*
|
|
76
|
+
* @param command Command to add.
|
|
77
|
+
* @returns Program instance.
|
|
78
|
+
*/
|
|
79
|
+
addCommand(command) {
|
|
80
|
+
this.commands.push(command);
|
|
81
|
+
return this;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Renders a React element using Ink and outputs to the configured streams.
|
|
85
|
+
*
|
|
86
|
+
* @param element The React element to render.
|
|
87
|
+
*/
|
|
88
|
+
async renderElement(element) {
|
|
89
|
+
if (this.isRendering) throw new ProgramError("NO_NESTED_RENDER");
|
|
90
|
+
else this.isRendering = true;
|
|
91
|
+
let { render } = await import("ink");
|
|
92
|
+
let { createElement } = await import("react");
|
|
93
|
+
let { stdin, stdout, stderr } = this.streams;
|
|
94
|
+
try {
|
|
95
|
+
render(createElement(Wrapper, { program: this }, element), {
|
|
96
|
+
exitOnCtrlC: true,
|
|
97
|
+
patchConsole: true,
|
|
98
|
+
stderr,
|
|
99
|
+
stdin,
|
|
100
|
+
stdout
|
|
101
|
+
});
|
|
102
|
+
} catch (error) {
|
|
103
|
+
if (error instanceof Error) await this.renderElement(createElement(this.ErrorBoundaryFallbackComponent, {
|
|
104
|
+
error,
|
|
105
|
+
resetErrorBoundary: () => {}
|
|
106
|
+
}));
|
|
107
|
+
} finally {
|
|
108
|
+
this.isRendering = false;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Runs the program.
|
|
113
|
+
*
|
|
114
|
+
* @param argv Unparsed command line arguments.
|
|
115
|
+
*/
|
|
116
|
+
async run(argv) {
|
|
117
|
+
let hasCommandRun = false;
|
|
118
|
+
for (let command of this.commands) {
|
|
119
|
+
let parsedArgs = command.parseArguments(argv);
|
|
120
|
+
if (parsedArgs.command) {
|
|
121
|
+
hasCommandRun = true;
|
|
122
|
+
if (parsedArgs.errors.length) {
|
|
123
|
+
let { createElement } = await import("react");
|
|
124
|
+
for (let error of parsedArgs.errors) {
|
|
125
|
+
process.exitCode = 1;
|
|
126
|
+
await this.renderElement(createElement(this.ErrorBoundaryFallbackComponent, {
|
|
127
|
+
error,
|
|
128
|
+
resetErrorBoundary: () => {}
|
|
129
|
+
}));
|
|
130
|
+
}
|
|
131
|
+
} else {
|
|
132
|
+
try {
|
|
133
|
+
await command.run(parsedArgs, this);
|
|
134
|
+
} catch (error) {
|
|
135
|
+
let { createElement } = await import("react");
|
|
136
|
+
if (error instanceof Error) {
|
|
137
|
+
process.exitCode = 1;
|
|
138
|
+
await this.renderElement(createElement(this.ErrorBoundaryFallbackComponent, {
|
|
139
|
+
error,
|
|
140
|
+
resetErrorBoundary: () => {}
|
|
141
|
+
}));
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
break;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
if (!hasCommandRun) {
|
|
149
|
+
let { createElement } = await import("react");
|
|
150
|
+
await this.renderElement(createElement(Help, { program: this }));
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
};
|
|
154
|
+
export { Program };
|
|
155
|
+
|
|
156
|
+
//# sourceMappingURL=Program.js.map
|
|
@@ -1,7 +1 @@
|
|
|
1
|
-
{
|
|
2
|
-
"version": 3,
|
|
3
|
-
"sources": ["../../source/program/Program.ts"],
|
|
4
|
-
"sourcesContent": ["/* eslint-disable @typescript-eslint/no-explicit-any -- needed */\nimport {type Argv, type OptionsConfig, type ParametersConfig} from '@jakubmazanec/args';\nimport fs from 'fs-extra';\nimport fetchLatestVersion from 'latest-version';\nimport path from 'node:path';\nimport {type ComponentType} from 'react';\nimport {type FallbackProps} from 'react-error-boundary';\n\nimport {CACHE_PATH} from '../constants.js';\nimport {Failure, Help} from '../ui.js';\nimport {Wrapper} from '../ui/internals.js';\nimport {type Command} from './Command.js';\nimport {type ProgramCreateOptions} from './ProgramCreateOptions.js';\nimport {ProgramError} from './ProgramError.js';\nimport {type ProgramOptions} from './ProgramOptions.js';\nimport {type ProgramStreams} from './ProgramStreams.js';\n\n/**\n * Program manages and executes {@link Command} instances attached to it based on parsing command\n * line arguments.\n */\nexport class Program {\n /** Program executable name. */\n readonly bin?: string;\n\n /** Program commands */\n readonly commands: Array<\n Command<string, ParametersConfig | undefined, OptionsConfig | undefined, boolean, boolean>\n > = [];\n\n /** Program description. */\n readonly description?: string;\n\n /** More readable program name. */\n readonly displayName?: string;\n\n /** Custom error boundary fallback component */\n ErrorBoundaryFallbackComponent: ComponentType<FallbackProps>;\n\n protected isRendering = false;\n\n /** Program latest version. */\n readonly latestVersion?: string;\n\n /** Program name. */\n readonly name?: string;\n\n readonly streams: ProgramStreams = {\n stderr: process.stderr,\n stdin: process.stdin,\n stdout: process.stdout,\n };\n\n /** Program version. */\n readonly version?: string;\n\n private constructor({\n name,\n displayName,\n version,\n latestVersion,\n description,\n bin,\n ErrorBoundaryFallbackComponent,\n }: ProgramOptions) {\n if (name) {\n this.name = name;\n }\n\n if (displayName) {\n this.displayName = displayName;\n }\n\n if (version) {\n this.version = version;\n }\n\n if (latestVersion) {\n this.latestVersion = latestVersion;\n }\n\n if (description) {\n this.description = description;\n }\n\n if (bin) {\n this.bin = bin;\n }\n\n if (ErrorBoundaryFallbackComponent) {\n this.ErrorBoundaryFallbackComponent = ErrorBoundaryFallbackComponent;\n } else {\n this.ErrorBoundaryFallbackComponent = Failure;\n }\n }\n\n static async create({\n name,\n version,\n description,\n bin,\n ErrorBoundaryFallbackComponent,\n checkForUpdate,\n }: ProgramCreateOptions) {\n let latestVersion: string | undefined;\n\n if (checkForUpdate && name) {\n // first we try to read the cached latest version\n type CachedLatestVersion = {\n value: string;\n datetime: number;\n };\n\n let cachePath = path.join(CACHE_PATH, name, 'latest-version.json');\n let cachedLatestVersion: CachedLatestVersion | undefined;\n\n try {\n cachedLatestVersion = (await fs.readJson(cachePath)) as CachedLatestVersion;\n } catch {\n // no-op\n }\n\n // if there is no cache or the cached value is old, refetch and recache\n if (!cachedLatestVersion || Date.now() - cachedLatestVersion.datetime > 86_400_00) {\n latestVersion = await fetchLatestVersion(name);\n\n await fs.ensureFile(cachePath);\n await fs.writeJson(cachePath, {\n value: latestVersion,\n datetime: Date.now(),\n });\n }\n }\n\n return new this({\n name,\n version,\n latestVersion,\n description,\n bin,\n ErrorBoundaryFallbackComponent,\n });\n }\n\n /**\n * Adds command to the program.\n *\n * @param command Command to add.\n * @returns Program instance.\n */\n addCommand(command: Command<any, any, any, any, any>): this {\n // eslint-disable-next-line @typescript-eslint/no-unsafe-argument -- it doesn't matter here; `any` is used to make TypeScript happy in this method, but isn't propagated further.\n this.commands.push(command);\n\n return this;\n }\n\n /**\n * Renders a React element using Ink and outputs to the configured streams.\n *\n * @param element The React element to render.\n */\n async renderElement(element: React.ReactElement): Promise<void> {\n if (this.isRendering) {\n throw new ProgramError('NO_NESTED_RENDER');\n } else {\n this.isRendering = true;\n }\n\n let {render} = await import('ink');\n let {createElement} = await import('react');\n let {stdin, stdout, stderr} = this.streams;\n\n try {\n render(createElement(Wrapper, {program: this}, element), {\n exitOnCtrlC: true,\n patchConsole: true,\n stderr,\n stdin,\n stdout,\n });\n } catch (error: unknown) {\n if (error instanceof Error) {\n await this.renderElement(\n createElement(this.ErrorBoundaryFallbackComponent, {error, resetErrorBoundary: () => {}}),\n );\n }\n } finally {\n this.isRendering = false;\n }\n }\n\n /**\n * Runs the program.\n *\n * @param argv Unparsed command line arguments.\n */\n async run(argv: Argv): Promise<void> {\n let hasCommandRun = false;\n\n for (let command of this.commands) {\n let parsedArgs = command.parseArguments(argv);\n\n if (parsedArgs.command) {\n hasCommandRun = true;\n\n if (parsedArgs.errors.length) {\n let {createElement} = await import('react');\n\n for (let error of parsedArgs.errors) {\n process.exitCode = 1;\n\n await this.renderElement(\n createElement(this.ErrorBoundaryFallbackComponent, {\n error,\n resetErrorBoundary: () => {},\n }),\n );\n }\n } else {\n try {\n await command.run(parsedArgs, this);\n } catch (error: unknown) {\n let {createElement} = await import('react');\n\n if (error instanceof Error) {\n process.exitCode = 1;\n\n await this.renderElement(\n createElement(this.ErrorBoundaryFallbackComponent, {\n error,\n resetErrorBoundary: () => {},\n }),\n );\n }\n }\n\n break;\n }\n }\n }\n\n if (!hasCommandRun) {\n let {createElement} = await import('react');\n\n await this.renderElement(createElement(Help, {program: this}));\n }\n }\n}\n"],
|
|
5
|
-
"mappings": "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",
|
|
6
|
-
"names": []
|
|
7
|
-
}
|
|
1
|
+
{"version":3,"file":"Program.js","names":[],"sources":["../../source/program/Program.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/no-explicit-any -- needed */\nimport {type Argv, type OptionsConfig, type ParametersConfig} from '@jakubmazanec/args';\nimport fs from 'fs-extra';\nimport fetchLatestVersion from 'latest-version';\nimport path from 'node:path';\nimport {type ComponentType} from 'react';\nimport {type FallbackProps} from 'react-error-boundary';\n\nimport {CACHE_PATH} from '../constants.js';\nimport {Failure, Help} from '../ui.js';\nimport {Wrapper} from '../ui/internals.js';\nimport {type Command} from './Command.js';\nimport {type ProgramCreateOptions} from './ProgramCreateOptions.js';\nimport {ProgramError} from './ProgramError.js';\nimport {type ProgramOptions} from './ProgramOptions.js';\nimport {type ProgramStreams} from './ProgramStreams.js';\n\n/**\n * Program manages and executes {@link Command} instances attached to it based on parsing command\n * line arguments.\n */\nexport class Program {\n /** Program executable name. */\n readonly bin?: string;\n\n /** Program commands */\n readonly commands: Array<\n Command<string, ParametersConfig | undefined, OptionsConfig | undefined, boolean, boolean>\n > = [];\n\n /** Program description. */\n readonly description?: string;\n\n /** More readable program name. */\n readonly displayName?: string;\n\n /** Custom error boundary fallback component */\n ErrorBoundaryFallbackComponent: ComponentType<FallbackProps>;\n\n protected isRendering = false;\n\n /** Program latest version. */\n readonly latestVersion?: string;\n\n /** Program name. */\n readonly name?: string;\n\n readonly streams: ProgramStreams = {\n stderr: process.stderr,\n stdin: process.stdin,\n stdout: process.stdout,\n };\n\n /** Program version. */\n readonly version?: string;\n\n private constructor({\n name,\n displayName,\n version,\n latestVersion,\n description,\n bin,\n ErrorBoundaryFallbackComponent,\n }: ProgramOptions) {\n if (name) {\n this.name = name;\n }\n\n if (displayName) {\n this.displayName = displayName;\n }\n\n if (version) {\n this.version = version;\n }\n\n if (latestVersion) {\n this.latestVersion = latestVersion;\n }\n\n if (description) {\n this.description = description;\n }\n\n if (bin) {\n this.bin = bin;\n }\n\n if (ErrorBoundaryFallbackComponent) {\n this.ErrorBoundaryFallbackComponent = ErrorBoundaryFallbackComponent;\n } else {\n this.ErrorBoundaryFallbackComponent = Failure;\n }\n }\n\n static async create({\n name,\n version,\n description,\n bin,\n ErrorBoundaryFallbackComponent,\n checkForUpdate,\n }: ProgramCreateOptions) {\n let latestVersion: string | undefined;\n\n if (checkForUpdate && name) {\n // first we try to read the cached latest version\n type CachedLatestVersion = {\n value: string;\n datetime: number;\n };\n\n let cachePath = path.join(CACHE_PATH, name, 'latest-version.json');\n let cachedLatestVersion: CachedLatestVersion | undefined;\n\n try {\n cachedLatestVersion = (await fs.readJson(cachePath)) as CachedLatestVersion;\n } catch {\n // no-op\n }\n\n // if there is no cache or the cached value is old, refetch and recache\n if (!cachedLatestVersion || Date.now() - cachedLatestVersion.datetime > 86_400_00) {\n latestVersion = await fetchLatestVersion(name);\n\n await fs.ensureFile(cachePath);\n await fs.writeJson(cachePath, {\n value: latestVersion,\n datetime: Date.now(),\n });\n }\n }\n\n return new this({\n name,\n version,\n latestVersion,\n description,\n bin,\n ErrorBoundaryFallbackComponent,\n });\n }\n\n /**\n * Adds command to the program.\n *\n * @param command Command to add.\n * @returns Program instance.\n */\n addCommand(command: Command<any, any, any, any, any>): this {\n // eslint-disable-next-line @typescript-eslint/no-unsafe-argument -- it doesn't matter here; `any` is used to make TypeScript happy in this method, but isn't propagated further.\n this.commands.push(command);\n\n return this;\n }\n\n /**\n * Renders a React element using Ink and outputs to the configured streams.\n *\n * @param element The React element to render.\n */\n async renderElement(element: React.ReactElement): Promise<void> {\n if (this.isRendering) {\n throw new ProgramError('NO_NESTED_RENDER');\n } else {\n this.isRendering = true;\n }\n\n let {render} = await import('ink');\n let {createElement} = await import('react');\n let {stdin, stdout, stderr} = this.streams;\n\n try {\n render(createElement(Wrapper, {program: this}, element), {\n exitOnCtrlC: true,\n patchConsole: true,\n stderr,\n stdin,\n stdout,\n });\n } catch (error: unknown) {\n if (error instanceof Error) {\n await this.renderElement(\n createElement(this.ErrorBoundaryFallbackComponent, {error, resetErrorBoundary: () => {}}),\n );\n }\n } finally {\n this.isRendering = false;\n }\n }\n\n /**\n * Runs the program.\n *\n * @param argv Unparsed command line arguments.\n */\n async run(argv: Argv): Promise<void> {\n let hasCommandRun = false;\n\n for (let command of this.commands) {\n let parsedArgs = command.parseArguments(argv);\n\n if (parsedArgs.command) {\n hasCommandRun = true;\n\n if (parsedArgs.errors.length) {\n let {createElement} = await import('react');\n\n for (let error of parsedArgs.errors) {\n process.exitCode = 1;\n\n await this.renderElement(\n createElement(this.ErrorBoundaryFallbackComponent, {\n error,\n resetErrorBoundary: () => {},\n }),\n );\n }\n } else {\n try {\n await command.run(parsedArgs, this);\n } catch (error: unknown) {\n let {createElement} = await import('react');\n\n if (error instanceof Error) {\n process.exitCode = 1;\n\n await this.renderElement(\n createElement(this.ErrorBoundaryFallbackComponent, {\n error,\n resetErrorBoundary: () => {},\n }),\n );\n }\n }\n\n break;\n }\n }\n }\n\n if (!hasCommandRun) {\n let {createElement} = await import('react');\n\n await this.renderElement(createElement(Help, {program: this}));\n }\n }\n}\n"],"mappings":"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"}
|
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
import { createCustomError } from "@jakubmazanec/error";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
2
|
+
// source/program/ProgramError.ts
|
|
3
|
+
/**
|
|
4
|
+
* A subclass of `Error` that indicates a program failure.
|
|
5
|
+
*/
|
|
6
|
+
const ProgramError = createCustomError("ProgramError", { NO_NESTED_RENDER: "A React render process already exists. This usually occurs by triggering nested render calls." });
|
|
7
|
+
export { ProgramError };
|
|
8
|
+
|
|
9
|
+
//# sourceMappingURL=ProgramError.js.map
|
|
@@ -1,7 +1 @@
|
|
|
1
|
-
{
|
|
2
|
-
"version": 3,
|
|
3
|
-
"sources": ["../../source/program/ProgramError.ts"],
|
|
4
|
-
"sourcesContent": ["import {createCustomError} from '@jakubmazanec/error';\n\n/**\n * A subclass of `Error` that indicates a program failure.\n */\nexport const ProgramError = createCustomError('ProgramError', {\n NO_NESTED_RENDER:\n 'A React render process already exists. This usually occurs by triggering nested render calls.',\n});\n"],
|
|
5
|
-
"mappings": "AAAA,SAAQ,yBAAwB;AAKzB,aAAM,eAAe,kBAAkB,gBAAgB;AAAA,EAC5D,kBACE;AACJ,CAAC;",
|
|
6
|
-
"names": []
|
|
7
|
-
}
|
|
1
|
+
{"version":3,"file":"ProgramError.js","names":[],"sources":["../../source/program/ProgramError.ts"],"sourcesContent":["import {createCustomError} from '@jakubmazanec/error';\n\n/**\n * A subclass of `Error` that indicates a program failure.\n */\nexport const ProgramError = createCustomError('ProgramError', {\n NO_NESTED_RENDER:\n 'A React render process already exists. This usually occurs by triggering nested render calls.',\n});\n"],"mappings":";;;;;AAKA,MAAa,eAAe,kBAAkB,gBAAgB,EAC5D,kBACE,gGACJ,CAAC"}
|