@teambit/cli 0.0.840 → 0.0.841

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.
@@ -0,0 +1 @@
1
+ !function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports["teambit.harmony/cli-preview"]=t():e["teambit.harmony/cli-preview"]=t()}(self,(()=>(()=>{"use strict";var e={20679:(e,t,o)=>{var n={id:"teambit.harmony/cli@0.0.841",homepage:"https://bit.cloud/teambit/harmony/cli",exported:!0};function r(){const e=a(o(87363));return r=function(){return e},e}function a(e){return e&&e.__esModule?e:{default:e}}Object.defineProperty(t,"__esModule",{value:!0}),t.Logo=void 0,r.__bit_component=n,a.__bit_component=n;const i=()=>r().default.createElement("div",{style:{height:"100%",display:"flex",justifyContent:"center"}},r().default.createElement("img",{style:{width:70},src:"https://static.bit.dev/extensions-icons/cli.svg"}));i.__bit_component=n,t.Logo=i},87363:e=>{e.exports=React}},t={};function o(n){var r=t[n];if(void 0!==r)return r.exports;var a=t[n]={exports:{}};return e[n](a,a.exports,o),a.exports}o.d=(e,t)=>{for(var n in t)o.o(t,n)&&!o.o(e,n)&&Object.defineProperty(e,n,{enumerable:!0,get:t[n]})},o.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),o.r=e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})};var n={};return(()=>{o.r(n),o.d(n,{compositions:()=>p,compositions_metadata:()=>u,overview:()=>l});var e={};o.r(e),o.d(e,{default:()=>m});var t=o(20679);o(87363);const r=MdxJsReact,a=TeambitMdxUiMdxScopeContext;var i=["components"];function s(){return s=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var o=arguments[t];for(var n in o)Object.prototype.hasOwnProperty.call(o,n)&&(e[n]=o[n])}return e},s.apply(this,arguments)}var c={},d="wrapper";function m(e){var t=e.components,o=function(e,t){if(null==e)return{};var o,n,r=function(e,t){if(null==e)return{};var o,n,r={},a=Object.keys(e);for(n=0;n<a.length;n++)o=a[n],t.indexOf(o)>=0||(r[o]=e[o]);return r}(e,t);if(Object.getOwnPropertySymbols){var a=Object.getOwnPropertySymbols(e);for(n=0;n<a.length;n++)o=a[n],t.indexOf(o)>=0||Object.prototype.propertyIsEnumerable.call(e,o)&&(r[o]=e[o])}return r}(e,i);return(0,r.mdx)(d,s({},c,o,{components:t,mdxType:"MDXLayout"}),(0,r.mdx)(a.MDXScopeProvider,{components:{},mdxType:"MDXScopeProvider"},(0,r.mdx)("p",null,"CLI Aspect manages the commands in the CLI. New commands are registered to this aspect with the necessary data such as, command-name, description and flags. Parsing the args from the CLI is done by Commander package."),(0,r.mdx)("h2",null,"Features"),(0,r.mdx)("ul",null,(0,r.mdx)("li",{parentName:"ul"},"Allow to register new commands"),(0,r.mdx)("li",{parentName:"ul"},"Use commander commands."),(0,r.mdx)("li",{parentName:"ul"},"Render to stdout as string or as a React component by Ink."))))}m.isMDXComponent=!0;const p=[t],l=[e],u={compositions:[{displayName:"Logo",identifier:"Logo"}]}})(),n})()));
@@ -1,5 +1,5 @@
1
- import * as compositions_0 from '/home/circleci/Library/Caches/Bit/capsules/8891be5ad3d35bfc38b9cd90c0e05b598a5a55af/teambit.harmony_cli@0.0.840/dist/cli.composition.js';
2
- import * as overview_0 from '/home/circleci/Library/Caches/Bit/capsules/8891be5ad3d35bfc38b9cd90c0e05b598a5a55af/teambit.harmony_cli@0.0.840/dist/cli.docs.mdx';
1
+ import * as compositions_0 from '/home/circleci/Library/Caches/Bit/capsules/8891be5ad3d35bfc38b9cd90c0e05b598a5a55af/teambit.harmony_cli@0.0.841/dist/cli.composition.js';
2
+ import * as overview_0 from '/home/circleci/Library/Caches/Bit/capsules/8891be5ad3d35bfc38b9cd90c0e05b598a5a55af/teambit.harmony_cli@0.0.841/dist/cli.docs.mdx';
3
3
 
