@travetto/cli 3.0.0-rc.9 → 3.0.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.
- package/README.md +9 -4
- package/package.json +6 -8
- package/src/command-manager.ts +6 -1
- package/src/command.ts +6 -2
- package/src/execute.ts +10 -33
- package/src/module.ts +8 -2
- package/src/scm.ts +2 -3
- package/support/cli.exec.ts +6 -2
- package/support/cli.main.ts +38 -0
package/README.md
CHANGED
|
@@ -1,11 +1,15 @@
|
|
|
1
1
|
<!-- This file was generated by @travetto/doc and should not be modified directly -->
|
|
2
2
|
<!-- Please modify https://github.com/travetto/travetto/tree/main/module/cli/DOC.ts and execute "npx trv doc" to rebuild -->
|
|
3
3
|
# Command Line Interface
|
|
4
|
-
## CLI infrastructure for
|
|
4
|
+
## CLI infrastructure for Travetto framework
|
|
5
5
|
|
|
6
6
|
**Install: @travetto/cli**
|
|
7
7
|
```bash
|
|
8
8
|
npm install @travetto/cli
|
|
9
|
+
|
|
10
|
+
# or
|
|
11
|
+
|
|
12
|
+
yarn add @travetto/cli
|
|
9
13
|
```
|
|
10
14
|
|
|
11
15
|
The cli is the primary structure for interacting with the external requirements of the framework. This can range from running tests, to running applications, to generating email templates. The main executable can be installed globally or locally. If installed globally and locally, it will defer to the local installation for execution.
|
|
@@ -21,12 +25,13 @@ $ trv --help
|
|
|
21
25
|
Usage: [options] [command]
|
|
22
26
|
|
|
23
27
|
Options:
|
|
24
|
-
-V, --version
|
|
25
|
-
-h, --help
|
|
28
|
+
-V, --version output the version number
|
|
29
|
+
-h, --help display help for command
|
|
26
30
|
|
|
27
31
|
Commands:
|
|
28
32
|
echo [options] [args...]
|
|
29
|
-
|
|
33
|
+
main <fileOrImport> [args...]
|
|
34
|
+
help [command] display help for command
|
|
30
35
|
```
|
|
31
36
|
|
|
32
37
|
This will show all the available options/choices that are exposed given the currently installed modules.
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@travetto/cli",
|
|
3
|
-
"version": "3.0.0
|
|
4
|
-
"description": "CLI infrastructure for
|
|
3
|
+
"version": "3.0.0",
|
|
4
|
+
"description": "CLI infrastructure for Travetto framework",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cli",
|
|
7
7
|
"travetto",
|
|
@@ -16,19 +16,17 @@
|
|
|
16
16
|
"files": [
|
|
17
17
|
"__index__.ts",
|
|
18
18
|
"src",
|
|
19
|
-
"support"
|
|
20
|
-
"bin"
|
|
19
|
+
"support"
|
|
21
20
|
],
|
|
22
|
-
"bin": "trv.js",
|
|
23
21
|
"main": "__index__.ts",
|
|
24
22
|
"repository": {
|
|
25
23
|
"url": "https://github.com/travetto/travetto.git",
|
|
26
24
|
"directory": "module/cli"
|
|
27
25
|
},
|
|
28
26
|
"dependencies": {
|
|
29
|
-
"@travetto/base": "^3.0.0
|
|
30
|
-
"@travetto/terminal": "^3.0.0
|
|
31
|
-
"@travetto/worker": "^3.0.0
|
|
27
|
+
"@travetto/base": "^3.0.0",
|
|
28
|
+
"@travetto/terminal": "^3.0.0",
|
|
29
|
+
"@travetto/worker": "^3.0.0",
|
|
32
30
|
"commander": "^10.0.0"
|
|
33
31
|
},
|
|
34
32
|
"travetto": {
|
package/src/command-manager.ts
CHANGED
|
@@ -46,9 +46,14 @@ export class CliCommandManager {
|
|
|
46
46
|
const matchedCfg = COMMAND_PACKAGE.find(([re]) => re.test(cmd));
|
|
47
47
|
if (matchedCfg) {
|
|
48
48
|
const [, pkg, prod] = matchedCfg;
|
|
49
|
+
let install: string;
|
|
50
|
+
switch (RootIndex.manifest.packageManager) {
|
|
51
|
+
case 'npm': install = `npm i ${prod ? '' : '--save-dev '}@travetto/${pkg}`; break;
|
|
52
|
+
case 'yarn': install = `yarn add ${prod ? '' : '--dev '}@travetto/${pkg}`; break;
|
|
53
|
+
}
|
|
49
54
|
console.error!(cliTpl`
|
|
50
55
|
${{ title: 'Missing Package' }}\n${'-'.repeat(20)}\nTo use ${{ input: cmd }} please run:\n
|
|
51
|
-
${{ identifier:
|
|
56
|
+
${{ identifier: install }}
|
|
52
57
|
`);
|
|
53
58
|
await ShutdownManager.exit(1);
|
|
54
59
|
}
|
package/src/command.ts
CHANGED
|
@@ -221,13 +221,14 @@ export abstract class CliCommand<V extends OptionMap = OptionMap> {
|
|
|
221
221
|
if (args) {
|
|
222
222
|
cmd = cmd.arguments(args);
|
|
223
223
|
}
|
|
224
|
+
|
|
224
225
|
for (const cfg of await this.finalizeOptions()) {
|
|
225
226
|
const pre = cfg.short ? `-${cfg.short}, ` : '';
|
|
226
227
|
if (cfg.type === Boolean) {
|
|
227
228
|
if (cfg.def) {
|
|
228
|
-
cmd.option(`${pre}--no-${cfg.name}`, `Disables: ${cfg.desc}`);
|
|
229
|
+
cmd = cmd.option(`${pre}--no-${cfg.name}`, `Disables: ${cfg.desc}`);
|
|
229
230
|
} else {
|
|
230
|
-
cmd.option(`${pre}--${cfg.name}`, cfg.desc);
|
|
231
|
+
cmd = cmd.option(`${pre}--${cfg.name}`, cfg.desc);
|
|
231
232
|
}
|
|
232
233
|
} else {
|
|
233
234
|
const key = `${pre}--${cfg.name} <${cfg.name}>`;
|
|
@@ -238,6 +239,9 @@ export abstract class CliCommand<V extends OptionMap = OptionMap> {
|
|
|
238
239
|
|
|
239
240
|
cmd = cmd.action(this.runAction.bind(this));
|
|
240
241
|
|
|
242
|
+
// @ts-expect-error, Do nothing
|
|
243
|
+
cmd.missingArgument = (): void => { };
|
|
244
|
+
|
|
241
245
|
return this.#cmd = cmd;
|
|
242
246
|
}
|
|
243
247
|
|
package/src/execute.ts
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
|
-
import fs from 'fs/promises';
|
|
2
1
|
import { program as commander } from 'commander';
|
|
3
2
|
|
|
4
|
-
import { PackageUtil
|
|
3
|
+
import { PackageUtil } from '@travetto/manifest';
|
|
5
4
|
import { GlobalTerminal } from '@travetto/terminal';
|
|
6
|
-
import { ShutdownManager } from '@travetto/base';
|
|
7
5
|
|
|
8
6
|
import { CliCommandManager } from './command-manager';
|
|
9
7
|
import { HelpUtil } from './help';
|
|
@@ -43,46 +41,25 @@ export class ExecutionManager {
|
|
|
43
41
|
}
|
|
44
42
|
}
|
|
45
43
|
|
|
46
|
-
/**
|
|
47
|
-
* Run file expecting a main method
|
|
48
|
-
*/
|
|
49
|
-
static async runMain(file: string, args: string[]): Promise<void> {
|
|
50
|
-
try {
|
|
51
|
-
// If referenced file exists
|
|
52
|
-
if (await (fs.stat(path.resolve(file)).then(() => true, () => false))) {
|
|
53
|
-
file = path.join(RootIndex.manifest.mainModule, file);
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
const imp = RootIndex.getFromImport(file)?.import;
|
|
57
|
-
if (!imp) {
|
|
58
|
-
throw new Error(`Unknown file: ${file}`);
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
const mod = await import(imp);
|
|
62
|
-
await ShutdownManager.exitWithResponse(await mod.main(...args));
|
|
63
|
-
} catch (err) {
|
|
64
|
-
await ShutdownManager.exitWithResponse(err, true);
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
|
|
68
44
|
/**
|
|
69
45
|
* Execute the command line
|
|
70
46
|
* @param args
|
|
71
47
|
*/
|
|
72
48
|
static async run(argv: string[]): Promise<void> {
|
|
73
|
-
|
|
74
|
-
await init();
|
|
49
|
+
await GlobalTerminal.init();
|
|
75
50
|
|
|
76
|
-
const width = GlobalTerminal.width;
|
|
77
51
|
commander
|
|
78
52
|
.version(PackageUtil.getFrameworkVersion())
|
|
79
|
-
.configureOutput({
|
|
53
|
+
.configureOutput({
|
|
54
|
+
getOutHelpWidth: () => GlobalTerminal.width,
|
|
55
|
+
getErrHelpWidth: () => GlobalTerminal.width
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
const { init } = await import('@travetto/base/support/init.js');
|
|
59
|
+
await init();
|
|
80
60
|
|
|
81
61
|
const [, , cmd, ...args] = argv;
|
|
82
|
-
if (cmd
|
|
83
|
-
const [file, ...rest] = args;
|
|
84
|
-
await this.runMain(file, rest);
|
|
85
|
-
} else if (cmd && !cmd.startsWith('-')) {
|
|
62
|
+
if (cmd && !cmd.startsWith('-')) {
|
|
86
63
|
await this.runCommand(cmd, args);
|
|
87
64
|
} else {
|
|
88
65
|
// Load all commands
|
package/src/module.ts
CHANGED
|
@@ -78,6 +78,10 @@ export class CliModuleUtil {
|
|
|
78
78
|
hash = await CliScmUtil.findLastRelease();
|
|
79
79
|
}
|
|
80
80
|
|
|
81
|
+
if (!hash) {
|
|
82
|
+
return RootIndex.getLocalModules();
|
|
83
|
+
}
|
|
84
|
+
|
|
81
85
|
const out = new Map<string, IndexedModule>();
|
|
82
86
|
for (const mod of await CliScmUtil.findChangedModulesSince(hash)) {
|
|
83
87
|
out.set(mod.name, mod);
|
|
@@ -108,8 +112,10 @@ export class CliModuleUtil {
|
|
|
108
112
|
/**
|
|
109
113
|
* Synchronize all workspace modules to have the correct versions from the current packages
|
|
110
114
|
*/
|
|
111
|
-
static async synchronizeModuleVersions(): Promise<
|
|
112
|
-
|
|
115
|
+
static async synchronizeModuleVersions(): Promise<Record<string, string>> {
|
|
116
|
+
const versions = {};
|
|
117
|
+
await PackageUtil.syncVersions((await this.findModules('all')).map(x => x.sourcePath), versions);
|
|
118
|
+
return versions;
|
|
113
119
|
}
|
|
114
120
|
|
|
115
121
|
/**
|
package/src/scm.ts
CHANGED
|
@@ -32,13 +32,12 @@ export class CliScmUtil {
|
|
|
32
32
|
* Find the last code release
|
|
33
33
|
* @returns
|
|
34
34
|
*/
|
|
35
|
-
static async findLastRelease(): Promise<string> {
|
|
35
|
+
static async findLastRelease(): Promise<string | undefined> {
|
|
36
36
|
const root = await RootIndex.manifest;
|
|
37
37
|
const { result } = ExecUtil.spawn('git', ['log', '--pretty=oneline'], { cwd: root.workspacePath });
|
|
38
38
|
return (await result).stdout
|
|
39
39
|
.split(/\n/)
|
|
40
|
-
.find(x => /Publish /.test(x))
|
|
41
|
-
.split(/\s+/)[0]!;
|
|
40
|
+
.find(x => /Publish /.test(x))?.split(/\s+/)?.[0];
|
|
42
41
|
}
|
|
43
42
|
|
|
44
43
|
/**
|
package/support/cli.exec.ts
CHANGED
|
@@ -37,11 +37,15 @@ export class RepoExecCommand extends CliCommand<Options> {
|
|
|
37
37
|
return { debug: false };
|
|
38
38
|
}
|
|
39
39
|
|
|
40
|
-
async action(): Promise<void> {
|
|
40
|
+
async action(cmd: string, args: string[]): Promise<void> {
|
|
41
|
+
if (!cmd) {
|
|
42
|
+
return this.showHelp('Command is a required field');
|
|
43
|
+
}
|
|
44
|
+
|
|
41
45
|
await CliModuleUtil.execOnModules(
|
|
42
46
|
this.cmd.changed ? 'changed' : 'all',
|
|
43
47
|
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
44
|
-
(mod, opts) => ExecUtil.spawn(
|
|
48
|
+
(mod, opts) => ExecUtil.spawn(cmd, args, opts),
|
|
45
49
|
{
|
|
46
50
|
progressMessage: mod => `Running '${this.args.join(' ')}' [%idx/%total] ${mod?.sourceFolder ?? ''}`,
|
|
47
51
|
showStdout: this.cmd.showStdout,
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import fs from 'fs/promises';
|
|
2
|
+
|
|
3
|
+
import { ShutdownManager } from '@travetto/base';
|
|
4
|
+
import { CliCommand } from '@travetto/cli';
|
|
5
|
+
import { path, RootIndex } from '@travetto/manifest';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* `npx trv main`
|
|
9
|
+
*
|
|
10
|
+
* Allows for running of main entry points
|
|
11
|
+
*/
|
|
12
|
+
export class MainCommand extends CliCommand {
|
|
13
|
+
|
|
14
|
+
name = 'main';
|
|
15
|
+
|
|
16
|
+
getArgs(): string {
|
|
17
|
+
return '<fileOrImport> [args...]';
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
async action(file: string, args: string[]): Promise<void> {
|
|
21
|
+
try {
|
|
22
|
+
// If referenced file exists
|
|
23
|
+
if (await (fs.stat(path.resolve(file)).then(() => true, () => false))) {
|
|
24
|
+
file = path.join(RootIndex.manifest.mainModule, file);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const imp = RootIndex.getFromImport(file)?.import;
|
|
28
|
+
if (!imp) {
|
|
29
|
+
throw new Error(`Unknown file: ${file}`);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const mod = await import(imp);
|
|
33
|
+
await ShutdownManager.exitWithResponse(await mod.main(...args));
|
|
34
|
+
} catch (err) {
|
|
35
|
+
await ShutdownManager.exitWithResponse(err, true);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|