Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor, remove LegacyCommand, move remote command to global-config #8992

Merged
merged 7 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions scopes/harmony/bit/load-bit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,19 +210,20 @@ function getMainAspect() {
*/
function shouldLoadInSafeMode() {
const currentCommand = process.argv[2];
const commandsToAlwaysRunInSafeMode = ['remote'];
// only legacy commands can ignore all aspects and load only the CLI aspect.
// harmony commands need the aspects to be loaded and register to the CLI aspect in order to work properly.
const commandsThatCanRunInSafeMode = [
...commandsToAlwaysRunInSafeMode,
'dependents',
'remote',
'doctor',
'cat-version-history',
'cat-component',
'cat-scope',
'cat-object',
'config',
'run-action',
];
const hasSafeModeFlag = process.argv.includes('--safe-mode');
const isSafeModeCommand = commandsToAlwaysRunInSafeMode.includes(currentCommand) || isClearCacheCommand();
return isSafeModeCommand || (hasSafeModeFlag && commandsThatCanRunInSafeMode.includes(currentCommand));
return isClearCacheCommand() || (hasSafeModeFlag && commandsThatCanRunInSafeMode.includes(currentCommand));
}

function isClearCacheCommand() {
Expand Down
3 changes: 1 addition & 2 deletions scopes/harmony/cli/cli.cmd.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// eslint-disable-next-line max-classes-per-file
import type { Command } from '@teambit/legacy/dist/cli/command';
import type { CommandOptions } from '@teambit/legacy/dist/cli/legacy-command';
import type { Command, CommandOptions } from '@teambit/legacy/dist/cli/command';
import legacyLogger from '@teambit/legacy/dist/logger/logger';
import { handleErrorAndExit } from '@teambit/legacy/dist/cli/handle-errors';
import { loadConsumerIfExist } from '@teambit/legacy/dist/consumer';
Expand Down
7 changes: 1 addition & 6 deletions scopes/harmony/cli/cli.main.runtime.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Slot, SlotRegistry } from '@teambit/harmony';
import { buildRegistry } from '@teambit/legacy/dist/cli';
import legacyLogger from '@teambit/legacy/dist/logger/logger';
import { CLIArgs, Flags, Command } from '@teambit/legacy/dist/cli/command';
import pMapSeries from 'p-map-series';
Expand All @@ -10,7 +9,6 @@ import { Logger, LoggerAspect, LoggerMain } from '@teambit/logger';
import { clone } from 'lodash';
import { CLIAspect, MainRuntime } from './cli.aspect';
import { getCommandId } from './get-command-id';
import { LegacyCommandAdapter } from './legacy-command-adapter';
import { CLIParser, findCommandByArgv } from './cli-parser';
import { CompletionCmd } from './completion.cmd';
import { CliCmd, CliGenerateCmd } from './cli.cmd';
Expand Down Expand Up @@ -185,15 +183,12 @@ export class CLIMain {
) {
const logger = loggerMain.createLogger(CLIAspect.id);
const cliMain = new CLIMain(commandsSlot, onStartSlot, onCommandStartSlot, onBeforeExitSlot, logger);
const legacyRegistry = buildRegistry();
await ensureWorkspaceAndScope();
const legacyCommands = legacyRegistry.commands;
const legacyCommandsAdapters = legacyCommands.map((command) => new LegacyCommandAdapter(command, cliMain));
const cliGenerateCmd = new CliGenerateCmd(cliMain);
const cliCmd = new CliCmd(cliMain);
const helpCmd = new HelpCmd(cliMain);
cliCmd.commands.push(cliGenerateCmd);
cliMain.register(...legacyCommandsAdapters, new CompletionCmd(), cliCmd, helpCmd, new VersionCmd());
cliMain.register(new CompletionCmd(), cliCmd, helpCmd, new VersionCmd());
return cliMain;
}
}
Expand Down
6 changes: 5 additions & 1 deletion scopes/harmony/cli/command-runner.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import logger, { shouldDisableLoader } from '@teambit/legacy/dist/logger/logger';
import { CLIArgs, Command, Flags } from '@teambit/legacy/dist/cli/command';
import { parseCommandName } from '@teambit/legacy/dist/cli/command-registry';
import loader from '@teambit/legacy/dist/cli/loader';
import { handleErrorAndExit } from '@teambit/legacy/dist/cli/handle-errors';
import { TOKEN_FLAG_NAME } from '@teambit/legacy/dist/constants';
Expand Down Expand Up @@ -108,3 +107,8 @@ export class CommandRunner {
return process.stdout.write(data, async () => logger.exitAfterFlush(exitCode, this.commandName, data));
}
}