4
4
  export const compositions = [compositions_0];
5
5
  export const overview = [overview_0];
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@teambit/cli",
3
- "version": "0.0.840",
3
+ "version": "0.0.841",
4
4
  "homepage": "https://bit.cloud/teambit/harmony/cli",
5
5
  "main": "dist/index.js",
6
6
  "componentId": {
7
7
  "scope": "teambit.harmony",
8
8
  "name": "cli",
9
- "version": "0.0.840"
9
+ "version": "0.0.841"
10
10
  },
11
11
  "dependencies": {
12
12
  "chalk": "2.4.2",
@@ -18,7 +18,7 @@
18
18
  "pad-right": "0.2.2",
19
19
  "@teambit/harmony": "0.4.6",
20
20
  "@teambit/bit-error": "0.0.404",
21
- "@teambit/logger": "0.0.933"
21
+ "@teambit/logger": "0.0.934"
22
22
  },
23
23
  "devDependencies": {
24
24
  "@types/didyoumean": "1.2.0",
@@ -27,7 +27,7 @@
27
27
  "@types/mocha": "9.1.0",
28
28
  "@types/jest": "^29.2.2",
29
29
  "@types/testing-library__jest-dom": "^5.9.5",
30
- "@teambit/harmony.envs.core-aspect-env": "0.0.13"
30
+ "@teambit/harmony.envs.core-aspect-env": "0.0.14"
31
31
  },
