@teambit/workspace 0.0.917 → 0.0.919
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/dist/build-graph-from-fs.d.ts +3 -2
- package/dist/build-graph-from-fs.js +17 -26
- package/dist/build-graph-from-fs.js.map +1 -1
- package/dist/build-graph-ids-from-fs.d.ts +45 -0
- package/dist/build-graph-ids-from-fs.js +222 -0
- package/dist/build-graph-ids-from-fs.js.map +1 -0
- package/dist/workspace-component/workspace-component-loader.js +1 -1
- package/dist/workspace-component/workspace-component-loader.js.map +1 -1
- package/dist/workspace.d.ts +3 -1
- package/dist/workspace.js +53 -2
- package/dist/workspace.js.map +1 -1
- package/dist/workspace.ui.runtime.js +1 -1
- package/dist/workspace.ui.runtime.js.map +1 -1
- package/package-tar/teambit-workspace-0.0.919.tgz +0 -0
- package/package.json +33 -33
- package/{preview-1669433164317.js → preview-1669865271197.js} +2 -2
- package/workspace-component/workspace-component-loader.ts +4 -1
- package/workspace.ui.runtime.tsx +1 -1
- package/package-tar/teambit-workspace-0.0.917.tgz +0 -0
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
import { Graph } from '@teambit/graph.cleargraph';
|
|
2
2
|
import { Component, ComponentID } from '@teambit/component';
|
|
3
|
+
import { DependencyResolverMain } from '@teambit/dependency-resolver';
|
|
3
4
|
import { Logger } from '@teambit/logger';
|
|
4
5
|
import { Workspace } from './workspace';
|
|
5
6
|
export declare type ShouldLoadFunc = (id: ComponentID) => Promise<boolean>;
|
|
6
7
|
export declare class GraphFromFsBuilder {
|
|
7
8
|
private workspace;
|
|
8
9
|
private logger;
|
|
10
|
+
private dependencyResolver;
|
|
9
11
|
private ignoreIds;
|
|
10
12
|
private shouldLoadItsDeps?;
|
|
11
13
|
private shouldThrowOnMissingDep;
|
|
@@ -13,9 +15,8 @@ export declare class GraphFromFsBuilder {
|
|
|
13
15
|
private completed;
|
|
14
16
|
private depth;
|
|
15
17
|
private consumer;
|
|
16
|
-
private legacyIdStrToComponentId;
|
|
17
18
|
private importedIds;
|
|
18
|
-
constructor(workspace: Workspace, logger: Logger, ignoreIds?: string[], shouldLoadItsDeps?: ShouldLoadFunc | undefined, shouldThrowOnMissingDep?: boolean);
|
|
19
|
+
constructor(workspace: Workspace, logger: Logger, dependencyResolver: DependencyResolverMain, ignoreIds?: string[], shouldLoadItsDeps?: ShouldLoadFunc | undefined, shouldThrowOnMissingDep?: boolean);
|
|
19
20
|
/**
|
|
20
21
|
* create a graph with all dependencies and flattened dependencies of the given components.
|
|
21
22
|
* the nodes are components and the edges has a label of the dependency type.
|
|
@@ -73,9 +73,10 @@ function _bitError() {
|
|
|
73
73
|
return data;
|
|
74
74
|
}
|
|
75
75
|
class GraphFromFsBuilder {
|
|
76
|
-
constructor(workspace, logger, ignoreIds = [], shouldLoadItsDeps, shouldThrowOnMissingDep = true) {
|
|
76
|
+
constructor(workspace, logger, dependencyResolver, ignoreIds = [], shouldLoadItsDeps, shouldThrowOnMissingDep = true) {
|
|
77
77
|
this.workspace = workspace;
|
|
78
78
|
this.logger = logger;
|
|
79
|
+
this.dependencyResolver = dependencyResolver;
|
|
79
80
|
this.ignoreIds = ignoreIds;
|
|
80
81
|
this.shouldLoadItsDeps = shouldLoadItsDeps;
|
|
81
82
|
this.shouldThrowOnMissingDep = shouldThrowOnMissingDep;
|
|
@@ -83,7 +84,6 @@ class GraphFromFsBuilder {
|
|
|
83
84
|
(0, _defineProperty2().default)(this, "completed", []);
|
|
84
85
|
(0, _defineProperty2().default)(this, "depth", 1);
|
|
85
86
|
(0, _defineProperty2().default)(this, "consumer", void 0);
|
|
86
|
-
(0, _defineProperty2().default)(this, "legacyIdStrToComponentId", {});
|
|
87
87
|
(0, _defineProperty2().default)(this, "importedIds", []);
|
|
88
88
|
this.consumer = this.workspace.consumer;
|
|
89
89
|
}
|
|
@@ -127,14 +127,8 @@ class GraphFromFsBuilder {
|
|
|
127
127
|
return this.graph;
|
|
128
128
|
}
|
|
129
129
|
async getAllDepsUnfiltered(component) {
|
|
130
|
-
const
|
|
131
|
-
const
|
|
132
|
-
const depsIds = await Promise.all(legacyDepsIds.map(async bitId => {
|
|
133
|
-
if (!this.legacyIdStrToComponentId[bitId.toString()]) {
|
|
134
|
-
this.legacyIdStrToComponentId[bitId.toString()] = await this.workspace.resolveComponentId(bitId);
|
|
135
|
-
}
|
|
136
|
-
return this.legacyIdStrToComponentId[bitId.toString()];
|
|
137
|
-
}));
|
|
130
|
+
const deps = await this.dependencyResolver.getComponentDependencies(component);
|
|
131
|
+
const depsIds = deps.map(dep => dep.componentId);
|
|
138
132
|
return depsIds.filter(depId => !this.ignoreIds.includes(depId.toString()));
|
|
139
133
|
}
|
|
140
134
|
async getAllDepsFiltered(component) {
|
|
@@ -182,25 +176,22 @@ class GraphFromFsBuilder {
|
|
|
182
176
|
const idStr = component.id.toString();
|
|
183
177
|
if (this.completed.includes(idStr)) return [];
|
|
184
178
|
const allIds = await this.getAllDepsFiltered(component);
|
|
185
|
-
const
|
|
186
|
-
const
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
if (this.
|
|
192
|
-
|
|
193
|
-
if (this.shouldThrowOnMissingDep) {
|
|
194
|
-
throw new Error(`buildOneComponent: missing node of ${depId.toString()}`);
|
|
195
|
-
}
|
|
196
|
-
this.logger.warn(`ignoring missing ${depId.toString()}`);
|
|
197
|
-
return;
|
|
179
|
+
const allDependenciesComps = await this.loadManyComponents(allIds, idStr);
|
|
180
|
+
const deps = await this.dependencyResolver.getComponentDependencies(component);
|
|
181
|
+
deps.forEach(dep => {
|
|
182
|
+
const depId = dep.componentId;
|
|
183
|
+
if (this.ignoreIds.includes(depId.toString())) return;
|
|
184
|
+
if (!this.graph.hasNode(depId.toString())) {
|
|
185
|
+
if (this.shouldThrowOnMissingDep) {
|
|
186
|
+
throw new Error(`buildOneComponent: missing node of ${depId.toString()}`);
|
|
198
187
|
}
|
|
199
|
-
this.
|
|
200
|
-
|
|
188
|
+
this.logger.warn(`ignoring missing ${depId.toString()}`);
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
this.graph.setEdge(new (_graph().Edge)(idStr, depId.toString(), dep.lifecycle));
|
|
201
192
|
});
|
|
202
193
|
this.completed.push(idStr);
|
|
203
|
-
return
|
|
194
|
+
return allDependenciesComps;
|
|
204
195
|
}
|
|
205
196
|
async loadManyComponents(componentsIds, dependenciesOf) {
|
|
206
197
|
const components = await (0, _pMapSeries().default)(componentsIds, async comp => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["GraphFromFsBuilder","constructor","workspace","logger","ignoreIds","shouldLoadItsDeps","shouldThrowOnMissingDep","Graph","consumer","buildGraph","ids","debug","length","start","Date","now","components","loadManyComponents","processManyComponents","graph","getAllDepsUnfiltered","component","consumerComp","state","_consumer","legacyDepsIds","getAllDependenciesIds","depsIds","Promise","all","map","bitId","legacyIdStrToComponentId","toString","resolveComponentId","filter","depId","includes","getAllDepsFiltered","depsWithoutIgnore","shouldLoadFunc","deps","mapSeries","shouldLoad","push","compact","depth","importObjects","allDependencies","processOneComponent","allDependenciesFlattened","flatten","workspaceIds","listIds","compOnWorkspaceOnly","comp","find","id","isEqual","allDeps","c","flat","allDepsNotImported","d","importedIds","allDepsWithScope","_legacy","dep","hasScope","scopeComponentsImporter","scope","scopeImporter","importMany","BitIds","uniqFromArray","throwForDependencyNotFound","throwForSeederNotFound","reFetchUnBuiltVersion","idStr","completed","allIds","consumerComponent","Object","entries","depsIdsGroupedByType","forEach","depType","depBitId","Error","hasNode","warn","setEdge","Edge","componentsIds","dependenciesOf","fromGraph","node","attr","get","setNode","Node","err","ComponentNotFound","ComponentNotFoundInScope","ScopeNotFound","BitError","error"],"sources":["build-graph-from-fs.ts"],"sourcesContent":["import mapSeries from 'p-map-series';\nimport { Graph, Node, Edge } from '@teambit/graph.cleargraph';\nimport { flatten } from 'lodash';\nimport { Consumer } from '@teambit/legacy/dist/consumer';\nimport { Component, ComponentID } from '@teambit/component';\nimport BitIds from '@teambit/legacy/dist/bit-id/bit-ids';\nimport ConsumerComponent from '@teambit/legacy/dist/consumer/component/consumer-component';\nimport { ComponentNotFound, ScopeNotFound } from '@teambit/legacy/dist/scope/exceptions';\nimport { ComponentNotFound as ComponentNotFoundInScope } from '@teambit/scope';\nimport compact from 'lodash.compact';\nimport { Logger } from '@teambit/logger';\nimport { BitError } from '@teambit/bit-error';\nimport { Workspace } from './workspace';\n\nexport type ShouldLoadFunc = (id: ComponentID) => Promise<boolean>;\n\nexport class GraphFromFsBuilder {\n private graph = new Graph<Component, string>();\n private completed: string[] = [];\n private depth = 1;\n private consumer: Consumer;\n private legacyIdStrToComponentId: { [bitIdStr: string]: ComponentID } = {};\n private importedIds: string[] = [];\n constructor(\n private workspace: Workspace,\n private logger: Logger,\n private ignoreIds: string[] = [],\n private shouldLoadItsDeps?: ShouldLoadFunc,\n private shouldThrowOnMissingDep = true\n ) {\n this.consumer = this.workspace.consumer;\n }\n\n /**\n * create a graph with all dependencies and flattened dependencies of the given components.\n * the nodes are components and the edges has a label of the dependency type.\n *\n * the way how it is done is iterations by depths. each depth we gather all the dependencies of\n * that depths, make sure all objects exist and then check their dependencies for the next depth.\n * once there is no dependency left, we're on the last depth level and the graph is ready.\n *\n * for example, imagine the following graph:\n * A1 -> A2 -> A3\n * B1 -> B2 -> B3\n * C1 -> C2 -> C3\n *\n * where the buildGraph is given [A1, B1, C1].\n * first, it saves all these components as nodes in the graph. then, it finds the dependencies of\n * the next level, in this case they're [A2, B2, C2]. it runs `importMany` in case some objects\n * are missing. then, it loads them all (some from FS, some from the model) and sets the edges\n * between the component and the dependencies.\n * once done, it finds all their dependencies, which are [A3, B3, C3] and repeat the process\n * above. since there are no more dependencies, the graph is completed.\n * in this case, the total depth levels are 3.\n *\n * even with a huge project, there are not many depth levels. by iterating through depth levels\n * we keep performance sane as the importMany doesn't run multiple time and therefore the round\n * trips to the remotes are minimal.\n *\n * normally, one importMany of the seeders is enough as importMany knows to fetch all flattened.\n * however, since this buildGraph is performed on the workspace, a dependency may be new or\n * modified and as such, we don't know its flattened yet.\n */\n async buildGraph(ids: ComponentID[]): Promise<Graph<Component, string>> {\n this.logger.debug(`GraphFromFsBuilder, buildGraph with ${ids.length} seeders`);\n const start = Date.now();\n const components = await this.loadManyComponents(ids);\n await this.processManyComponents(components);\n this.logger.debug(\n `GraphFromFsBuilder, buildGraph with ${ids.length} seeders completed (${(Date.now() - start) / 1000} sec)`\n );\n return this.graph;\n }\n\n private async getAllDepsUnfiltered(component: Component): Promise<ComponentID[]> {\n const consumerComp = component.state._consumer as ConsumerComponent;\n const legacyDepsIds = consumerComp.getAllDependenciesIds();\n const depsIds = await Promise.all(\n legacyDepsIds.map(async (bitId) => {\n if (!this.legacyIdStrToComponentId[bitId.toString()]) {\n this.legacyIdStrToComponentId[bitId.toString()] = await this.workspace.resolveComponentId(bitId);\n }\n return this.legacyIdStrToComponentId[bitId.toString()];\n })\n );\n return depsIds.filter((depId) => !this.ignoreIds.includes(depId.toString()));\n }\n\n private async getAllDepsFiltered(component: Component): Promise<ComponentID[]> {\n const depsWithoutIgnore = await this.getAllDepsUnfiltered(component);\n const shouldLoadFunc = this.shouldLoadItsDeps;\n if (!shouldLoadFunc) return depsWithoutIgnore;\n const deps = await mapSeries(depsWithoutIgnore, async (depId) => {\n const shouldLoad = await shouldLoadFunc(depId);\n if (!shouldLoad) this.ignoreIds.push(depId.toString());\n return shouldLoad ? depId : null;\n });\n return compact(deps);\n }\n\n private async processManyComponents(components: Component[]) {\n this.logger.debug(`GraphFromFsBuilder.processManyComponents depth ${this.depth}, ${components.length} components`);\n this.depth += 1;\n await this.importObjects(components);\n const allDependencies = await mapSeries(components, (component) => this.processOneComponent(component));\n const allDependenciesFlattened = flatten(allDependencies);\n if (allDependenciesFlattened.length) await this.processManyComponents(allDependenciesFlattened);\n }\n\n /**\n * only for components from the workspace that can be modified to add/remove dependencies, we need to make sure that\n * all their dependencies are imported.\n * remember that `importMany` fetches all flattened dependencies. once a component from scope is imported, we know\n * that all its flattened dependencies are there. no need to call importMany again for them.\n */\n private async importObjects(components: Component[]) {\n const workspaceIds = await this.workspace.listIds();\n const compOnWorkspaceOnly = components.filter((comp) => workspaceIds.find((id) => id.isEqual(comp.id)));\n const allDeps = (await Promise.all(compOnWorkspaceOnly.map((c) => this.getAllDepsUnfiltered(c)))).flat();\n const allDepsNotImported = allDeps.filter((d) => !this.importedIds.includes(d.toString()));\n const allDepsWithScope = allDepsNotImported.map((id) => id._legacy).filter((dep) => dep.hasScope());\n const scopeComponentsImporter = this.consumer.scope.scopeImporter;\n await scopeComponentsImporter.importMany({\n ids: BitIds.uniqFromArray(allDepsWithScope),\n throwForDependencyNotFound: this.shouldThrowOnMissingDep,\n throwForSeederNotFound: this.shouldThrowOnMissingDep,\n reFetchUnBuiltVersion: false,\n });\n allDepsNotImported.map((id) => this.importedIds.push(id.toString()));\n }\n\n private async processOneComponent(component: Component) {\n const idStr = component.id.toString();\n if (this.completed.includes(idStr)) return [];\n const allIds = await this.getAllDepsFiltered(component);\n\n const allDependencies = await this.loadManyComponents(allIds, idStr);\n const consumerComponent = component.state._consumer as ConsumerComponent;\n Object.entries(consumerComponent.depsIdsGroupedByType).forEach(([depType, depsIds]) => {\n depsIds.forEach((depBitId) => {\n const depId = this.legacyIdStrToComponentId[depBitId.toString()];\n if (!depId) throw new Error(`unable to find ${depBitId.toString()} inside legacyIdStrToComponentId`);\n if (this.ignoreIds.includes(depId.toString())) return;\n if (!this.graph.hasNode(depId.toString())) {\n if (this.shouldThrowOnMissingDep) {\n throw new Error(`buildOneComponent: missing node of ${depId.toString()}`);\n }\n this.logger.warn(`ignoring missing ${depId.toString()}`);\n return;\n }\n this.graph.setEdge(new Edge(idStr, depId.toString(), depType));\n });\n });\n this.completed.push(idStr);\n return allDependencies;\n }\n\n private async loadManyComponents(componentsIds: ComponentID[], dependenciesOf?: string): Promise<Component[]> {\n const components = await mapSeries(componentsIds, async (comp) => {\n const idStr = comp.toString();\n const fromGraph = this.graph.node(idStr)?.attr;\n if (fromGraph) return fromGraph;\n try {\n const component = await this.workspace.get(comp);\n this.graph.setNode(new Node(idStr, component));\n return component;\n } catch (err: any) {\n if (\n err instanceof ComponentNotFound ||\n err instanceof ComponentNotFoundInScope ||\n err instanceof ScopeNotFound\n ) {\n if (dependenciesOf && !this.shouldThrowOnMissingDep) {\n this.logger.warn(\n `component ${idStr}, dependency of ${dependenciesOf} was not found. continuing without it`\n );\n return null;\n }\n throw new BitError(\n `error: component \"${idStr}\" was not found.\\nthis component is a dependency of \"${\n dependenciesOf || '<none>'\n }\" and is needed as part of the graph generation`\n );\n }\n if (dependenciesOf) this.logger.error(`failed loading dependencies of ${dependenciesOf}`);\n throw err;\n }\n });\n return compact(components);\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;EAAA;EAAA;IAAA;EAAA;EAAA;AAAA;AACA;EAAA;EAAA;IAAA;EAAA;EAAA;AAAA;AACA;EAAA;EAAA;IAAA;EAAA;EAAA;AAAA;AAGA;EAAA;EAAA;IAAA;EAAA;EAAA;AAAA;AAEA;EAAA;EAAA;IAAA;EAAA;EAAA;AAAA;AACA;EAAA;EAAA;IAAA;EAAA;EAAA;AAAA;AACA;EAAA;EAAA;IAAA;EAAA;EAAA;AAAA;AAEA;EAAA;EAAA;IAAA;EAAA;EAAA;AAAA;AAKO,MAAMA,kBAAkB,CAAC;EAO9BC,WAAW,CACDC,SAAoB,EACpBC,MAAc,EACdC,SAAmB,GAAG,EAAE,EACxBC,iBAAkC,EAClCC,uBAAuB,GAAG,IAAI,EACtC;IAAA,KALQJ,SAAoB,GAApBA,SAAoB;IAAA,KACpBC,MAAc,GAAdA,MAAc;IAAA,KACdC,SAAmB,GAAnBA,SAAmB;IAAA,KACnBC,iBAAkC,GAAlCA,iBAAkC;IAAA,KAClCC,uBAAuB,GAAvBA,uBAAuB;IAAA,+CAXjB,KAAIC,cAAK,GAAqB;IAAA,mDAChB,EAAE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ACb;UACA,MAAM,KAAIW,oBAAQ,EACf,qBAAoB3B,KAAM,wDACzBgB,cAAc,IAAI,QACnB,iDAAgD,CAClD;QACH;QACA,IAAIA,cAAc,EAAE,IAAI,CAAC3F,MAAM,CAACuG,KAAK,CAAE,kCAAiCZ,cAAe,EAAC,CAAC;QACzF,MAAMO,GAAG;MACX;IACF,CAAC,CAAC;IACF,OAAO,IAAAxD,kBAAO,EAAC7B,UAAU,CAAC;EAC5B;AACF;AAAC"}
|
|
1
|
+
{"version":3,"names":["GraphFromFsBuilder","constructor","workspace","logger","dependencyResolver","ignoreIds","shouldLoadItsDeps","shouldThrowOnMissingDep","Graph","consumer","buildGraph","ids","debug","length","start","Date","now","components","loadManyComponents","processManyComponents","graph","getAllDepsUnfiltered","component","deps","getComponentDependencies","depsIds","map","dep","componentId","filter","depId","includes","toString","getAllDepsFiltered","depsWithoutIgnore","shouldLoadFunc","mapSeries","shouldLoad","push","compact","depth","importObjects","allDependencies","processOneComponent","allDependenciesFlattened","flatten","workspaceIds","listIds","compOnWorkspaceOnly","comp","find","id","isEqual","allDeps","Promise","all","c","flat","allDepsNotImported","d","importedIds","allDepsWithScope","_legacy","hasScope","scopeComponentsImporter","scope","scopeImporter","importMany","BitIds","uniqFromArray","throwForDependencyNotFound","throwForSeederNotFound","reFetchUnBuiltVersion","idStr","completed","allIds","allDependenciesComps","forEach","hasNode","Error","warn","setEdge","Edge","lifecycle","componentsIds","dependenciesOf","fromGraph","node","attr","get","setNode","Node","err","ComponentNotFound","ComponentNotFoundInScope","ScopeNotFound","BitError","error"],"sources":["build-graph-from-fs.ts"],"sourcesContent":["import mapSeries from 'p-map-series';\nimport { Graph, Node, Edge } from '@teambit/graph.cleargraph';\nimport { flatten } from 'lodash';\nimport { Consumer } from '@teambit/legacy/dist/consumer';\nimport { Component, ComponentID } from '@teambit/component';\nimport { DependencyResolverMain } from '@teambit/dependency-resolver';\nimport BitIds from '@teambit/legacy/dist/bit-id/bit-ids';\nimport { ComponentNotFound, ScopeNotFound } from '@teambit/legacy/dist/scope/exceptions';\nimport { ComponentNotFound as ComponentNotFoundInScope } from '@teambit/scope';\nimport compact from 'lodash.compact';\nimport { Logger } from '@teambit/logger';\nimport { BitError } from '@teambit/bit-error';\nimport { Workspace } from './workspace';\n\nexport type ShouldLoadFunc = (id: ComponentID) => Promise<boolean>;\n\nexport class GraphFromFsBuilder {\n private graph = new Graph<Component, string>();\n private completed: string[] = [];\n private depth = 1;\n private consumer: Consumer;\n private importedIds: string[] = [];\n constructor(\n private workspace: Workspace,\n private logger: Logger,\n private dependencyResolver: DependencyResolverMain,\n private ignoreIds: string[] = [],\n private shouldLoadItsDeps?: ShouldLoadFunc,\n private shouldThrowOnMissingDep = true\n ) {\n this.consumer = this.workspace.consumer;\n }\n\n /**\n * create a graph with all dependencies and flattened dependencies of the given components.\n * the nodes are components and the edges has a label of the dependency type.\n *\n * the way how it is done is iterations by depths. each depth we gather all the dependencies of\n * that depths, make sure all objects exist and then check their dependencies for the next depth.\n * once there is no dependency left, we're on the last depth level and the graph is ready.\n *\n * for example, imagine the following graph:\n * A1 -> A2 -> A3\n * B1 -> B2 -> B3\n * C1 -> C2 -> C3\n *\n * where the buildGraph is given [A1, B1, C1].\n * first, it saves all these components as nodes in the graph. then, it finds the dependencies of\n * the next level, in this case they're [A2, B2, C2]. it runs `importMany` in case some objects\n * are missing. then, it loads them all (some from FS, some from the model) and sets the edges\n * between the component and the dependencies.\n * once done, it finds all their dependencies, which are [A3, B3, C3] and repeat the process\n * above. since there are no more dependencies, the graph is completed.\n * in this case, the total depth levels are 3.\n *\n * even with a huge project, there are not many depth levels. by iterating through depth levels\n * we keep performance sane as the importMany doesn't run multiple time and therefore the round\n * trips to the remotes are minimal.\n *\n * normally, one importMany of the seeders is enough as importMany knows to fetch all flattened.\n * however, since this buildGraph is performed on the workspace, a dependency may be new or\n * modified and as such, we don't know its flattened yet.\n */\n async buildGraph(ids: ComponentID[]): Promise<Graph<Component, string>> {\n this.logger.debug(`GraphFromFsBuilder, buildGraph with ${ids.length} seeders`);\n const start = Date.now();\n const components = await this.loadManyComponents(ids);\n await this.processManyComponents(components);\n this.logger.debug(\n `GraphFromFsBuilder, buildGraph with ${ids.length} seeders completed (${(Date.now() - start) / 1000} sec)`\n );\n return this.graph;\n }\n\n private async getAllDepsUnfiltered(component: Component): Promise<ComponentID[]> {\n const deps = await this.dependencyResolver.getComponentDependencies(component);\n const depsIds = deps.map((dep) => dep.componentId);\n\n return depsIds.filter((depId) => !this.ignoreIds.includes(depId.toString()));\n }\n\n private async getAllDepsFiltered(component: Component): Promise<ComponentID[]> {\n const depsWithoutIgnore = await this.getAllDepsUnfiltered(component);\n const shouldLoadFunc = this.shouldLoadItsDeps;\n if (!shouldLoadFunc) return depsWithoutIgnore;\n const deps = await mapSeries(depsWithoutIgnore, async (depId) => {\n const shouldLoad = await shouldLoadFunc(depId);\n if (!shouldLoad) this.ignoreIds.push(depId.toString());\n return shouldLoad ? depId : null;\n });\n return compact(deps);\n }\n\n private async processManyComponents(components: Component[]) {\n this.logger.debug(`GraphFromFsBuilder.processManyComponents depth ${this.depth}, ${components.length} components`);\n this.depth += 1;\n await this.importObjects(components);\n const allDependencies = await mapSeries(components, (component) => this.processOneComponent(component));\n const allDependenciesFlattened = flatten(allDependencies);\n if (allDependenciesFlattened.length) await this.processManyComponents(allDependenciesFlattened);\n }\n\n /**\n * only for components from the workspace that can be modified to add/remove dependencies, we need to make sure that\n * all their dependencies are imported.\n * remember that `importMany` fetches all flattened dependencies. once a component from scope is imported, we know\n * that all its flattened dependencies are there. no need to call importMany again for them.\n */\n private async importObjects(components: Component[]) {\n const workspaceIds = await this.workspace.listIds();\n const compOnWorkspaceOnly = components.filter((comp) => workspaceIds.find((id) => id.isEqual(comp.id)));\n const allDeps = (await Promise.all(compOnWorkspaceOnly.map((c) => this.getAllDepsUnfiltered(c)))).flat();\n const allDepsNotImported = allDeps.filter((d) => !this.importedIds.includes(d.toString()));\n const allDepsWithScope = allDepsNotImported.map((id) => id._legacy).filter((dep) => dep.hasScope());\n const scopeComponentsImporter = this.consumer.scope.scopeImporter;\n await scopeComponentsImporter.importMany({\n ids: BitIds.uniqFromArray(allDepsWithScope),\n throwForDependencyNotFound: this.shouldThrowOnMissingDep,\n throwForSeederNotFound: this.shouldThrowOnMissingDep,\n reFetchUnBuiltVersion: false,\n });\n allDepsNotImported.map((id) => this.importedIds.push(id.toString()));\n }\n\n private async processOneComponent(component: Component) {\n const idStr = component.id.toString();\n if (this.completed.includes(idStr)) return [];\n const allIds = await this.getAllDepsFiltered(component);\n\n const allDependenciesComps = await this.loadManyComponents(allIds, idStr);\n const deps = await this.dependencyResolver.getComponentDependencies(component);\n deps.forEach((dep) => {\n const depId = dep.componentId;\n if (this.ignoreIds.includes(depId.toString())) return;\n if (!this.graph.hasNode(depId.toString())) {\n if (this.shouldThrowOnMissingDep) {\n throw new Error(`buildOneComponent: missing node of ${depId.toString()}`);\n }\n this.logger.warn(`ignoring missing ${depId.toString()}`);\n return;\n }\n this.graph.setEdge(new Edge(idStr, depId.toString(), dep.lifecycle));\n });\n\n this.completed.push(idStr);\n return allDependenciesComps;\n }\n\n private async loadManyComponents(componentsIds: ComponentID[], dependenciesOf?: string): Promise<Component[]> {\n const components = await mapSeries(componentsIds, async (comp) => {\n const idStr = comp.toString();\n const fromGraph = this.graph.node(idStr)?.attr;\n if (fromGraph) return fromGraph;\n try {\n const component = await this.workspace.get(comp);\n this.graph.setNode(new Node(idStr, component));\n return component;\n } catch (err: any) {\n if (\n err instanceof ComponentNotFound ||\n err instanceof ComponentNotFoundInScope ||\n err instanceof ScopeNotFound\n ) {\n if (dependenciesOf && !this.shouldThrowOnMissingDep) {\n this.logger.warn(\n `component ${idStr}, dependency of ${dependenciesOf} was not found. continuing without it`\n );\n return null;\n }\n throw new BitError(\n `error: component \"${idStr}\" was not found.\\nthis component is a dependency of \"${\n dependenciesOf || '<none>'\n }\" and is needed as part of the graph generation`\n );\n }\n if (dependenciesOf) this.logger.error(`failed loading dependencies of ${dependenciesOf}`);\n throw err;\n }\n });\n return compact(components);\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;EAAA;EAAA;IAAA;EAAA;EAAA;AAAA;AACA;EAAA;EAAA;IAAA;EAAA;EAAA;AAAA;AACA;EAAA;EAAA;IAAA;EAAA;EAAA;AAAA;AAIA;EAAA;EAAA;IAAA;EAAA;EAAA;AAAA;AACA;EAAA;EAAA;IAAA;EAAA;EAAA;AAAA;AACA;EAAA;EAAA;IAAA;EAAA;EAAA;AAAA;AACA;EAAA;EAAA;IAAA;EAAA;EAAA;AAAA;AAEA;EAAA;EAAA;IAAA;EAAA;EAAA;AAAA;AAKO,MAAMA,kBAAkB,CAAC;EAM9BC,WAAW,CACDC,SAAoB,EACpBC,MAAc,EACdC,kBAA0C,EAC1CC,SAAmB,GAAG,EAAE,EACxBC,iBAAkC,EAClCC,uBAAuB,GAAG,IAAI,EACtC;IAAA,KANQL,SAAoB,GAApBA,SAAoB;IAAA,KACpBC,MAAc,GAAdA,MAAc;IAAA,KACdC,kBAA0C,GAA1CA,kBAA0C;IAAA,KAC1CC,SAAmB,GAAnBA,SAAmB;IAAA,KACnBC,iBAAkC,GAAlCA,iBAAkC;IAAA,KAClCC,uBAAuB,GAAvBA,uBAAuB;IAAA,+CAXjB,KAAIC,cAAK,GAAqB;IAAA,mDAChB,EAAE;IAAA,+CAChB,CAAC;IAAA;IAAA,qDAEe,EAAE;IAShC,IAAI,CAACC,QAAQ,GAAG,IAAI,CAACP,SAAS,CAACO,QAAQ;EACzC;;EAEA;AACF;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;EACE,MAAMC,UAAU,CAACC,GAAkB,EAAqC;IACtE,IAAI,CAACR,MAAM,CAACS,KAAK,CAAE,uCAAsCD,GAAG,CAACE,MAAO,UAAS,CAA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AYE,0BAAwB,IACvCF,GAAG,YAAYG,2BAAa,EAC5B;UACA,IAAIV,cAAc,IAAI,CAAC,IAAI,CAAC9E,uBAAuB,EAAE;YACnD,IAAI,CAACJ,MAAM,CAAC6E,IAAI,CACb,aAAYP,KAAM,mBAAkBY,cAAe,uCAAsC,CAC3F;YACD,OAAO,IAAI;UACb;UACA,MAAM,KAAIW,oBAAQ,EACf,qBAAoBvB,KAAM,wDACzBY,cAAc,IAAI,QACnB,iDAAgD,CAClD;QACH;QACA,IAAIA,cAAc,EAAE,IAAI,CAAClF,MAAM,CAAC8F,KAAK,CAAE,kCAAiCZ,cAAe,EAAC,CAAC;QACzF,MAAMO,GAAG;MACX;IACF,CAAC,CAAC;IACF,OAAO,IAAArD,kBAAO,EAACtB,UAAU,CAAC;EAC5B;AACF;AAAC"}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { Graph } from '@teambit/graph.cleargraph';
|
|
2
|
+
import { ComponentID } from '@teambit/component';
|
|
3
|
+
import { ComponentDependency, DependencyResolverMain } from '@teambit/dependency-resolver';
|
|
4
|
+
import { DepEdgeType } from '@teambit/graph';
|
|
5
|
+
import { Logger } from '@teambit/logger';
|
|
6
|
+
import { Workspace } from './workspace';
|
|
7
|
+
export declare function lifecycleToDepType(compDep: ComponentDependency): DepEdgeType;
|
|
8
|
+
export declare class GraphIdsFromFsBuilder {
|
|
9
|
+
private workspace;
|
|
10
|
+
private logger;
|
|
11
|
+
private dependencyResolver;
|
|
12
|
+
private shouldThrowOnMissingDep;
|
|
13
|
+
private graph;
|
|
14
|
+
private completed;
|
|
15
|
+
private depth;
|
|
16
|
+
private consumer;
|
|
17
|
+
private loadedComponents;
|
|
18
|
+
private importedIds;
|
|
19
|
+
constructor(workspace: Workspace, logger: Logger, dependencyResolver: DependencyResolverMain, shouldThrowOnMissingDep?: boolean);
|
|
20
|
+
/**
|
|
21
|
+
* create a graph with all dependencies and flattened dependencies of the given components.
|
|
22
|
+
* the nodes are component-ids and the edges has a label of the dependency type.
|
|
23
|
+
* to get some info about this the graph build take a look into build-graph-from-fs.buildGraph() docs.
|
|
24
|
+
*/
|
|
25
|
+
buildGraph(ids: ComponentID[]): Promise<Graph<ComponentID, DepEdgeType>>;
|
|
26
|
+
private processManyComponents;
|
|
27
|
+
/**
|
|
28
|
+
* only for components from the workspace that can be modified to add/remove dependencies, we need to make sure that
|
|
29
|
+
* all their dependencies are imported.
|
|
30
|
+
* once a component from scope is imported, we know that either we have its dependency graph or all flattened deps
|
|
31
|
+
*/
|
|
32
|
+
private importObjects;
|
|
33
|
+
private processOneComponent;
|
|
34
|
+
/**
|
|
35
|
+
* this is tricky.
|
|
36
|
+
* the component is in the workspace so it can be modified. dependencies can be added/removed/updated/downgraded.
|
|
37
|
+
* we have the graph-dependencies from the last snap, so we prefer to use it whenever possible for performance reasons.
|
|
38
|
+
* if we can't use it, we have to recursively load dependencies components and get the data from there.
|
|
39
|
+
* to maximize the performance, we iterate the direct dependencies, if we find a dep with the same id in the graph,
|
|
40
|
+
* then ask the graph for all its successors. otherwise, if it's not there, fallback to load the deps components.
|
|
41
|
+
*/
|
|
42
|
+
private processCompFromWorkspaceWithGraph;
|
|
43
|
+
private addDepEdge;
|
|
44
|
+
private loadManyComponents;
|
|
45
|
+
}
|
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
require("core-js/modules/es.array.iterator.js");
|
|
5
|
+
require("core-js/modules/es.promise.js");
|
|
6
|
+
Object.defineProperty(exports, "__esModule", {
|
|
7
|
+
value: true
|
|
8
|
+
});
|
|
9
|
+
exports.GraphIdsFromFsBuilder = void 0;
|
|
10
|
+
exports.lifecycleToDepType = lifecycleToDepType;
|
|
11
|
+
function _defineProperty2() {
|
|
12
|
+
const data = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
13
|
+
_defineProperty2 = function () {
|
|
14
|
+
return data;
|
|
15
|
+
};
|
|
16
|
+
return data;
|
|
17
|
+
}
|
|
18
|
+
function _pMapSeries() {
|
|
19
|
+
const data = _interopRequireDefault(require("p-map-series"));
|
|
20
|
+
_pMapSeries = function () {
|
|
21
|
+
return data;
|
|
22
|
+
};
|
|
23
|
+
return data;
|
|
24
|
+
}
|
|
25
|
+
function _graph() {
|
|
26
|
+
const data = require("@teambit/graph.cleargraph");
|
|
27
|
+
_graph = function () {
|
|
28
|
+
return data;
|
|
29
|
+
};
|
|
30
|
+
return data;
|
|
31
|
+
}
|
|
32
|
+
function _lodash() {
|
|
33
|
+
const data = require("lodash");
|
|
34
|
+
_lodash = function () {
|
|
35
|
+
return data;
|
|
36
|
+
};
|
|
37
|
+
return data;
|
|
38
|
+
}
|
|
39
|
+
function _bitIds() {
|
|
40
|
+
const data = _interopRequireDefault(require("@teambit/legacy/dist/bit-id/bit-ids"));
|
|
41
|
+
_bitIds = function () {
|
|
42
|
+
return data;
|
|
43
|
+
};
|
|
44
|
+
return data;
|
|
45
|
+
}
|
|
46
|
+
function _exceptions() {
|
|
47
|
+
const data = require("@teambit/legacy/dist/scope/exceptions");
|
|
48
|
+
_exceptions = function () {
|
|
49
|
+
return data;
|
|
50
|
+
};
|
|
51
|
+
return data;
|
|
52
|
+
}
|
|
53
|
+
function _scope() {
|
|
54
|
+
const data = require("@teambit/scope");
|
|
55
|
+
_scope = function () {
|
|
56
|
+
return data;
|
|
57
|
+
};
|
|
58
|
+
return data;
|
|
59
|
+
}
|
|
60
|
+
function _lodash2() {
|
|
61
|
+
const data = _interopRequireDefault(require("lodash.compact"));
|
|
62
|
+
_lodash2 = function () {
|
|
63
|
+
return data;
|
|
64
|
+
};
|
|
65
|
+
return data;
|
|
66
|
+
}
|
|
67
|
+
function _bitError() {
|
|
68
|
+
const data = require("@teambit/bit-error");
|
|
69
|
+
_bitError = function () {
|
|
70
|
+
return data;
|
|
71
|
+
};
|
|
72
|
+
return data;
|
|
73
|
+
}
|
|
74
|
+
function lifecycleToDepType(compDep) {
|
|
75
|
+
if (compDep.isExtension) return 'ext';
|
|
76
|
+
switch (compDep.lifecycle) {
|
|
77
|
+
case 'dev':
|
|
78
|
+
return 'dev';
|
|
79
|
+
case 'runtime':
|
|
80
|
+
return 'prod';
|
|
81
|
+
default:
|
|
82
|
+
throw new Error(`lifecycle ${compDep.lifecycle} is not support`);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
class GraphIdsFromFsBuilder {
|
|
86
|
+
constructor(workspace, logger, dependencyResolver, shouldThrowOnMissingDep = true) {
|
|
87
|
+
this.workspace = workspace;
|
|
88
|
+
this.logger = logger;
|
|
89
|
+
this.dependencyResolver = dependencyResolver;
|
|
90
|
+
this.shouldThrowOnMissingDep = shouldThrowOnMissingDep;
|
|
91
|
+
(0, _defineProperty2().default)(this, "graph", new (_graph().Graph)());
|
|
92
|
+
(0, _defineProperty2().default)(this, "completed", []);
|
|
93
|
+
(0, _defineProperty2().default)(this, "depth", 1);
|
|
94
|
+
(0, _defineProperty2().default)(this, "consumer", void 0);
|
|
95
|
+
(0, _defineProperty2().default)(this, "loadedComponents", {});
|
|
96
|
+
(0, _defineProperty2().default)(this, "importedIds", []);
|
|
97
|
+
this.consumer = this.workspace.consumer;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* create a graph with all dependencies and flattened dependencies of the given components.
|
|
102
|
+
* the nodes are component-ids and the edges has a label of the dependency type.
|
|
103
|
+
* to get some info about this the graph build take a look into build-graph-from-fs.buildGraph() docs.
|
|
104
|
+
*/
|
|
105
|
+
async buildGraph(ids) {
|
|
106
|
+
this.logger.debug(`GraphIdsFromFsBuilder, buildGraph with ${ids.length} seeders`);
|
|
107
|
+
const start = Date.now();
|
|
108
|
+
const components = await this.loadManyComponents(ids);
|
|
109
|
+
await this.processManyComponents(components);
|
|
110
|
+
this.logger.debug(`GraphIdsFromFsBuilder, buildGraph with ${ids.length} seeders completed (${(Date.now() - start) / 1000} sec)`);
|
|
111
|
+
return this.graph;
|
|
112
|
+
}
|
|
113
|
+
async processManyComponents(components) {
|
|
114
|
+
this.logger.debug(`GraphIdsFromFsBuilder.processManyComponents depth ${this.depth}, ${components.length} components`);
|
|
115
|
+
this.depth += 1;
|
|
116
|
+
await this.importObjects(components);
|
|
117
|
+
const allDependencies = await (0, _pMapSeries().default)(components, component => this.processOneComponent(component));
|
|
118
|
+
const allDependenciesFlattened = (0, _lodash().flatten)(allDependencies);
|
|
119
|
+
if (allDependenciesFlattened.length) await this.processManyComponents(allDependenciesFlattened);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* only for components from the workspace that can be modified to add/remove dependencies, we need to make sure that
|
|
124
|
+
* all their dependencies are imported.
|
|
125
|
+
* once a component from scope is imported, we know that either we have its dependency graph or all flattened deps
|
|
126
|
+
*/
|
|
127
|
+
async importObjects(components) {
|
|
128
|
+
const workspaceIds = await this.workspace.listIds();
|
|
129
|
+
const compOnWorkspaceOnly = components.filter(comp => workspaceIds.find(id => id.isEqual(comp.id)));
|
|
130
|
+
const notImported = compOnWorkspaceOnly.map(c => c.id).filter(id => !this.importedIds.includes(id.toString()));
|
|
131
|
+
const withScope = notImported.map(id => id._legacy).filter(dep => dep.hasScope());
|
|
132
|
+
const scopeComponentsImporter = this.consumer.scope.scopeImporter;
|
|
133
|
+
await scopeComponentsImporter.importMany({
|
|
134
|
+
ids: _bitIds().default.uniqFromArray(withScope),
|
|
135
|
+
throwForDependencyNotFound: this.shouldThrowOnMissingDep,
|
|
136
|
+
throwForSeederNotFound: this.shouldThrowOnMissingDep,
|
|
137
|
+
reFetchUnBuiltVersion: false,
|
|
138
|
+
preferDependencyGraph: true
|
|
139
|
+
});
|
|
140
|
+
notImported.map(id => this.importedIds.push(id.toString()));
|
|
141
|
+
}
|
|
142
|
+
async processOneComponent(component) {
|
|
143
|
+
const idStr = component.id.toString();
|
|
144
|
+
if (this.completed.includes(idStr)) return [];
|
|
145
|
+
const graphFromScope = await this.workspace.getSavedGraphOfComponentIfExist(component);
|
|
146
|
+
if (graphFromScope !== null && graphFromScope !== void 0 && graphFromScope.edges.length) {
|
|
147
|
+
const isOnWorkspace = await this.workspace.hasId(component.id);
|
|
148
|
+
if (isOnWorkspace) {
|
|
149
|
+
const allDependenciesComps = await this.processCompFromWorkspaceWithGraph(graphFromScope, component);
|
|
150
|
+
this.completed.push(idStr);
|
|
151
|
+
return allDependenciesComps;
|
|
152
|
+
}
|
|
153
|
+
this.graph.merge([graphFromScope]);
|
|
154
|
+
this.completed.push(idStr);
|
|
155
|
+
return [];
|
|
156
|
+
}
|
|
157
|
+
const deps = await this.dependencyResolver.getComponentDependencies(component);
|
|
158
|
+
const allDepsIds = deps.map(d => d.componentId);
|
|
159
|
+
const allDependenciesComps = await this.loadManyComponents(allDepsIds, idStr);
|
|
160
|
+
deps.forEach(dep => this.addDepEdge(idStr, dep));
|
|
161
|
+
this.completed.push(idStr);
|
|
162
|
+
return allDependenciesComps;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* this is tricky.
|
|
167
|
+
* the component is in the workspace so it can be modified. dependencies can be added/removed/updated/downgraded.
|
|
168
|
+
* we have the graph-dependencies from the last snap, so we prefer to use it whenever possible for performance reasons.
|
|
169
|
+
* if we can't use it, we have to recursively load dependencies components and get the data from there.
|
|
170
|
+
* to maximize the performance, we iterate the direct dependencies, if we find a dep with the same id in the graph,
|
|
171
|
+
* then ask the graph for all its successors. otherwise, if it's not there, fallback to load the deps components.
|
|
172
|
+
*/
|
|
173
|
+
async processCompFromWorkspaceWithGraph(graphFromScope, component) {
|
|
174
|
+
const deps = await this.dependencyResolver.getComponentDependencies(component);
|
|
175
|
+
const [depsInScopeGraph, depsNotInScopeGraph] = (0, _lodash().partition)(deps, dep => graphFromScope.hasNode(dep.componentId.toString()));
|
|
176
|
+
const subGraphs = depsInScopeGraph.map(dep => graphFromScope.successorsSubgraph([dep.componentId.toString()]));
|
|
177
|
+
this.graph.merge(subGraphs);
|
|
178
|
+
const allDepsIds = depsNotInScopeGraph.map(d => d.componentId);
|
|
179
|
+
const idStr = component.id.toString();
|
|
180
|
+
const allDependenciesComps = await this.loadManyComponents(allDepsIds, idStr);
|
|
181
|
+
deps.forEach(dep => this.addDepEdge(idStr, dep));
|
|
182
|
+
return allDependenciesComps;
|
|
183
|
+
}
|
|
184
|
+
addDepEdge(idStr, dep) {
|
|
185
|
+
const depId = dep.componentId;
|
|
186
|
+
if (!this.graph.hasNode(depId.toString())) {
|
|
187
|
+
if (this.shouldThrowOnMissingDep) {
|
|
188
|
+
throw new Error(`buildOneComponent: missing node of ${depId.toString()}`);
|
|
189
|
+
}
|
|
190
|
+
this.logger.warn(`ignoring missing ${depId.toString()}`);
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
this.graph.setEdge(new (_graph().Edge)(idStr, depId.toString(), lifecycleToDepType(dep)));
|
|
194
|
+
}
|
|
195
|
+
async loadManyComponents(componentsIds, dependenciesOf) {
|
|
196
|
+
const components = await (0, _pMapSeries().default)(componentsIds, async comp => {
|
|
197
|
+
const idStr = comp.toString();
|
|
198
|
+
const fromCache = this.loadedComponents[idStr];
|
|
199
|
+
if (fromCache) return fromCache;
|
|
200
|
+
try {
|
|
201
|
+
const component = await this.workspace.get(comp);
|
|
202
|
+
this.loadedComponents[idStr] = component;
|
|
203
|
+
this.graph.setNode(new (_graph().Node)(idStr, component.id));
|
|
204
|
+
return component;
|
|
205
|
+
} catch (err) {
|
|
206
|
+
if (err instanceof _exceptions().ComponentNotFound || err instanceof _scope().ComponentNotFound || err instanceof _exceptions().ScopeNotFound) {
|
|
207
|
+
if (dependenciesOf && !this.shouldThrowOnMissingDep) {
|
|
208
|
+
this.logger.warn(`component ${idStr}, dependency of ${dependenciesOf} was not found. continuing without it`);
|
|
209
|
+
return null;
|
|
210
|
+
}
|
|
211
|
+
throw new (_bitError().BitError)(`error: component "${idStr}" was not found.\nthis component is a dependency of "${dependenciesOf || '<none>'}" and is needed as part of the graph generation`);
|
|
212
|
+
}
|
|
213
|
+
if (dependenciesOf) this.logger.error(`failed loading dependencies of ${dependenciesOf}`);
|
|
214
|
+
throw err;
|
|
215
|
+
}
|
|
216
|
+
});
|
|
217
|
+
return (0, _lodash2().default)(components);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
exports.GraphIdsFromFsBuilder = GraphIdsFromFsBuilder;
|
|
221
|
+
|
|
222
|
+
//# sourceMappingURL=build-graph-ids-from-fs.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["lifecycleToDepType","compDep","isExtension","lifecycle","Error","GraphIdsFromFsBuilder","constructor","workspace","logger","dependencyResolver","shouldThrowOnMissingDep","Graph","consumer","buildGraph","ids","debug","length","start","Date","now","components","loadManyComponents","processManyComponents","graph","depth","importObjects","allDependencies","mapSeries","component","processOneComponent","allDependenciesFlattened","flatten","workspaceIds","listIds","compOnWorkspaceOnly","filter","comp","find","id","isEqual","notImported","map","c","importedIds","includes","toString","withScope","_legacy","dep","hasScope","scopeComponentsImporter","scope","scopeImporter","importMany","BitIds","uniqFromArray","throwForDependencyNotFound","throwForSeederNotFound","reFetchUnBuiltVersion","preferDependencyGraph","push","idStr","completed","graphFromScope","getSavedGraphOfComponentIfExist","edges","isOnWorkspace","hasId","allDependenciesComps","processCompFromWorkspaceWithGraph","merge","deps","getComponentDependencies","allDepsIds","d","componentId","forEach","addDepEdge","depsInScopeGraph","depsNotInScopeGraph","partition","hasNode","subGraphs","successorsSubgraph","depId","warn","setEdge","Edge","componentsIds","dependenciesOf","fromCache","loadedComponents","get","setNode","Node","err","ComponentNotFound","ComponentNotFoundInScope","ScopeNotFound","BitError","error","compact"],"sources":["build-graph-ids-from-fs.ts"],"sourcesContent":["import mapSeries from 'p-map-series';\nimport { Graph, Node, Edge } from '@teambit/graph.cleargraph';\nimport { flatten, partition } from 'lodash';\nimport { Consumer } from '@teambit/legacy/dist/consumer';\nimport { Component, ComponentID } from '@teambit/component';\nimport BitIds from '@teambit/legacy/dist/bit-id/bit-ids';\nimport { ComponentDependency, DependencyResolverMain } from '@teambit/dependency-resolver';\nimport { CompIdGraph, DepEdgeType } from '@teambit/graph';\nimport { ComponentNotFound, ScopeNotFound } from '@teambit/legacy/dist/scope/exceptions';\nimport { ComponentNotFound as ComponentNotFoundInScope } from '@teambit/scope';\nimport compact from 'lodash.compact';\nimport { Logger } from '@teambit/logger';\nimport { BitError } from '@teambit/bit-error';\nimport { Workspace } from './workspace';\n\nexport function lifecycleToDepType(compDep: ComponentDependency): DepEdgeType {\n if (compDep.isExtension) return 'ext';\n switch (compDep.lifecycle) {\n case 'dev':\n return 'dev';\n case 'runtime':\n return 'prod';\n default:\n throw new Error(`lifecycle ${compDep.lifecycle} is not support`);\n }\n}\n\nexport class GraphIdsFromFsBuilder {\n private graph = new Graph<ComponentID, DepEdgeType>();\n private completed: string[] = [];\n private depth = 1;\n private consumer: Consumer;\n private loadedComponents: { [idStr: string]: Component } = {};\n private importedIds: string[] = [];\n constructor(\n private workspace: Workspace,\n private logger: Logger,\n private dependencyResolver: DependencyResolverMain,\n private shouldThrowOnMissingDep = true\n ) {\n this.consumer = this.workspace.consumer;\n }\n\n /**\n * create a graph with all dependencies and flattened dependencies of the given components.\n * the nodes are component-ids and the edges has a label of the dependency type.\n * to get some info about this the graph build take a look into build-graph-from-fs.buildGraph() docs.\n */\n async buildGraph(ids: ComponentID[]): Promise<Graph<ComponentID, DepEdgeType>> {\n this.logger.debug(`GraphIdsFromFsBuilder, buildGraph with ${ids.length} seeders`);\n const start = Date.now();\n const components = await this.loadManyComponents(ids);\n await this.processManyComponents(components);\n this.logger.debug(\n `GraphIdsFromFsBuilder, buildGraph with ${ids.length} seeders completed (${(Date.now() - start) / 1000} sec)`\n );\n return this.graph;\n }\n\n private async processManyComponents(components: Component[]) {\n this.logger.debug(\n `GraphIdsFromFsBuilder.processManyComponents depth ${this.depth}, ${components.length} components`\n );\n this.depth += 1;\n await this.importObjects(components);\n const allDependencies = await mapSeries(components, (component) => this.processOneComponent(component));\n const allDependenciesFlattened = flatten(allDependencies);\n if (allDependenciesFlattened.length) await this.processManyComponents(allDependenciesFlattened);\n }\n\n /**\n * only for components from the workspace that can be modified to add/remove dependencies, we need to make sure that\n * all their dependencies are imported.\n * once a component from scope is imported, we know that either we have its dependency graph or all flattened deps\n */\n private async importObjects(components: Component[]) {\n const workspaceIds = await this.workspace.listIds();\n const compOnWorkspaceOnly = components.filter((comp) => workspaceIds.find((id) => id.isEqual(comp.id)));\n const notImported = compOnWorkspaceOnly.map((c) => c.id).filter((id) => !this.importedIds.includes(id.toString()));\n const withScope = notImported.map((id) => id._legacy).filter((dep) => dep.hasScope());\n const scopeComponentsImporter = this.consumer.scope.scopeImporter;\n await scopeComponentsImporter.importMany({\n ids: BitIds.uniqFromArray(withScope),\n throwForDependencyNotFound: this.shouldThrowOnMissingDep,\n throwForSeederNotFound: this.shouldThrowOnMissingDep,\n reFetchUnBuiltVersion: false,\n preferDependencyGraph: true,\n });\n notImported.map((id) => this.importedIds.push(id.toString()));\n }\n\n private async processOneComponent(component: Component) {\n const idStr = component.id.toString();\n if (this.completed.includes(idStr)) return [];\n const graphFromScope = await this.workspace.getSavedGraphOfComponentIfExist(component);\n if (graphFromScope?.edges.length) {\n const isOnWorkspace = await this.workspace.hasId(component.id);\n if (isOnWorkspace) {\n const allDependenciesComps = await this.processCompFromWorkspaceWithGraph(graphFromScope, component);\n this.completed.push(idStr);\n return allDependenciesComps;\n }\n this.graph.merge([graphFromScope]);\n this.completed.push(idStr);\n return [];\n }\n\n const deps = await this.dependencyResolver.getComponentDependencies(component);\n const allDepsIds = deps.map((d) => d.componentId);\n const allDependenciesComps = await this.loadManyComponents(allDepsIds, idStr);\n\n deps.forEach((dep) => this.addDepEdge(idStr, dep));\n this.completed.push(idStr);\n\n return allDependenciesComps;\n }\n\n /**\n * this is tricky.\n * the component is in the workspace so it can be modified. dependencies can be added/removed/updated/downgraded.\n * we have the graph-dependencies from the last snap, so we prefer to use it whenever possible for performance reasons.\n * if we can't use it, we have to recursively load dependencies components and get the data from there.\n * to maximize the performance, we iterate the direct dependencies, if we find a dep with the same id in the graph,\n * then ask the graph for all its successors. otherwise, if it's not there, fallback to load the deps components.\n */\n private async processCompFromWorkspaceWithGraph(\n graphFromScope: CompIdGraph,\n component: Component\n ): Promise<Component[]> {\n const deps = await this.dependencyResolver.getComponentDependencies(component);\n const [depsInScopeGraph, depsNotInScopeGraph] = partition(deps, (dep) =>\n graphFromScope.hasNode(dep.componentId.toString())\n );\n const subGraphs = depsInScopeGraph.map((dep) => graphFromScope.successorsSubgraph([dep.componentId.toString()]));\n this.graph.merge(subGraphs);\n\n const allDepsIds = depsNotInScopeGraph.map((d) => d.componentId);\n const idStr = component.id.toString();\n const allDependenciesComps = await this.loadManyComponents(allDepsIds, idStr);\n deps.forEach((dep) => this.addDepEdge(idStr, dep));\n return allDependenciesComps;\n }\n\n private addDepEdge(idStr: string, dep: ComponentDependency) {\n const depId = dep.componentId;\n if (!this.graph.hasNode(depId.toString())) {\n if (this.shouldThrowOnMissingDep) {\n throw new Error(`buildOneComponent: missing node of ${depId.toString()}`);\n }\n this.logger.warn(`ignoring missing ${depId.toString()}`);\n return;\n }\n this.graph.setEdge(new Edge(idStr, depId.toString(), lifecycleToDepType(dep)));\n }\n\n private async loadManyComponents(componentsIds: ComponentID[], dependenciesOf?: string): Promise<Component[]> {\n const components = await mapSeries(componentsIds, async (comp) => {\n const idStr = comp.toString();\n const fromCache = this.loadedComponents[idStr];\n if (fromCache) return fromCache;\n try {\n const component = await this.workspace.get(comp);\n this.loadedComponents[idStr] = component;\n this.graph.setNode(new Node(idStr, component.id));\n return component;\n } catch (err: any) {\n if (\n err instanceof ComponentNotFound ||\n err instanceof ComponentNotFoundInScope ||\n err instanceof ScopeNotFound\n ) {\n if (dependenciesOf && !this.shouldThrowOnMissingDep) {\n this.logger.warn(\n `component ${idStr}, dependency of ${dependenciesOf} was not found. continuing without it`\n );\n return null;\n }\n throw new BitError(\n `error: component \"${idStr}\" was not found.\\nthis component is a dependency of \"${\n dependenciesOf || '<none>'\n }\" and is needed as part of the graph generation`\n );\n }\n if (dependenciesOf) this.logger.error(`failed loading dependencies of ${dependenciesOf}`);\n throw err;\n }\n });\n return compact(components);\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AAAA;EAAA;EAAA;IAAA;EAAA;EAAA;AAAA;AACA;EAAA;EAAA;IAAA;EAAA;EAAA;AAAA;AACA;EAAA;EAAA;IAAA;EAAA;EAAA;AAAA;AAGA;EAAA;EAAA;IAAA;EAAA;EAAA;AAAA;AAGA;EAAA;EAAA;IAAA;EAAA;EAAA;AAAA;AACA;EAAA;EAAA;IAAA;EAAA;EAAA;AAAA;AACA;EAAA;EAAA;IAAA;EAAA;EAAA;AAAA;AAEA;EAAA;EAAA;IAAA;EAAA;EAAA;AAAA;AAGO,SAASA,kBAAkB,CAACC,OAA4B,EAAe;EAC5E,IAAIA,OAAO,CAACC,WAAW,EAAE,OAAO,KAAK;EACrC,QAAQD,OAAO,CAACE,SAAS;IACvB,KAAK,KAAK;MACR,OAAO,KAAK;IACd,KAAK,SAAS;MACZ,OAAO,MAAM;IACf;MACE,MAAM,IAAIC,KAAK,CAAE,aAAYH,OAAO,CAACE,SAAU,iBAAgB,CAAC;EAAC;AAEvE;AAEO,MAAME,qBAAqB,CAAC;EAOjCC,WAAW,CACDC,SAAoB,EACpBC,MAAc,EACdC,kBAA0C,EAC1CC,uBAAuB,GAAG,IAAI,EACtC;IAAA,KAJQH,SAAoB,GAApBA,SAAoB;IAAA,KACpBC,MAAc,GAAdA,MAAc;IAAA,KACdC,kBAA0C,GAA1CA,kBAA0C;IAAA,KAC1CC,uBAAuB,GAAvBA,uBAAuB;IAAA,+CAVjB,KAAIC,cAAK,GAA4B;IAAA,mDACvB,EAAE;IAAA,+CAChB,CAAC;IAAA;IAAA,0DAE0C,CAAC,CAAC;IAAA,qDAC7B,EAAE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F,CAAC,CAAC;IACF,OAAO,IAAAM,kBAAO,EAACjF,UAAU,CAAC;EAC5B;AACF;AAAC"}
|
|
@@ -309,7 +309,7 @@ class WorkspaceComponentLoader {
|
|
|
309
309
|
const envsData = await this.workspace.getEnvSystemDescriptor(component);
|
|
310
310
|
|
|
311
311
|
// Move to deps resolver main runtime once we switch ws<> deps resolver direction
|
|
312
|
-
const policy = await this.dependencyResolver.mergeVariantPolicies(component.config.extensions);
|
|
312
|
+
const policy = await this.dependencyResolver.mergeVariantPolicies(component.config.extensions, component.id._legacy);
|
|
313
313
|
const dependenciesList = await this.dependencyResolver.extractDepsFromLegacy(component, policy);
|
|
314
314
|
let dependenciesFromUnmergedHead;
|
|
315
315
|
// Get dependencies from unmerged head if needed
|