-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcli.ts
More file actions
131 lines (119 loc) · 3.58 KB
/
cli.ts
File metadata and controls
131 lines (119 loc) · 3.58 KB
1
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
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
import { App, GoogleAnalytics, Util } from "@igniteui/cli-core";
import yargs from "yargs";
import {
add,
ADD_COMMAND_NAME,
ALL_COMMANDS,
build,
config,
doc,
generate,
list,
mcp,
newCommand,
start,
test,
upgrade,
} from './commands';
import { PromptSession } from "./PromptSession";
import { TemplateManager } from "./TemplateManager";
process.title = "Ignite UI CLI";
function logHelp() {
GoogleAnalytics.post({
t: "screenview",
cd: "$ig help"
});
}
export async function run(args = null) {
App.initialize();
const templateManager = new TemplateManager();
newCommand.addChoices(templateManager.getFrameworkIds());
newCommand.templateManager = templateManager;
add.templateManager = templateManager;
build.templateManager = templateManager;
start.templateManager = templateManager;
generate.templateManager = templateManager;
list.templateManager = templateManager;
upgrade.templateManager = templateManager;
const yargsModule = args ? yargs(args) : yargs;
await yargsModule
.scriptName("") // prevent the addition of the name of the executing script in the usage output
.usage("") // do not show any usage instructions before the commands list
.command(newCommand)
.command(add)
.command(build)
.command(start)
.command(generate)
.command(config)
.command(doc)
.command(test)
.command(list)
.command(upgrade)
.command(mcp)
.version(false) // disable built-in `yargs.version` to override it with our custom option
.options({
version: {
alias: "v",
description: "Show current Ignite UI CLI version",
global: true,
type: "boolean"
}
})
.options({ // testMode option to allow the addition of all templates at once
testMode: {
default: false,
type: "boolean",
hidden: true
}
})
.middleware((argv) => {
// invoked after parsing and before the `yargsModule.parseAsync` callback
const command = argv._[0];
if (command === ADD_COMMAND_NAME && !add.check(argv)) {
argv.skipExecution = true;
yargsModule.showHelp();
}
},
false // setting this to `true` is supposed to exec the middleware after parsing and before arg validation
// but in reality it also does not trigger the command's handler (╯°□°)╯︵ ┻━┻
)
.help().alias("help", "h")
.parseAsync(
args, // the args to parse to argv
{}, // docs say context to pass in to handlers, but nuh-uh, it's just garbage
async (err, argv, output) => {
// `argv._` are the positional arguments passed in to the script
const command = argv._[0];
if (err) {
Util.error(`The ${command} command threw error - ${err.name}`, "red");
Util.error(`Message: ${err.message}`, "red");
if (err.stack) {
Util.error(`Stack: ${err.stack}`, "red");
}
process.exit(1);
}
const helpRequest = argv.h || argv.help;
if (helpRequest) {
logHelp();
}
// since we are providing a custom callback to `yargsModule.parseAsync`, we need to handle the output as well
// ref - https://yargs.js.org/docs/#api-reference-parseargs-context-parsecallback
if (output) {
Util.log(output);
}
if (argv.version) {
Util.showVersion(__dirname + "/../ignite-ui-cli.txt");
return;
}
// internal testing only
/* istanbul ignore next */
App.testMode = !!argv.testMode;
if (!helpRequest && !ALL_COMMANDS.has(command?.toString())) {
Util.log("Starting Step by step mode.", "green");
Util.log("For available commands, stop this execution and use --help.", "green");
const prompts = new PromptSession(templateManager);
prompts.start();
}
}
);
}