32
32
  "peerDependencies": {
33
33
  "react": "^17.0.0 || ^18.0.0",
package/cli-parser.ts DELETED
@@ -1,296 +0,0 @@
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 DELETED
@@ -1,11 +0,0 @@
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 DELETED
@@ -1,110 +0,0 @@
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
- }
@@ -1,193 +0,0 @@
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
- }
package/command-runner.ts DELETED
@@ -1,143 +0,0 @@
1
- import { render } from 'ink';
2
- import { migrate } from '@teambit/legacy/dist/api/consumer';
3
- import logger, { LoggerLevel } from '@teambit/legacy/dist/logger/logger';
4
- import { CLIArgs, Command, Flags, RenderResult } from '@teambit/legacy/dist/cli/command';
5
- import { parseCommandName } from '@teambit/legacy/dist/cli/command-registry';
6
- import loader from '@teambit/legacy/dist/cli/loader';
7
- import { handleErrorAndExit } from '@teambit/legacy/dist/cli/handle-errors';
8
- import { TOKEN_FLAG_NAME } from '@teambit/legacy/dist/constants';
9
- import globalFlags from '@teambit/legacy/dist/cli/global-flags';
10
- import { Analytics } from '@teambit/legacy/dist/analytics/analytics';
11
-
12
- export class CommandRunner {
13
- private commandName: string;
14
- constructor(private command: Command, private args: CLIArgs, private flags: Flags) {
15
- this.commandName = parseCommandName(this.command.name);
16
- }
17
-
18
- /**
19
- * run command using one of the handler, "json"/"report"/"render". once done, exit the process.
20
- */
21
- async runCommand() {
22
- try {
23
- this.bootstrapCommand();
24
- await this.runMigrateIfNeeded();
25
- this.determineConsoleWritingDuringCommand();
26
- if (this.flags.json) {
27
- return await this.runJsonHandler();
28
- }
29
- if (this.shouldRunRender()) {
30
- return await this.runRenderHandler();
31
- }
32
- if (this.command.report) {
33
- return await this.runReportHandler();
34
- }
35
- } catch (err: any) {
36
- return handleErrorAndExit(err, this.commandName, this.command.internal);
37
- }
38
-
39
- throw new Error(`command "${this.commandName}" doesn't implement "render" nor "report" methods`);
40
- }
41
-
42
- private bootstrapCommand() {
43
- Analytics.init(this.commandName, this.flags, this.args);
44
- logger.info(`[*] started a new command: "${this.commandName}" with the following data:`, {
45
- args: this.args,
46
- flags: this.flags,
47
- });
48
- const token = this.flags[TOKEN_FLAG_NAME];
49
- if (token) {
50
- globalFlags.token = token.toString();
51
- }
52
- }
53
-
54
- /**
55
- * when both "render" and "report" were implemented, check whether it's a terminal.
56
- * if it's a terminal, use "render", if not, use "report" because "report" is just a string
57
- */
58
- private shouldRunRender() {
59
- const isTerminal = process.stdout.isTTY;
60
- if (this.command.report && !isTerminal) {
61
- return false;
62
- }
63
- return Boolean(this.command.render);
64
- }
65
-
66
- /**
67
- * this works for both, Harmony commands and Legacy commands (the legacy-command-adapter
68
- * implements json() method)
69
- */
70
- private async runJsonHandler() {
71
- if (!this.flags.json) return null;
72
- if (!this.command.json) throw new Error(`command "${this.commandName}" doesn't implement "json" method`);
73
- const result = await this.command.json(this.args, this.flags);
74
- const code = result.code || 0;
75
- const data = result.data || result;
76
- return this.writeAndExit(JSON.stringify(data, null, 2), code);
77
- }
78
-
79
- private async runRenderHandler() {
80
- if (!this.command.render) throw new Error('runRenderHandler expects command.render to be implemented');
81
- const result = await this.command.render(this.args, this.flags);
82
- loader.off();
83
-
84
- const { data, code } = toRenderResult(result);
85
-
86
- const { waitUntilExit } = render(data);
87
- await waitUntilExit();
88
- return logger.exitAfterFlush(code, this.commandName);
89
- }
90
-
91
- private async runReportHandler() {
92
- if (!this.command.report) throw new Error('runReportHandler expects command.report to be implemented');
93
- const result = await this.command.report(this.args, this.flags);
94
- loader.off();
95
- const data = typeof result === 'string' ? result : result.data;
96
- const exitCode = typeof result === 'string' ? 0 : result.code;
97
- return this.writeAndExit(`${data}\n`, exitCode);
98
- }
99
-
100
- /**
101
- * the loader and logger.console write output to the console during the command execution.
102
- * for internals commands, such as, _put, _fetch, the command.loader = false.
103
- */
104
- private determineConsoleWritingDuringCommand() {
105
- if (this.command.loader && !this.flags.json && !this.flags['get-yargs-completions']) {
106
- loader.on();
107
- loader.start(`running command "${this.commandName}"...`);
108
- logger.shouldWriteToConsole = true;
109
- } else {
110
- loader.off();
111
- logger.shouldWriteToConsole = false;
112
- }
113
- if (this.flags.log) {
114
- // probably not necessary anymore. it is handled in src/logger - determineWritingLogToScreen()
115
- const logValue = typeof this.flags.log === 'string' ? this.flags.log : undefined;
116
- logger.switchToConsoleLogger(logValue as LoggerLevel);
117
- }
118
- }
119
-
120
- private async writeAndExit(data: string, exitCode: number) {
121
- // eslint-disable-next-line @typescript-eslint/no-misused-promises
122
- return process.stdout.write(data, async () => logger.exitAfterFlush(exitCode, this.commandName, data));
123
- }
124
-
125
- private async runMigrateIfNeeded(): Promise<any> {
126
- // @ts-ignore LegacyCommandAdapter has .migration
127
- if (this.command.migration) {
128
- logger.debug('Checking if a migration is needed');
129
- // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
130
- return migrate(null, false);
131
- }
132
- return null;
133
- }
134
- }
135
-
136
- function toRenderResult(obj: RenderResult | React.ReactElement) {
137
- return isRenderResult(obj) ? obj : { data: obj, code: 0 };
138
- }
139
-
140
- function isRenderResult(obj: RenderResult | any): obj is RenderResult {
141
- // eslint-disable-next-line no-prototype-builtins
142
- return typeof obj === 'object' && typeof obj.code === 'number' && obj.hasOwnProperty('data');
143
- }
package/completion.cmd.ts DELETED
@@ -1,9 +0,0 @@
1
- import { Command } from '@teambit/legacy/dist/cli/command';
2
-
3
- export class CompletionCmd implements Command {
4
- name = 'completion';
5
- description = 'enable bash/zsh-completion shortcuts for commands and options';
6
- alias = '';
7
- group = 'general';
8
- options = [];
9
- }
@@ -1,167 +0,0 @@
1
- import { Command, CommandArg } from '@teambit/legacy/dist/cli/command';
2
- import { CommandOptions } from '@teambit/legacy/dist/cli/legacy-command';
3
- import { pick } from 'lodash';
4
- import { getCommandId } from './get-command-id';
5
-
6
- export type GenerateOpts = {
7
- metadata?: Record<string, string>;
8
- };
9
-
10
- type CommandObject = ReturnType<typeof oneCommandToObject> & { commands?: any };
11
-
12
- export class GenerateCommandsDoc {
13
- constructor(private commands: Command[], private options: GenerateOpts) {}
14
-
15
- generate(): string {
16
- const commands = this.getAllPublicCommandsSorted();
17
- let output = `${this.getFrontmatter()}
18
- # CLI Reference
19
-
20
- Run the following to list all available Bit commands (alternatively, use the \`-h\` alias, instead of \`--help\`):
21
-
22
- \`\`\`sh
23
- bit --help
24
- \`\`\`
25
-
26
- Run the following to get help on a specific command:
27
-
28
- \`\`\`sh
29
- bit COMMAND --help
30
- \`\`\`
31
-
32
- Run the following to get help on a specific sub-command:
33
-
34
- \`\`\`sh
35
- bit COMMAND SUB_COMMAND --help
36
- \`\`\`
37
- `;
38
- output += commands.map((cmd) => this.generateCommand(cmd)).join('\n');
39
-
40
- return output;
41
- }
42
-
43
- generateJson() {
44
- return this.commandsToObjects();
45
- }
46
-
47
- private commandsToObjects(commands: Command[] = this.commands): CommandObject[] {
48
- return commands.map((command) => {
49
- const cmdObject: CommandObject = oneCommandToObject(command);
50
- if (command.commands?.length) {
51
- cmdObject.commands = this.commandsToObjects(command.commands);
52
- }
53
- return cmdObject;
54
- });
55
- }
56
-
57
- private getFrontmatter() {
58
- const metadata = this.options.metadata;
59
- if (!metadata) {
60
- return '';
61
- }
62
- const metadataStr = Object.keys(metadata)
63
- .map((key) => `${key}: ${metadata[key]}`)
64
- .join('\n');
65
-
66
- return `---
67
- ${metadataStr}
68
- ---
69
- `;
70
- }
71
-
72
- private getAllPublicCommandsSorted() {
73
- const publicCommands = this.commands.filter((cmd) => !cmd.private);
74
- return publicCommands.sort((a, b) => a.name.localeCompare(b.name));
75
- }
76
-
77
- private generateCommand(cmd: Command) {
78
- const commandName = getCommandId(cmd.name);
79
- let result = `## ${commandName} \n\n`;
80
- if (cmd.alias && cmd.alias.length > 0) {
81
- result += `**Alias**: \`${cmd.alias}\` \n`;
82
- }
83
- result += `**Description**: ${this.formatDescription(cmd)}`;
84
- result += `\`bit ${cmd.name}\` \n\n`;
85
-
86
- if (cmd.commands && cmd.commands.length > 0) {
87
- result += this.generateSubCommands(cmd.commands, cmd);
88
- }
89
- result += this.generateArguments(cmd.arguments);
90
- result += this.generateOptions(cmd.options);
91
- result += `--- \n`;
92
-
93
- return result;
94
- }
95
-
96
- private generateSubCommands(subCommands: Command[], command: Command) {
97
- let ret = '';
98
- subCommands.forEach((subCommand) => {
99
- const commandName = getCommandId(command.name);
100
- const subcommandName = getCommandId(subCommand.name);
101
- const usage = `${commandName} ${subCommand.name}`;
102
- ret += `### ${commandName} ${subcommandName} \n`;
103
- ret += `**Usage**: \`${usage}\` \n\n`;
104
- ret += `**Description**: ${this.formatDescription(subCommand)}`;
105
-
106
- ret += '\n';
107
- ret += this.generateArguments(subCommand.arguments);
108
- ret += this.generateOptions(subCommand.options);
109
- });
110
- return ret;
111
- }
112
-
113
- private generateArguments(args?: CommandArg[]): string {
114
- if (!args || !args.length) return '';
115
- let output = `| **Arg** | **Description** | \n`;
116
- output += `|---|:-----:|\n`;
117
- args.forEach((arg) => {
118
- const { name, description } = arg;
119
- output += `|\`${name}\`|${(description || '').replaceAll('\n', ' ')}|\n`;
120
- });
121
- output += `\n`;
122
- return output;
123
- }
124
-
125
- private generateOptions(options: CommandOptions): string {
126
- if (!options || options.length <= 0) return '';
127
- let output = `| **Option** | **Option alias** | **Description**| \n`;
128
- output += `|---|:-----:|---|\n`;
129
- options.forEach((opt) => {
130
- const [alias, flag, description] = opt;
131
- const aliasFormatted = alias ? `\`-${alias}\`` : ' ';
132
- const flagFormatted = `--${flag}`;
133
- output += `|\`${flagFormatted}\`|${aliasFormatted}|${description.replaceAll('\n', ' ')}|\n`;
134
- });
135
- output += `\n`;
136
- return output;
137
- }
138
-
139
- private formatStringToMD(str: string): string {
140
- return str.split('\n').join(' \n');
141
- }
142
-
143
- private formatDescription(command: Command): string {
144
- const extendedDescription = command.extendedDescription
145
- ? ` \n${this.formatStringToMD(command.extendedDescription)}`
146
- : '';
147
- const description = this.formatStringToMD(command.description as string);
148
- return `${description}${extendedDescription} \n\n`;
149
- }
150
- }
151
-
152
- function oneCommandToObject(command: Command) {
153
- return pick(command, [
154
- 'name',
155
- 'alias',
156
- 'options',
157
- 'description',
158
- 'extendedDescription',
159
- 'group',
160
- 'private',
161
- 'internal',
162
- 'remoteOp',
163
- 'skipWorkspace',
164
- 'arguments',
165
- 'examples',
166
- ]);
167
- }
package/get-command-id.ts DELETED
@@ -1,3 +0,0 @@
1
- export function getCommandId(cmdName: string) {
2
- return cmdName.split(' ')[0].trim();
3
- }
package/help.cmd.ts DELETED
@@ -1,18 +0,0 @@
1
- import { Command, CommandOptions } from '@teambit/cli';
2
- import { CLIMain } from './cli.main.runtime';
3
- import { formatHelp } from './help';
4
-
5
- export class HelpCmd implements Command {
6
- name = 'help';
7
- description = 'shows help';
8
- alias = '$0'; // default command (meaning, if no args are provided, this will be used), see https://github.com/yargs/yargs/blob/master/docs/advanced.md#default-commands
9
- loader = false;
10
- group = 'general';
11
- options = [['', 'internal', 'show internal commands']] as CommandOptions;
12
-
13
- constructor(private cliMain: CLIMain) {}
14
-
15
- async report(_, { internal }: { internal: boolean }) {
16
- return formatHelp(this.cliMain.commands, this.cliMain.groups, internal);
17
- }
18
- }
package/help.ts DELETED
@@ -1,80 +0,0 @@
1
- import chalk from 'chalk';
2
- import rightpad from 'pad-right';
3
- import { capitalize } from 'lodash';
4
- import { GroupsType } from '@teambit/legacy/dist/cli/command-groups';
5
- import { CommandList } from './cli.main.runtime';
6
- import { getCommandId } from './get-command-id';
7
-
8
- const SPACE = ' ';
9
- const TITLE_LEFT_SPACES_NUMBER = 2;
10
- const COMMAND_LEFT_SPACES_NUMBER = 4;
11
- const NAME_WITH_SPACES_LENGTH = 15;
12
-
13
- type HelpProps = {
14
- [groupName: string]: GroupContent;
15
- };
16
-
17
- type GroupContent = {
18
- commands: { [cmdName: string]: string };
19
- description: string;
20
- };
21
-
22
- export function formatHelp(commands: CommandList, groups: GroupsType, showPrivateCommands = false) {
23
- const helpProps = groupCommands(commands, groups, showPrivateCommands);
24
- const commandsStr = formatCommandsHelp(helpProps);
25
-
26
- return `${getHeader()}
27
-
28
- ${commandsStr}
29
-
30
- ${getFooter()}`;
31
- }
32
-
33
- function groupCommands(commands: CommandList, groups: GroupsType, showPrivateCommands = false): HelpProps {
34
- const help: HelpProps = commands
35
- .filter((command) => (showPrivateCommands ? true : !command.private && command.description))
36
- .reduce(function (partialHelp, command) {
37
- const groupName = command.group as string; // at this stage, it must be set
38
- partialHelp[groupName] = partialHelp[groupName] || {
39
- commands: {},
40
- description: groups[groupName] || capitalize(command.group),
41
- };
42
- const cmdId = getCommandId(command.name);
43
- partialHelp[groupName].commands[cmdId] = command.description;
44
- return partialHelp;
45
- }, {});
46
- return help;
47
- }
48
-
49
- function formatCommandsHelp(helpProps: HelpProps): string {
50
- return Object.keys(helpProps)
51
- .map((groupName) => commandsSectionTemplate(helpProps[groupName]))
52
- .join('\n\n');
53
- }
54
-
55
- function commandsSectionTemplate(section: GroupContent): string {
56
- const titleSpace = SPACE.repeat(TITLE_LEFT_SPACES_NUMBER);
57
- const title = `${titleSpace}${chalk.underline.bold.blue(section.description)}`;
58
- const commands = Object.keys(section.commands)
59
- .map((cmdName) => commandTemplate(cmdName, section.commands[cmdName]))
60
- .join('\n');
61
- const res = `${title}\n${commands}`;
62
- return res;
63
- }
64
-
65
- function commandTemplate(name: string, description: string): string {
66
- const nameSpace = SPACE.repeat(COMMAND_LEFT_SPACES_NUMBER);
67
- const nameWithRightSpace = rightpad(name, NAME_WITH_SPACES_LENGTH, SPACE);
68
- const res = `${nameSpace}${chalk.green(nameWithRightSpace)}${description}`;
69
- return res;
70
- }
71
-
72
- function getHeader(): string {
73
- return `${chalk.bold('usage: bit [--version] [--help] <command> [<args>]')}
74
-
75
- ${chalk.yellow(`bit documentation: https://bit.dev/`)}`;
76
- }
77
-
78
- function getFooter(): string {
79
- return `${chalk.yellow("please use 'bit <command> --help' for more information and guides on specific commands.")}`;
80
- }
package/index.ts DELETED
@@ -1,8 +0,0 @@
1
- import { CLIAspect, MainRuntime } from './cli.aspect';
2
-
3
- export type { CLIMain, CommandList, CommandsSlot } from './cli.main.runtime';
4
- export type { Command, CLIArgs, Flags, GenericObject } from '@teambit/legacy/dist/cli/command';
5
- export type { CommandOptions } from '@teambit/legacy/dist/cli/legacy-command';
6
- export * from './exceptions';
7
-
8
- export { CLIAspect as default, MainRuntime, CLIAspect };
@@ -1,68 +0,0 @@
1
- import { LegacyCommand } from '@teambit/legacy/dist/cli/legacy-command';
2
- import { Command, CommandOptions, GenericObject } from '.';
3
- import { CLIMain } from './cli.main.runtime';
4
-
5
- export class LegacyCommandAdapter implements Command {
6
- alias: string;
7
- name: string;
8
- description: string;
9
- options: CommandOptions;
10
- extendedDescription?: string;
11
- group?: string;
12
- loader?: boolean;
13
- commands: Command[];
14
- private?: boolean;
15
- migration?: boolean;
16
- internal?: boolean;
17
- skipWorkspace?: boolean;
18
- helpUrl?: string;
19
- _packageManagerArgs?: string[];
20
- constructor(private cmd: LegacyCommand, cliExtension: CLIMain) {
21
- this.name = cmd.name;
22
- this.description = cmd.description;
23
- this.helpUrl = cmd.helpUrl;
24
- this.options = cmd.opts || [];
25
- this.alias = cmd.alias;
26
- this.extendedDescription = cmd.extendedDescription;
27
- this.skipWorkspace = cmd.skipWorkspace;
28
- this.group = cmd.group;
29
- this.loader = cmd.loader;
30
- this.private = cmd.private;
31
- this.migration = cmd.migration;
32
- this.internal = cmd.internal;
33
- this.commands = (cmd.commands || []).map((sub) => new LegacyCommandAdapter(sub, cliExtension));
34
- }
35
-
36
- private async action(params: any, options: { [key: string]: any }): Promise<ActionResult> {
37
- const res = await this.cmd.action(params, options, this._packageManagerArgs);
38
- let data = res;
39
- let code = 0;
40
- if (res && res.__code !== undefined) {
41
- data = res.data;
42
- code = res.__code;
43
- }
44
- const report = this.cmd.report(data, params, options);
45
- return {
46
- code,
47
- report,
48
- };
49
- }
50
-
51
- async report(params: any, options: { [key: string]: any }): Promise<{ data: string; code: number }> {
52
- const actionResult = await this.action(params, options);
53
- return { data: actionResult.report, code: actionResult.code };
54
- }
55
-
56
- async json(params: any, options: { [key: string]: any }): Promise<GenericObject> {
57
- const actionResult = await this.action(params, options);
58
- return {
59
- data: JSON.parse(actionResult.report),
60
- code: actionResult.code,
61
- };
62
- }
63
- }
64
-
65
- type ActionResult = {
66
- code: number;
67
- report: string;
68
- };
package/version.cmd.ts DELETED
@@ -1,21 +0,0 @@
1
- import { Command, CommandOptions } from '@teambit/cli';
2
- import { getHarmonyVersion } from '@teambit/legacy/dist/bootstrap';
3
-
4
- export class VersionCmd implements Command {
5
- name = 'version';
6
- description = 'shows bit version';
7
- alias = '';
8
- loader = false;
9
- group = 'general';
10
- options = [['j', 'json', 'return the version in json format']] as CommandOptions;
11
-
12
- async report() {
13
- const results = await this.json();
14
- return results.bit;
15
- }
16
-
17
- async json() {
18
- const bit = getHarmonyVersion(true);
19
- return { bit };
20
- }
21
- }
package/yargs-adapter.ts DELETED
@@ -1,109 +0,0 @@
1
- import { Command } from '@teambit/legacy/dist/cli/command';
2
- import { Arguments, CommandModule, Argv, Options } from 'yargs';
3
- import { TOKEN_FLAG } from '@teambit/legacy/dist/constants';
4
- import { camelCase } from 'lodash';
5
- import { CommandRunner } from './command-runner';
6
-
7
- export const GLOBAL_GROUP = 'Global';
8
- export const STANDARD_GROUP = 'Options';
9
-
10
- export class YargsAdapter implements CommandModule {
11
- command: string;
12
- describe?: string;
13
- aliases?: string;
14
- constructor(private commanderCommand: Command) {
15
- this.command = commanderCommand.name;
16
- this.describe = commanderCommand.description;
17
- this.aliases = commanderCommand.alias;
18
- }
19
-
20
- builder(yargs: Argv) {
21
- const options = YargsAdapter.optionsToBuilder(this.commanderCommand);
22
- yargs.option(options);
23
- this.commanderCommand.arguments?.forEach((arg) => {
24
- yargs.positional(arg.name, { description: arg.description });
25
- });
26
- this.commanderCommand.examples?.forEach((example) => {
27
- yargs.example(example.cmd, example.description);
28
- });
29
-
30
- return yargs;
31
- }
32
-
33
- handler(argv: Arguments) {
34
- const enteredArgs = getArgsFromCommandName(this.commanderCommand.name);
35
- const argsValues = enteredArgs.map((a) => argv[a]) as any[];
36
- // a workaround to get a flag syntax such as "--all [version]" work with yargs.
37
- const flags = Object.keys(argv).reduce((acc, current) => {
38
- if (current === '_' || current === '$0' || current === '--') return acc;
39
- // const flagName = current.split(' ')[0];
40
- const val = typeof argv[current] === 'string' && !argv[current] ? true : argv[current];
41
- acc[current] = val;
42
- return acc;
43
- }, {});
44
- this.commanderCommand._packageManagerArgs = (argv['--'] || []) as string[];
45
-
46
- const commandRunner = new CommandRunner(this.commanderCommand, argsValues, flags);
47
- return commandRunner.runCommand();
48
- }
49
-
50
- get positional() {
51
- return this.commanderCommand.arguments;
52
- }
53
-
54
- static optionsToBuilder(command: Command): { [key: string]: Options } {
55
- const option = command.options.reduce((acc, [alias, opt, desc]) => {
56
- const optName = opt.split(' ')[0];
57
- acc[optName] = {
58
- alias,
59
- describe: desc,
60
- group: STANDARD_GROUP,
61
- type: opt.includes(' ') ? 'string' : 'boolean',
62
- requiresArg: opt.includes('<'),
63
- } as Options;
64
- return acc;
65
- }, {});
66
- const globalOptions = YargsAdapter.getGlobalOptions(command);
67
-
68
- return { ...option, ...globalOptions };
69
- }
70
-
71
- static getGlobalOptions(command: Command): Record<string, any> {
72
- const globalOptions: Record<string, any> = {};
73
- if (command.remoteOp) {
74
- globalOptions[TOKEN_FLAG] = {
75
- describe: 'authentication token',
76
- group: GLOBAL_GROUP,
77
- };
78
- }
79
- if (!command.internal) {
80
- globalOptions.log = {
81
- describe:
82
- 'print log messages to the screen, options are: [trace, debug, info, warn, error, fatal], the default is info',
83
- group: GLOBAL_GROUP,
84
- };
85
- globalOptions['safe-mode'] = {
86
- describe:
87
- 'bootstrap the bare-minimum with only the CLI aspect. useful mainly for low-level commands when bit refuses to load',
88
- group: GLOBAL_GROUP,
89
- };
90
- }
91
- return globalOptions;
92
- }
93
- }
94
-
95
- function getArgsFromCommandName(commandName: string) {
96
- const commandSplit = commandName.split(' ');
97
- commandSplit.shift(); // remove the first element, it's the command-name
98
-
99
- return commandSplit.map((existArg) => {
100
- const trimmed = existArg.trim();
101
- if ((!trimmed.startsWith('<') && !trimmed.startsWith('[')) || (!trimmed.endsWith('>') && !trimmed.endsWith(']'))) {
102
- throw new Error(`expect arg "${trimmed}" of "${commandName}" to be wrapped with "[]" or "<>"`);
103
- }
104
- // remove the opening and closing brackets
105
- const withoutBrackets = trimmed.slice(1, -1);
106
-
107
- return camelCase(withoutBrackets);
108
- });
109
- }