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, move legacy-show-cmd to Harmony show-cmd #8987

Merged
merged 3 commits into from
Jun 28, 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
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -46,4 +46,4 @@ export default (async function getConsumerBit({
}
await consumer.onDestroy('get-component');
return { component, dependentsInfo, dependenciesInfo };
});
}
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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[] = [];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
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';

export default async function show({
const BEFORE_SHOW_REMOTE = 'showing a component...';

export async function show({
id,
json,
versions,
Expand Down
7 changes: 7 additions & 0 deletions scopes/component/component/show/nothing-to-compare-to.ts
Original file line number Diff line number Diff line change
@@ -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');
}
}
114 changes: 114 additions & 0 deletions scopes/component/component/show/show-legacy-cmd.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import { BitError } from '@teambit/bit-error';
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';

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<any> {
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);
}
7 changes: 3 additions & 4 deletions scopes/component/component/show/show.cmd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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(
Expand Down
28 changes: 14 additions & 14 deletions src/api.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -7,16 +7,16 @@ import HooksManager from './hooks';

HooksManager.init();

export function show(scopePath: string, id: string, opts?: Record<string, any>) {
// 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<string, any>) {
// // 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();
// }
// );
// }
5 changes: 1 addition & 4 deletions src/api/consumer/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
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';

export { listScope, getConsumerComponent, getScopeComponent, remoteAdd, remoteList, remoteRm, show };
export { listScope, remoteAdd, remoteList, remoteRm };
10 changes: 0 additions & 10 deletions src/api/consumer/lib/exceptions/nothing-to-compare-to.ts

This file was deleted.

Loading