export function parseCommandName(commandName: string): string {
if (!commandName) return '';
return commandName.split(' ')[0];
}
3 changes: 1 addition & 2 deletions scopes/harmony/cli/generate-doc-md.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Command, CommandArg } from '@teambit/legacy/dist/cli/command';
import { CommandOptions } from '@teambit/legacy/dist/cli/legacy-command';
import { CommandOptions, Command, CommandArg } from '@teambit/legacy/dist/cli/command';
import { pick } from 'lodash';
import { getCommandId } from './get-command-id';

Expand Down
3 changes: 1 addition & 2 deletions scopes/harmony/cli/help.cmd.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { Command } from '@teambit/legacy/dist/cli/command';
import type { CommandOptions } from '@teambit/legacy/dist/cli/legacy-command';
import type { Command, CommandOptions } from '@teambit/legacy/dist/cli/command';
import { CLIMain } from './cli.main.runtime';
import { formatHelp } from './help';

Expand Down
3 changes: 1 addition & 2 deletions scopes/harmony/cli/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { CLIAspect, MainRuntime } from './cli.aspect';

export type { CLIMain, CommandList, CommandsSlot } from './cli.main.runtime';
export type { Command, CLIArgs, Flags, GenericObject } from '@teambit/legacy/dist/cli/command';
export type { CommandOptions } from '@teambit/legacy/dist/cli/legacy-command';
export type { Command, CLIArgs, Flags, GenericObject, CommandOptions } from '@teambit/legacy/dist/cli/command';
export * from './exceptions';

export { CLIAspect as default, MainRuntime, CLIAspect };
66 changes: 0 additions & 66 deletions scopes/harmony/cli/legacy-command-adapter.ts

This file was deleted.

3 changes: 1 addition & 2 deletions scopes/harmony/cli/version.cmd.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { Command } from '@teambit/legacy/dist/cli/command';
import type { CommandOptions } from '@teambit/legacy/dist/cli/legacy-command';
import type { CommandOptions, Command } from '@teambit/legacy/dist/cli/command';
import { getHarmonyVersion } from '@teambit/legacy/dist/bootstrap';

