@teambit/cli 0.0.838 → 0.0.840
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/cli-parser.ts +296 -0
- package/cli.aspect.ts +11 -0
- package/cli.cmd.ts +110 -0
- package/cli.main.runtime.ts +193 -0
- package/command-runner.ts +143 -0
- package/completion.cmd.ts +9 -0
- package/dist/cli-parser.js +7 -10
- package/dist/cli-parser.js.map +1 -1
- package/dist/cli.cmd.js +3 -3
- package/dist/cli.cmd.js.map +1 -1
- package/dist/cli.composition.d.ts +2 -2
- package/dist/cli.main.runtime.d.ts +6 -6
- package/dist/cli.main.runtime.js +2 -2
- package/dist/cli.main.runtime.js.map +1 -1
- package/dist/command-runner.d.ts +1 -1
- package/dist/command-runner.js +2 -2
- package/dist/command-runner.js.map +1 -1
- package/dist/completion.cmd.d.ts +1 -1
- package/dist/completion.cmd.js +2 -2
- package/dist/exceptions/command-not-found.js +2 -2
- package/dist/exceptions/command-not-found.js.map +1 -1
- package/dist/generate-doc-md.d.ts +2 -2
- package/dist/generate-doc-md.js +1 -2
- package/dist/generate-doc-md.js.map +1 -1
- package/dist/help.cmd.js +2 -2
- package/dist/help.cmd.js.map +1 -1
- package/dist/legacy-command-adapter.js +2 -2
- package/dist/{preview-1703387662836.js → preview-1703647408454.js} +2 -2
- package/dist/version.cmd.js +2 -2
- package/dist/version.cmd.js.map +1 -1
- package/dist/yargs-adapter.d.ts +2 -2
- package/dist/yargs-adapter.js +4 -5
- package/dist/yargs-adapter.js.map +1 -1
- package/generate-doc-md.ts +167 -0
- package/get-command-id.ts +3 -0
- package/help.cmd.ts +18 -0
- package/help.ts +80 -0
- package/index.ts +8 -0
- package/legacy-command-adapter.ts +68 -0
- package/package.json +12 -19
- package/tsconfig.json +16 -21
- package/types/asset.d.ts +15 -3
- package/version.cmd.ts +21 -0
- package/yargs-adapter.ts +109 -0
package/cli-parser.ts
ADDED
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
import didYouMean from 'didyoumean';
|
|
2
|
+
import yargs from 'yargs';
|
|
3
|
+
import { Command } from '@teambit/legacy/dist/cli/command';
|
|
4
|
+
import { GroupsType } from '@teambit/legacy/dist/cli/command-groups';
|
|
5
|
+
import { compact } from 'lodash';
|
|
6
|
+
import { loadConsumerIfExist } from '@teambit/legacy/dist/consumer';
|
|
7
|
+
import logger from '@teambit/legacy/dist/logger/logger';
|
|
8
|
+
import loader from '@teambit/legacy/dist/cli/loader';
|
|
9
|
+
import chalk from 'chalk';
|
|
10
|
+
import { getCommandId } from './get-command-id';
|
|
11
|
+
import { formatHelp } from './help';
|
|
12
|
+
import { GLOBAL_GROUP, STANDARD_GROUP, YargsAdapter } from './yargs-adapter';
|
|
13
|
+
import { CommandNotFound } from './exceptions/command-not-found';
|
|
14
|
+
|
|
15
|
+
export class CLIParser {
|
|
16
|
+
public parser = yargs;
|
|
17
|
+
constructor(private commands: Command[], private groups: GroupsType) {}
|
|
18
|
+
|
|
19
|
+
async parse(args = process.argv.slice(2)) {
|
|
20
|
+
this.throwForNonExistsCommand(args[0]);
|
|
21
|
+
logger.debug(`[+] CLI-INPUT: ${args.join(' ')}`);
|
|
22
|
+
logger.writeCommandHistoryStart();
|
|
23
|
+
yargs(args);
|
|
24
|
+
yargs.help(false);
|
|
25
|
+
this.configureParser();
|
|
26
|
+
this.commands.forEach((command: Command) => {
|
|
27
|
+
if (command.commands && command.commands.length) {
|
|
28
|
+
this.parseCommandWithSubCommands(command);
|
|
29
|
+
} else {
|
|
30
|
+
const yargsCommand = this.getYargsCommand(command);
|
|
31
|
+
yargs.command(yargsCommand);
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
this.configureGlobalFlags();
|
|
35
|
+
this.setHelpMiddleware();
|
|
36
|
+
this.handleCommandFailure();
|
|
37
|
+
this.configureCompletion();
|
|
38
|
+
yargs.strict(); // don't allow non-exist flags and non-exist commands
|
|
39
|
+
|
|
40
|
+
yargs
|
|
41
|
+
// .recommendCommands() // don't use it, it brings the global help of yargs, we have a custom one
|
|
42
|
+
.wrap(null);
|
|
43
|
+
|
|
44
|
+
await yargs.parse();
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
private setHelpMiddleware() {
|
|
48
|
+
yargs.middleware((argv) => {
|
|
49
|
+
if (argv._.length === 0 && argv.help) {
|
|
50
|
+
const shouldShowInternalCommands = Boolean(argv.internal);
|
|
51
|
+
// this is the main help page
|
|
52
|
+
this.printHelp(shouldShowInternalCommands);
|
|
53
|
+
process.exit(0);
|
|
54
|
+
}
|
|
55
|
+
if (argv.help) {
|
|
56
|
+
loader.off(); // stop the "loading bit..." before showing help if needed
|
|
57
|
+
// this is a command help page
|
|
58
|
+
yargs.showHelp(this.logCommandHelp.bind(this));
|
|
59
|
+
if (!logger.isDaemon) process.exit(0);
|
|
60
|
+
}
|
|
61
|
+
}, true);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
private handleCommandFailure() {
|
|
65
|
+
yargs.fail((msg, err) => {
|
|
66
|
+
loader.stop();
|
|
67
|
+
if (err) {
|
|
68
|
+
throw err;
|
|
69
|
+
}
|
|
70
|
+
yargs.showHelp(this.logCommandHelp.bind(this));
|
|
71
|
+
const args = process.argv.slice(2);
|
|
72
|
+
const isHelpFlagEntered = args.includes('--help') || args.includes('-h');
|
|
73
|
+
const isMsgAboutMissingArgs = msg.startsWith('Not enough non-option arguments');
|
|
74
|
+
// avoid showing the "Not enough non-option arguments" message when the user is trying to get the command help
|
|
75
|
+
if (!isMsgAboutMissingArgs || !isHelpFlagEntered) {
|
|
76
|
+
// eslint-disable-next-line no-console
|
|
77
|
+
console.log(`\n${chalk.yellow(msg)}`);
|
|
78
|
+
}
|
|
79
|
+
if (!logger.isDaemon) process.exit(1);
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
private configureCompletion() {
|
|
84
|
+
const commandsToShowComponentIdsForCompletion = [
|
|
85
|
+
'show',
|
|
86
|
+
'diff',
|
|
87
|
+
'tag',
|
|
88
|
+
'export',
|
|
89
|
+
'env',
|
|
90
|
+
'envs',
|
|
91
|
+
'compile',
|
|
92
|
+
'build',
|
|
93
|
+
'test',
|
|
94
|
+
'lint',
|
|
95
|
+
'log',
|
|
96
|
+
'dependents',
|
|
97
|
+
'dependencies',
|
|
98
|
+
];
|
|
99
|
+
// @ts-ignore
|
|
100
|
+
yargs.completion('completion', async function (current, argv, completionFilter, done) {
|
|
101
|
+
if (!current.startsWith('-') && commandsToShowComponentIdsForCompletion.includes(argv._[1])) {
|
|
102
|
+
const consumer = await loadConsumerIfExist();
|
|
103
|
+
done(consumer?.bitmapIdsFromCurrentLane.map((id) => id.toStringWithoutVersion()));
|
|
104
|
+
} else {
|
|
105
|
+
completionFilter();
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
private printHelp(shouldShowInternalCommands = false) {
|
|
111
|
+
const help = formatHelp(this.commands, this.groups, shouldShowInternalCommands);
|
|
112
|
+
// eslint-disable-next-line no-console
|
|
113
|
+
console.log(help);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
private configureParser() {
|
|
117
|
+
yargs.parserConfiguration({
|
|
118
|
+
// 'strip-dashed': true, // we can't enable it, otherwise, the completion doesn't work
|
|
119
|
+
'strip-aliased': true,
|
|
120
|
+
'boolean-negation': false,
|
|
121
|
+
'populate--': true,
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
private parseCommandWithSubCommands(command: Command) {
|
|
126
|
+
const yarnCommand = this.getYargsCommand(command);
|
|
127
|
+
const builderFunc = () => {
|
|
128
|
+
command.commands?.forEach((cmd) => {
|
|
129
|
+
const subCommand = this.getYargsCommand(cmd);
|
|
130
|
+
yargs.command(subCommand);
|
|
131
|
+
});
|
|
132
|
+
// since the "builder" method is overridden, the global flags of the main command are gone, this fixes it.
|
|
133
|
+
yargs.options(YargsAdapter.getGlobalOptions(command));
|
|
134
|
+
return yargs;
|
|
135
|
+
};
|
|
136
|
+
yarnCommand.builder = builderFunc;
|
|
137
|
+
yargs.command(yarnCommand);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
private getYargsCommand(command: Command): YargsAdapter {
|
|
141
|
+
const yarnCommand = new YargsAdapter(command);
|
|
142
|
+
yarnCommand.builder = yarnCommand.builder.bind(yarnCommand);
|
|
143
|
+
yarnCommand.handler = yarnCommand.handler.bind(yarnCommand);
|
|
144
|
+
|
|
145
|
+
return yarnCommand;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
private configureGlobalFlags() {
|
|
149
|
+
yargs
|
|
150
|
+
.option('help', {
|
|
151
|
+
alias: 'h',
|
|
152
|
+
describe: 'show help',
|
|
153
|
+
group: GLOBAL_GROUP,
|
|
154
|
+
})
|
|
155
|
+
.option('version', {
|
|
156
|
+
global: false,
|
|
157
|
+
alias: 'v',
|
|
158
|
+
describe: 'show version',
|
|
159
|
+
group: GLOBAL_GROUP,
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
private throwForNonExistsCommand(commandName: string) {
|
|
164
|
+
if (!commandName || commandName.startsWith('-')) {
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
const commandsNames = this.commands.map((c) => getCommandId(c.name));
|
|
168
|
+
const aliases = this.commands.map((c) => c.alias).filter((a) => a);
|
|
169
|
+
const existingGlobalFlags = ['-V', '--version'];
|
|
170
|
+
const validCommands = [...commandsNames, ...aliases, ...existingGlobalFlags];
|
|
171
|
+
const commandExist = validCommands.includes(commandName);
|
|
172
|
+
|
|
173
|
+
if (!commandExist) {
|
|
174
|
+
didYouMean.returnFirstMatch = true;
|
|
175
|
+
const suggestions = didYouMean(
|
|
176
|
+
commandName,
|
|
177
|
+
this.commands.filter((c) => !c.private).map((c) => getCommandId(c.name))
|
|
178
|
+
);
|
|
179
|
+
const suggestion = suggestions && Array.isArray(suggestions) ? suggestions[0] : suggestions;
|
|
180
|
+
|
|
181
|
+
throw new CommandNotFound(commandName, suggestion as string);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* manipulate the command help output. there is no API from Yarn to do any of this, so it needs to be done manually.
|
|
187
|
+
* see https://github.com/yargs/yargs/issues/1956
|
|
188
|
+
*
|
|
189
|
+
* the original order of the output:
|
|
190
|
+
* description
|
|
191
|
+
* Options
|
|
192
|
+
* Commands
|
|
193
|
+
* Global
|
|
194
|
+
* Positionals
|
|
195
|
+
* Examples
|
|
196
|
+
*/
|
|
197
|
+
private logCommandHelp(help: string) {
|
|
198
|
+
const command = this.findCommandByArgv();
|
|
199
|
+
|
|
200
|
+
const replacer = (_, p1, p2) => `${p1}${chalk.green(p2)}`;
|
|
201
|
+
const lines = help.split('\n');
|
|
202
|
+
const linesWithoutEmpty = compact(lines);
|
|
203
|
+
const cmdLine = linesWithoutEmpty[0];
|
|
204
|
+
const description: string[] = [];
|
|
205
|
+
const options: string[] = [];
|
|
206
|
+
const globalOptions: string[] = [];
|
|
207
|
+
const subCommands: string[] = [];
|
|
208
|
+
const args: string[] = [];
|
|
209
|
+
const examples: string[] = [];
|
|
210
|
+
|
|
211
|
+
let optionsStarted = false;
|
|
212
|
+
let globalStarted = false;
|
|
213
|
+
let subCommandsStarted = false;
|
|
214
|
+
let positionalsStarted = false;
|
|
215
|
+
let examplesStarted = false;
|
|
216
|
+
for (let i = 1; i < linesWithoutEmpty.length; i += 1) {
|
|
217
|
+
const currentLine = linesWithoutEmpty[i];
|
|
218
|
+
if (currentLine === STANDARD_GROUP) {
|
|
219
|
+
optionsStarted = true;
|
|
220
|
+
} else if (currentLine === GLOBAL_GROUP) {
|
|
221
|
+
globalStarted = true;
|
|
222
|
+
} else if (currentLine === 'Commands:') {
|
|
223
|
+
subCommandsStarted = true;
|
|
224
|
+
} else if (currentLine === 'Positionals:') {
|
|
225
|
+
positionalsStarted = true;
|
|
226
|
+
} else if (currentLine === 'Examples:') {
|
|
227
|
+
examplesStarted = true;
|
|
228
|
+
} else if (examplesStarted) {
|
|
229
|
+
examples.push(currentLine);
|
|
230
|
+
} else if (positionalsStarted) {
|
|
231
|
+
args.push(currentLine);
|
|
232
|
+
} else if (globalStarted) {
|
|
233
|
+
globalOptions.push(currentLine);
|
|
234
|
+
} else if (optionsStarted) {
|
|
235
|
+
options.push(currentLine);
|
|
236
|
+
} else if (subCommandsStarted) {
|
|
237
|
+
subCommands.push(currentLine);
|
|
238
|
+
} else {
|
|
239
|
+
description.push(currentLine);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
// show the flags in green
|
|
244
|
+
const optionsColored = options.map((opt) => opt.replace(/(--)([\w-]+)/, replacer).replace(/(-)([\w-]+)/, replacer));
|
|
245
|
+
const argsColored = args.map((arg) => arg.replace(/^ {2}\S+/, (argName) => chalk.green(argName))); // regex: two spaces then the first word until a white space
|
|
246
|
+
const optionsStr = options.length ? `\n${STANDARD_GROUP}\n${optionsColored.join('\n')}\n` : '';
|
|
247
|
+
const argumentsStr = args.length ? `\nArguments:\n${argsColored.join('\n')}\n` : '';
|
|
248
|
+
const examplesStr = examples.length ? `\nExamples:\n${examples.join('\n')}\n` : '';
|
|
249
|
+
const subCommandsStr = subCommands.length ? `\n${'Commands:'}\n${subCommands.join('\n')}\n` : '';
|
|
250
|
+
// show the description in bold
|
|
251
|
+
const descriptionColored = description.map((desc) => chalk.bold(desc));
|
|
252
|
+
if (command?.extendedDescription) {
|
|
253
|
+
descriptionColored.push(command?.extendedDescription);
|
|
254
|
+
}
|
|
255
|
+
if (command?.helpUrl) {
|
|
256
|
+
descriptionColored.push(`for more info, visit: ${chalk.underline(command.helpUrl)}`);
|
|
257
|
+
}
|
|
258
|
+
const descriptionStr = descriptionColored.join('\n');
|
|
259
|
+
const globalOptionsStr = globalOptions.join('\n');
|
|
260
|
+
|
|
261
|
+
const finalOutput = `${cmdLine}
|
|
262
|
+
|
|
263
|
+
${descriptionStr}
|
|
264
|
+
${argumentsStr}${subCommandsStr}${optionsStr}${examplesStr}
|
|
265
|
+
${GLOBAL_GROUP}
|
|
266
|
+
${globalOptionsStr}`;
|
|
267
|
+
|
|
268
|
+
// eslint-disable-next-line no-console
|
|
269
|
+
console.log(finalOutput);
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
private findCommandByArgv(): Command | undefined {
|
|
273
|
+
const args = process.argv.slice(2);
|
|
274
|
+
const enteredCommand = args[0];
|
|
275
|
+
const enteredSubCommand = args[1];
|
|
276
|
+
if (!enteredCommand) {
|
|
277
|
+
return undefined;
|
|
278
|
+
}
|
|
279
|
+
const isCommandMatch = (cmd: Command, str: string) => {
|
|
280
|
+
return (
|
|
281
|
+
cmd.name.startsWith(`${str} `) || // e.g. "tag <id>".startsWith("tag ")
|
|
282
|
+
cmd.name === str || // e.g. "globals" === "globals"
|
|
283
|
+
cmd.alias === str
|
|
284
|
+
); // e.g. "t" === "t"
|
|
285
|
+
};
|
|
286
|
+
const command = this.commands.find((cmd) => isCommandMatch(cmd, enteredCommand));
|
|
287
|
+
if (!command) {
|
|
288
|
+
return undefined;
|
|
289
|
+
}
|
|
290
|
+
if (!command.commands || !enteredSubCommand) {
|
|
291
|
+
return command; // no sub-commands.
|
|
292
|
+
}
|
|
293
|
+
const subCommand = command.commands.find((cmd) => isCommandMatch(cmd, enteredSubCommand));
|
|
294
|
+
return subCommand || command;
|
|
295
|
+
}
|
|
296
|
+
}
|
package/cli.aspect.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Aspect, RuntimeDefinition } from '@teambit/harmony';
|
|
2
|
+
|
|
3
|
+
export const MainRuntime = new RuntimeDefinition('main');
|
|
4
|
+
|
|
5
|
+
export const CLIAspect = Aspect.create({
|
|
6
|
+
id: 'teambit.harmony/cli',
|
|
7
|
+
dependencies: [],
|
|
8
|
+
declareRuntime: MainRuntime,
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
export default CLIAspect;
|
package/cli.cmd.ts
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
// eslint-disable-next-line max-classes-per-file
|
|
2
|
+
import { Command, CommandOptions } from '@teambit/cli';
|
|
3
|
+
import legacyLogger from '@teambit/legacy/dist/logger/logger';
|
|
4
|
+
import { handleErrorAndExit } from '@teambit/legacy/dist/cli/handle-errors';
|
|
5
|
+
import { loadConsumerIfExist } from '@teambit/legacy/dist/consumer';
|
|
6
|
+
import { getHarmonyVersion } from '@teambit/legacy/dist/bootstrap';
|
|
7
|
+
import readline from 'readline';
|
|
8
|
+
import { CLIParser } from './cli-parser';
|
|
9
|
+
import { CLIMain } from './cli.main.runtime';
|
|
10
|
+
import { GenerateCommandsDoc, GenerateOpts } from './generate-doc-md';
|
|
11
|
+
|
|
12
|
+
export class CliGenerateCmd implements Command {
|
|
13
|
+
name = 'generate';
|
|
14
|
+
description = 'generate an .md file with all commands details';
|
|
15
|
+
alias = '';
|
|
16
|
+
loader = false;
|
|
17
|
+
group = 'general';
|
|
18
|
+
options = [
|
|
19
|
+
[
|
|
20
|
+
'',
|
|
21
|
+
'metadata',
|
|
22
|
+
'metadata/front-matter to place at the top of the .md file, enter as an object e.g. --metadata.id=cli --metadata.title=commands',
|
|
23
|
+
],
|
|
24
|
+
['', 'docs', 'generate the cli-reference.docs.mdx file'],
|
|
25
|
+
['j', 'json', 'output the commands info as JSON'],
|
|
26
|
+
] as CommandOptions;
|
|
27
|
+
|
|
28
|
+
constructor(private cliMain: CLIMain) {}
|
|
29
|
+
|
|
30
|
+
async report(args, { metadata, docs }: GenerateOpts & { docs?: boolean }): Promise<string> {
|
|
31
|
+
if (docs) {
|
|
32
|
+
return `---
|
|
33
|
+
description: 'Bit command synopses. Bit version: ${getHarmonyVersion()}'
|
|
34
|
+
labels: ['cli', 'mdx', 'docs']
|
|
35
|
+
---
|
|
36
|
+
`;
|
|
37
|
+
}
|
|
38
|
+
return new GenerateCommandsDoc(this.cliMain.commands, { metadata }).generate();
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
async json() {
|
|
42
|
+
return new GenerateCommandsDoc(this.cliMain.commands, {}).generateJson();
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export class CliCmd implements Command {
|
|
47
|
+
name = 'cli';
|
|
48
|
+
description = 'EXPERIMENTAL. enters bit cli program and generates commands list';
|
|
49
|
+
alias = '';
|
|
50
|
+
commands: Command[] = [];
|
|
51
|
+
loader = false;
|
|
52
|
+
group = 'general';
|
|
53
|
+
options = [] as CommandOptions;
|
|
54
|
+
|
|
55
|
+
constructor(private cliMain: CLIMain) {}
|
|
56
|
+
|
|
57
|
+
async report(): Promise<string> {
|
|
58
|
+
legacyLogger.isDaemon = true;
|
|
59
|
+
const rl = readline.createInterface({
|
|
60
|
+
input: process.stdin,
|
|
61
|
+
output: process.stdout,
|
|
62
|
+
prompt: 'bit > ',
|
|
63
|
+
completer: (line, cb) => completer(line, cb, this.cliMain),
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
const cliParser = new CLIParser(this.cliMain.commands, this.cliMain.groups);
|
|
67
|
+
|
|
68
|
+
rl.prompt();
|
|
69
|
+
|
|
70
|
+
return new Promise((resolve) => {
|
|
71
|
+
// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
|
72
|
+
rl.on('line', async (line) => {
|
|
73
|
+
const cmd = line.trim().split(' ');
|
|
74
|
+
try {
|
|
75
|
+
await cliParser.parse(cmd);
|
|
76
|
+
} catch (err: any) {
|
|
77
|
+
await handleErrorAndExit(err, cmd[0]);
|
|
78
|
+
}
|
|
79
|
+
rl.prompt();
|
|
80
|
+
}).on('close', () => {
|
|
81
|
+
resolve('Have a great day!');
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function completer(line: string, cb: Function, cliMain: CLIMain) {
|
|
88
|
+
const lineSplit = line.split(' ');
|
|
89
|
+
let values: string[] = [];
|
|
90
|
+
if (lineSplit.length <= 1) {
|
|
91
|
+
values = completeCommand(line, cliMain);
|
|
92
|
+
cb(null, [values, line]);
|
|
93
|
+
}
|
|
94
|
+
loadConsumerIfExist()
|
|
95
|
+
.then((consumer) => {
|
|
96
|
+
const comps = consumer?.bitmapIdsFromCurrentLane.map((id) => id.toStringWithoutVersion()) || [];
|
|
97
|
+
values = comps.filter((c) => c.includes(lineSplit[1]));
|
|
98
|
+
// eslint-disable-next-line promise/no-callback-in-promise
|
|
99
|
+
cb(null, [values, line]);
|
|
100
|
+
})
|
|
101
|
+
.catch((err) => {
|
|
102
|
+
// eslint-disable-next-line promise/no-callback-in-promise
|
|
103
|
+
cb(err, [[], line]);
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function completeCommand(line: string, cliMain: CLIMain): string[] {
|
|
108
|
+
const commands = cliMain.commands.filter((cmd) => cmd.name.startsWith(line));
|
|
109
|
+
return commands.map((c) => c.name).map((name) => name.split(' ')[0]);
|
|
110
|
+
}
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
import { Slot, SlotRegistry } from '@teambit/harmony';
|
|
2
|
+
import { buildRegistry } from '@teambit/legacy/dist/cli';
|
|
3
|
+
import legacyLogger from '@teambit/legacy/dist/logger/logger';
|
|
4
|
+
import { Command } from '@teambit/legacy/dist/cli/command';
|
|
5
|
+
import pMapSeries from 'p-map-series';
|
|
6
|
+
import { groups, GroupsType } from '@teambit/legacy/dist/cli/command-groups';
|
|
7
|
+
import { loadConsumerIfExist } from '@teambit/legacy/dist/consumer';
|
|
8
|
+
import { Logger, LoggerAspect, LoggerMain } from '@teambit/logger';
|
|
9
|
+
import { clone } from 'lodash';
|
|
10
|
+
import { CLIAspect, MainRuntime } from './cli.aspect';
|
|
11
|
+
import { getCommandId } from './get-command-id';
|
|
12
|
+
import { LegacyCommandAdapter } from './legacy-command-adapter';
|
|
13
|
+
import { CLIParser } from './cli-parser';
|
|
14
|
+
import { CompletionCmd } from './completion.cmd';
|
|
15
|
+
import { CliCmd, CliGenerateCmd } from './cli.cmd';
|
|
16
|
+
import { HelpCmd } from './help.cmd';
|
|
17
|
+
import { VersionCmd } from './version.cmd';
|
|
18
|
+
|
|
19
|
+
export type CommandList = Array<Command>;
|
|
20
|
+
export type OnStart = (hasWorkspace: boolean, currentCommand: string) => Promise<void>;
|
|
21
|
+
export type OnBeforeExitFn = () => Promise<void>;
|
|
22
|
+
|
|
23
|
+
export type OnStartSlot = SlotRegistry<OnStart>;
|
|
24
|
+
export type CommandsSlot = SlotRegistry<CommandList>;
|
|
25
|
+
export type OnBeforeExitSlot = SlotRegistry<OnBeforeExitFn>;
|
|
26
|
+
|
|
27
|
+
export class CLIMain {
|
|
28
|
+
public groups: GroupsType = clone(groups); // if it's not cloned, it is cached across loadBit() instances
|
|
29
|
+
constructor(
|
|
30
|
+
private commandsSlot: CommandsSlot,
|
|
31
|
+
private onStartSlot: OnStartSlot,
|
|
32
|
+
private onBeforeExitSlot: OnBeforeExitSlot,
|
|
33
|
+
private logger: Logger
|
|
34
|
+
) {}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* registers a new command in to the CLI.
|
|
38
|
+
*/
|
|
39
|
+
register(...commands: CommandList) {
|
|
40
|
+
commands.forEach((command) => {
|
|
41
|
+
this.setDefaults(command);
|
|
42
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
43
|
+
command.commands!.forEach((cmd) => this.setDefaults(cmd));
|
|
44
|
+
});
|
|
45
|
+
this.commandsSlot.register(commands);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* helpful for having the same command name in different environments (e.g. legacy and non-legacy).
|
|
50
|
+
* for example `cli.unregister('tag');` removes the "bit tag" command.
|
|
51
|
+
*/
|
|
52
|
+
unregister(commandName: string) {
|
|
53
|
+
this.commandsSlot.toArray().forEach(([aspectId, commands]) => {
|
|
54
|
+
const filteredCommands = commands.filter((command) => {
|
|
55
|
+
return getCommandId(command.name) !== commandName;
|
|
56
|
+
});
|
|
57
|
+
this.commandsSlot.map.set(aspectId, filteredCommands);
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* list of all registered commands. (legacy and new).
|
|
63
|
+
*/
|
|
64
|
+
get commands(): CommandList {
|
|
65
|
+
return this.commandsSlot.values().flat();
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* get an instance of a registered command. (useful for aspects to modify and extend existing commands)
|
|
70
|
+
*/
|
|
71
|
+
getCommand(name: string): Command | undefined {
|
|
72
|
+
return this.commands.find((command) => getCommandId(command.name) === name);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* when running `bit help`, commands are grouped by categories.
|
|
77
|
+
* this method helps registering a new group by providing its name and a description.
|
|
78
|
+
* the name is what needs to be assigned to the `group` property of the Command interface.
|
|
79
|
+
* the description is what shown in the `bit help` output.
|
|
80
|
+
*/
|
|
81
|
+
registerGroup(name: string, description: string) {
|
|
82
|
+
if (this.groups[name]) {
|
|
83
|
+
this.logger.consoleWarning(`CLI group "${name}" is already registered`);
|
|
84
|
+
} else {
|
|
85
|
+
this.groups[name] = description;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
registerOnStart(onStartFn: OnStart) {
|
|
90
|
+
this.onStartSlot.register(onStartFn);
|
|
91
|
+
return this;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* This will register a function to be called before the process exits.
|
|
96
|
+
* This will run only for "regular" exits
|
|
97
|
+
* e.g.
|
|
98
|
+
* yes - command run and finished successfully
|
|
99
|
+
* yes - command run and failed gracefully (code 1)
|
|
100
|
+
* not SIGKILL (kill -9)
|
|
101
|
+
* not SIGINT (Ctrl+C)
|
|
102
|
+
* not SIGTERM (kill)
|
|
103
|
+
* not uncaughtException
|
|
104
|
+
* not unhandledRejection
|
|
105
|
+
*
|
|
106
|
+
* @param onBeforeExitFn
|
|
107
|
+
* @returns
|
|
108
|
+
*/
|
|
109
|
+
registerOnBeforeExit(onBeforeExitFn: OnBeforeExitFn) {
|
|
110
|
+
this.onBeforeExitSlot.register(onBeforeExitFn);
|
|
111
|
+
legacyLogger.registerOnBeforeExitFn(onBeforeExitFn);
|
|
112
|
+
return this;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* execute commands registered to this aspect.
|
|
117
|
+
*/
|
|
118
|
+
async run(hasWorkspace: boolean) {
|
|
119
|
+
await this.invokeOnStart(hasWorkspace);
|
|
120
|
+
const CliParser = new CLIParser(this.commands, this.groups);
|
|
121
|
+
await CliParser.parse();
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
private async invokeOnStart(hasWorkspace: boolean) {
|
|
125
|
+
const onStartFns = this.onStartSlot.values();
|
|
126
|
+
const currentCommand = process.argv[2];
|
|
127
|
+
await pMapSeries(onStartFns, (onStart) => onStart(hasWorkspace, currentCommand));
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
private setDefaults(command: Command) {
|
|
131
|
+
command.alias = command.alias || '';
|
|
132
|
+
command.description = command.description || '';
|
|
133
|
+
command.extendedDescription = command.extendedDescription || '';
|
|
134
|
+
command.group = command.group || 'ungrouped';
|
|
135
|
+
command.options = command.options || [];
|
|
136
|
+
command.private = command.private || false;
|
|
137
|
+
command.commands = command.commands || [];
|
|
138
|
+
command.name = command.name.trim();
|
|
139
|
+
if (command.loader === undefined) {
|
|
140
|
+
if (command.internal) {
|
|
141
|
+
command.loader = false;
|
|
142
|
+
} else {
|
|
143
|
+
command.loader = true;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
if (command.helpUrl && !isFullUrl(command.helpUrl)) {
|
|
147
|
+
command.helpUrl = `https://bit.dev/${command.helpUrl}`;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
static dependencies = [LoggerAspect];
|
|
152
|
+
static runtime = MainRuntime;
|
|
153
|
+
static slots = [Slot.withType<CommandList>(), Slot.withType<OnStart>(), Slot.withType<OnBeforeExitFn>()];
|
|
154
|
+
|
|
155
|
+
static async provider(
|
|
156
|
+
[loggerMain]: [LoggerMain],
|
|
157
|
+
config,
|
|
158
|
+
[commandsSlot, onStartSlot, onBeforeExitSlot]: [CommandsSlot, OnStartSlot, OnBeforeExitSlot]
|
|
159
|
+
) {
|
|
160
|
+
const logger = loggerMain.createLogger(CLIAspect.id);
|
|
161
|
+
const cliMain = new CLIMain(commandsSlot, onStartSlot, onBeforeExitSlot, logger);
|
|
162
|
+
const legacyRegistry = buildRegistry();
|
|
163
|
+
await ensureWorkspaceAndScope();
|
|
164
|
+
const legacyCommands = legacyRegistry.commands;
|
|
165
|
+
const legacyCommandsAdapters = legacyCommands.map((command) => new LegacyCommandAdapter(command, cliMain));
|
|
166
|
+
const cliGenerateCmd = new CliGenerateCmd(cliMain);
|
|
167
|
+
const cliCmd = new CliCmd(cliMain);
|
|
168
|
+
const helpCmd = new HelpCmd(cliMain);
|
|
169
|
+
cliCmd.commands.push(cliGenerateCmd);
|
|
170
|
+
cliMain.register(...legacyCommandsAdapters, new CompletionCmd(), cliCmd, helpCmd, new VersionCmd());
|
|
171
|
+
return cliMain;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
CLIAspect.addRuntime(CLIMain);
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* kind of a hack.
|
|
179
|
+
* in the legacy, this is running at the beginning and it take care of issues when Bit files are missing,
|
|
180
|
+
* such as ".bit".
|
|
181
|
+
* (to make this process better, you can easily remove it and run the e2e-tests. you'll see some failing)
|
|
182
|
+
*/
|
|
183
|
+
async function ensureWorkspaceAndScope() {
|
|
184
|
+
try {
|
|
185
|
+
await loadConsumerIfExist();
|
|
186
|
+
} catch (err) {
|
|
187
|
+
// do nothing. it could fail for example with ScopeNotFound error, which is taken care of in "bit init".
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
function isFullUrl(url: string) {
|
|
192
|
+
return url.startsWith('http://') || url.startsWith('https://');
|
|
193
|
+
}
|