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

improvement(api-ide), save env icons in the local scope for the IDE to show it #9172

Merged
merged 2 commits into from
Sep 3, 2024
Merged
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
55 changes: 53 additions & 2 deletions scopes/harmony/api-server/api-for-ide.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import path from 'path';
import fs from 'fs-extra';
import filenamify from 'filenamify';
import { CompFiles, Workspace, FilesStatus } from '@teambit/workspace';
import { PathOsBasedAbsolute, PathOsBasedRelative, pathJoinLinux } from '@teambit/legacy.utils';
import pMap from 'p-map';
Expand All @@ -16,15 +17,17 @@ import { GeneratorMain } from '@teambit/generator';
import { getParsedHistoryMetadata } from '@teambit/legacy/dist/consumer/consumer';
import RemovedObjects from '@teambit/legacy/dist/scope/removed-components';
import { RemoveMain } from '@teambit/remove';
import { compact } from 'lodash';
import { compact, uniq } 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';
import fetch from 'node-fetch';

const FILES_HISTORY_DIR = 'files-history';
const ENV_ICONS_DIR = 'env-icons';
const LAST_SNAP_DIR = 'last-snap';
const CMD_HISTORY = 'command-history-ide';

Expand Down Expand Up @@ -70,10 +73,12 @@ type CompMetadata = {
id: string;
name?: string;
icon: string;
localIconPath?: string;
};
};

export class APIForIDE {
private existingEnvIcons: string[] | undefined;
constructor(
private workspace: Workspace,
private snapping: SnappingMain,
Expand Down Expand Up @@ -199,7 +204,7 @@ export class APIForIDE {
async getCompsMetadata(): Promise<CompMetadata[]> {
const comps = await this.workspace.list();
const apps = await this.application.listAppsIdsAndNames();
const results = await pMap(
const results: CompMetadata[] = await pMap(
comps,
async (comp) => {
const id = comp.id;
Expand All @@ -219,9 +224,55 @@ export class APIForIDE {
},
{ concurrency: 30 }
);
const allIcons = uniq(compact(results.map((r) => r.env.icon)));
const iconsMap = await this.getEnvIconsMapFetchIfMissing(allIcons);
results.forEach((r) => {
r.env.localIconPath = iconsMap[r.env.icon];
});
return results;
}

private getEnvIconsFullPath() {
return path.join(this.workspace.scope.path, ENV_ICONS_DIR);
}

private async getExistingEnvIcons(): Promise<string[]> {
if (!this.existingEnvIcons) {
const envIconsDir = this.getEnvIconsFullPath();
await fs.ensureDir(envIconsDir);
const existingIcons = await fs.readdir(envIconsDir);
this.existingEnvIcons = existingIcons;
}
return this.existingEnvIcons;
}

private async getEnvIconsMapFetchIfMissing(icons: string[]): Promise<{ [iconHttpUrl: string]: string }> {
const existingIcons = await this.getExistingEnvIcons();
const iconsMap: Record<string, string> = {};
await Promise.all(
icons.map(async (icon) => {
const iconFileName = filenamify(icon, { replacement: '-' });
const fullIconPath = path.join(this.workspace.scope.path, ENV_ICONS_DIR, iconFileName);
if (existingIcons.includes(iconFileName)) {
iconsMap[icon] = fullIconPath;
return;
}
let res;
// download the icon from the url and save it locally.
try {
res = await fetch(icon);
} catch (err: any) {
throw new Error(`failed to get the icon from ${icon}, error: ${err.message}`);
}
const svgText = await res.text();
await fs.outputFile(fullIconPath, svgText);
iconsMap[icon] = fullIconPath;
this.existingEnvIcons?.push(iconFileName);
})
);
return iconsMap;
}

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