export class VersionCmd implements Command {
Expand Down
4 changes: 4 additions & 0 deletions scopes/harmony/global-config/config-cmd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class ConfigGet implements Command {
description = 'get a value from global configuration';
alias = '';
options = [] as CommandOptions;
loadAspects = false;

async report([key]: [string]) {
const value = await config.get(key);
Expand All @@ -39,6 +40,7 @@ class ConfigList implements Command {
description = 'list all configuration(s)';
alias = '';
options = [] as CommandOptions;
loadAspects = false;

async report() {
const conf: { [key: string]: string } = await config.list();
Expand All @@ -55,6 +57,7 @@ class ConfigDel implements Command {
description = 'delete given key from global configuration';
alias = '';
options = [] as CommandOptions;
loadAspects = false;

async report([key]: [string]) {
await config.del(key);
Expand All @@ -68,6 +71,7 @@ export class ConfigCmd implements Command {
extendedDescription = `${BASE_DOCS_DOMAIN}reference/config/bit-config`;
group = 'general';
alias = '';
loadAspects = false;
commands = [new ConfigSet(), new ConfigDel(), new ConfigGet(), new ConfigList()];
options = [] as CommandOptions;

Expand Down
3 changes: 2 additions & 1 deletion scopes/harmony/global-config/global-config.main.runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { GlobalConfigAspect } from './global-config.aspect';
import { GlobalsCmd } from './globals.cmd';
import { SystemCmd, SystemLogCmd, SystemTailLogCmd } from './system.cmd';
import { ConfigCmd } from './config-cmd';
import { RemoteCmd } from './remote-cmd';

export class GlobalConfigMain {
static runtime = MainRuntime;
Expand Down Expand Up @@ -90,7 +91,7 @@ export class GlobalConfigMain {
const globalConfig = new GlobalConfigMain();
const systemCmd = new SystemCmd();
systemCmd.commands = [new SystemLogCmd(), new SystemTailLogCmd()];
cli.register(new GlobalsCmd(globalConfig), systemCmd, new ConfigCmd());
cli.register(new GlobalsCmd(globalConfig), systemCmd, new ConfigCmd(), new RemoteCmd());
return globalConfig;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,58 +3,50 @@
import chalk from 'chalk';
import Table from 'cli-table';
import { forEach, isEmpty } from 'lodash';
import { add, list, remove } from './remote';
import { Command, CommandOptions } from '@teambit/cli';

import { remoteAdd, remoteList, remoteRm } from '../../../api/consumer';
import { Group } from '../../command-groups';
import { CommandOptions, LegacyCommand } from '../../legacy-command';

class RemoteAdd implements LegacyCommand {
class RemoteAdd implements Command {
name = 'add <url>';
description = 'add a bare-scope as a remote';
extendedDescription = `supported protocols are [file, http].
for example: "http://localhost:3000", "file:///tmp/local-scope"`;
alias = '';
opts = [['g', 'global', 'configure a remote bit scope']] as CommandOptions;

action([url]: [string], { global }: { global: boolean }): Promise<any> {
return remoteAdd(url, global);
}
loadAspects = false;
options = [['g', 'global', 'configure a remote bit scope']] as CommandOptions;

report({ name, host }: { name: string; host: string }): string {
async report([url]: [string], { global }: { global: boolean }) {
const { name, host }: { name: string; host: string } = await add(url, global);
return chalk.green(`added remote scope '${chalk.bold(name)}' with host '${chalk.bold(host)}'`);
}
}

class RemoteRm implements LegacyCommand {
class RemoteRm implements Command {
name = 'del <name>';
description = 'remove a tracked bit remote';
alias = '';
opts = [['g', 'global', 'remove a globally configured remote scope']] as CommandOptions;
loadAspects = false;
options = [['g', 'global', 'remove a globally configured remote scope']] as CommandOptions;

action([name]: [string], { global }: { global: boolean }): Promise<any> {
return remoteRm(name, global);
}

report(name: string): string {
async report([name]: [string], { global }: { global: boolean }) {
await remove(name, global);
return chalk.green(`successfully removed remote ${chalk.bold(name)}`);
}
}

export default class Remote implements LegacyCommand {
export class RemoteCmd implements Command {
name = 'remote';
description = 'manage set of tracked bit scope(s)';
group: Group = 'collaborate';
group = 'collaborate';
helpUrl = 'reference/scope/remote-scopes';
alias = '';
opts = [['g', 'global', 'see globally configured remotes']] as CommandOptions;
loadAspects = false;
options = [['g', 'global', 'see globally configured remotes']] as CommandOptions;
// @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
commands = [new RemoteAdd(), new RemoteRm()];

action(args: string[], { global }: { global: boolean }): Promise<any> {
return remoteList(global);
}

report(remotes: { [key: string]: string }): string {
async report(args: string[], { global }: { global: boolean }) {
const remotes: { [key: string]: string } = await list(global);
if (isEmpty(remotes)) return chalk.red('no configured remotes found in scope');

const table = new Table({ head: ['scope name', 'host'], style: { head: ['cyan'] } });
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { BitError } from '@teambit/bit-error';
import { GlobalRemotes } from '../../../global-config';
import { Remote } from '../../../remotes';
import { loadScope } from '../../../scope';
import { getScopeRemotes } from '../../../scope/scope-remotes';
import { GlobalRemotes } from '@teambit/legacy/dist/global-config';
import { Remote } from '@teambit/legacy/dist/remotes';
import { loadScope } from '@teambit/legacy/dist/scope';
import { getScopeRemotes } from '@teambit/legacy/dist/scope/scope-remotes';

function buildRemote(url: string): Remote {
// @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
return new Remote(url);
}

export function add(url: string, global: boolean) {
export async function add(url: string, global: boolean) {
const remote = buildRemote(url);
return remote.scope().then((scopeDesc) => {
remote.name = scopeDesc.name;
Expand Down Expand Up @@ -56,7 +56,7 @@ export async function remove(name: string, global: boolean) {
return name;
}

export function list(global: boolean) {
export async function list(global: boolean) {
if (global) {
return GlobalRemotes.load().then((globalRemotes) => globalRemotes.toPlainObject());
}
Expand All @@ -66,5 +66,3 @@ export function list(global: boolean) {
return getScopeRemotes(scope).then((remotes) => remotes.toPlainObject());
});
}

export function refresh() {}
3 changes: 0 additions & 3 deletions src/api/consumer/index.ts

This file was deleted.

7 changes: 0 additions & 7 deletions src/cli/command-registry-builder.ts

This file was deleted.

20 changes: 0 additions & 20 deletions src/cli/command-registry.ts

This file was deleted.

4 changes: 3 additions & 1 deletion src/cli/command.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Group } from './command-groups';
import { CommandOptions } from './legacy-command';

type CommandOption = [string, string, string];
export type CommandOptions = Array<CommandOption>;

export interface Command {
/**
Expand Down
2 changes: 0 additions & 2 deletions src/cli/index.ts

This file was deleted.

Loading