@teambit/workspace 1.0.106 → 1.0.108
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/aspects-merger.ts +319 -0
- package/bit-map.ts +277 -0
- package/build-graph-from-fs.ts +188 -0
- package/build-graph-ids-from-fs.ts +216 -0
- package/capsule.cmd.ts +217 -0
- package/constants.ts +1 -0
- package/dist/aspects-merger.js +13 -22
- package/dist/aspects-merger.js.map +1 -1
- package/dist/bit-map.d.ts +1 -1
- package/dist/build-graph-from-fs.d.ts +2 -2
- package/dist/build-graph-from-fs.js +1 -2
- package/dist/build-graph-from-fs.js.map +1 -1
- package/dist/build-graph-ids-from-fs.js +1 -1
- package/dist/build-graph-ids-from-fs.js.map +1 -1
- package/dist/capsule.cmd.d.ts +2 -2
- package/dist/capsule.cmd.js +1 -2
- package/dist/capsule.cmd.js.map +1 -1
- package/dist/component-config-file/component-config-file.d.ts +2 -2
- package/dist/component-tree.widget.d.ts +2 -2
- package/dist/eject-conf.cmd.d.ts +2 -2
- package/dist/envs-subcommands/envs-replace.cmd.d.ts +1 -1
- package/dist/envs-subcommands/envs-set.cmd.d.ts +1 -1
- package/dist/envs-subcommands/envs-unset.cmd.d.ts +1 -1
- package/dist/envs-subcommands/envs-update.cmd.d.ts +1 -1
- package/dist/filter.d.ts +1 -1
- package/dist/filter.js +1 -1
- package/dist/filter.js.map +1 -1
- package/dist/merge-conflict-file.d.ts +1 -1
- package/dist/merge-conflict-file.js +1 -2
- package/dist/merge-conflict-file.js.map +1 -1
- package/dist/on-component-events.d.ts +6 -6
- package/dist/{preview-1703505948637.js → preview-1703647408454.js} +2 -2
- package/dist/scope-subcommands/scope-set.cmd.d.ts +1 -1
- package/dist/ui/workspace/use-workspace.d.ts +8 -8
- package/dist/ui/workspace/use-workspace.js +7 -18
- package/dist/ui/workspace/use-workspace.js.map +1 -1
- package/dist/ui/workspace/workspace-model.d.ts +3 -3
- package/dist/ui/workspace/workspace-overview/workspace-overview.d.ts +2 -2
- package/dist/ui/workspace/workspace-overview/workspace-overview.js +1 -2
- package/dist/ui/workspace/workspace-overview/workspace-overview.js.map +1 -1
- package/dist/ui/workspace/workspace-provider.d.ts +3 -3
- package/dist/ui/workspace/workspace.d.ts +3 -3
- package/dist/workspace-aspects-loader.d.ts +3 -3
- package/dist/workspace-aspects-loader.js +7 -11
- package/dist/workspace-aspects-loader.js.map +1 -1
- package/dist/workspace-component/comp-files.d.ts +3 -3
- package/dist/workspace-component/component-status.d.ts +3 -3
- package/dist/workspace-component/workspace-component-loader.d.ts +5 -5
- package/dist/workspace-component/workspace-component-loader.js +31 -28
- package/dist/workspace-component/workspace-component-loader.js.map +1 -1
- package/dist/workspace.composition.d.ts +2 -2
- package/dist/workspace.d.ts +6 -6
- package/dist/workspace.graphql.d.ts +2 -2
- package/dist/workspace.graphql.js +2 -4
- package/dist/workspace.graphql.js.map +1 -1
- package/dist/workspace.js +16 -23
- package/dist/workspace.js.map +1 -1
- package/dist/workspace.provider.d.ts +12 -12
- package/dist/workspace.provider.js +8 -11
- package/dist/workspace.provider.js.map +1 -1
- package/dist/workspace.ui.drawer.d.ts +1 -1
- package/dist/workspace.ui.drawer.js +5 -6
- package/dist/workspace.ui.drawer.js.map +1 -1
- package/dist/workspace.ui.runtime.d.ts +1 -1
- package/dist/workspace.ui.runtime.js +2 -8
- package/dist/workspace.ui.runtime.js.map +1 -1
- package/eject-conf.cmd.ts +57 -0
- package/filter.ts +139 -0
- package/index.ts +22 -0
- package/merge-conflict-file.ts +129 -0
- package/on-component-events.ts +24 -0
- package/package.json +39 -46
- package/pattern.cmd.ts +53 -0
- package/tsconfig.json +16 -21
- package/types/asset.d.ts +15 -3
- package/types.ts +71 -0
- package/use.cmd.ts +25 -0
- package/workspace-aspects-loader.ts +885 -0
- package/workspace-component/workspace-component-loader.ts +25 -16
- package/workspace-section.ts +14 -0
- package/workspace.aspect.ts +9 -0
- package/workspace.graphql.ts +133 -0
- package/workspace.main.runtime.ts +56 -0
- package/workspace.provider.ts +282 -0
- package/workspace.ts +2014 -0
- package/workspace.ui-root.ts +65 -0
|
@@ -0,0 +1,188 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,216 @@
|
|
|
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
|
+
}
|
package/capsule.cmd.ts
ADDED
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
// eslint-disable-next-line max-classes-per-file
|
|
2
|
+
import { Command, CommandOptions } from '@teambit/cli';
|
|
3
|
+
import { CapsuleList, IsolateComponentsOptions, IsolatorMain } from '@teambit/isolator';
|
|
4
|
+
import type { ScopeMain } from '@teambit/scope';
|
|
5
|
+
import chalk from 'chalk';
|
|
6
|
+
|
|
7
|
+
import { Workspace } from '.';
|
|
8
|
+
|
|
9
|
+
type CreateOpts = {
|
|
10
|
+
baseDir?: string;
|
|
11
|
+
rootBaseDir?: string;
|
|
12
|
+
alwaysNew?: boolean;
|
|
13
|
+
seedersOnly?: boolean;
|
|
14
|
+
useHash?: boolean;
|
|
15
|
+
id: string;
|
|
16
|
+
installPackages?: boolean;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export class CapsuleCreateCmd implements Command {
|
|
20
|
+
name = 'create [component-id...]';
|
|
21
|
+
description = `create capsules for components`;
|
|
22
|
+
helpUrl = 'reference/build-pipeline/capsule';
|
|
23
|
+
group = 'capsules';
|
|
24
|
+
alias = '';
|
|
25
|
+
options = [
|
|
26
|
+
[
|
|
27
|
+
'b',
|
|
28
|
+
'base-dir <name>',
|
|
29
|
+
'set base dir of all capsules (hashed to create the base dir inside the root dir - host path by default)',
|
|
30
|
+
],
|
|
31
|
+
['r', 'root-base-dir <name>', 'set root base dir of all capsules (absolute path to use as root dir)'],
|
|
32
|
+
['a', 'always-new', 'create new environment for capsule'],
|
|
33
|
+
['s', 'seeders-only', 'create capsules for the seeders only (not for the entire graph)'],
|
|
34
|
+
['i', 'id <name>', 'reuse capsule of certain name'],
|
|
35
|
+
['', 'use-hash', 'whether to use hash function (of base dir) as capsules root dir name'],
|
|
36
|
+
['j', 'json', 'json format'],
|
|
37
|
+
['d', 'install-packages', 'install packages by the package-manager'],
|
|
38
|
+
['p', 'package-manager <name>', 'npm, yarn or pnpm, default to npm'],
|
|
39
|
+
] as CommandOptions;
|
|
40
|
+
|
|
41
|
+
constructor(private workspace: Workspace | undefined, private scope: ScopeMain, private isolator: IsolatorMain) {}
|
|
42
|
+
|
|
43
|
+
async create(
|
|
44
|
+
[componentIds = []]: [string[]],
|
|
45
|
+
{ baseDir, rootBaseDir, alwaysNew = false, id, installPackages = false, seedersOnly = false, useHash }: CreateOpts
|
|
46
|
+
): Promise<CapsuleList> {
|
|
47
|
+
// @todo: why it is not an array?
|
|
48
|
+
if (componentIds && !Array.isArray(componentIds)) componentIds = [componentIds];
|
|
49
|
+
let finalUseHash = useHash;
|
|
50
|
+
if (useHash === undefined) {
|
|
51
|
+
if (baseDir) {
|
|
52
|
+
finalUseHash = false;
|
|
53
|
+
} else {
|
|
54
|
+
finalUseHash = this.workspace
|
|
55
|
+
? this.workspace?.shouldUseHashForCapsules()
|
|
56
|
+
: this.scope.shouldUseHashForCapsules();
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const baseInstallOptions = { installPackages };
|
|
61
|
+
const additionalInstallOptions = this.workspace
|
|
62
|
+
? {}
|
|
63
|
+
: {
|
|
64
|
+
copyPeerToRuntimeOnRoot: true,
|
|
65
|
+
useNesting: true,
|
|
66
|
+
copyPeerToRuntimeOnComponents: true,
|
|
67
|
+
installPeersFromEnvs: true,
|
|
68
|
+
};
|
|
69
|
+
const installOptions = { ...baseInstallOptions, ...additionalInstallOptions };
|
|
70
|
+
|
|
71
|
+
const capsuleOptions: IsolateComponentsOptions = {
|
|
72
|
+
baseDir,
|
|
73
|
+
rootBaseDir,
|
|
74
|
+
installOptions,
|
|
75
|
+
alwaysNew,
|
|
76
|
+
seedersOnly,
|
|
77
|
+
includeFromNestedHosts: true,
|
|
78
|
+
name: id,
|
|
79
|
+
useHash: finalUseHash,
|
|
80
|
+
};
|
|
81
|
+
const host = this.workspace || this.scope;
|
|
82
|
+
const ids = await host.resolveMultipleComponentIds(componentIds);
|
|
83
|
+
const network = await this.isolator.isolateComponents(ids, capsuleOptions);
|
|
84
|
+
const capsules = network.graphCapsules;
|
|
85
|
+
return capsules;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
async report([componentIds]: [string[]], opts: CreateOpts) {
|
|
89
|
+
// @ts-ignore
|
|
90
|
+
const capsules = await this.create(componentIds, opts);
|
|
91
|
+
const capsuleOutput = capsules
|
|
92
|
+
.map((capsule) => `${chalk.bold(capsule.component.id.toString())} - ${capsule.path}`)
|
|
93
|
+
.join('\n');
|
|
94
|
+
const title = `${capsules.length} capsule(s) were created successfully`;
|
|
95
|
+
return `${chalk.green(title)}\n${capsuleOutput}`;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
async json([componentIds]: [string[]], opts: CreateOpts) {
|
|
99
|
+
// @ts-ignore
|
|
100
|
+
const capsules = await this.create(componentIds, opts);
|
|
101
|
+
return capsules.map((c) => ({
|
|
102
|
+
id: c.component.id.toString(),
|
|
103
|
+
path: c.path,
|
|
104
|
+
}));
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export class CapsuleListCmd implements Command {
|
|
109
|
+
name = 'list';
|
|
110
|
+
description = `list the capsules generated for this workspace`;
|
|
111
|
+
group = 'capsules';
|
|
112
|
+
alias = '';
|
|
113
|
+
options = [['j', 'json', 'json format']] as CommandOptions;
|
|
114
|
+
|
|
115
|
+
constructor(private isolator: IsolatorMain, private workspace: Workspace | undefined, private scope: ScopeMain) {}
|
|
116
|
+
|
|
117
|
+
async report() {
|
|
118
|
+
const { workspaceCapsulesRootDir, scopeAspectsCapsulesRootDir } = this.getCapsulesRootDirs();
|
|
119
|
+
const listWs = workspaceCapsulesRootDir ? await this.isolator.list(workspaceCapsulesRootDir) : undefined;
|
|
120
|
+
const listScope = await this.isolator.list(scopeAspectsCapsulesRootDir);
|
|
121
|
+
|
|
122
|
+
const hostPath = this.workspace ? this.workspace.path : this.scope.path;
|
|
123
|
+
const numOfWsCapsules = listWs ? listWs.capsules.length : listScope.capsules.length;
|
|
124
|
+
const hostType = this.workspace ? 'workspace' : 'scope';
|
|
125
|
+
|
|
126
|
+
const title = chalk.green(
|
|
127
|
+
`found ${chalk.cyan(numOfWsCapsules.toString())} capsule(s) for ${hostType}: ${chalk.cyan(hostPath)}`
|
|
128
|
+
);
|
|
129
|
+
const wsLine = listWs
|
|
130
|
+
? chalk.green(`workspace capsules root-dir: ${chalk.cyan(workspaceCapsulesRootDir)}`)
|
|
131
|
+
: undefined;
|
|
132
|
+
const scopeLine = chalk.green(`scope's aspects capsules root-dir: ${chalk.cyan(scopeAspectsCapsulesRootDir)}`);
|
|
133
|
+
const suggestLine = chalk.green(`use --json to get the list of all capsules`);
|
|
134
|
+
const lines = [title, wsLine, scopeLine, suggestLine].filter((x) => x).join('\n');
|
|
135
|
+
|
|
136
|
+
// TODO: improve output
|
|
137
|
+
return lines;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
async json() {
|
|
141
|
+
const rootDirs = this.getCapsulesRootDirs();
|
|
142
|
+
const listWs = rootDirs.workspaceCapsulesRootDir
|
|
143
|
+
? await this.isolator.list(rootDirs.workspaceCapsulesRootDir)
|
|
144
|
+
: undefined;
|
|
145
|
+
const listScope = await this.isolator.list(rootDirs.scopeAspectsCapsulesRootDir);
|
|
146
|
+
const capsules = listWs ? listWs.capsules : [];
|
|
147
|
+
const scopeCapsules = listScope ? listScope.capsules : [];
|
|
148
|
+
return { ...rootDirs, capsules, scopeCapsules };
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
private getCapsulesRootDirs() {
|
|
152
|
+
return getCapsulesRootDirs(this.isolator, this.scope, this.workspace);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
export class CapsuleDeleteCmd implements Command {
|
|
157
|
+
name = 'delete';
|
|
158
|
+
description = `delete capsules`;
|
|
159
|
+
extendedDescription = `with no args, only workspace's capsules are deleted`;
|
|
160
|
+
group = 'capsules';
|
|
161
|
+
alias = '';
|
|
162
|
+
options = [
|
|
163
|
+
['', 'scope-aspects', 'delete scope-aspects capsules'],
|
|
164
|
+
['a', 'all', 'delete all capsules for all workspaces and scopes'],
|
|
165
|
+
] as CommandOptions;
|
|
166
|
+
|
|
167
|
+
constructor(private isolator: IsolatorMain, private scope: ScopeMain, private workspace?: Workspace) {}
|
|
168
|
+
|
|
169
|
+
async report(args: [], { all, scopeAspects }: { all: boolean; scopeAspects: boolean }) {
|
|
170
|
+
const capsuleBaseDirToDelete = (): string | undefined => {
|
|
171
|
+
if (all) return undefined;
|
|
172
|
+
if (scopeAspects) {
|
|
173
|
+
const { scopeAspectsCapsulesRootDir } = getCapsulesRootDirs(this.isolator, this.scope, this.workspace);
|
|
174
|
+
return scopeAspectsCapsulesRootDir;
|
|
175
|
+
}
|
|
176
|
+
return undefined;
|
|
177
|
+
};
|
|
178
|
+
const capsuleBaseDir = capsuleBaseDirToDelete();
|
|
179
|
+
const deletedDir = await this.isolator.deleteCapsules(capsuleBaseDir);
|
|
180
|
+
return chalk.green(`the following capsules dir has been deleted ${chalk.bold(deletedDir)}`);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
export class CapsuleCmd implements Command {
|
|
185
|
+
name = 'capsule';
|
|
186
|
+
description = 'manage capsules';
|
|
187
|
+
extendedDescription = `a capsule is a directory containing the component code, isolated from the workspace.
|
|
188
|
+
normally, capsules are created during the build process, the component files are copied and the packages are installed
|
|
189
|
+
via the configured package-manager. the purpose is to compile/test them in isolation to make sure they will work for
|
|
190
|
+
other users after publishing/exporting them.`;
|
|
191
|
+
alias = '';
|
|
192
|
+
group = 'capsules';
|
|
193
|
+
commands: Command[] = [];
|
|
194
|
+
options = [['j', 'json', 'json format']] as CommandOptions;
|
|
195
|
+
|
|
196
|
+
constructor(private isolator: IsolatorMain, private workspace: Workspace | undefined, private scope: ScopeMain) {}
|
|
197
|
+
|
|
198
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
199
|
+
async report(args: [string]) {
|
|
200
|
+
return new CapsuleListCmd(this.isolator, this.workspace, this.scope).report();
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
function getCapsulesRootDirs(isolator, scope, workspace) {
|
|
205
|
+
const workspaceCapsulesRootDir = workspace
|
|
206
|
+
? isolator.getCapsulesRootDir({
|
|
207
|
+
baseDir: workspace.getCapsulePath(),
|
|
208
|
+
useHash: workspace.shouldUseHashForCapsules(),
|
|
209
|
+
})
|
|
210
|
+
: undefined;
|
|
211
|
+
const scopeAspectsCapsulesRootDir = isolator.getCapsulesRootDir({
|
|
212
|
+
baseDir: scope.getAspectCapsulePath(),
|
|
213
|
+
useHash: scope.shouldUseHashForCapsules(),
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
return { workspaceCapsulesRootDir, scopeAspectsCapsulesRootDir };
|
|
217
|
+
}
|
package/constants.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const EXT_NAME = 'teambit.workspace/workspace';
|