@teambit/workspace 1.0.108 → 1.0.109
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/artifacts/preview/static/css/teambit.workspace/workspace-preview.115618a8.css +55 -0
- package/artifacts/preview/static/images/2f046646df38433e90d7.png +0 -0
- package/artifacts/preview/static/images/b40d450de296e275550f.png +0 -0
- package/artifacts/preview/static/images/cb67fb802479e2710016.png +0 -0
- package/artifacts/preview/teambit_workspace_workspace-preview.js +2 -0
- package/dist/{preview-1703647408454.js → preview-1703698405864.js} +2 -2
- package/package.json +27 -27
- package/aspects-merger.ts +0 -319
- package/bit-map.ts +0 -277
- package/build-graph-from-fs.ts +0 -188
- package/build-graph-ids-from-fs.ts +0 -216
- package/capsule.cmd.ts +0 -217
- package/constants.ts +0 -1
- package/eject-conf.cmd.ts +0 -57
- package/filter.ts +0 -139
- package/index.ts +0 -22
- package/merge-conflict-file.ts +0 -129
- package/on-component-events.ts +0 -24
- package/pattern.cmd.ts +0 -53
- package/types.ts +0 -71
- package/use.cmd.ts +0 -25
- package/workspace-aspects-loader.ts +0 -885
- package/workspace-section.ts +0 -14
- package/workspace.aspect.ts +0 -9
- package/workspace.graphql.ts +0 -133
- package/workspace.main.runtime.ts +0 -56
- package/workspace.provider.ts +0 -282
- package/workspace.ts +0 -2014
- package/workspace.ui-root.ts +0 -65
package/bit-map.ts
DELETED
|
@@ -1,277 +0,0 @@
|
|
|
1
|
-
import { isEqual, merge } from 'lodash';
|
|
2
|
-
import { ComponentID } from '@teambit/component-id';
|
|
3
|
-
import LegacyBitMap from '@teambit/legacy/dist/consumer/bit-map';
|
|
4
|
-
import { Consumer } from '@teambit/legacy/dist/consumer';
|
|
5
|
-
import { GetBitMapComponentOptions } from '@teambit/legacy/dist/consumer/bit-map/bit-map';
|
|
6
|
-
import ComponentMap from '@teambit/legacy/dist/consumer/bit-map/component-map';
|
|
7
|
-
import { REMOVE_EXTENSION_SPECIAL_SIGN } from '@teambit/legacy/dist/consumer/config';
|
|
8
|
-
import { BitError } from '@teambit/bit-error';
|
|
9
|
-
import { LaneId } from '@teambit/lane-id';
|
|
10
|
-
import EnvsAspect from '@teambit/envs';
|
|
11
|
-
import { getPathStatIfExist } from '@teambit/legacy/dist/utils/fs/last-modified';
|
|
12
|
-
import { PathOsBasedAbsolute } from '@teambit/legacy/dist/utils/path';
|
|
13
|
-
|
|
14
|
-
export type MergeOptions = {
|
|
15
|
-
mergeStrategy?: 'theirs' | 'ours' | 'manual';
|
|
16
|
-
};
|
|
17
|
-
/**
|
|
18
|
-
* consider extracting to a new component.
|
|
19
|
-
* (pro: making Workspace aspect smaller. con: it's an implementation details of the workspace)
|
|
20
|
-
*/
|
|
21
|
-
export class BitMap {
|
|
22
|
-
constructor(private legacyBitMap: LegacyBitMap, private consumer: Consumer) {}
|
|
23
|
-
|
|
24
|
-
mergeBitmaps(bitmapContent: string, otherBitmapContent: string, opts: MergeOptions = {}): string {
|
|
25
|
-
return LegacyBitMap.mergeContent(bitmapContent, otherBitmapContent, opts);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
getPath(): PathOsBasedAbsolute {
|
|
29
|
-
return this.legacyBitMap.mapPath;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* adds component config to the .bitmap file.
|
|
34
|
-
* later, upon `bit tag`, the data is saved in the scope.
|
|
35
|
-
* returns a boolean indicating whether a change has been made.
|
|
36
|
-
*/
|
|
37
|
-
addComponentConfig(
|
|
38
|
-
id: ComponentID,
|
|
39
|
-
aspectId: string,
|
|
40
|
-
config: Record<string, any> = {},
|
|
41
|
-
shouldMergeConfig = false
|
|
42
|
-
): boolean {
|
|
43
|
-
if (!aspectId || typeof aspectId !== 'string') throw new Error(`expect aspectId to be string, got ${aspectId}`);
|
|
44
|
-
const bitMapEntry = this.getBitmapEntry(id, { ignoreVersion: true });
|
|
45
|
-
const currentConfig = (bitMapEntry.config ||= {})[aspectId];
|
|
46
|
-
if (isEqual(currentConfig, config)) {
|
|
47
|
-
return false; // no changes
|
|
48
|
-
}
|
|
49
|
-
const getNewConfig = () => {
|
|
50
|
-
if (!config) return null;
|
|
51
|
-
if (!shouldMergeConfig) return config;
|
|
52
|
-
// should merge
|
|
53
|
-
if (!currentConfig) return config;
|
|
54
|
-
if (currentConfig === '-') return config;
|
|
55
|
-
// lodash merge performs a deep merge. (the native concatenation don't)
|
|
56
|
-
return merge(currentConfig, config);
|
|
57
|
-
};
|
|
58
|
-
const newConfig = getNewConfig();
|
|
59
|
-
if (newConfig) {
|
|
60
|
-
bitMapEntry.config[aspectId] = newConfig;
|
|
61
|
-
} else {
|
|
62
|
-
delete bitMapEntry.config[aspectId];
|
|
63
|
-
}
|
|
64
|
-
this.legacyBitMap.markAsChanged();
|
|
65
|
-
|
|
66
|
-
return true; // changes have been made
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
updateDefaultScope(oldScope: string, newScope: string) {
|
|
70
|
-
const changedId: ComponentID[] = [];
|
|
71
|
-
this.legacyBitMap.components.forEach((componentMap) => {
|
|
72
|
-
// only new components (not snapped/tagged) can be changed
|
|
73
|
-
if (componentMap.defaultScope === oldScope && !componentMap.id.hasVersion()) {
|
|
74
|
-
componentMap.defaultScope = newScope;
|
|
75
|
-
changedId.push(componentMap.id);
|
|
76
|
-
}
|
|
77
|
-
});
|
|
78
|
-
if (changedId.length) {
|
|
79
|
-
this.legacyBitMap.markAsChanged();
|
|
80
|
-
}
|
|
81
|
-
return changedId;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
markAsChanged() {
|
|
85
|
-
this.legacyBitMap.markAsChanged();
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
removeComponentConfig(id: ComponentID, aspectId: string, markWithMinusIfNotExist: boolean): boolean {
|
|
89
|
-
if (!aspectId || typeof aspectId !== 'string') throw new Error(`expect aspectId to be string, got ${aspectId}`);
|
|
90
|
-
const bitMapEntry = this.getBitmapEntry(id, { ignoreVersion: true });
|
|
91
|
-
const currentConfig = (bitMapEntry.config ||= {})[aspectId];
|
|
92
|
-
if (currentConfig) {
|
|
93
|
-
delete bitMapEntry.config[aspectId];
|
|
94
|
-
} else {
|
|
95
|
-
if (!markWithMinusIfNotExist) {
|
|
96
|
-
return false; // no changes
|
|
97
|
-
}
|
|
98
|
-
bitMapEntry.config[aspectId] = REMOVE_EXTENSION_SPECIAL_SIGN;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
this.legacyBitMap.markAsChanged();
|
|
102
|
-
|
|
103
|
-
return true; // changes have been made
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
removeEntireConfig(id: ComponentID): boolean {
|
|
107
|
-
const bitMapEntry = this.getBitmapEntry(id, { ignoreVersion: true });
|
|
108
|
-
if (!bitMapEntry.config) return false;
|
|
109
|
-
delete bitMapEntry.config;
|
|
110
|
-
this.legacyBitMap.markAsChanged();
|
|
111
|
-
return true;
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
setEntireConfig(id: ComponentID, config: Record<string, any>) {
|
|
115
|
-
const bitMapEntry = this.getBitmapEntry(id, { ignoreVersion: true });
|
|
116
|
-
bitMapEntry.config = config;
|
|
117
|
-
this.legacyBitMap.markAsChanged();
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
removeDefaultScope(id: ComponentID) {
|
|
121
|
-
const bitMapEntry = this.getBitmapEntry(id, { ignoreVersion: true });
|
|
122
|
-
if (bitMapEntry.defaultScope) {
|
|
123
|
-
delete bitMapEntry.defaultScope;
|
|
124
|
-
this.legacyBitMap.markAsChanged();
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
setDefaultScope(id: ComponentID, defaultScope: string) {
|
|
129
|
-
const bitMapEntry = this.getBitmapEntry(id, { ignoreVersion: true });
|
|
130
|
-
bitMapEntry.defaultScope = defaultScope;
|
|
131
|
-
bitMapEntry.id = bitMapEntry.id.changeDefaultScope(defaultScope);
|
|
132
|
-
this.legacyBitMap.markAsChanged();
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
/**
|
|
136
|
-
* write .bitmap object to the filesystem
|
|
137
|
-
* optionally pass a reason for the change to be saved in the local scope `bitmap-history-metadata.txt` file.
|
|
138
|
-
*/
|
|
139
|
-
async write(reasonForChange?: string) {
|
|
140
|
-
await this.consumer.writeBitMap(reasonForChange);
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
/**
|
|
144
|
-
* get the data saved in the .bitmap file for this component-id.
|
|
145
|
-
* throws if not found
|
|
146
|
-
* @see this.getBitmapEntryIfExist
|
|
147
|
-
*/
|
|
148
|
-
getBitmapEntry(id: ComponentID, { ignoreVersion }: GetBitMapComponentOptions = {}): ComponentMap {
|
|
149
|
-
return this.legacyBitMap.getComponent(id, { ignoreVersion });
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
getBitmapEntryIfExist(id: ComponentID, { ignoreVersion }: GetBitMapComponentOptions = {}): ComponentMap | undefined {
|
|
153
|
-
return this.legacyBitMap.getComponentIfExist(id, { ignoreVersion });
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
getAspectIdFromConfig(
|
|
157
|
-
componentId: ComponentID,
|
|
158
|
-
aspectId: ComponentID,
|
|
159
|
-
ignoreAspectVersion = false
|
|
160
|
-
): string | undefined {
|
|
161
|
-
const bitMapEntry = this.getBitmapEntry(componentId);
|
|
162
|
-
const config = bitMapEntry.config;
|
|
163
|
-
if (!config) {
|
|
164
|
-
return undefined;
|
|
165
|
-
}
|
|
166
|
-
if (config[aspectId.toString()]) {
|
|
167
|
-
return aspectId.toString();
|
|
168
|
-
}
|
|
169
|
-
if (!ignoreAspectVersion) {
|
|
170
|
-
return undefined;
|
|
171
|
-
}
|
|
172
|
-
const allVersions = Object.keys(config).filter((id) => id.startsWith(`${aspectId.toStringWithoutVersion()}@`));
|
|
173
|
-
if (allVersions.length > 1) {
|
|
174
|
-
throw new BitError(
|
|
175
|
-
`error: the same aspect ${
|
|
176
|
-
aspectId.toStringWithoutVersion
|
|
177
|
-
} configured multiple times for "${componentId.toString()}"\n${allVersions.join('\n')}`
|
|
178
|
-
);
|
|
179
|
-
}
|
|
180
|
-
return allVersions.length === 1 ? allVersions[0] : undefined;
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
/**
|
|
184
|
-
* components that were not tagged yet are safe to rename them from the .bitmap file.
|
|
185
|
-
*/
|
|
186
|
-
renameNewComponent(sourceId: ComponentID, targetId: ComponentID) {
|
|
187
|
-
const bitMapEntry = this.getBitmapEntry(sourceId);
|
|
188
|
-
if (bitMapEntry.id.hasVersion()) {
|
|
189
|
-
throw new Error(`unable to rename tagged or exported component: ${bitMapEntry.id.toString()}`);
|
|
190
|
-
}
|
|
191
|
-
if (sourceId.isEqual(targetId)) {
|
|
192
|
-
throw new Error(`source-id and target-id are equal: "${sourceId.toString()}"`);
|
|
193
|
-
}
|
|
194
|
-
if (sourceId.fullName !== targetId.fullName) {
|
|
195
|
-
this.legacyBitMap.removeComponent(bitMapEntry.id);
|
|
196
|
-
bitMapEntry.id = targetId;
|
|
197
|
-
this.legacyBitMap.setComponent(bitMapEntry.id, bitMapEntry);
|
|
198
|
-
}
|
|
199
|
-
if (sourceId.scope !== targetId.scope) {
|
|
200
|
-
this.setDefaultScope(sourceId, targetId.scope);
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
/**
|
|
205
|
-
* helpful when reaming an aspect and this aspect is used in the config of other components.
|
|
206
|
-
*/
|
|
207
|
-
renameAspectInConfig(sourceId: ComponentID, targetId: ComponentID) {
|
|
208
|
-
this.legacyBitMap.components.forEach((componentMap) => {
|
|
209
|
-
const config = componentMap.config;
|
|
210
|
-
if (!config) return;
|
|
211
|
-
Object.keys(config).forEach((aspectId) => {
|
|
212
|
-
if (aspectId === sourceId.toString()) {
|
|
213
|
-
config[targetId.toString()] = config[aspectId];
|
|
214
|
-
delete config[aspectId];
|
|
215
|
-
this.markAsChanged();
|
|
216
|
-
}
|
|
217
|
-
if (aspectId === EnvsAspect.id) {
|
|
218
|
-
const envConfig = config[aspectId];
|
|
219
|
-
if (envConfig !== REMOVE_EXTENSION_SPECIAL_SIGN && envConfig.env === sourceId.toString()) {
|
|
220
|
-
envConfig.env = targetId.toString();
|
|
221
|
-
this.markAsChanged();
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
});
|
|
225
|
-
componentMap.config = config;
|
|
226
|
-
});
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
removeComponent(id: ComponentID) {
|
|
230
|
-
this.legacyBitMap.removeComponent(id);
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
/**
|
|
234
|
-
* this is the lane-id of the recently exported lane. in case of a new lane, which was not exported yet, this will be
|
|
235
|
-
* empty.
|
|
236
|
-
*/
|
|
237
|
-
getExportedLaneId(): LaneId | undefined {
|
|
238
|
-
return this.legacyBitMap.isLaneExported ? this.legacyBitMap.laneId : undefined;
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
makeComponentsAvailableOnMain(ids: ComponentID[]) {
|
|
242
|
-
ids.forEach((id) => {
|
|
243
|
-
const componentMap = this.getBitmapEntry(id);
|
|
244
|
-
componentMap.isAvailableOnCurrentLane = true;
|
|
245
|
-
delete componentMap.onLanesOnly;
|
|
246
|
-
});
|
|
247
|
-
this.legacyBitMap.markAsChanged();
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
/**
|
|
251
|
-
* whether .bitmap file has changed in-memory
|
|
252
|
-
*/
|
|
253
|
-
hasChanged(): boolean {
|
|
254
|
-
return this.legacyBitMap.hasChanged;
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
takeSnapshot(): ComponentMap[] {
|
|
258
|
-
return this.legacyBitMap.components.map((comp) => comp.clone());
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
restoreFromSnapshot(componentMaps: ComponentMap[]) {
|
|
262
|
-
this.legacyBitMap.components = componentMaps;
|
|
263
|
-
this.legacyBitMap._invalidateCache();
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
/**
|
|
267
|
-
* .bitmap file could be changed by other sources (e.g. manually or by "git pull") not only by bit.
|
|
268
|
-
* this method returns the timestamp when the .bitmap has changed through bit. (e.g. as part of snap/tag/export/merge
|
|
269
|
-
* process)
|
|
270
|
-
*/
|
|
271
|
-
async getLastModifiedBitmapThroughBit(): Promise<number | undefined> {
|
|
272
|
-
const bitmapHistoryDir = this.consumer.getBitmapHistoryDir();
|
|
273
|
-
const stat = await getPathStatIfExist(bitmapHistoryDir);
|
|
274
|
-
if (!stat) return undefined;
|
|
275
|
-
return stat.mtimeMs;
|
|
276
|
-
}
|
|
277
|
-
}
|
package/build-graph-from-fs.ts
DELETED
|
@@ -1,188 +0,0 @@
|
|
|
1
|
-
import mapSeries from 'p-map-series';
|
|
2
|
-
import { Graph, Node, Edge } from '@teambit/graph.cleargraph';
|
|
3
|
-
import { flatten } from 'lodash';
|
|
4
|
-
import { Consumer } from '@teambit/legacy/dist/consumer';
|
|
5
|
-
import { Component, ComponentID } from '@teambit/component';
|
|
6
|
-
import { DependencyResolverMain } from '@teambit/dependency-resolver';
|
|
7
|
-
import { ComponentIdList } from '@teambit/component-id';
|
|
8
|
-
import { Lane } from '@teambit/legacy/dist/scope/models';
|
|
9
|
-
import { ComponentNotFound, ScopeNotFound } from '@teambit/legacy/dist/scope/exceptions';
|
|
10
|
-
import { ComponentNotFound as ComponentNotFoundInScope } from '@teambit/scope';
|
|
11
|
-
import compact from 'lodash.compact';
|
|
12
|
-
import { Logger } from '@teambit/logger';
|
|
13
|
-
import { BitError } from '@teambit/bit-error';
|
|
14
|
-
import { Workspace } from './workspace';
|
|
15
|
-
|
|
16
|
-
export type ShouldLoadFunc = (id: ComponentID) => Promise<boolean>;
|
|
17
|
-
|
|
18
|
-
export class GraphFromFsBuilder {
|
|
19
|
-
private graph = new Graph<Component, string>();
|
|
20
|
-
private completed: string[] = [];
|
|
21
|
-
private depth = 1;
|
|
22
|
-
private consumer: Consumer;
|
|
23
|
-
private importedIds: string[] = [];
|
|
24
|
-
private currentLane: Lane | null;
|
|
25
|
-
constructor(
|
|
26
|
-
private workspace: Workspace,
|
|
27
|
-
private logger: Logger,
|
|
28
|
-
private dependencyResolver: DependencyResolverMain,
|
|
29
|
-
private ignoreIds: string[] = [],
|
|
30
|
-
private shouldLoadItsDeps?: ShouldLoadFunc,
|
|
31
|
-
private shouldThrowOnMissingDep = true
|
|
32
|
-
) {
|
|
33
|
-
this.consumer = this.workspace.consumer;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* create a graph with all dependencies and flattened dependencies of the given components.
|
|
38
|
-
* the nodes are components and the edges has a label of the dependency type.
|
|
39
|
-
*
|
|
40
|
-
* the way how it is done is iterations by depths. each depth we gather all the dependencies of
|
|
41
|
-
* that depths, make sure all objects exist and then check their dependencies for the next depth.
|
|
42
|
-
* once there is no dependency left, we're on the last depth level and the graph is ready.
|
|
43
|
-
*
|
|
44
|
-
* for example, imagine the following graph:
|
|
45
|
-
* A1 -> A2 -> A3
|
|
46
|
-
* B1 -> B2 -> B3
|
|
47
|
-
* C1 -> C2 -> C3
|
|
48
|
-
*
|
|
49
|
-
* where the buildGraph is given [A1, B1, C1].
|
|
50
|
-
* first, it saves all these components as nodes in the graph. then, it finds the dependencies of
|
|
51
|
-
* the next level, in this case they're [A2, B2, C2]. it runs `importMany` in case some objects
|
|
52
|
-
* are missing. then, it loads them all (some from FS, some from the model) and sets the edges
|
|
53
|
-
* between the component and the dependencies.
|
|
54
|
-
* once done, it finds all their dependencies, which are [A3, B3, C3] and repeat the process
|
|
55
|
-
* above. since there are no more dependencies, the graph is completed.
|
|
56
|
-
* in this case, the total depth levels are 3.
|
|
57
|
-
*
|
|
58
|
-
* even with a huge project, there are not many depth levels. by iterating through depth levels
|
|
59
|
-
* we keep performance sane as the importMany doesn't run multiple time and therefore the round
|
|
60
|
-
* trips to the remotes are minimal.
|
|
61
|
-
*
|
|
62
|
-
* normally, one importMany of the seeders is enough as importMany knows to fetch all flattened.
|
|
63
|
-
* however, since this buildGraph is performed on the workspace, a dependency may be new or
|
|
64
|
-
* modified and as such, we don't know its flattened yet.
|
|
65
|
-
*/
|
|
66
|
-
async buildGraph(ids: ComponentID[]): Promise<Graph<Component, string>> {
|
|
67
|
-
this.logger.debug(`GraphFromFsBuilder, buildGraph with ${ids.length} seeders`);
|
|
68
|
-
const start = Date.now();
|
|
69
|
-
const components = await this.loadManyComponents(ids);
|
|
70
|
-
this.currentLane = await this.workspace.consumer.getCurrentLaneObject();
|
|
71
|
-
await this.processManyComponents(components);
|
|
72
|
-
this.logger.debug(
|
|
73
|
-
`GraphFromFsBuilder, buildGraph with ${ids.length} seeders completed (${(Date.now() - start) / 1000} sec)`
|
|
74
|
-
);
|
|
75
|
-
return this.graph;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
private async getAllDepsUnfiltered(component: Component): Promise<ComponentID[]> {
|
|
79
|
-
const deps = await this.dependencyResolver.getComponentDependencies(component);
|
|
80
|
-
const depsIds = deps.map((dep) => dep.componentId);
|
|
81
|
-
|
|
82
|
-
return depsIds.filter((depId) => !this.ignoreIds.includes(depId.toString()));
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
private async getAllDepsFiltered(component: Component): Promise<ComponentID[]> {
|
|
86
|
-
const depsWithoutIgnore = await this.getAllDepsUnfiltered(component);
|
|
87
|
-
const shouldLoadFunc = this.shouldLoadItsDeps;
|
|
88
|
-
if (!shouldLoadFunc) return depsWithoutIgnore;
|
|
89
|
-
const deps = await mapSeries(depsWithoutIgnore, async (depId) => {
|
|
90
|
-
const shouldLoad = await shouldLoadFunc(depId);
|
|
91
|
-
if (!shouldLoad) this.ignoreIds.push(depId.toString());
|
|
92
|
-
return shouldLoad ? depId : null;
|
|
93
|
-
});
|
|
94
|
-
return compact(deps);
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
private async processManyComponents(components: Component[]) {
|
|
98
|
-
this.logger.debug(`GraphFromFsBuilder.processManyComponents depth ${this.depth}, ${components.length} components`);
|
|
99
|
-
this.depth += 1;
|
|
100
|
-
await this.importObjects(components);
|
|
101
|
-
const allDependencies = await mapSeries(components, (component) => this.processOneComponent(component));
|
|
102
|
-
const allDependenciesFlattened = flatten(allDependencies);
|
|
103
|
-
if (allDependenciesFlattened.length) await this.processManyComponents(allDependenciesFlattened);
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
/**
|
|
107
|
-
* only for components from the workspace that can be modified to add/remove dependencies, we need to make sure that
|
|
108
|
-
* all their dependencies are imported.
|
|
109
|
-
* remember that `importMany` fetches all flattened dependencies. once a component from scope is imported, we know
|
|
110
|
-
* that all its flattened dependencies are there. no need to call importMany again for them.
|
|
111
|
-
*/
|
|
112
|
-
private async importObjects(components: Component[]) {
|
|
113
|
-
const workspaceIds = await this.workspace.listIds();
|
|
114
|
-
const compOnWorkspaceOnly = components.filter((comp) => workspaceIds.find((id) => id.isEqual(comp.id)));
|
|
115
|
-
const allDeps = (await Promise.all(compOnWorkspaceOnly.map((c) => this.getAllDepsUnfiltered(c)))).flat();
|
|
116
|
-
const allDepsNotImported = allDeps.filter((d) => !this.importedIds.includes(d.toString()));
|
|
117
|
-
const exportedDeps = allDepsNotImported.map((id) => id).filter((dep) => this.workspace.isExported(dep));
|
|
118
|
-
const scopeComponentsImporter = this.consumer.scope.scopeImporter;
|
|
119
|
-
await scopeComponentsImporter.importMany({
|
|
120
|
-
ids: ComponentIdList.uniqFromArray(exportedDeps),
|
|
121
|
-
preferDependencyGraph: false,
|
|
122
|
-
throwForDependencyNotFound: this.shouldThrowOnMissingDep,
|
|
123
|
-
throwForSeederNotFound: this.shouldThrowOnMissingDep,
|
|
124
|
-
reFetchUnBuiltVersion: false,
|
|
125
|
-
lane: this.currentLane || undefined,
|
|
126
|
-
reason: 'for building a graph from the workspace',
|
|
127
|
-
});
|
|
128
|
-
allDepsNotImported.map((id) => this.importedIds.push(id.toString()));
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
private async processOneComponent(component: Component) {
|
|
132
|
-
const idStr = component.id.toString();
|
|
133
|
-
if (this.completed.includes(idStr)) return [];
|
|
134
|
-
const allIds = await this.getAllDepsFiltered(component);
|
|
135
|
-
|
|
136
|
-
const allDependenciesComps = await this.loadManyComponents(allIds, idStr);
|
|
137
|
-
const deps = await this.dependencyResolver.getComponentDependencies(component);
|
|
138
|
-
deps.forEach((dep) => {
|
|
139
|
-
const depId = dep.componentId;
|
|
140
|
-
if (this.ignoreIds.includes(depId.toString())) return;
|
|
141
|
-
if (!this.graph.hasNode(depId.toString())) {
|
|
142
|
-
if (this.shouldThrowOnMissingDep) {
|
|
143
|
-
throw new Error(`buildOneComponent: missing node of ${depId.toString()}`);
|
|
144
|
-
}
|
|
145
|
-
this.logger.warn(`ignoring missing ${depId.toString()}`);
|
|
146
|
-
return;
|
|
147
|
-
}
|
|
148
|
-
this.graph.setEdge(new Edge(idStr, depId.toString(), dep.lifecycle));
|
|
149
|
-
});
|
|
150
|
-
|
|
151
|
-
this.completed.push(idStr);
|
|
152
|
-
return allDependenciesComps;
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
private async loadManyComponents(componentsIds: ComponentID[], dependenciesOf?: string): Promise<Component[]> {
|
|
156
|
-
const components = await mapSeries(componentsIds, async (comp) => {
|
|
157
|
-
const idStr = comp.toString();
|
|
158
|
-
const fromGraph = this.graph.node(idStr)?.attr;
|
|
159
|
-
if (fromGraph) return fromGraph;
|
|
160
|
-
try {
|
|
161
|
-
const component = await this.workspace.get(comp);
|
|
162
|
-
this.graph.setNode(new Node(idStr, component));
|
|
163
|
-
return component;
|
|
164
|
-
} catch (err: any) {
|
|
165
|
-
if (
|
|
166
|
-
err instanceof ComponentNotFound ||
|
|
167
|
-
err instanceof ComponentNotFoundInScope ||
|
|
168
|
-
err instanceof ScopeNotFound
|
|
169
|
-
) {
|
|
170
|
-
if (dependenciesOf && !this.shouldThrowOnMissingDep) {
|
|
171
|
-
this.logger.warn(
|
|
172
|
-
`component ${idStr}, dependency of ${dependenciesOf} was not found. continuing without it`
|
|
173
|
-
);
|
|
174
|
-
return null;
|
|
175
|
-
}
|
|
176
|
-
throw new BitError(
|
|
177
|
-
`error: component "${idStr}" was not found.\nthis component is a dependency of "${
|
|
178
|
-
dependenciesOf || '<none>'
|
|
179
|
-
}" and is needed as part of the graph generation`
|
|
180
|
-
);
|
|
181
|
-
}
|
|
182
|
-
if (dependenciesOf) this.logger.error(`failed loading dependencies of ${dependenciesOf}`);
|
|
183
|
-
throw err;
|
|
184
|
-
}
|
|
185
|
-
});
|
|
186
|
-
return compact(components);
|
|
187
|
-
}
|
|
188
|
-
}
|
|
@@ -1,216 +0,0 @@
|
|
|
1
|
-
import mapSeries from 'p-map-series';
|
|
2
|
-
import { Graph, Node, Edge } from '@teambit/graph.cleargraph';
|
|
3
|
-
import { flatten, partition } from 'lodash';
|
|
4
|
-
import { Consumer } from '@teambit/legacy/dist/consumer';
|
|
5
|
-
import { Component, ComponentID } from '@teambit/component';
|
|
6
|
-
import ConsumerComponent from '@teambit/legacy/dist/consumer/component';
|
|
7
|
-
import { ComponentIdList } from '@teambit/component-id';
|
|
8
|
-
import { ComponentDependency, DependencyResolverMain } from '@teambit/dependency-resolver';
|
|
9
|
-
import { CompIdGraph, DepEdgeType } from '@teambit/graph';
|
|
10
|
-
import { ComponentNotFound, ScopeNotFound } from '@teambit/legacy/dist/scope/exceptions';
|
|
11
|
-
import { ComponentNotFound as ComponentNotFoundInScope } from '@teambit/scope';
|
|
12
|
-
import compact from 'lodash.compact';
|
|
13
|
-
import { Logger } from '@teambit/logger';
|
|
14
|
-
import { BitError } from '@teambit/bit-error';
|
|
15
|
-
import { Workspace } from './workspace';
|
|
16
|
-
|
|
17
|
-
export function lifecycleToDepType(compDep: ComponentDependency): DepEdgeType {
|
|
18
|
-
if (compDep.isExtension) return 'ext';
|
|
19
|
-
switch (compDep.lifecycle) {
|
|
20
|
-
case 'dev':
|
|
21
|
-
return 'dev';
|
|
22
|
-
case 'runtime':
|
|
23
|
-
return 'prod';
|
|
24
|
-
default:
|
|
25
|
-
throw new Error(`lifecycle ${compDep.lifecycle} is not support`);
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export class GraphIdsFromFsBuilder {
|
|
30
|
-
private graph = new Graph<ComponentID, DepEdgeType>();
|
|
31
|
-
private completed: string[] = [];
|
|
32
|
-
private depth = 1;
|
|
33
|
-
private consumer: Consumer;
|
|
34
|
-
private loadedComponents: { [idStr: string]: Component } = {};
|
|
35
|
-
private importedIds: string[] = [];
|
|
36
|
-
private shouldThrowOnInvalidDeps = true; // for now it has the same value as shouldThrowOnMissingDep. change if needed
|
|
37
|
-
constructor(
|
|
38
|
-
private workspace: Workspace,
|
|
39
|
-
private logger: Logger,
|
|
40
|
-
private dependencyResolver: DependencyResolverMain,
|
|
41
|
-
private shouldThrowOnMissingDep = true
|
|
42
|
-
) {
|
|
43
|
-
this.consumer = this.workspace.consumer;
|
|
44
|
-
this.shouldThrowOnInvalidDeps = this.shouldThrowOnMissingDep;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* create a graph with all dependencies and flattened dependencies of the given components.
|
|
49
|
-
* the nodes are component-ids and the edges has a label of the dependency type.
|
|
50
|
-
* to get some info about this the graph build take a look into build-graph-from-fs.buildGraph() docs.
|
|
51
|
-
*/
|
|
52
|
-
async buildGraph(ids: ComponentID[]): Promise<Graph<ComponentID, DepEdgeType>> {
|
|
53
|
-
this.logger.debug(`GraphIdsFromFsBuilder, buildGraph with ${ids.length} seeders`);
|
|
54
|
-
const start = Date.now();
|
|
55
|
-
const components = await this.loadManyComponents(ids);
|
|
56
|
-
await this.processManyComponents(components);
|
|
57
|
-
this.logger.debug(
|
|
58
|
-
`GraphIdsFromFsBuilder, buildGraph with ${ids.length} seeders completed (${(Date.now() - start) / 1000} sec)`
|
|
59
|
-
);
|
|
60
|
-
return this.graph;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
private async processManyComponents(components: Component[]) {
|
|
64
|
-
this.logger.debug(
|
|
65
|
-
`GraphIdsFromFsBuilder.processManyComponents depth ${this.depth}, ${components.length} components`
|
|
66
|
-
);
|
|
67
|
-
this.depth += 1;
|
|
68
|
-
await this.importObjects(components);
|
|
69
|
-
const allDependencies = await mapSeries(components, (component) => this.processOneComponent(component));
|
|
70
|
-
const allDependenciesFlattened = flatten(allDependencies);
|
|
71
|
-
if (allDependenciesFlattened.length) await this.processManyComponents(allDependenciesFlattened);
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* only for components from the workspace that can be modified to add/remove dependencies, we need to make sure that
|
|
76
|
-
* all their dependencies are imported.
|
|
77
|
-
* once a component from scope is imported, we know that either we have its dependency graph or all flattened deps
|
|
78
|
-
*/
|
|
79
|
-
private async importObjects(components: Component[]) {
|
|
80
|
-
const workspaceIds = await this.workspace.listIds();
|
|
81
|
-
const compOnWorkspaceOnly = components.filter((comp) => workspaceIds.find((id) => id.isEqual(comp.id)));
|
|
82
|
-
const notImported = compOnWorkspaceOnly.map((c) => c.id).filter((id) => !this.importedIds.includes(id.toString()));
|
|
83
|
-
const exportedDeps = notImported.filter((dep) => this.workspace.isExported(dep));
|
|
84
|
-
const scopeComponentsImporter = this.consumer.scope.scopeImporter;
|
|
85
|
-
await scopeComponentsImporter.importMany({
|
|
86
|
-
ids: ComponentIdList.uniqFromArray(exportedDeps),
|
|
87
|
-
throwForDependencyNotFound: this.shouldThrowOnMissingDep,
|
|
88
|
-
throwForSeederNotFound: this.shouldThrowOnMissingDep,
|
|
89
|
-
reFetchUnBuiltVersion: false,
|
|
90
|
-
lane: (await this.workspace.getCurrentLaneObject()) || undefined,
|
|
91
|
-
reason: 'for building graph-ids from the workspace',
|
|
92
|
-
});
|
|
93
|
-
notImported.map((id) => this.importedIds.push(id.toString()));
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
private async processOneComponent(component: Component) {
|
|
97
|
-
const idStr = component.id.toString();
|
|
98
|
-
if (this.completed.includes(idStr)) return [];
|
|
99
|
-
const graphFromScope = await this.workspace.getSavedGraphOfComponentIfExist(component);
|
|
100
|
-
if (graphFromScope?.edges.length) {
|
|
101
|
-
const isOnWorkspace = await this.workspace.hasId(component.id);
|
|
102
|
-
if (isOnWorkspace) {
|
|
103
|
-
const allDependenciesComps = await this.processCompFromWorkspaceWithGraph(graphFromScope, component);
|
|
104
|
-
this.completed.push(idStr);
|
|
105
|
-
return allDependenciesComps;
|
|
106
|
-
}
|
|
107
|
-
this.graph.merge([graphFromScope]);
|
|
108
|
-
this.completed.push(idStr);
|
|
109
|
-
return [];
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
const deps = await this.dependencyResolver.getComponentDependencies(component);
|
|
113
|
-
const allDepsIds = deps.map((d) => d.componentId);
|
|
114
|
-
const allDependenciesComps = await this.loadManyComponents(allDepsIds, idStr);
|
|
115
|
-
|
|
116
|
-
deps.forEach((dep) => this.addDepEdge(idStr, dep));
|
|
117
|
-
this.completed.push(idStr);
|
|
118
|
-
|
|
119
|
-
return allDependenciesComps;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
/**
|
|
123
|
-
* this is tricky.
|
|
124
|
-
* the component is in the workspace so it can be modified. dependencies can be added/removed/updated/downgraded.
|
|
125
|
-
* we have the graph-dependencies from the last snap, so we prefer to use it whenever possible for performance reasons.
|
|
126
|
-
* if we can't use it, we have to recursively load dependencies components and get the data from there.
|
|
127
|
-
* to maximize the performance, we iterate the direct dependencies, if we find a dep with the same id in the graph,
|
|
128
|
-
* and that id is not in the workspace then ask the graph for all its successors. otherwise, if it's not there, or
|
|
129
|
-
* it's there but it's also in the workspace (which therefore can be modified), we recursively load the dep components
|
|
130
|
-
* and get its dependencies.
|
|
131
|
-
*/
|
|
132
|
-
private async processCompFromWorkspaceWithGraph(
|
|
133
|
-
graphFromScope: CompIdGraph,
|
|
134
|
-
component: Component
|
|
135
|
-
): Promise<Component[]> {
|
|
136
|
-
const deps = await this.dependencyResolver.getComponentDependencies(component);
|
|
137
|
-
const workspaceIds = await this.workspace.listIds();
|
|
138
|
-
const [depsInScopeGraph, depsNotInScopeGraph] = partition(
|
|
139
|
-
deps,
|
|
140
|
-
(dep) =>
|
|
141
|
-
graphFromScope.hasNode(dep.componentId.toString()) && !workspaceIds.find((id) => id.isEqual(dep.componentId))
|
|
142
|
-
);
|
|
143
|
-
|
|
144
|
-
const depsInScopeGraphIds = depsInScopeGraph.map((dep) => dep.componentId.toString());
|
|
145
|
-
const depsInScopeGraphIdsNotCompleted = depsInScopeGraphIds.filter((id) => !this.completed.includes(id));
|
|
146
|
-
if (depsInScopeGraphIdsNotCompleted.length) {
|
|
147
|
-
const subGraphs = graphFromScope.successorsSubgraph(depsInScopeGraphIdsNotCompleted);
|
|
148
|
-
this.graph.merge([subGraphs]);
|
|
149
|
-
this.completed.push(...depsInScopeGraphIdsNotCompleted);
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
const allDepsIds = depsNotInScopeGraph.map((d) => d.componentId);
|
|
153
|
-
const idStr = component.id.toString();
|
|
154
|
-
const allDependenciesComps = await this.loadManyComponents(allDepsIds, idStr);
|
|
155
|
-
deps.forEach((dep) => this.addDepEdge(idStr, dep));
|
|
156
|
-
return allDependenciesComps;
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
private addDepEdge(idStr: string, dep: ComponentDependency) {
|
|
160
|
-
const depId = dep.componentId;
|
|
161
|
-
if (!this.graph.hasNode(depId.toString())) {
|
|
162
|
-
if (this.shouldThrowOnMissingDep) {
|
|
163
|
-
throw new Error(`buildOneComponent: missing node of ${depId.toString()}`);
|
|
164
|
-
}
|
|
165
|
-
this.logger.warn(`ignoring missing ${depId.toString()}`);
|
|
166
|
-
return;
|
|
167
|
-
}
|
|
168
|
-
this.graph.setEdge(new Edge(idStr, depId.toString(), lifecycleToDepType(dep)));
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
private async loadManyComponents(componentsIds: ComponentID[], dependenciesOf?: string): Promise<Component[]> {
|
|
172
|
-
const components = await mapSeries(componentsIds, async (comp) => {
|
|
173
|
-
const idStr = comp.toString();
|
|
174
|
-
const fromCache = this.loadedComponents[idStr];
|
|
175
|
-
if (fromCache) return fromCache;
|
|
176
|
-
try {
|
|
177
|
-
const component = await this.workspace.get(comp);
|
|
178
|
-
this.loadedComponents[idStr] = component;
|
|
179
|
-
this.graph.setNode(new Node(idStr, component.id));
|
|
180
|
-
return component;
|
|
181
|
-
} catch (err: any) {
|
|
182
|
-
if (
|
|
183
|
-
err instanceof ComponentNotFound ||
|
|
184
|
-
err instanceof ComponentNotFoundInScope ||
|
|
185
|
-
err instanceof ScopeNotFound
|
|
186
|
-
) {
|
|
187
|
-
if (dependenciesOf && !this.shouldThrowOnMissingDep) {
|
|
188
|
-
this.logger.warn(
|
|
189
|
-
`component ${idStr}, dependency of ${dependenciesOf} was not found. continuing without it`
|
|
190
|
-
);
|
|
191
|
-
return null;
|
|
192
|
-
}
|
|
193
|
-
throw new BitError(
|
|
194
|
-
`error: component "${idStr}" was not found.\nthis component is a dependency of "${
|
|
195
|
-
dependenciesOf || '<none>'
|
|
196
|
-
}" and is needed as part of the graph generation`
|
|
197
|
-
);
|
|
198
|
-
}
|
|
199
|
-
if (ConsumerComponent.isComponentInvalidByErrorType(err)) {
|
|
200
|
-
if (dependenciesOf && !this.shouldThrowOnInvalidDeps) {
|
|
201
|
-
this.logger.warn(`component ${idStr}, dependency of ${dependenciesOf} is invalid. continuing without it`);
|
|
202
|
-
return null;
|
|
203
|
-
}
|
|
204
|
-
throw new BitError(
|
|
205
|
-
`error: component "${idStr}" is invalid (${err.message}).\nthis component is a dependency of "${
|
|
206
|
-
dependenciesOf || '<none>'
|
|
207
|
-
}" and is needed as part of the graph generation`
|
|
208
|
-
);
|
|
209
|
-
}
|
|
210
|
-
if (dependenciesOf) this.logger.error(`failed loading dependencies of ${dependenciesOf}`);
|
|
211
|
-
throw err;
|
|
212
|
-
}
|
|
213
|
-
});
|
|
214
|
-
return compact(components);
|
|
215
|
-
}
|
|
216
|
-
}
|