@jakubmazanec/cli 0.1.0-265acad → 0.1.0-593aae1
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.
|
@@ -4,7 +4,7 @@ import { type Command } from './Command.js';
|
|
|
4
4
|
import { type ProgramOptions } from './ProgramOptions.js';
|
|
5
5
|
import { type ProgramStreams } from './ProgramStreams.js';
|
|
6
6
|
/**
|
|
7
|
-
* Program manages and executes {@linkcode
|
|
7
|
+
* Program manages and executes {@linkcode Command} instances attached to it based on parsing command line arguments.
|
|
8
8
|
*/
|
|
9
9
|
export declare class Program {
|
|
10
10
|
/** Program name. */
|
package/dist/program/Program.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
import { Wrapper } from '../ui/internals.js';
|
|
3
3
|
import { Failure, Help } from '../ui.js';
|
|
4
4
|
/**
|
|
5
|
-
* Program manages and executes {@linkcode
|
|
5
|
+
* Program manages and executes {@linkcode Command} instances attached to it based on parsing command line arguments.
|
|
6
6
|
*/ export class Program {
|
|
7
7
|
/** Program name. */ name;
|
|
8
8
|
/** Program version. */ version;
|
|
@@ -88,7 +88,16 @@ import { Failure, Help } from '../ui.js';
|
|
|
88
88
|
}));
|
|
89
89
|
}
|
|
90
90
|
} else {
|
|
91
|
-
|
|
91
|
+
try {
|
|
92
|
+
await command.run(parsedArgs, this);
|
|
93
|
+
} catch (error) {
|
|
94
|
+
let { createElement } = await import('react');
|
|
95
|
+
if (error instanceof Error) {
|
|
96
|
+
await this.renderElement(createElement(Failure, {
|
|
97
|
+
error
|
|
98
|
+
}));
|
|
99
|
+
}
|
|
100
|
+
}
|
|
92
101
|
break;
|
|
93
102
|
}
|
|
94
103
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/program/Program.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/no-explicit-any -- needed */\nimport {type ParametersConfig, type Argv, type OptionsConfig} from '@jakubmazanec/args';\n\nimport {type Command} from './Command.js';\nimport {ProgramError} from './ProgramError.js';\nimport {type ProgramOptions} from './ProgramOptions.js';\nimport {type ProgramStreams} from './ProgramStreams.js';\nimport {Wrapper} from '../ui/internals.js';\nimport {Failure, Help} from '../ui.js';\n\n/**\n * Program manages and executes {@linkcode
|
|
1
|
+
{"version":3,"sources":["../../src/program/Program.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/no-explicit-any -- needed */\nimport {type ParametersConfig, type Argv, type OptionsConfig} from '@jakubmazanec/args';\n\nimport {type Command} from './Command.js';\nimport {ProgramError} from './ProgramError.js';\nimport {type ProgramOptions} from './ProgramOptions.js';\nimport {type ProgramStreams} from './ProgramStreams.js';\nimport {Wrapper} from '../ui/internals.js';\nimport {Failure, Help} from '../ui.js';\n\n/**\n * Program manages and executes {@linkcode Command} instances attached to it based on parsing command line arguments.\n */\nexport class Program {\n /** Program name. */\n readonly name?: string;\n\n /** Program version. */\n readonly version?: string;\n\n /** Program description. */\n readonly description?: string;\n\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 protected isRendering = false;\n\n readonly streams: ProgramStreams = {\n stderr: process.stderr,\n stdin: process.stdin,\n stdout: process.stdout,\n };\n\n constructor({name, version, description, bin}: ProgramOptions) {\n if (name) {\n this.name = name;\n }\n\n if (version) {\n this.version = version;\n }\n\n if (description) {\n this.description = description;\n }\n\n if (bin) {\n this.bin = bin;\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 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(createElement(Failure, {error}));\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 await this.renderElement(createElement(Failure, {error}));\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 await this.renderElement(createElement(Failure, {error}));\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"],"names":["ProgramError","Wrapper","Failure","Help","Program","name","version","description","bin","commands","isRendering","streams","stderr","process","stdin","stdout","constructor","addCommand","command","push","renderElement","element","render","createElement","program","exitOnCtrlC","patchConsole","error","Error","run","argv","hasCommandRun","parsedArgs","parseArguments","errors","length"],"mappings":"AAAA,+DAA+D,GAI/D,SAAQA,YAAY,QAAO,oBAAoB;AAG/C,SAAQC,OAAO,QAAO,qBAAqB;AAC3C,SAAQC,OAAO,EAAEC,IAAI,QAAO,WAAW;AAEvC;;CAEC,GACD,OAAO,MAAMC;IACX,kBAAkB,GAClB,AAASC,KAAc;IAEvB,qBAAqB,GACrB,AAASC,QAAiB;IAE1B,yBAAyB,GACzB,AAASC,YAAqB;IAE9B,6BAA6B,GAC7B,AAASC,IAAa;IAEtB,qBAAq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,GAAG;QACrB;IACF;IAEA;;;;GAIC,GACD,MAAMmB,IAAIC,IAAU,EAAiB;QACnC,IAAIC,gBAAgB;QAEpB,KAAK,IAAIb,WAAW,IAAI,CAACT,QAAQ,CAAE;YACjC,IAAIuB,aAAad,QAAQe,cAAc,CAACH;YAExC,IAAIE,WAAWd,OAAO,EAAE;gBACtBa,gBAAgB;gBAEhB,IAAIC,WAAWE,MAAM,CAACC,MAAM,EAAE;oBAC5B,IAAI,EAACZ,aAAa,EAAC,GAAG,MAAM,MAAM,CAAC;oBAEnC,KAAK,IAAII,SAASK,WAAWE,MAAM,CAAE;wBACnC,MAAM,IAAI,CAACd,aAAa,CAACG,cAAcrB,SAAS;4BAACyB;wBAAK;oBACxD;gBACF,OAAO;oBACL,IAAI;wBACF,MAAMT,QAAQW,GAAG,CAACG,YAAY,IAAI;oBACpC,EAAE,OAAOL,OAAgB;wBACvB,IAAI,EAACJ,aAAa,EAAC,GAAG,MAAM,MAAM,CAAC;wBAEnC,IAAII,iBAAiBC,OAAO;4BAC1B,MAAM,IAAI,CAACR,aAAa,CAACG,cAAcrB,SAAS;gCAACyB;4BAAK;wBACxD;oBACF;oBAEA;gBACF;YACF;QACF;QAEA,IAAI,CAACI,eAAe;YAClB,IAAI,EAACR,aAAa,EAAC,GAAG,MAAM,MAAM,CAAC;YAEnC,MAAM,IAAI,CAACH,aAAa,CAACG,cAAcpB,MAAM;gBAACqB,SAAS,IAAI;YAAA;QAC7D;IACF;AACF"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jakubmazanec/cli",
|
|
3
|
-
"version": "0.1.0-
|
|
3
|
+
"version": "0.1.0-593aae1",
|
|
4
4
|
"description": "TypeScript-first library for building CLIs.",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -27,10 +27,10 @@
|
|
|
27
27
|
"typecheck": "tsc --project tsconfig.typecheck.json"
|
|
28
28
|
},
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@jakubmazanec/args": "1.0.1-
|
|
31
|
-
"@jakubmazanec/error": "2.0.1-
|
|
32
|
-
"@jakubmazanec/ts-utils": "2.1.0-
|
|
33
|
-
"@jakubmazanec/zod-utils": "1.0.0-
|
|
30
|
+
"@jakubmazanec/args": "1.0.1-593aae1",
|
|
31
|
+
"@jakubmazanec/error": "2.0.1-593aae1",
|
|
32
|
+
"@jakubmazanec/ts-utils": "2.1.0-593aae1",
|
|
33
|
+
"@jakubmazanec/zod-utils": "1.0.0-593aae1",
|
|
34
34
|
"chalk": "^5.2.0",
|
|
35
35
|
"lodash": "^4.17.21",
|
|
36
36
|
"react-error-boundary": "^4.0.7"
|
package/src/program/Program.ts
CHANGED
|
@@ -9,7 +9,7 @@ import {Wrapper} from '../ui/internals.js';
|
|
|
9
9
|
import {Failure, Help} from '../ui.js';
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
|
-
* Program manages and executes {@linkcode
|
|
12
|
+
* Program manages and executes {@linkcode Command} instances attached to it based on parsing command line arguments.
|
|
13
13
|
*/
|
|
14
14
|
export class Program {
|
|
15
15
|
/** Program name. */
|
|
@@ -120,7 +120,15 @@ export class Program {
|
|
|
120
120
|
await this.renderElement(createElement(Failure, {error}));
|
|
121
121
|
}
|
|
122
122
|
} else {
|
|
123
|
-
|
|
123
|
+
try {
|
|
124
|
+
await command.run(parsedArgs, this);
|
|
125
|
+
} catch (error: unknown) {
|
|
126
|
+
let {createElement} = await import('react');
|
|
127
|
+
|
|
128
|
+
if (error instanceof Error) {
|
|
129
|
+
await this.renderElement(createElement(Failure, {error}));
|
|
130
|
+
}
|
|
131
|
+
}
|
|
124
132
|
|
|
125
133
|
break;
|
|
126
134
|
}
|