From f6502b68acf1a200d883e005ad8251dc4cd1eed9 Mon Sep 17 00:00:00 2001 From: David First Date: Fri, 28 Jun 2024 10:58:55 -0400 Subject: [PATCH 1/2] refactor, move legacy-show-cmd to Harmony show-cmd --- .../component/show}/get-consumer-component.ts | 10 +- .../component/show}/get-scope-component.ts | 17 +-- .../component/component/show/legacy-show.ts | 8 +- .../component/show/nothing-to-compare-to.ts | 7 + .../component/show/show-legacy-cmd.ts | 114 ++++++++++++++ scopes/component/component/show/show.cmd.ts | 7 +- src/api.ts | 28 ++-- src/api/consumer/index.ts | 15 +- .../lib/exceptions/nothing-to-compare-to.ts | 10 -- src/cli/commands/public-cmds/show-cmd.ts | 139 ------------------ src/cli/default-error-handler.ts | 2 - src/cli/loader/loader-messages.ts | 1 - 12 files changed, 157 insertions(+), 201 deletions(-) rename {src/api/consumer/lib => scopes/component/component/show}/get-consumer-component.ts (84%) rename {src/api/consumer/lib => scopes/component/component/show}/get-scope-component.ts (78%) rename src/api/consumer/lib/show.ts => scopes/component/component/show/legacy-show.ts (83%) create mode 100644 scopes/component/component/show/nothing-to-compare-to.ts create mode 100644 scopes/component/component/show/show-legacy-cmd.ts delete mode 100644 src/api/consumer/lib/exceptions/nothing-to-compare-to.ts delete mode 100644 src/cli/commands/public-cmds/show-cmd.ts diff --git a/src/api/consumer/lib/get-consumer-component.ts b/scopes/component/component/show/get-consumer-component.ts similarity index 84% rename from src/api/consumer/lib/get-consumer-component.ts rename to scopes/component/component/show/get-consumer-component.ts index 555a65929f4a..ef8edf6ce8e3 100644 --- a/src/api/consumer/lib/get-consumer-component.ts +++ b/scopes/component/component/show/get-consumer-component.ts @@ -1,8 +1,8 @@ -import { Consumer, loadConsumer } from '../../../consumer'; -import DependencyGraph, { DependenciesInfo } from '../../../scope/graph/scope-graph'; -import NothingToCompareTo from './exceptions/nothing-to-compare-to'; +import { Consumer, loadConsumer } from '@teambit/legacy/dist/consumer'; +import DependencyGraph, { DependenciesInfo } from '@teambit/legacy/dist/scope/graph/scope-graph'; +import { NothingToCompareTo } from './nothing-to-compare-to'; -export default (async function getConsumerBit({ +export async function getConsumerComponent({ id, compare, allVersions, @@ -46,4 +46,4 @@ export default (async function getConsumerBit({ } await consumer.onDestroy('get-component'); return { component, dependentsInfo, dependenciesInfo }; -}); +} diff --git a/src/api/consumer/lib/get-scope-component.ts b/scopes/component/component/show/get-scope-component.ts similarity index 78% rename from src/api/consumer/lib/get-scope-component.ts rename to scopes/component/component/show/get-scope-component.ts index 944d6bd4d77e..71ddcff1ddea 100644 --- a/src/api/consumer/lib/get-scope-component.ts +++ b/scopes/component/component/show/get-scope-component.ts @@ -1,13 +1,12 @@ import { ComponentID } from '@teambit/component-id'; -import loader from '../../../cli/loader'; -import { BEFORE_REMOTE_SHOW } from '../../../cli/loader/loader-messages'; -import { Consumer, loadConsumerIfExist } from '../../../consumer'; -import Component from '../../../consumer/component'; -import getRemoteByName from '../../../remotes/get-remote-by-name'; -import { loadScope, Scope } from '../../../scope'; -import { DependenciesInfo } from '../../../scope/graph/scope-graph'; +import loader from '@teambit/legacy/dist/cli/loader'; +import { Consumer, loadConsumerIfExist } from '@teambit/legacy/dist/consumer'; +import Component from '@teambit/legacy/dist/consumer/component'; +import getRemoteByName from '@teambit/legacy/dist/remotes/get-remote-by-name'; +import { loadScope, Scope } from '@teambit/legacy/dist/scope'; +import { DependenciesInfo } from '@teambit/legacy/dist/scope/graph/scope-graph'; -export default async function getScopeComponent({ +export async function getScopeComponent({ id, allVersions, scopePath, @@ -33,7 +32,7 @@ export default async function getScopeComponent({ const consumer: Consumer | undefined = await loadConsumerIfExist(); const remote = await getRemoteByName(bitId.scope, consumer); - loader.start(BEFORE_REMOTE_SHOW); + loader.start('showing a component...'); const component = await remote.show(bitId); let dependenciesInfo: DependenciesInfo[] = []; let dependentsInfo: DependenciesInfo[] = []; diff --git a/src/api/consumer/lib/show.ts b/scopes/component/component/show/legacy-show.ts similarity index 83% rename from src/api/consumer/lib/show.ts rename to scopes/component/component/show/legacy-show.ts index 3c6203734a39..30dab5e29bb4 100644 --- a/src/api/consumer/lib/show.ts +++ b/scopes/component/component/show/legacy-show.ts @@ -1,6 +1,8 @@ -import { getConsumerComponent, getScopeComponent } from '..'; -import loader from '../../../cli/loader/loader'; -import { BEFORE_SHOW_REMOTE } from '../../../cli/loader/loader-messages'; +import { getConsumerComponent } from './get-consumer-component'; +import { getScopeComponent } from './get-scope-component'; +import loader from '@teambit/legacy/dist/cli/loader/loader'; + +const BEFORE_SHOW_REMOTE = 'showing a component...'; export default async function show({ id, diff --git a/scopes/component/component/show/nothing-to-compare-to.ts b/scopes/component/component/show/nothing-to-compare-to.ts new file mode 100644 index 000000000000..0d72fb6e2274 --- /dev/null +++ b/scopes/component/component/show/nothing-to-compare-to.ts @@ -0,0 +1,7 @@ +import { BitError } from '@teambit/bit-error'; + +export class NothingToCompareTo extends BitError { + constructor(public id: string) { + super('no previous versions to compare'); + } +} diff --git a/scopes/component/component/show/show-legacy-cmd.ts b/scopes/component/component/show/show-legacy-cmd.ts new file mode 100644 index 000000000000..03f4ad432211 --- /dev/null +++ b/scopes/component/component/show/show-legacy-cmd.ts @@ -0,0 +1,114 @@ +import { BitError } from '@teambit/bit-error'; +import { show } from '@teambit/legacy/dist/api/consumer'; +import ConsumerComponent from '@teambit/legacy/dist/consumer/component/consumer-component'; +import { DependenciesInfo } from '@teambit/legacy/dist/scope/graph/scope-graph'; +import paintComponent from '@teambit/legacy/dist/cli/templates/component-template'; + +export function actionLegacy( + [id]: [string], + { + json, + versions, + remote = false, + outdated = false, + compare = false, + detailed = false, + dependents = false, + dependencies = false, + }: { + json?: boolean; + versions: boolean | null | undefined; + remote: boolean; + outdated?: boolean; + compare?: boolean; + detailed?: boolean; + dependents?: boolean; + dependencies?: boolean; + } +): Promise { + if (versions && (compare || outdated)) { + throw new BitError('the [--compare] or [--outdated] flag cannot be used along with --versions'); + } + if (versions && remote) { + throw new BitError('the [--versions] and [--remote] flags cannot be used together'); + } + if (compare && outdated) { + throw new BitError('the [--compare] and [--outdated] flags cannot be used together'); + } + + return show({ + id, + // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX! + json, + versions, + remote, + outdated, + compare, + detailed, + dependents, + dependencies, + }); +} + +export function reportLegacy({ + component, + componentModel, + dependenciesInfo, + dependentsInfo, + json, + versions, + components, + outdated, + detailed, +}: { + component: ConsumerComponent; + componentModel?: ConsumerComponent; + dependenciesInfo: DependenciesInfo[]; + dependentsInfo: DependenciesInfo[]; + json: boolean | null | undefined; + versions: boolean | null | undefined; + components: ConsumerComponent[] | null | undefined; + outdated: boolean; + detailed: boolean; +}): string { + if (versions) { + return JSON.stringify( + (components || []).map((c) => c.toObject()), + null, + ' ' + ); + } + if (component.componentFromModel) { + component.scopesList = component.componentFromModel.scopesList; + } + if (json) { + const makeComponentReadable = (comp: ConsumerComponent) => { + if (!comp) return comp; + const componentObj = comp.toObject(); + componentObj.files = comp.files.map((file) => file.toReadableString()); + + if (comp.componentMap) { + componentObj.componentDir = comp.componentMap.getComponentDir(); + } + + return componentObj; + }; + const componentFromFileSystem = makeComponentReadable(component); + if (dependenciesInfo) { + // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX! + componentFromFileSystem.dependenciesInfo = dependenciesInfo; + } + if (dependentsInfo) { + // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX! + componentFromFileSystem.dependentsInfo = dependentsInfo; + } + if (component.scopesList) { + // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX! + componentFromFileSystem.scopesList = component.scopesList; + } + const componentFromModel = componentModel ? makeComponentReadable(componentModel) : undefined; + const jsonObject = componentFromModel ? { componentFromFileSystem, componentFromModel } : componentFromFileSystem; + return JSON.stringify(jsonObject, null, ' '); + } + return paintComponent(component, componentModel, outdated, detailed, dependenciesInfo, dependentsInfo); +} diff --git a/scopes/component/component/show/show.cmd.ts b/scopes/component/component/show/show.cmd.ts index f2d807adf483..a4418b8a4f69 100644 --- a/scopes/component/component/show/show.cmd.ts +++ b/scopes/component/component/show/show.cmd.ts @@ -6,8 +6,8 @@ import { compact } from 'lodash'; import { CLITable } from '@teambit/cli-table'; import { MissingBitMapComponent } from '@teambit/legacy/dist/consumer/bit-map/exceptions'; import { ComponentID } from '@teambit/component-id'; -import LegacyShow from '@teambit/legacy/dist/cli/commands/public-cmds/show-cmd'; import { Logger } from '@teambit/logger'; +import { reportLegacy, actionLegacy } from './show-legacy-cmd'; import { ComponentMain } from '../component.main.runtime'; export class ShowCmd implements Command { @@ -51,15 +51,14 @@ to see the legacy bit show, please use "--legacy" flag`); } async useLegacy(id: string, json = false, remote = false, compare = false) { - const legacyShow = new LegacyShow(); - const showData = await legacyShow.action([id], { + const showData = await actionLegacy([id], { json, versions: undefined, remote, compare, }); - return legacyShow.report(showData); + return reportLegacy(showData); } async report( diff --git a/src/api.ts b/src/api.ts index e22017a15fbc..3007cc7fc811 100644 --- a/src/api.ts +++ b/src/api.ts @@ -1,4 +1,4 @@ -import { getScopeComponent } from './api/consumer/index'; +// import { getScopeComponent } from './api/consumer/index'; import HooksManager from './hooks'; // import { registerCoreExtensions } from './extensions/bit'; // import { manifestsMap as coreExtensions } from './extensions/bit'; @@ -7,16 +7,16 @@ import HooksManager from './hooks'; HooksManager.init(); -export function show(scopePath: string, id: string, opts?: Record) { - // When using the API programmatically do not use the scope cache by default - const loadScopeFromCache = opts && opts.loadScopeFromCache !== undefined ? !!opts.loadScopeFromCache : false; - // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX! - return getScopeComponent({ scopePath, id, allVersions: opts && opts.versions, loadScopeFromCache }).then( - ({ component }) => { - if (Array.isArray(component)) { - return component.map((v) => v.toObject()); - } - return component.toObject(); - } - ); -} +// export function show(scopePath: string, id: string, opts?: Record) { +// // When using the API programmatically do not use the scope cache by default +// const loadScopeFromCache = opts && opts.loadScopeFromCache !== undefined ? !!opts.loadScopeFromCache : false; +// // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX! +// return getScopeComponent({ scopePath, id, allVersions: opts && opts.versions, loadScopeFromCache }).then( +// ({ component }) => { +// if (Array.isArray(component)) { +// return component.map((v) => v.toObject()); +// } +// return component.toObject(); +// } +// ); +// } diff --git a/src/api/consumer/index.ts b/src/api/consumer/index.ts index 2fd7d21fe48b..b8b7adac0608 100644 --- a/src/api/consumer/index.ts +++ b/src/api/consumer/index.ts @@ -1,19 +1,6 @@ import getComponentLogs from './lib/get-component-logs'; -import getConsumerComponent from './lib/get-consumer-component'; -import getScopeComponent from './lib/get-scope-component'; import { listScope } from './lib/list-scope'; import { add as remoteAdd, list as remoteList, remove as remoteRm } from './lib/remote'; -import show from './lib/show'; import { clearCache } from './lib/clear-cache'; -export { - listScope, - getConsumerComponent, - getScopeComponent, - getComponentLogs, - remoteAdd, - remoteList, - remoteRm, - show, - clearCache, -}; +export { listScope, getComponentLogs, remoteAdd, remoteList, remoteRm, clearCache }; diff --git a/src/api/consumer/lib/exceptions/nothing-to-compare-to.ts b/src/api/consumer/lib/exceptions/nothing-to-compare-to.ts deleted file mode 100644 index 7023b0e2e2e4..000000000000 --- a/src/api/consumer/lib/exceptions/nothing-to-compare-to.ts +++ /dev/null @@ -1,10 +0,0 @@ -import AbstractError from '../../../../error/abstract-error'; - -export default class NothingToCompareTo extends AbstractError { - id: string; - - constructor(id: string) { - super(); - this.id = id; - } -} diff --git a/src/cli/commands/public-cmds/show-cmd.ts b/src/cli/commands/public-cmds/show-cmd.ts deleted file mode 100644 index 5bd2c3fbcef9..000000000000 --- a/src/cli/commands/public-cmds/show-cmd.ts +++ /dev/null @@ -1,139 +0,0 @@ -import { BitError } from '@teambit/bit-error'; -import { show } from '../../../api/consumer'; -import { BASE_DOCS_DOMAIN } from '../../../constants'; -import ConsumerComponent from '../../../consumer/component/consumer-component'; -import { DependenciesInfo } from '../../../scope/graph/scope-graph'; -import { Group } from '../../command-groups'; -import { CommandOptions, LegacyCommand } from '../../legacy-command'; -import paintComponent from '../../templates/component-template'; - -export default class Show implements LegacyCommand { - name = 'show '; - description = 'show component overview'; - extendedDescription = `${BASE_DOCS_DOMAIN}reference/components/component-config`; - group: Group = 'info'; - alias = ''; - opts = [ - ['j', 'json', 'return component overview in json format'], - ['r', 'remote', 'show overview of a remote component'], - ['v', 'versions', 'return a json of all the versions of the component'], - ['o', 'outdated', 'highlight outdated components, in comparison with their latest remote version (if one exists)'], - ['c', 'compare', 'compare current file system component to latest tagged component [default=latest]'], - ['d', 'detailed', 'show extra component detail'], - ['', 'dependents', 'show all dependents recursively'], - ['', 'dependencies', 'show all dependencies recursively'], - ['', 'legacy', ''], - ] as CommandOptions; - loader = true; - skipWorkspace = true; - remoteOp = true; - - action( - [id]: [string], - { - json, - versions, - remote = false, - outdated = false, - compare = false, - detailed = false, - dependents = false, - dependencies = false, - }: { - json?: boolean; - versions: boolean | null | undefined; - remote: boolean; - outdated?: boolean; - compare?: boolean; - detailed?: boolean; - dependents?: boolean; - dependencies?: boolean; - } - ): Promise { - if (versions && (compare || outdated)) { - throw new BitError('the [--compare] or [--outdated] flag cannot be used along with --versions'); - } - if (versions && remote) { - throw new BitError('the [--versions] and [--remote] flags cannot be used together'); - } - if (compare && outdated) { - throw new BitError('the [--compare] and [--outdated] flags cannot be used together'); - } - - return show({ - id, - // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX! - json, - versions, - remote, - outdated, - compare, - detailed, - dependents, - dependencies, - }); - } - - report({ - component, - componentModel, - dependenciesInfo, - dependentsInfo, - json, - versions, - components, - outdated, - detailed, - }: { - component: ConsumerComponent; - componentModel?: ConsumerComponent; - dependenciesInfo: DependenciesInfo[]; - dependentsInfo: DependenciesInfo[]; - json: boolean | null | undefined; - versions: boolean | null | undefined; - components: ConsumerComponent[] | null | undefined; - outdated: boolean; - detailed: boolean; - }): string { - if (versions) { - return JSON.stringify( - (components || []).map((c) => c.toObject()), - null, - ' ' - ); - } - if (component.componentFromModel) { - component.scopesList = component.componentFromModel.scopesList; - } - if (json) { - const makeComponentReadable = (comp: ConsumerComponent) => { - if (!comp) return comp; - const componentObj = comp.toObject(); - componentObj.files = comp.files.map((file) => file.toReadableString()); - - if (comp.componentMap) { - componentObj.componentDir = comp.componentMap.getComponentDir(); - } - - return componentObj; - }; - const componentFromFileSystem = makeComponentReadable(component); - if (dependenciesInfo) { - // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX! - componentFromFileSystem.dependenciesInfo = dependenciesInfo; - } - if (dependentsInfo) { - // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX! - componentFromFileSystem.dependentsInfo = dependentsInfo; - } - if (component.scopesList) { - // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX! - componentFromFileSystem.scopesList = component.scopesList; - } - const componentFromModel = componentModel ? makeComponentReadable(componentModel) : undefined; - const jsonObject = componentFromModel ? { componentFromFileSystem, componentFromModel } : componentFromFileSystem; - return JSON.stringify(jsonObject, null, ' '); - } - return paintComponent(component, componentModel, outdated, detailed, dependenciesInfo, dependentsInfo); - } -} diff --git a/src/cli/default-error-handler.ts b/src/cli/default-error-handler.ts index f5ae2f329db9..0bad5df1e28d 100644 --- a/src/cli/default-error-handler.ts +++ b/src/cli/default-error-handler.ts @@ -4,7 +4,6 @@ import chalk from 'chalk'; import { BitError } from '@teambit/bit-error'; import { Analytics, LEVEL } from '@teambit/legacy.analytics'; import NoIdMatchWildcard from '../api/consumer/lib/exceptions/no-id-match-wildcard'; -import NothingToCompareTo from '../api/consumer/lib/exceptions/nothing-to-compare-to'; import ObjectsWithoutConsumer from '../api/consumer/lib/exceptions/objects-without-consumer'; import { BASE_DOCS_DOMAIN } from '../constants'; import { InvalidBitMap, MissingMainFile } from '../consumer/bit-map/exceptions'; @@ -215,7 +214,6 @@ please use "bit remove" to delete the component or "bit add" with "--main" and " NoIdMatchWildcard, (err) => `unable to find component ids that match the following: ${err.idsWithWildcards.join(', ')}`, ], - [NothingToCompareTo, () => 'no previous versions to compare'], [ NewerVersionFound, // err => JSON.stringify(err.newerVersions) diff --git a/src/cli/loader/loader-messages.ts b/src/cli/loader/loader-messages.ts index df2b7b301193..7edfae6ffb50 100644 --- a/src/cli/loader/loader-messages.ts +++ b/src/cli/loader/loader-messages.ts @@ -14,4 +14,3 @@ export const BEFORE_EXPORTS = 'exporting components'; export const BEFORE_LOADING_COMPONENTS = 'loading components'; export const BEFORE_STATUS = 'fetching status'; export const BEFORE_CHECKOUT = 'switching component version...'; -export const BEFORE_SHOW_REMOTE = 'showing a component...'; From 07d64db4a2319b05619720ad31d9a693a4302253 Mon Sep 17 00:00:00 2001 From: David First Date: Fri, 28 Jun 2024 12:14:44 -0400 Subject: [PATCH 2/2] fix lint --- scopes/component/component/show/legacy-show.ts | 2 +- scopes/component/component/show/show-legacy-cmd.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/scopes/component/component/show/legacy-show.ts b/scopes/component/component/show/legacy-show.ts index 30dab5e29bb4..c8150f827c44 100644 --- a/scopes/component/component/show/legacy-show.ts +++ b/scopes/component/component/show/legacy-show.ts @@ -4,7 +4,7 @@ import loader from '@teambit/legacy/dist/cli/loader/loader'; const BEFORE_SHOW_REMOTE = 'showing a component...'; -export default async function show({ +export async function show({ id, json, versions, diff --git a/scopes/component/component/show/show-legacy-cmd.ts b/scopes/component/component/show/show-legacy-cmd.ts index 03f4ad432211..19cd893ddcb1 100644 --- a/scopes/component/component/show/show-legacy-cmd.ts +++ b/scopes/component/component/show/show-legacy-cmd.ts @@ -1,5 +1,5 @@ import { BitError } from '@teambit/bit-error'; -import { show } from '@teambit/legacy/dist/api/consumer'; +import { show } from './legacy-show'; import ConsumerComponent from '@teambit/legacy/dist/consumer/component/consumer-component'; import { DependenciesInfo } from '@teambit/legacy/dist/scope/graph/scope-graph'; import paintComponent from '@teambit/legacy/dist/cli/templates/component-template';