-
Notifications
You must be signed in to change notification settings - Fork 965
Expand file tree
/
Copy pathapi-for-ide.ts
More file actions
849 lines (761 loc) · 31.6 KB
/
Copy pathapi-for-ide.ts
File metadata and controls
849 lines (761 loc) · 31.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
import path from 'path';
import fs from 'fs-extra';
import filenamify from 'filenamify';
import type { CompFiles, Workspace, FilesStatus } from '@teambit/workspace';
import type { PathOsBasedAbsolute, PathOsBasedRelative } from '@teambit/legacy.utils';
import { pathJoinLinux } from '@teambit/legacy.utils';
import pMap from 'p-map';
import type { SnappingMain } from '@teambit/snapping';
import type { LanesMain } from '@teambit/lanes';
import type { InstallMain } from '@teambit/install';
import type { ExportMain } from '@teambit/export';
import type { CheckoutMain } from '@teambit/checkout';
import type { ApplyVersionResults } from '@teambit/component.modules.merge-helper';
import type { ComponentLogMain, FileHashDiffFromParent } from '@teambit/component-log';
import type { LaneLog } from '@teambit/objects';
import type { ComponentCompareMain } from '@teambit/component-compare';
import type {
GenerateResult,
GeneratorMain,
PromptOption,
PromptResults,
TemplateDescriptor,
} from '@teambit/generator';
import { getParsedHistoryMetadata } from '@teambit/legacy.consumer';
import type { RemovedObjects } from '@teambit/legacy.scope';
import type { RemoveMain } from '@teambit/remove';
import { compact, uniq } from 'lodash';
import type { ConfigMain } from '@teambit/config';
import { LANE_REMOTE_DELIMITER, LaneId } from '@teambit/lane-id';
import type { ApplicationMain } from '@teambit/application';
import type { DeprecationMain } from '@teambit/deprecation';
import type { EnvsMain } from '@teambit/envs';
import fetch from 'node-fetch';
import type { GraphMain } from '@teambit/graph';
import type { ScopeMain } from '@teambit/scope';
import { ComponentNotFound } from '@teambit/scope';
import type { ComponentMain, ComponentMap } from '@teambit/component';
import type { SchemaMain } from '@teambit/schema';
import { ComponentUrl } from '@teambit/component.modules.component-url';
import type { Logger } from '@teambit/logger';
import { LaneDiffGenerator } from '@teambit/lanes.modules.diff';
import type { LaneDiffResults } from '@teambit/lanes.modules.diff';
const FILES_HISTORY_DIR = 'files-history';
const ENV_ICONS_DIR = 'env-icons';
const LAST_SNAP_DIR = 'last-snap';
const CMD_HISTORY = 'command-history-ide';
type LaneDiffForIDEResult = {
newCompsFrom: string[];
newCompsTo: string[];
compsWithDiff: {
id: string;
hasDiff: boolean;
filesDiff: { filePath: string; status: string; fromContent?: string; toContent?: string }[];
fieldsDiff?: { fieldName: string; diffOutput: string }[] | null;
apiDiff?: Record<string, any> | null;
}[];
compsWithNoChanges: string[];
toLaneName: string;
fromLaneName: string;
failures: { id: string; msg: string }[];
};
type PathLinux = string; // problematic to get it from @teambit/legacy/dist/utils/path.
type PathFromLastSnap = { [relativeToWorkspace: PathLinux]: string };
type ObjectPathsFromLastSnap = { [relativeToWorkspace: PathLinux]: string };
type InitSCMEntry = {
filesStatus: FilesStatus;
pathsFromLastSnap: PathFromLastSnap;
objectPathsFromLastSnap: ObjectPathsFromLastSnap;
compDir: PathLinux;
};
type DataToInitSCM = { [compId: string]: InitSCMEntry };
type LaneObj = {
name: string;
scope: string;
id: string;
log: LaneLog;
components: Array<{ id: string; head: string }>;
isNew: boolean;
forkedFrom?: string;
};
type ModifiedByConfig = {
id: string;
version: string;
dependencies?: { workspace: string[]; scope: string[] };
aspects?: { workspace: Record<string, any>; scope: Record<string, any> };
};
type WorkspaceHistory = {
current: PathOsBasedAbsolute;
history: Array<{ path: PathOsBasedAbsolute; fileId: string; reason?: string }>;
};
type CompMetadata = {
id: string;
isDeprecated: boolean;
appName?: string; // in case it's an app
env: {
id: string;
name?: string;
icon: string;
localIconPath?: string;
};
};
export class APIForIDE {
private existingEnvIcons: string[] | undefined;
constructor(
private workspace: Workspace,
private snapping: SnappingMain,
private lanes: LanesMain,
private installer: InstallMain,
private exporter: ExportMain,
private checkout: CheckoutMain,
private componentLog: ComponentLogMain,
private componentCompare: ComponentCompareMain,
private generator: GeneratorMain,
private remove: RemoveMain,
private config: ConfigMain,
private application: ApplicationMain,
private deprecation: DeprecationMain,
private envs: EnvsMain,
private graph: GraphMain,
private scope: ScopeMain,
private component: ComponentMain,
private schema: SchemaMain,
private logger: Logger
) {}
async logStartCmdHistory(op: string) {
const str = `${op}, started`;
await this.writeToCmdHistory(str);
}
async logFinishCmdHistory(op: string, code: number) {
const endStr = code === 0 ? 'succeeded' : 'failed';
const str = `${op}, ${endStr}`;
await this.writeToCmdHistory(str);
}
getProcessPid() {
return process.pid;
}
private async writeToCmdHistory(str: string) {
await fs.appendFile(path.join(this.workspace.scope.path, CMD_HISTORY), `${new Date().toISOString()} ${str}\n`);
}
async listIdsWithPaths() {
const ids = this.workspace.listIds();
return ids.reduce((acc, id) => {
acc[id.toStringWithoutVersion()] = this.workspace.componentDir(id);
return acc;
}, {});
}
async getMainFilePath(id: string): Promise<PathOsBasedAbsolute> {
const compId = await this.workspace.resolveComponentId(id);
const comp = await this.workspace.get(compId);
return path.join(this.workspace.componentDir(compId), comp.state._consumer.mainFile);
}
async getWorkspaceHistory(): Promise<WorkspaceHistory> {
const current = this.workspace.bitMap.getPath();
const bitmapHistoryDir = this.workspace.consumer.getBitmapHistoryDir();
const historyPaths = await fs.readdir(bitmapHistoryDir);
const historyMetadata = await this.workspace.consumer.getParsedBitmapHistoryMetadata();
const history = historyPaths.map((historyPath) => {
const fileName = path.basename(historyPath);
const fileId = fileName.replace('.bitmap-', '');
const reason = historyMetadata[fileId];
return { path: path.join(bitmapHistoryDir, fileName), fileId, reason };
});
const historySorted = history.sort((a, b) => fileIdToTimestamp(b.fileId) - fileIdToTimestamp(a.fileId));
return { current, history: historySorted };
}
async getConfigHistory(): Promise<WorkspaceHistory> {
const workspaceConfig = this.config.workspaceConfig;
if (!workspaceConfig) throw new Error('getConfigHistory(), workspace config is missing');
const current = workspaceConfig.path;
const configHistoryDir = workspaceConfig.getBackupHistoryDir();
const historyPaths = await fs.readdir(configHistoryDir);
const historyMetadata = await getParsedHistoryMetadata(workspaceConfig.getBackupMetadataFilePath());
const history = historyPaths.map((historyPath) => {
const fileName = path.basename(historyPath);
const fileId = fileName;
const reason = historyMetadata[fileId];
return { path: path.join(configHistoryDir, fileName), fileId, reason };
});
const historySorted = history.sort((a, b) => fileIdToTimestamp(b.fileId) - fileIdToTimestamp(a.fileId));
return { current, history: historySorted };
}
async importLane(
laneName: string,
{ skipDependencyInstallation }: { skipDependencyInstallation?: boolean }
): Promise<string[]> {
const results = await this.lanes.switchLanes(laneName, {
skipDependencyInstallation,
});
return (results.components || []).map((c) => c.id.toString());
}
async getCurrentLaneObject(): Promise<LaneObj | undefined> {
const currentLane = await this.lanes.getCurrentLane();
if (!currentLane) return undefined;
const components = currentLane.components.map((c) => {
return {
id: c.id.toStringWithoutVersion(),
head: c.head.toString(),
};
});
return {
name: currentLane.name,
scope: currentLane.scope,
id: currentLane.id(),
log: currentLane.log,
components,
isNew: currentLane.isNew,
forkedFrom: currentLane.forkedFrom?.toString(),
};
}
async listLanes() {
return this.lanes.getLanes({ showDefaultLane: true });
}
async createLane(name: string) {
if (name.includes(LANE_REMOTE_DELIMITER)) {
const laneId = LaneId.parse(name);
return this.lanes.createLane(laneId.name, { scope: laneId.scope });
}
return this.lanes.createLane(name);
}
async getCompsMetadata(): Promise<CompMetadata[]> {
const comps = await this.workspace.list();
const apps = await this.application.listAppsIdsAndNames();
const results: CompMetadata[] = await pMap(
comps,
async (comp) => {
const id = comp.id;
const deprecationInfo = await this.deprecation.getDeprecationInfo(comp);
const foundApp = apps.find((app) => app.id === id.toString());
const env = this.envs.getEnv(comp);
return {
id: id.toStringWithoutVersion(),
isDeprecated: deprecationInfo.isDeprecate,
appName: foundApp?.name,
env: {
id: env.id,
name: env.name,
icon: env.icon,
},
};
},
{ 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);
const dirAbs = this.workspace.componentDir(comp.id);
const filesRelative = comp.state.filesystem.files.map((file) => file.relative);
return { dirAbs, filesRelative };
}
async getGraphIdsAsSVG(id?: string, opts: { includeLocalOnly?: boolean } = {}): Promise<string> {
const compId = id ? await this.workspace.resolveComponentId(id) : undefined;
const visualGraph = await this.graph.getVisualGraphIds(compId ? [compId] : undefined, opts);
const svg = await visualGraph.getAsSVGString();
if (!svg) throw new Error('failed to render the graph');
return svg;
}
async getWorkspaceDependencies(): Promise<{ [pkgName: string]: string }> {
const allDeps = await this.workspace.getAllDedupedDirectDependencies();
const allDepsObj = {};
for (const dep of allDeps) {
allDepsObj[dep.name] = dep.currentRange;
}
return allDepsObj;
}
async getCompFilesDirPathFromLastSnap(id: string): Promise<{ [relativePath: string]: string }> {
const compId = await this.workspace.resolveComponentId(id);
if (!compId.hasVersion()) return {}; // it's a new component.
const compDir = this.workspace.componentDir(compId, { ignoreVersion: true }, { relative: true });
// const dirName = filenamify(compId.toString(), { replacement: '_' });
const filePathsRootDir = path.join(this.workspace.scope.path, FILES_HISTORY_DIR, LAST_SNAP_DIR, compDir);
await fs.remove(filePathsRootDir); // in case it has old data
await fs.ensureDir(filePathsRootDir);
const modelComponent = await this.workspace.scope.getBitObjectModelComponent(compId);
if (!modelComponent) {
throw new Error(`unable to find ${compId.toString()} in the local scope, please run "bit import"`);
}
const versionObject = await this.workspace.scope.getBitObjectVersion(modelComponent, compId.version as string);
if (!versionObject)
throw new Error(`unable to find the Version object of ${compId.toString()}, please run "bit import"`);
const sourceFiles = await versionObject.modelFilesToSourceFiles(this.workspace.scope.legacyScope.objects);
const results: { [relativePath: string]: string } = {};
await Promise.all(
sourceFiles.map(async (file) => {
const filePath = path.join(filePathsRootDir, file.relative);
await fs.outputFile(filePath, file.contents);
results[pathJoinLinux(compDir, file.relative)] = filePath;
})
);
return results;
}
async catObject(hash: string) {
const object = await this.workspace.scope.legacyScope.getRawObject(hash);
return JSON.stringify(object.content.toString());
}
async logFile(filePath: string) {
const results = await this.componentLog.getFileHistoryHashes(filePath);
return results;
}
async blame(filePath: string) {
const results = await this.componentLog.blame(filePath);
return results;
}
async changedFilesFromParent(id: string): Promise<FileHashDiffFromParent[]> {
const results = await this.componentLog.getChangedFilesFromParent(id);
return results;
}
async getConfigForDiff(id: string) {
const results = await this.componentCompare.getConfigForDiffById(id);
return results;
}
async setDefaultScope(scopeName: string) {
await this.workspace.setDefaultScope(scopeName);
return scopeName;
}
getDefaultScope() {
return this.workspace.defaultScope;
}
async getCompFilesDirPathFromLastSnapUsingCompFiles(
compFiles: CompFiles
): Promise<{ [relativePath: string]: string }> {
const compId = compFiles.id;
if (!compId.hasVersion()) return {}; // it's a new component.
const compDir = compFiles.compDir;
const filePathsRootDir = path.join(this.workspace.scope.path, FILES_HISTORY_DIR, LAST_SNAP_DIR, compDir);
await fs.remove(filePathsRootDir); // in case it has old data
const sourceFiles = await compFiles.getHeadFiles();
const results: { [relativePath: string]: string } = {};
await Promise.all(
sourceFiles.map(async (file) => {
const filePath = path.join(filePathsRootDir, file.relative);
await fs.outputFile(filePath, file.contents);
results[pathJoinLinux(compDir, file.relative)] = filePath;
})
);
return results;
}
getCompFileObjectPathsFromLastSnap(compFiles: CompFiles): { [relativePath: string]: string } {
if (!compFiles.id.hasVersion()) return {}; // it's a new component.
const repo = this.workspace.scope.legacyScope.objects;
const results: { [relativePath: string]: string } = {};
for (const modelFile of compFiles.modelFiles) {
results[pathJoinLinux(compFiles.compDir, modelFile.relativePath)] = repo.objectPath(modelFile.file);
}
return results;
}
async warmWorkspaceCache() {
await this.workspace.warmCache();
}
async clearCache() {
await this.workspace.clearCache();
this.workspace.clearAllComponentsCache();
}
async install(options = {}, packages?: string[]): Promise<ComponentMap<string>> {
const opts = {
optimizeReportForNonTerminal: true,
dedupe: true,
updateExisting: false,
import: false,
...options,
};
return this.installer.install(packages, opts);
}
async export() {
const { componentsIds, removedIds, exportedLanes, rippleJobUrls } = await this.exporter.export();
return {
componentsIds: componentsIds.map((c) => c.toString()),
removedIds: removedIds.map((c) => c.toString()),
exportedLanes: exportedLanes.map((l) => l.id()),
rippleJobs: rippleJobUrls, // for backward compatibility. bit-extension until 1.1.52 expects rippleJobs.
rippleJobUrls,
};
}
async checkoutHead() {
const results = await this.checkout.checkout({
head: true,
skipNpmInstall: true,
ids: await this.workspace.listIds(),
});
return this.adjustCheckoutResultsToIde(results);
}
async importObjectsIfOutdatedAgainstBitmap(): Promise<void> {
return this.workspace.importObjectsIfOutdatedAgainstBitmap();
}
async getTemplates(): Promise<TemplateDescriptor[]> {
const templates = await this.generator.listTemplates();
return templates;
}
async getPromptOptionsForTemplate(templateName: string): Promise<PromptOption[] | undefined> {
const template = await this.generator.getTemplateWithId(templateName);
return template.template.promptOptions?.();
}
async createComponent(
templateName: string,
idIncludeScope: string,
promptResults?: PromptResults
): Promise<GenerateResult[]> {
if (!idIncludeScope.includes('/')) {
throw new Error('id should include the scope name');
}
const [scope, ...nameSplit] = idIncludeScope.split('/');
return this.generator.generateComponentTemplate(
[nameSplit.join('/')],
templateName,
{ scope },
{ optimizeReportForNonTerminal: true },
promptResults
);
}
async removeComponent(componentsPattern: string) {
const results = await this.remove.remove({
componentsPattern,
force: true,
});
const serializedResults = (results.localResult as RemovedObjects).serialize();
return serializedResults;
}
async deleteComponents(componentsPattern: string, opts): Promise<string[]> {
const results = await this.remove.deleteComps(componentsPattern, opts);
const serializedResults = results.map((c) => c.id.toString());
return serializedResults;
}
async deprecateComponent(id: string, newId?: string, range?: string): Promise<boolean> {
return this.deprecation.deprecateByCLIValues(id, newId, range);
}
async undeprecateComponent(id: string): Promise<boolean> {
return this.deprecation.unDeprecateByCLIValues(id);
}
async switchLane(name: string) {
const results = await this.lanes.switchLanes(name, { skipDependencyInstallation: true });
return this.adjustCheckoutResultsToIde(results);
}
private adjustCheckoutResultsToIde(output: ApplyVersionResults) {
const { components, failedComponents } = output;
const skipped = failedComponents?.filter((f) => f.unchangedLegitimately).map((f) => f.id.toString());
const failed = failedComponents?.filter((f) => !f.unchangedLegitimately).map((f) => f.id.toString());
return {
succeed: components?.map((c) => c.id.toString()),
skipped,
failed,
};
}
async getModifiedByConfig(): Promise<ModifiedByConfig[]> {
const modifiedComps = await this.workspace.modified();
const autoTagIds = await this.workspace.listAutoTagPendingComponentIds();
const autoTagComps = await this.workspace.getMany(autoTagIds);
const locallyDeletedIds = await this.workspace.locallyDeletedIds();
const locallyDeletedComps = await this.workspace.getMany(locallyDeletedIds);
const allComps = [...modifiedComps, ...autoTagComps, ...locallyDeletedComps];
const allIds = allComps.map((c) => c.id);
const results = await Promise.all(
allComps.map(async (comp) => {
const wsComp = await this.componentCompare.getConfigForDiffByCompObject(comp, allIds);
const scopeComp = await this.componentCompare.getConfigForDiffById(comp.id.toString());
const hasSameDeps = JSON.stringify(wsComp.dependencies) === JSON.stringify(scopeComp.dependencies);
const hasSameAspects = JSON.stringify(wsComp.aspects) === JSON.stringify(scopeComp.aspects);
if (hasSameDeps && hasSameAspects) return null;
const result: ModifiedByConfig = {
id: comp.id.toStringWithoutVersion(),
version: comp.id.version as string,
};
if (!hasSameDeps) result.dependencies = { workspace: wsComp.dependencies, scope: scopeComp.dependencies || [] };
if (!hasSameAspects) result.aspects = { workspace: wsComp.aspects, scope: scopeComp.aspects || {} };
return result;
})
);
return compact(results);
}
async getDataToInitSCM(options?: { useHashes?: boolean }): Promise<DataToInitSCM> {
const useHashes = options?.useHashes;
if (useHashes) {
// clean up old materialized files since hash-based reads don't need them
const lastSnapDir = path.join(this.workspace.scope.path, FILES_HISTORY_DIR, LAST_SNAP_DIR);
await fs.remove(lastSnapDir);
}
const ids = this.workspace.listIds();
const results: DataToInitSCM = {};
await pMap(
ids,
async (id) => {
const compFiles = await this.workspace.getFilesModification(id);
// only compute object paths when the extension supports direct object reads, otherwise materialize files to disk
const objectPathsFromLastSnap = useHashes ? this.getCompFileObjectPathsFromLastSnap(compFiles) : {};
const pathsFromLastSnap = useHashes ? {} : await this.getCompFilesDirPathFromLastSnapUsingCompFiles(compFiles);
const idStr = id.toStringWithoutVersion();
results[idStr] = {
filesStatus: compFiles.getFilesStatus(),
pathsFromLastSnap,
objectPathsFromLastSnap,
compDir: compFiles.compDir,
};
},
{ concurrency: 30 }
);
return results;
}
async getFilesStatus(id: string): Promise<FilesStatus> {
const componentId = await this.workspace.resolveComponentId(id);
const compFiles = await this.workspace.getFilesModification(componentId);
return compFiles.getFilesStatus();
}
async getCompFilesDirPathFromLastSnapForAllComps(): Promise<{ [relativePath: string]: string }> {
const ids = this.workspace.listIds();
let results = {};
await pMap(
ids,
async (id) => {
const idStr = id.toStringWithoutVersion();
const compResults = await this.getCompFilesDirPathFromLastSnap(idStr);
results = { ...results, ...compResults };
},
{ concurrency: 30 }
);
return results;
}
async getLaneHistoryForIDE(laneName?: string) {
const laneId = laneName ? await this.lanes.parseLaneId(laneName) : this.workspace.getCurrentLaneId();
if (laneId.isDefault()) {
return { entries: [], isMain: true };
}
await this.lanes.importLaneHistory(laneId);
const laneHistory = await this.lanes.getLaneHistory(laneId);
const historyIds = laneHistory.getHistoryIds();
const history = laneHistory.getHistory();
const entries = historyIds.map((id) => {
const item = history[id];
return {
id,
date: item.log.date,
username: item.log.username,
email: item.log.email,
message: item.log.message,
components: item.components,
deleted: item.deleted,
};
});
return { entries, isMain: false };
}
async getLaneHistoryDiffForIDE(
fromHistoryId: string,
toHistoryId: string,
laneName?: string
): Promise<LaneDiffForIDEResult> {
const laneId = laneName ? await this.lanes.parseLaneId(laneName) : this.workspace.getCurrentLaneId();
if (laneId.isDefault()) {
throw new Error('lane history diff is not available on main');
}
await this.lanes.importLaneHistory(laneId);
const laneHistory = await this.lanes.getLaneHistory(laneId);
const laneObj = await this.lanes.loadLane(laneId);
if (!laneObj) throw new Error(`unable to find lane "${laneId.toString()}"`);
const diffGenerator = new LaneDiffGenerator(this.workspace, this.scope, this.componentCompare, this.schema);
const diffResults = await diffGenerator.generateDiffHistory(laneObj, laneHistory, fromHistoryId, toHistoryId);
return this.toLaneDiffForIDEResult(diffResults);
}
async getLaneDiffForIDE(): Promise<LaneDiffForIDEResult> {
const currentLaneId = this.workspace.getCurrentLaneId();
if (currentLaneId.isDefault()) {
throw new Error('lane diff is not available on main');
}
const diffGenerator = new LaneDiffGenerator(this.workspace, this.scope, this.componentCompare, this.schema);
const diffResults = await diffGenerator.generate([]);
return this.toLaneDiffForIDEResult(diffResults);
}
private toLaneDiffForIDEResult(diffResults: LaneDiffResults): LaneDiffForIDEResult {
return {
newCompsFrom: diffResults.newCompsFrom,
newCompsTo: diffResults.newCompsTo,
compsWithDiff: diffResults.compsWithDiff.map((d) => ({
id: d.id.toString(),
hasDiff: d.hasDiff,
filesDiff: (d.filesDiff || []).map((f) => ({
filePath: f.filePath,
status: f.status,
fromContent: f.status === 'UNCHANGED' ? undefined : f.fromContent,
toContent: f.status === 'UNCHANGED' ? undefined : f.toContent,
})),
fieldsDiff: d.fieldsDiff,
apiDiff: d.apiDiff ?? null,
})),
compsWithNoChanges: diffResults.compsWithNoChanges,
toLaneName: diffResults.toLaneName,
fromLaneName: diffResults.fromLaneName,
failures: diffResults.failures.map((f) => ({
id: f.id.toString(),
msg: f.msg,
})),
};
}
getCurrentLaneName(includeScope = false): string {
const currentLaneId = this.workspace.getCurrentLaneId();
if (!includeScope) return currentLaneId.name;
if (currentLaneId.isDefault()) return currentLaneId.name;
return currentLaneId.toString();
}
async tagOrSnap(message = '') {
const params = { message, build: false };
return this.workspace.isOnMain() ? this.snapping.tag(params) : this.snapping.snap(params);
}
async tag(message = ''): Promise<string[]> {
const params = { message, build: false };
const results = await this.snapping.tag(params);
return (results?.taggedComponents || []).map((c) => c.id.toString());
}
async snap(message = ''): Promise<string[]> {
const params = { message, build: false };
const results = await this.snapping.snap(params);
return (results?.snappedComponents || []).map((c) => c.id.toString());
}
async getCompDetails(id: string, includeSchema = false) {
this.logger.debug(`getCompDetails(${id}, ${includeSchema})`);
const compId = await this.workspace.resolveComponentId(id);
const existsLocally = this.workspace.hasId(compId);
const getComp = async () => {
if (existsLocally) {
return this.workspace.get(compId);
}
const comp = await this.scope.get(compId);
if (comp) return comp;
throw new ComponentNotFound(compId);
};
const comp = await getComp();
const fragments = this.component.getShowFragments();
const titlesToInclude = ['id', 'env', 'package name', 'files', 'dev files', 'deprecated'];
const showResults: Record<string, any> = {};
await Promise.all(
fragments.map(async (fragment) => {
const result = fragment.json ? await fragment.json(comp) : undefined;
if (!result || !titlesToInclude.includes(result.title)) return;
if (result.title === 'deprecated' && !result.json.isDeprecate) return; // skip if not deprecated
showResults[result.title] = result.json;
})
);
const deps = comp.getDependencies();
showResults.dependencies = deps.map((dep) => {
const pkg = dep.getPackageName?.() || dep.id;
const pkgWithVer = `${pkg}@${dep.version}`;
const compIdStr = dep.type === 'component' ? `, component-id: ${dep.id}` : '';
return `${pkgWithVer} (lifecycle: ${dep.lifecycle}, type: ${dep.type}, source: ${dep.source}${compIdStr})`;
});
if (includeSchema) {
try {
const schema = existsLocally
? await this.schema.getSchema(comp)
: await this.schema.getSchemaFromRemote(comp.id.toString());
showResults.publicAPI = schema.toStringPerType();
} catch (error) {
// If schema fails, add error info instead of crashing
showResults.publicAPI = `Error fetching schema: ${(error as Error).message}`;
}
}
// Add docs content if available from teambit.docs/docs aspect
try {
const docsAspectFiles = showResults['dev files']?.[`teambit.docs/docs`];
if (docsAspectFiles && Array.isArray(docsAspectFiles) && docsAspectFiles.length > 0) {
showResults.docs = {};
docsAspectFiles.forEach((relativePath: string) => {
const file = comp.filesystem.files.find((f) => f.relative === relativePath);
if (file) {
showResults.docs[relativePath] = file.contents.toString();
}
});
}
} catch (error) {
// If docs extraction fails, add error info but don't crash
showResults.docs = `Error fetching docs: ${(error as Error).message}`;
}
// Add usage examples from teambit.compositions/compositions aspect
try {
const compositionsAspectFiles = showResults['dev files']?.[`teambit.compositions/compositions`];
if (compositionsAspectFiles && Array.isArray(compositionsAspectFiles) && compositionsAspectFiles.length > 0) {
showResults.usageExamples = {};
compositionsAspectFiles.forEach((relativePath: string) => {
const file = comp.filesystem.files.find((f) => f.relative === relativePath);
if (file) {
const fileContent = file.contents.toString();
showResults.usageExamples[relativePath] = fileContent;
}
});
}
} catch (error) {
// If composition extraction fails, add error info but don't crash
showResults.usageExamples = `Error fetching usage examples: ${(error as Error).message}`;
}
showResults.url = ComponentUrl.toUrl(compId, { includeVersion: false });
// Add component location status
// Check if component exists as a package by looking it up in the pnpm lock file
const packageName = comp.getPackageName();
let componentLocation: string;
if (existsLocally) {
const compDir = this.workspace.componentDir(compId, { ignoreVersion: true }, { relative: true });
componentLocation = `exists locally in the workspace at '${compDir}'`;
} else {
let isInstalledAsPackage = false;
try {
const lockFilePath = path.join(this.workspace.path, 'node_modules', '.pnpm', 'lock.yaml');
if (await fs.pathExists(lockFilePath)) {
const lockFileContent = await fs.readFile(lockFilePath, 'utf8');
// Check if the package name exists in the lock file
isInstalledAsPackage = lockFileContent.includes(packageName);
}
} catch (error) {
this.logger.warn(`Error checking package existence for ${packageName}: ${(error as Error).message}`);
// If error occurs during package check, default to false
isInstalledAsPackage = false;
}
componentLocation = isInstalledAsPackage ? 'installed as a package' : 'a remote component';
}
showResults.componentLocation = componentLocation;
return showResults;
}
}
function fileIdToTimestamp(dateStr: string): number {
const [year, month, day, hours, minutes, seconds] = dateStr.split('-');
const date = new Date(Number(year), Number(month) - 1, Number(day), Number(hours), Number(minutes), Number(seconds));
return date.getTime();
}