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

feat(api-ide), provide component metadata such as env-id/env-icon/is-app #9139

Merged
merged 3 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
45 changes: 44 additions & 1 deletion scopes/harmony/api-server/api-for-ide.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ import { compact } from 'lodash';
import { getCloudDomain } from '@teambit/legacy/dist/constants';
import { ConfigMain } from '@teambit/config';
import { LANE_REMOTE_DELIMITER, LaneId } from '@teambit/lane-id';
import { ApplicationMain } from '@teambit/application';
import { DeprecationMain } from '@teambit/deprecation';
import { EnvsMain } from '@teambit/envs';

const FILES_HISTORY_DIR = 'files-history';
const LAST_SNAP_DIR = 'last-snap';
Expand Down Expand Up @@ -59,6 +62,17 @@ type WorkspaceHistory = {
history: Array<{ path: PathOsBasedAbsolute; fileId: string; reason?: string }>;
};

type CompMetadata = {
id: string;
isDeprecated: boolean;
isApp: boolean;
env: {
id: string;
name?: string;
icon: string;
};
};

export class APIForIDE {
constructor(
private workspace: Workspace,
Expand All @@ -71,7 +85,10 @@ export class APIForIDE {
private componentCompare: ComponentCompareMain,
private generator: GeneratorMain,
private remove: RemoveMain,
private config: ConfigMain
private config: ConfigMain,
private application: ApplicationMain,
private deprecation: DeprecationMain,
private envs: EnvsMain
) {}

async logStartCmdHistory(op: string) {
Expand Down Expand Up @@ -179,6 +196,32 @@ export class APIForIDE {
return this.lanes.createLane(name);
}

async getCompsMetadata(): Promise<CompMetadata[]> {
const comps = await this.workspace.list();
const apps = await this.application.listAppsIdsAndNames();
const results = await pMap(
comps,
async (comp) => {
const id = comp.id;
const deprecationInfo = await this.deprecation.getDeprecationInfo(comp);
const isApp = apps.find((app) => app.id === id.toString());
const env = this.envs.getEnv(comp);
return {
id: id.toStringWithoutVersion(),
isDeprecated: deprecationInfo.isDeprecate,
isApp: Boolean(isApp),
env: {
id: env.id,
name: env.name,
icon: env.icon,
},
};
},
{ concurrency: 30 }
);
return results;
}

async getCompFiles(id: string): Promise<{ dirAbs: string; filesRelative: PathOsBasedRelative[] }> {
const compId = await this.workspace.resolveComponentId(id);
const comp = await this.workspace.get(compId);
Expand Down
19 changes: 17 additions & 2 deletions scopes/harmony/api-server/api-server.main.runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ import { APIForIDE } from './api-for-ide';
import { SSEEventsRoute } from './sse-events.route';
import { join } from 'path';
import { CLIRawRoute } from './cli-raw.route';
import { ApplicationAspect, ApplicationMain } from '@teambit/application';
import DeprecationAspect, { DeprecationMain } from '@teambit/deprecation';
import EnvsAspect, { EnvsMain } from '@teambit/envs';

export class ApiServerMain {
constructor(
Expand Down Expand Up @@ -187,6 +190,9 @@ export class ApiServerMain {
GeneratorAspect,
RemoveAspect,
ConfigAspect,
ApplicationAspect,
DeprecationAspect,
EnvsAspect,
];
static runtime = MainRuntime;
static async provider([
Expand All @@ -206,6 +212,9 @@ export class ApiServerMain {
generator,
remove,
config,
application,
deprecation,
envs,
]: [
CLIMain,
Workspace,
Expand All @@ -222,7 +231,10 @@ export class ApiServerMain {
ComponentCompareMain,
GeneratorMain,
RemoveMain,
ConfigMain
ConfigMain,
ApplicationMain,
DeprecationMain,
EnvsMain
]) {
const logger = loggerMain.createLogger(ApiServerAspect.id);
const apiServer = new ApiServerMain(workspace, logger, express, watcher, installer, importer);
Expand All @@ -239,7 +251,10 @@ export class ApiServerMain {
componentCompare,
generator,
remove,
config
config,
application,
deprecation,
envs
);
const cliRoute = new CLIRoute(logger, cli, apiForIDE);
const cliRawRoute = new CLIRawRoute(logger, cli, apiForIDE);
Expand Down
23 changes: 9 additions & 14 deletions scopes/harmony/application/app.cmd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import { Command, CommandOptions } from '@teambit/cli';
// import { Logger } from '@teambit/logger';
import chalk from 'chalk';
import { BitError } from '@teambit/bit-error';
import { CLITable } from '@teambit/cli-table';
import { ApplicationMain } from './application.main.runtime';

Expand All @@ -16,22 +15,18 @@ export class AppListCmd implements Command {

constructor(private applicationAspect: ApplicationMain) {}

async report(args: [string], { json }: { json: boolean }) {
await this.applicationAspect.loadAllAppsAsAspects();
const appComponents = this.applicationAspect.mapApps();
if (json) return JSON.stringify(appComponents, null, 2);
if (!appComponents.length) return chalk.yellow('no apps found');

const rows = appComponents.flatMap(([id, apps]) => {
return apps.map((app) => {
if (!app.name) throw new BitError(`app ${id.toString()} is missing a name`);
return [id, app.name];
});
});

async report() {
const idsAndNames = await this.applicationAspect.listAppsIdsAndNames();
if (!idsAndNames.length) return chalk.yellow('no apps found');
const rows = idsAndNames.map(({ id, name }) => [id, name]);
const table = new CLITable(['id', 'name'], rows);
return table.render();
}

async json() {
const idsAndNames = await this.applicationAspect.listAppsIdsAndNames();
return idsAndNames;
}
}

export class AppCmd implements Command {
Expand Down
11 changes: 11 additions & 0 deletions scopes/harmony/application/application.main.runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,17 @@ export class ApplicationMain {
return flatten(this.appSlot.values());
}

async listAppsIdsAndNames(): Promise<{ id: string; name: string }[]> {
await this.loadAllAppsAsAspects();
const appComponents = this.mapApps();
return appComponents.flatMap(([id, apps]) => {
return apps.map((app) => {
if (!app.name) throw new BitError(`app ${id.toString()} is missing a name`);
return { id, name: app.name };
});
});
}

/**
* map all apps by component ID.
*/
Expand Down