@teambit/isolator 0.0.551 → 0.0.556
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/capsule/capsule.d.ts +39 -1
- package/dist/isolator.main.runtime.d.ts +46 -0
- package/dist/network.d.ts +7 -0
- package/package.json +18 -12
- package/capsule/capsule.ts +0 -138
- package/capsule/container-exec.ts +0 -31
- package/capsule/container.ts +0 -128
- package/capsule/index.ts +0 -3
- package/capsule-list.ts +0 -33
- package/index.ts +0 -6
- package/isolator.aspect.ts +0 -7
- package/isolator.docs.md +0 -41
- package/isolator.main.runtime.ts +0 -520
- package/network.ts +0 -35
- package/package-tar/teambit-isolator-0.0.551.tgz +0 -0
- package/symlink-bit-legacy-to-capsules.ts +0 -53
- package/symlink-dependencies-to-capsules.ts +0 -58
- package/tsconfig.json +0 -35
- package/types/asset.d.ts +0 -29
- package/types/style.d.ts +0 -42
package/isolator.main.runtime.ts
DELETED
@@ -1,520 +0,0 @@
|
|
1
|
-
import { MainRuntime } from '@teambit/cli';
|
2
|
-
import { compact, pick } from 'lodash';
|
3
|
-
import { Component, ComponentMap, ComponentAspect, ComponentID } from '@teambit/component';
|
4
|
-
import type { ComponentMain, ComponentFactory } from '@teambit/component';
|
5
|
-
import { getComponentPackageVersion } from '@teambit/component-package-version';
|
6
|
-
import { GraphAspect } from '@teambit/graph';
|
7
|
-
import type { GraphBuilder } from '@teambit/graph';
|
8
|
-
import {
|
9
|
-
DependencyResolverAspect,
|
10
|
-
DependencyResolverMain,
|
11
|
-
LinkingOptions,
|
12
|
-
WorkspacePolicy,
|
13
|
-
InstallOptions,
|
14
|
-
DependencyList,
|
15
|
-
ComponentDependency,
|
16
|
-
KEY_NAME_BY_LIFECYCLE_TYPE,
|
17
|
-
} from '@teambit/dependency-resolver';
|
18
|
-
import legacyLogger from '@teambit/legacy/dist/logger/logger';
|
19
|
-
import { Logger, LoggerAspect, LoggerMain } from '@teambit/logger';
|
20
|
-
import { BitIds } from '@teambit/legacy/dist/bit-id';
|
21
|
-
import LegacyScope from '@teambit/legacy/dist/scope/scope';
|
22
|
-
import GlobalConfigAspect, { GlobalConfigMain } from '@teambit/global-config';
|
23
|
-
import {
|
24
|
-
CACHE_ROOT,
|
25
|
-
DEPENDENCIES_FIELDS,
|
26
|
-
PACKAGE_JSON,
|
27
|
-
CFG_CAPSULES_ROOT_BASE_DIR,
|
28
|
-
} from '@teambit/legacy/dist/constants';
|
29
|
-
import ConsumerComponent from '@teambit/legacy/dist/consumer/component';
|
30
|
-
import PackageJsonFile from '@teambit/legacy/dist/consumer/component/package-json-file';
|
31
|
-
import { importMultipleDistsArtifacts } from '@teambit/legacy/dist/consumer/component/sources/artifact-files';
|
32
|
-
import { PathOsBasedAbsolute } from '@teambit/legacy/dist/utils/path';
|
33
|
-
import { Scope } from '@teambit/legacy/dist/scope';
|
34
|
-
import fs from 'fs-extra';
|
35
|
-
import hash from 'object-hash';
|
36
|
-
import path from 'path';
|
37
|
-
import equals from 'ramda/src/equals';
|
38
|
-
import BitMap from '@teambit/legacy/dist/consumer/bit-map';
|
39
|
-
import ComponentWriter, { ComponentWriterProps } from '@teambit/legacy/dist/consumer/component-ops/component-writer';
|
40
|
-
import { Capsule } from './capsule';
|
41
|
-
import CapsuleList from './capsule-list';
|
42
|
-
import { IsolatorAspect } from './isolator.aspect';
|
43
|
-
import { symlinkBitLegacyToCapsules } from './symlink-bit-legacy-to-capsules';
|
44
|
-
import { symlinkOnCapsuleRoot, symlinkDependenciesToCapsules } from './symlink-dependencies-to-capsules';
|
45
|
-
import { Network } from './network';
|
46
|
-
|
47
|
-
const DEFAULT_CAPSULES_BASE_DIR = path.join(CACHE_ROOT, 'capsules'); // TODO: move elsewhere
|
48
|
-
|
49
|
-
export type ListResults = {
|
50
|
-
workspace: string;
|
51
|
-
capsules: string[];
|
52
|
-
};
|
53
|
-
|
54
|
-
export type IsolateComponentsInstallOptions = {
|
55
|
-
installPackages?: boolean; // default: true
|
56
|
-
// TODO: add back when depResolver.getInstaller support it
|
57
|
-
// packageManager?: string;
|
58
|
-
dedupe?: boolean;
|
59
|
-
copyPeerToRuntimeOnComponents?: boolean;
|
60
|
-
copyPeerToRuntimeOnRoot?: boolean;
|
61
|
-
installTeambitBit?: boolean;
|
62
|
-
};
|
63
|
-
|
64
|
-
type CreateGraphOptions = {
|
65
|
-
/**
|
66
|
-
* include components that exists in nested hosts. for example include components that exist in scope but not in the workspace
|
67
|
-
*/
|
68
|
-
includeFromNestedHosts?: boolean;
|
69
|
-
|
70
|
-
/**
|
71
|
-
* Force specific host to get the component from.
|
72
|
-
*/
|
73
|
-
host?: ComponentFactory;
|
74
|
-
};
|
75
|
-
|
76
|
-
export type IsolateComponentsOptions = CreateGraphOptions & {
|
77
|
-
name?: string;
|
78
|
-
/**
|
79
|
-
* absolute path to put all the capsules dirs inside.
|
80
|
-
*/
|
81
|
-
rootBaseDir?: string;
|
82
|
-
|
83
|
-
/**
|
84
|
-
* the capsule root-dir based on a *hash* of this baseDir, not on the baseDir itself.
|
85
|
-
* A folder with this hash as its name will be created in the rootBaseDir
|
86
|
-
* By default this value will be the host path
|
87
|
-
*/
|
88
|
-
baseDir?: string;
|
89
|
-
|
90
|
-
/**
|
91
|
-
* create a new capsule with a random string attached to the path suffix
|
92
|
-
*/
|
93
|
-
alwaysNew?: boolean;
|
94
|
-
|
95
|
-
/**
|
96
|
-
* installation options
|
97
|
-
*/
|
98
|
-
installOptions?: IsolateComponentsInstallOptions;
|
99
|
-
|
100
|
-
linkingOptions?: LinkingOptions;
|
101
|
-
|
102
|
-
/**
|
103
|
-
* delete the capsule rootDir first. it makes sure that the isolation process starts fresh with
|
104
|
-
* no previous capsules. for build and tag this is true.
|
105
|
-
*/
|
106
|
-
emptyRootDir?: boolean;
|
107
|
-
|
108
|
-
/**
|
109
|
-
* skip the reproduction of the capsule in case it exists.
|
110
|
-
*/
|
111
|
-
skipIfExists?: boolean;
|
112
|
-
|
113
|
-
/**
|
114
|
-
* get existing capsule without doing any changes, no writes, no installations.
|
115
|
-
*/
|
116
|
-
getExistingAsIs?: boolean;
|
117
|
-
|
118
|
-
/**
|
119
|
-
* place the package-manager cache on the capsule-root
|
120
|
-
*/
|
121
|
-
cachePackagesOnCapsulesRoot?: boolean;
|
122
|
-
|
123
|
-
/**
|
124
|
-
* do not build graph with all dependencies. isolate the seeders only.
|
125
|
-
*/
|
126
|
-
seedersOnly?: boolean;
|
127
|
-
|
128
|
-
/**
|
129
|
-
* Force specific host to get the component from.
|
130
|
-
*/
|
131
|
-
host?: ComponentFactory;
|
132
|
-
};
|
133
|
-
|
134
|
-
type CapsulePackageJsonData = {
|
135
|
-
capsule: Capsule;
|
136
|
-
currentPackageJson?: Record<string, any>;
|
137
|
-
previousPackageJson: Record<string, any> | null;
|
138
|
-
};
|
139
|
-
|
140
|
-
const DEFAULT_ISOLATE_INSTALL_OPTIONS: IsolateComponentsInstallOptions = {
|
141
|
-
installPackages: true,
|
142
|
-
dedupe: true,
|
143
|
-
copyPeerToRuntimeOnComponents: false,
|
144
|
-
copyPeerToRuntimeOnRoot: true,
|
145
|
-
};
|
146
|
-
|
147
|
-
export class IsolatorMain {
|
148
|
-
static runtime = MainRuntime;
|
149
|
-
static dependencies = [DependencyResolverAspect, LoggerAspect, ComponentAspect, GraphAspect, GlobalConfigAspect];
|
150
|
-
static defaultConfig = {};
|
151
|
-
_componentsPackagesVersionCache: { [idStr: string]: string } = {}; // cache packages versions of components
|
152
|
-
|
153
|
-
static async provider([dependencyResolver, loggerExtension, componentAspect, graphAspect, globalConfig]: [
|
154
|
-
DependencyResolverMain,
|
155
|
-
LoggerMain,
|
156
|
-
ComponentMain,
|
157
|
-
GraphBuilder,
|
158
|
-
GlobalConfigMain
|
159
|
-
]): Promise<IsolatorMain> {
|
160
|
-
const logger = loggerExtension.createLogger(IsolatorAspect.id);
|
161
|
-
const isolator = new IsolatorMain(dependencyResolver, logger, componentAspect, graphAspect, globalConfig);
|
162
|
-
return isolator;
|
163
|
-
}
|
164
|
-
constructor(
|
165
|
-
private dependencyResolver: DependencyResolverMain,
|
166
|
-
private logger: Logger,
|
167
|
-
private componentAspect: ComponentMain,
|
168
|
-
private graphBuilder: GraphBuilder,
|
169
|
-
private globalConfig: GlobalConfigMain
|
170
|
-
) {}
|
171
|
-
|
172
|
-
// TODO: the legacy scope used for the component writer, which then decide if it need to write the artifacts and dists
|
173
|
-
// TODO: we should think of another way to provide it (maybe a new opts) then take the scope internally from the host
|
174
|
-
async isolateComponents(
|
175
|
-
seeders: ComponentID[],
|
176
|
-
opts: IsolateComponentsOptions = {},
|
177
|
-
legacyScope?: LegacyScope
|
178
|
-
): Promise<Network> {
|
179
|
-
const host = this.componentAspect.getHost();
|
180
|
-
const longProcessLogger = this.logger.createLongProcessLogger('create capsules network');
|
181
|
-
legacyLogger.debug(
|
182
|
-
`isolatorExt, createNetwork ${seeders.join(', ')}. opts: ${JSON.stringify(
|
183
|
-
Object.assign({}, opts, { host: opts.host?.name })
|
184
|
-
)}`
|
185
|
-
);
|
186
|
-
const createGraphOpts = pick(opts, ['includeFromNestedHosts', 'host']);
|
187
|
-
const componentsToIsolate = opts.seedersOnly
|
188
|
-
? await host.getMany(seeders)
|
189
|
-
: await this.createGraph(seeders, createGraphOpts);
|
190
|
-
opts.baseDir = opts.baseDir || host.path;
|
191
|
-
const capsuleList = await this.createCapsules(componentsToIsolate, opts, legacyScope);
|
192
|
-
longProcessLogger.end();
|
193
|
-
this.logger.consoleSuccess();
|
194
|
-
return new Network(capsuleList, seeders, this.getCapsulesRootDir(opts.baseDir, opts.rootBaseDir));
|
195
|
-
}
|
196
|
-
|
197
|
-
private async createGraph(seeders: ComponentID[], opts: CreateGraphOptions = {}): Promise<Component[]> {
|
198
|
-
const host = this.componentAspect.getHost();
|
199
|
-
const getGraphOpts = pick(opts, ['host']);
|
200
|
-
const graph = await this.graphBuilder.getGraph(seeders, getGraphOpts);
|
201
|
-
const successorsSubgraph = graph.successorsSubgraph(seeders.map((id) => id.toString()));
|
202
|
-
const compsAndDeps = successorsSubgraph.nodes.map((node) => node.attr);
|
203
|
-
// do not ignore the version here. a component might be in .bitmap with one version and
|
204
|
-
// installed as a package with another version. we don't want them both.
|
205
|
-
const existingCompsP = compsAndDeps.map(async (c) => {
|
206
|
-
let existing;
|
207
|
-
if (opts.includeFromNestedHosts) {
|
208
|
-
existing = await host.hasIdNested(c.id, true);
|
209
|
-
} else {
|
210
|
-
existing = await host.hasId(c.id);
|
211
|
-
}
|
212
|
-
if (existing) return c;
|
213
|
-
return undefined;
|
214
|
-
});
|
215
|
-
const existingComps = await Promise.all(existingCompsP);
|
216
|
-
return compact(existingComps);
|
217
|
-
}
|
218
|
-
|
219
|
-
/**
|
220
|
-
* Create capsules for the provided components
|
221
|
-
* do not use this outside directly, use isolate components which build the entire network
|
222
|
-
* @param components
|
223
|
-
* @param opts
|
224
|
-
* @param legacyScope
|
225
|
-
*/
|
226
|
-
private async createCapsules(
|
227
|
-
components: Component[],
|
228
|
-
opts: IsolateComponentsOptions,
|
229
|
-
legacyScope?: Scope
|
230
|
-
): Promise<CapsuleList> {
|
231
|
-
const config = { installPackages: true, ...opts };
|
232
|
-
const capsulesDir = this.getCapsulesRootDir(opts.baseDir as string, opts.rootBaseDir);
|
233
|
-
if (opts.emptyRootDir) {
|
234
|
-
await fs.emptyDir(capsulesDir);
|
235
|
-
}
|
236
|
-
const capsules = await this.createCapsulesFromComponents(components, capsulesDir, config);
|
237
|
-
const capsuleList = CapsuleList.fromArray(capsules);
|
238
|
-
if (opts.getExistingAsIs) {
|
239
|
-
return capsuleList;
|
240
|
-
}
|
241
|
-
|
242
|
-
if (opts.skipIfExists) {
|
243
|
-
const existingCapsules = CapsuleList.fromArray(
|
244
|
-
capsuleList.filter((capsule) => capsule.fs.existsSync('package.json'))
|
245
|
-
);
|
246
|
-
|
247
|
-
if (existingCapsules.length === capsuleList.length) return existingCapsules;
|
248
|
-
}
|
249
|
-
const capsulesWithPackagesData = await this.getCapsulesPreviousPackageJson(capsules);
|
250
|
-
|
251
|
-
await this.writeComponentsInCapsules(components, capsuleList, legacyScope);
|
252
|
-
await this.updateWithCurrentPackageJsonData(capsulesWithPackagesData, capsuleList);
|
253
|
-
const installOptions = Object.assign({}, DEFAULT_ISOLATE_INSTALL_OPTIONS, opts.installOptions || {});
|
254
|
-
if (installOptions.installPackages) {
|
255
|
-
await this.installInCapsules(capsulesDir, capsuleList, installOptions, opts.cachePackagesOnCapsulesRoot ?? false);
|
256
|
-
await this.linkInCapsules(capsulesDir, capsuleList, capsulesWithPackagesData, opts.linkingOptions ?? {});
|
257
|
-
}
|
258
|
-
|
259
|
-
// rewrite the package-json with the component dependencies in it. the original package.json
|
260
|
-
// that was written before, didn't have these dependencies in order for the package-manager to
|
261
|
-
// be able to install them without crushing when the versions don't exist yet
|
262
|
-
capsulesWithPackagesData.forEach((capsuleWithPackageData) => {
|
263
|
-
capsuleWithPackageData.capsule.fs.writeFileSync(
|
264
|
-
PACKAGE_JSON,
|
265
|
-
JSON.stringify(capsuleWithPackageData.currentPackageJson, null, 2)
|
266
|
-
);
|
267
|
-
});
|
268
|
-
|
269
|
-
return capsuleList;
|
270
|
-
}
|
271
|
-
|
272
|
-
private async installInCapsules(
|
273
|
-
capsulesDir: string,
|
274
|
-
capsuleList: CapsuleList,
|
275
|
-
isolateInstallOptions: IsolateComponentsInstallOptions,
|
276
|
-
cachePackagesOnCapsulesRoot: boolean
|
277
|
-
) {
|
278
|
-
const installer = this.dependencyResolver.getInstaller({
|
279
|
-
rootDir: capsulesDir,
|
280
|
-
cacheRootDirectory: cachePackagesOnCapsulesRoot ? capsulesDir : undefined,
|
281
|
-
});
|
282
|
-
// When using isolator we don't want to use the policy defined in the workspace directly,
|
283
|
-
// we only want to instal deps from components and the peer from the workspace
|
284
|
-
|
285
|
-
const peerOnlyPolicy = this.getPeersOnlyPolicy();
|
286
|
-
const installOptions: InstallOptions = {
|
287
|
-
installTeambitBit: !!isolateInstallOptions.installTeambitBit,
|
288
|
-
};
|
289
|
-
const packageManagerInstallOptions = {
|
290
|
-
dedupe: isolateInstallOptions.dedupe,
|
291
|
-
copyPeerToRuntimeOnComponents: isolateInstallOptions.copyPeerToRuntimeOnComponents,
|
292
|
-
copyPeerToRuntimeOnRoot: isolateInstallOptions.copyPeerToRuntimeOnRoot,
|
293
|
-
};
|
294
|
-
await installer.install(
|
295
|
-
capsulesDir,
|
296
|
-
peerOnlyPolicy,
|
297
|
-
this.toComponentMap(capsuleList),
|
298
|
-
installOptions,
|
299
|
-
packageManagerInstallOptions
|
300
|
-
);
|
301
|
-
}
|
302
|
-
|
303
|
-
private async linkInCapsules(
|
304
|
-
capsulesDir: string,
|
305
|
-
capsuleList: CapsuleList,
|
306
|
-
capsulesWithPackagesData: CapsulePackageJsonData[],
|
307
|
-
linkingOptions: LinkingOptions
|
308
|
-
) {
|
309
|
-
const linker = this.dependencyResolver.getLinker({
|
310
|
-
rootDir: capsulesDir,
|
311
|
-
linkingOptions,
|
312
|
-
});
|
313
|
-
const peerOnlyPolicy = this.getPeersOnlyPolicy();
|
314
|
-
const capsulesWithModifiedPackageJson = this.getCapsulesWithModifiedPackageJson(capsulesWithPackagesData);
|
315
|
-
await linker.link(capsulesDir, peerOnlyPolicy, this.toComponentMap(capsuleList), {
|
316
|
-
...linkingOptions,
|
317
|
-
legacyLink: false,
|
318
|
-
});
|
319
|
-
await symlinkOnCapsuleRoot(capsuleList, this.logger, capsulesDir);
|
320
|
-
await symlinkDependenciesToCapsules(capsulesWithModifiedPackageJson, capsuleList, this.logger);
|
321
|
-
// TODO: this is a hack to have access to the bit bin project in order to access core extensions from user extension
|
322
|
-
// TODO: remove this after exporting core extensions as components
|
323
|
-
await symlinkBitLegacyToCapsules(capsulesWithModifiedPackageJson, this.logger);
|
324
|
-
// await copyBitLegacyToCapsuleRoot(capsulesDir, this.logger);
|
325
|
-
}
|
326
|
-
|
327
|
-
private getCapsulesWithModifiedPackageJson(capsulesWithPackagesData: CapsulePackageJsonData[]) {
|
328
|
-
const capsulesWithModifiedPackageJson: Capsule[] = capsulesWithPackagesData
|
329
|
-
.filter((capsuleWithPackageData) => {
|
330
|
-
const packageJsonHasChanged = this.wereDependenciesInPackageJsonChanged(capsuleWithPackageData);
|
331
|
-
// @todo: when a component is tagged, it changes all package-json of its dependents, but it
|
332
|
-
// should not trigger any "npm install" because they dependencies are symlinked by us
|
333
|
-
return packageJsonHasChanged;
|
334
|
-
})
|
335
|
-
.map((capsuleWithPackageData) => capsuleWithPackageData.capsule);
|
336
|
-
return capsulesWithModifiedPackageJson;
|
337
|
-
}
|
338
|
-
|
339
|
-
private async writeComponentsInCapsules(components: Component[], capsuleList: CapsuleList, legacyScope?: Scope) {
|
340
|
-
const legacyComponents = components.map((component) => component.state._consumer.clone());
|
341
|
-
if (legacyScope) await importMultipleDistsArtifacts(legacyScope, legacyComponents);
|
342
|
-
const allIds = BitIds.fromArray(legacyComponents.map((c) => c.id));
|
343
|
-
await Promise.all(
|
344
|
-
components.map(async (component) => {
|
345
|
-
const capsule = capsuleList.getCapsule(component.id);
|
346
|
-
if (!capsule) return;
|
347
|
-
const params = this.getComponentWriteParams(component.state._consumer, allIds, legacyScope);
|
348
|
-
const componentWriter = new ComponentWriter(params);
|
349
|
-
await componentWriter.populateComponentsFilesToWrite();
|
350
|
-
await component.state._consumer.dataToPersist.persistAllToCapsule(capsule, { keepExistingCapsule: true });
|
351
|
-
})
|
352
|
-
);
|
353
|
-
}
|
354
|
-
|
355
|
-
private getPeersOnlyPolicy(): WorkspacePolicy {
|
356
|
-
const workspacePolicy = this.dependencyResolver.getWorkspacePolicy();
|
357
|
-
const peerOnlyPolicy = workspacePolicy.byLifecycleType('peer');
|
358
|
-
return peerOnlyPolicy;
|
359
|
-
}
|
360
|
-
|
361
|
-
private getComponentWriteParams(
|
362
|
-
component: ConsumerComponent,
|
363
|
-
ids: BitIds,
|
364
|
-
legacyScope?: Scope
|
365
|
-
): ComponentWriterProps {
|
366
|
-
return {
|
367
|
-
component,
|
368
|
-
// @ts-ignore
|
369
|
-
bitMap: new BitMap(undefined, undefined, undefined, false),
|
370
|
-
writeToPath: '.',
|
371
|
-
origin: 'IMPORTED',
|
372
|
-
consumer: undefined,
|
373
|
-
scope: legacyScope,
|
374
|
-
override: false,
|
375
|
-
writePackageJson: true,
|
376
|
-
writeConfig: false,
|
377
|
-
ignoreBitDependencies: ids,
|
378
|
-
excludeRegistryPrefix: false,
|
379
|
-
isolated: true,
|
380
|
-
};
|
381
|
-
}
|
382
|
-
|
383
|
-
private toComponentMap(capsuleList: CapsuleList): ComponentMap<string> {
|
384
|
-
const tuples: [Component, string][] = capsuleList.map((capsule) => {
|
385
|
-
return [capsule.component, capsule.path];
|
386
|
-
});
|
387
|
-
|
388
|
-
return ComponentMap.create(tuples);
|
389
|
-
}
|
390
|
-
|
391
|
-
async list(workspacePath: string): Promise<ListResults> {
|
392
|
-
try {
|
393
|
-
const workspaceCapsuleFolder = this.getCapsulesRootDir(workspacePath);
|
394
|
-
const capsules = await fs.readdir(workspaceCapsuleFolder);
|
395
|
-
const capsuleFullPaths = capsules.map((c) => path.join(workspaceCapsuleFolder, c));
|
396
|
-
return {
|
397
|
-
workspace: workspacePath,
|
398
|
-
capsules: capsuleFullPaths,
|
399
|
-
};
|
400
|
-
} catch (e: any) {
|
401
|
-
if (e.code === 'ENOENT') {
|
402
|
-
return { workspace: workspacePath, capsules: [] };
|
403
|
-
}
|
404
|
-
throw e;
|
405
|
-
}
|
406
|
-
}
|
407
|
-
|
408
|
-
getCapsulesRootDir(baseDir: string, rootBaseDir?: string): PathOsBasedAbsolute {
|
409
|
-
const capsulesRootBaseDir =
|
410
|
-
rootBaseDir || this.globalConfig.getSync(CFG_CAPSULES_ROOT_BASE_DIR) || DEFAULT_CAPSULES_BASE_DIR;
|
411
|
-
return path.join(capsulesRootBaseDir, hash(baseDir));
|
412
|
-
}
|
413
|
-
|
414
|
-
private async createCapsulesFromComponents(
|
415
|
-
components: Component[],
|
416
|
-
baseDir: string,
|
417
|
-
opts: IsolateComponentsOptions
|
418
|
-
): Promise<Capsule[]> {
|
419
|
-
const capsules: Capsule[] = await Promise.all(
|
420
|
-
components.map((component: Component) => {
|
421
|
-
return Capsule.createFromComponent(component, baseDir, opts);
|
422
|
-
})
|
423
|
-
);
|
424
|
-
return capsules;
|
425
|
-
}
|
426
|
-
|
427
|
-
private wereDependenciesInPackageJsonChanged(capsuleWithPackageData: CapsulePackageJsonData): boolean {
|
428
|
-
const { previousPackageJson, currentPackageJson } = capsuleWithPackageData;
|
429
|
-
if (!previousPackageJson) return true;
|
430
|
-
// @ts-ignore at this point, currentPackageJson is set
|
431
|
-
return DEPENDENCIES_FIELDS.some((field) => !equals(previousPackageJson[field], currentPackageJson[field]));
|
432
|
-
}
|
433
|
-
|
434
|
-
private async getCapsulesPreviousPackageJson(capsules: Capsule[]): Promise<CapsulePackageJsonData[]> {
|
435
|
-
return Promise.all(
|
436
|
-
capsules.map(async (capsule) => {
|
437
|
-
const packageJsonPath = path.join(capsule.path, 'package.json');
|
438
|
-
let previousPackageJson: any = null;
|
439
|
-
try {
|
440
|
-
const previousPackageJsonRaw = await capsule.fs.promises.readFile(packageJsonPath, { encoding: 'utf8' });
|
441
|
-
previousPackageJson = JSON.parse(previousPackageJsonRaw);
|
442
|
-
} catch (e: any) {
|
443
|
-
// package-json doesn't exist in the capsule, that's fine, it'll be considered as a cache miss
|
444
|
-
}
|
445
|
-
return {
|
446
|
-
capsule,
|
447
|
-
previousPackageJson,
|
448
|
-
};
|
449
|
-
})
|
450
|
-
);
|
451
|
-
}
|
452
|
-
|
453
|
-
private async updateWithCurrentPackageJsonData(
|
454
|
-
capsulesWithPackagesData: CapsulePackageJsonData[],
|
455
|
-
capsules: CapsuleList
|
456
|
-
) {
|
457
|
-
const updateP = capsules.map(async (capsule) => {
|
458
|
-
const packageJson = await this.getCurrentPackageJson(capsule, capsules);
|
459
|
-
const found = capsulesWithPackagesData.find((c) => c.capsule.component.id.isEqual(capsule.component.id));
|
460
|
-
if (!found) throw new Error(`updateWithCurrentPackageJsonData unable to find ${capsule.component.id}`);
|
461
|
-
found.currentPackageJson = packageJson.packageJsonObject;
|
462
|
-
});
|
463
|
-
return Promise.all(updateP);
|
464
|
-
}
|
465
|
-
|
466
|
-
private async getCurrentPackageJson(capsule: Capsule, capsules: CapsuleList): Promise<PackageJsonFile> {
|
467
|
-
const component: Component = capsule.component;
|
468
|
-
const currentVersion = await this.getComponentPackageVersionWithCache(component);
|
469
|
-
// const newVersion = '0.0.1-new';
|
470
|
-
const getComponentDepsManifest = async (dependencies: DependencyList) => {
|
471
|
-
const manifest = {
|
472
|
-
dependencies: {},
|
473
|
-
devDependencies: {},
|
474
|
-
};
|
475
|
-
const compDeps = dependencies.toTypeArray<ComponentDependency>('component');
|
476
|
-
const promises = compDeps.map(async (dep) => {
|
477
|
-
const depCapsule = capsules.getCapsule(dep.componentId);
|
478
|
-
let version = dep.version;
|
479
|
-
if (depCapsule) {
|
480
|
-
version = await this.getComponentPackageVersionWithCache(depCapsule?.component);
|
481
|
-
}
|
482
|
-
const keyName = KEY_NAME_BY_LIFECYCLE_TYPE[dep.lifecycle];
|
483
|
-
const entry = dep.toManifest();
|
484
|
-
if (entry) {
|
485
|
-
manifest[keyName][entry.packageName] = version;
|
486
|
-
}
|
487
|
-
});
|
488
|
-
await Promise.all(promises);
|
489
|
-
return manifest;
|
490
|
-
};
|
491
|
-
const deps = await this.dependencyResolver.getDependencies(component);
|
492
|
-
const manifest = await getComponentDepsManifest(deps);
|
493
|
-
|
494
|
-
// unfortunately, component.packageJsonFile is not available here.
|
495
|
-
// the reason is that `writeComponentsToCapsules` clones the component before writing them
|
496
|
-
// also, don't use `PackageJsonFile.createFromComponent`, as it looses the intermediate changes
|
497
|
-
// such as postInstall scripts for custom-module-resolution.
|
498
|
-
const packageJson = PackageJsonFile.loadFromCapsuleSync(capsule.path);
|
499
|
-
|
500
|
-
const addDependencies = (packageJsonFile: PackageJsonFile) => {
|
501
|
-
packageJsonFile.addDependencies(manifest.dependencies);
|
502
|
-
packageJsonFile.addDevDependencies(manifest.devDependencies);
|
503
|
-
};
|
504
|
-
addDependencies(packageJson);
|
505
|
-
packageJson.addOrUpdateProperty('version', currentVersion);
|
506
|
-
return packageJson;
|
507
|
-
}
|
508
|
-
|
509
|
-
private async getComponentPackageVersionWithCache(component: Component): Promise<string> {
|
510
|
-
const idStr = component.id.toString();
|
511
|
-
if (this._componentsPackagesVersionCache[idStr]) {
|
512
|
-
return this._componentsPackagesVersionCache[idStr];
|
513
|
-
}
|
514
|
-
const version = await getComponentPackageVersion(component);
|
515
|
-
this._componentsPackagesVersionCache[idStr] = version;
|
516
|
-
return version;
|
517
|
-
}
|
518
|
-
}
|
519
|
-
|
520
|
-
IsolatorAspect.addRuntime(IsolatorMain);
|
package/network.ts
DELETED
@@ -1,35 +0,0 @@
|
|
1
|
-
import { ComponentID } from '@teambit/component';
|
2
|
-
import { PathOsBasedAbsolute } from '@teambit/legacy/dist/utils/path';
|
3
|
-
import CapsuleList from './capsule-list';
|
4
|
-
|
5
|
-
export class Network {
|
6
|
-
constructor(
|
7
|
-
private _graphCapsules: CapsuleList,
|
8
|
-
private seedersIds: ComponentID[],
|
9
|
-
private _capsulesRootDir: string
|
10
|
-
) {}
|
11
|
-
|
12
|
-
/**
|
13
|
-
* seeders capsules only without the entire graph. normally, this includes the capsules of one
|
14
|
-
* env.
|
15
|
-
*/
|
16
|
-
get seedersCapsules(): CapsuleList {
|
17
|
-
const capsules = this.seedersIds.map((seederId) => {
|
18
|
-
const capsule = this.graphCapsules.getCapsule(seederId);
|
19
|
-
if (!capsule) throw new Error(`unable to find ${seederId.toString()} in the capsule list`);
|
20
|
-
return capsule;
|
21
|
-
});
|
22
|
-
return CapsuleList.fromArray(capsules);
|
23
|
-
}
|
24
|
-
|
25
|
-
/**
|
26
|
-
* all capsules, including the dependencies of the seeders. (even when they belong to another env)
|
27
|
-
*/
|
28
|
-
get graphCapsules(): CapsuleList {
|
29
|
-
return this._graphCapsules;
|
30
|
-
}
|
31
|
-
|
32
|
-
get capsulesRootDir(): PathOsBasedAbsolute {
|
33
|
-
return this._capsulesRootDir;
|
34
|
-
}
|
35
|
-
}
|
Binary file
|
@@ -1,53 +0,0 @@
|
|
1
|
-
import { Logger } from '@teambit/logger';
|
2
|
-
import createSymlinkOrCopy from '@teambit/legacy/dist/utils/fs/create-symlink-or-copy';
|
3
|
-
import fs from 'fs-extra';
|
4
|
-
import path from 'path';
|
5
|
-
|
6
|
-
import { Capsule } from './capsule';
|
7
|
-
|
8
|
-
export async function symlinkBitLegacyToCapsules(capsules: Capsule[], logger: Logger) {
|
9
|
-
logger.debug(`symlink bit bin to capsules, ${capsules.length} capsules`);
|
10
|
-
const linksP = capsules.map(async (capsule) => linkBitLegacyInCapsule(capsule));
|
11
|
-
return Promise.all(linksP);
|
12
|
-
}
|
13
|
-
|
14
|
-
export async function copyBitLegacyToCapsuleRoot(root: string, logger: Logger) {
|
15
|
-
logger.debug(`symlink @teambit/legacy package to capsule root`);
|
16
|
-
const localBitLegacyPath = path.join(__dirname, '@teambit/legacy/dist/..');
|
17
|
-
const targetPath = path.join(root, './node_modules/@teambit/legacy');
|
18
|
-
await fs.copy(localBitLegacyPath, targetPath);
|
19
|
-
}
|
20
|
-
|
21
|
-
async function linkBitLegacyInCapsule(capsule: Capsule) {
|
22
|
-
const bitLegacyPath = path.join(capsule.wrkDir, './node_modules/@teambit/legacy');
|
23
|
-
const getLocalBitLegacyPath = () => {
|
24
|
-
const pathOutsideNodeModules = path.join(__dirname, '@teambit/legacy/dist/..');
|
25
|
-
const dirInIsolator = path.normalize('node_modules/@teambit/isolator/dist/@teambit/legacy');
|
26
|
-
if (pathOutsideNodeModules.includes(dirInIsolator)) {
|
27
|
-
return pathOutsideNodeModules.replace(dirInIsolator, '');
|
28
|
-
}
|
29
|
-
return pathOutsideNodeModules;
|
30
|
-
// if (pathOutsideNodeModules.endsWith(`${path.sep}dist`)) {
|
31
|
-
// return pathOutsideNodeModules;
|
32
|
-
// }
|
33
|
-
// if (__dirname.includes('build-harmony')) {
|
34
|
-
// // for @teambit/legacy development, the cli extension is installed as a package in build-harmony directory
|
35
|
-
// return path.join(__dirname.split('build-harmony')[0], 'dist');
|
36
|
-
// }
|
37
|
-
// throw new Error('unable to link @teambit/legacy to the capsule, the location of @teambit/legacy is unknown');
|
38
|
-
};
|
39
|
-
const localBitLegacyPath = getLocalBitLegacyPath();
|
40
|
-
// if there are no deps, sometimes the node_modules folder is not created
|
41
|
-
// and we need it in order to perform the linking
|
42
|
-
try {
|
43
|
-
capsule.fs.mkdirSync('node_modules');
|
44
|
-
} catch (e: any) {
|
45
|
-
// fail silently - we only need to create it if it doesn't already exist
|
46
|
-
}
|
47
|
-
|
48
|
-
// we use fs directly here rather than the capsule.fs because there are some edge cases
|
49
|
-
// that the capsule fs does not deal with well (eg. identifying and deleting
|
50
|
-
// a symlink rather than the what the symlink links to)
|
51
|
-
await fs.remove(bitLegacyPath);
|
52
|
-
createSymlinkOrCopy(localBitLegacyPath, bitLegacyPath);
|
53
|
-
}
|
@@ -1,58 +0,0 @@
|
|
1
|
-
import { ComponentID } from '@teambit/component';
|
2
|
-
import { Logger } from '@teambit/logger';
|
3
|
-
import { BitId } from '@teambit/legacy-bit-id';
|
4
|
-
import ConsumerComponent from '@teambit/legacy/dist/consumer/component';
|
5
|
-
import Symlink from '@teambit/legacy/dist/links/symlink';
|
6
|
-
import componentIdToPackageName from '@teambit/legacy/dist/utils/bit/component-id-to-package-name';
|
7
|
-
import path from 'path';
|
8
|
-
|
9
|
-
import { Capsule } from './capsule';
|
10
|
-
import CapsuleList from './capsule-list';
|
11
|
-
|
12
|
-
export async function symlinkDependenciesToCapsules(capsules: Capsule[], capsuleList: CapsuleList, logger: Logger) {
|
13
|
-
logger.debug(`symlinkDependenciesToCapsules, ${capsules.length} capsules`);
|
14
|
-
await Promise.all(
|
15
|
-
capsules.map((capsule) => {
|
16
|
-
return symlinkComponent(capsule.component.state._consumer, capsuleList, logger);
|
17
|
-
})
|
18
|
-
);
|
19
|
-
}
|
20
|
-
|
21
|
-
export async function symlinkOnCapsuleRoot(capsuleList: CapsuleList, logger: Logger, capsuleRoot: string) {
|
22
|
-
const modulesPath = path.join(capsuleRoot, 'node_modules');
|
23
|
-
const symlinks = capsuleList.map((capsule) => {
|
24
|
-
const packageName = componentIdToPackageName(capsule.component.state._consumer);
|
25
|
-
const dest = path.join(modulesPath, packageName);
|
26
|
-
const src = path.relative(path.resolve(dest, '..'), capsule.path);
|
27
|
-
|
28
|
-
return new Symlink(src, dest, capsule.component.id._legacy);
|
29
|
-
});
|
30
|
-
|
31
|
-
await Promise.all(symlinks.map((symlink) => symlink.write()));
|
32
|
-
}
|
33
|
-
|
34
|
-
async function symlinkComponent(component: ConsumerComponent, capsuleList: CapsuleList, logger: Logger) {
|
35
|
-
const componentCapsule = capsuleList.getCapsuleIgnoreScopeAndVersion(new ComponentID(component.id));
|
36
|
-
if (!componentCapsule) throw new Error(`unable to find the capsule for ${component.id.toString()}`);
|
37
|
-
const allDeps = component.getAllDependenciesIds();
|
38
|
-
const symlinks = allDeps.map((depId: BitId) => {
|
39
|
-
// TODO: this is dangerous - we might have 2 capsules for the same component with different version, then we might link to the wrong place
|
40
|
-
const devCapsule = capsuleList.getCapsuleIgnoreScopeAndVersion(new ComponentID(depId));
|
41
|
-
if (!devCapsule) {
|
42
|
-
// happens when a dependency is not in the workspace. (it gets installed via the package manager)
|
43
|
-
logger.debug(
|
44
|
-
`symlinkComponentToCapsule: unable to find the capsule for ${depId.toStringWithoutVersion()}. skipping`
|
45
|
-
);
|
46
|
-
return null;
|
47
|
-
}
|
48
|
-
const packageName = componentIdToPackageName(devCapsule.component.state._consumer);
|
49
|
-
const devCapsulePath = devCapsule.path;
|
50
|
-
// @todo: this is a hack, the capsule should be the one responsible to symlink, this works only for FS capsules.
|
51
|
-
const dest = path.join(componentCapsule.path, 'node_modules', packageName);
|
52
|
-
// use relative symlink in capsules to make it really isolated from the machine fs
|
53
|
-
const src = path.relative(path.resolve(dest, '..'), devCapsulePath);
|
54
|
-
return new Symlink(src, dest, component.id);
|
55
|
-
});
|
56
|
-
|
57
|
-
await Promise.all(symlinks.map((symlink) => symlink && symlink.write()));
|
58
|
-
}
|