isolate-package 1.6.2-0 → 1.7.0

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/index.mjs CHANGED
@@ -1,30 +1,77 @@
1
1
  // src/isolate.ts
2
- import fs15 from "fs-extra";
2
+ import fs16 from "fs-extra";
3
3
  import assert6 from "node:assert";
4
- import path15 from "node:path";
4
+ import path17 from "node:path";
5
5
 
6
- // src/helpers/adapt-internal-package-manifests.ts
7
- import fs9 from "fs-extra";
8
- import path8 from "node:path";
9
- import { omit as omit2 } from "ramda";
6
+ // src/lib/config.ts
7
+ import fs5 from "fs-extra";
8
+ import assert from "node:assert";
9
+ import path2 from "node:path";
10
+ import { isEmpty } from "ramda";
10
11
 
11
- // src/helpers/adapt-manifest-internal-deps.ts
12
- import { omit } from "ramda";
12
+ // src/lib/logger.ts
13
+ import chalk from "chalk";
14
+ var _loggerHandlers = {
15
+ debug(...args) {
16
+ console.log(chalk.blue("debug"), ...args);
17
+ },
18
+ info(...args) {
19
+ console.log(chalk.green("info"), ...args);
20
+ },
21
+ warn(...args) {
22
+ console.log(chalk.yellow("warning"), ...args);
23
+ },
24
+ error(...args) {
25
+ console.log(chalk.red("error"), ...args);
26
+ }
27
+ };
28
+ var _logger = {
29
+ debug(...args) {
30
+ if (_logLevel === "debug") {
31
+ _loggerHandlers.debug(...args);
32
+ }
33
+ },
34
+ info(...args) {
35
+ if (_logLevel === "debug" || _logLevel === "info") {
36
+ _loggerHandlers.info(...args);
37
+ }
38
+ },
39
+ warn(...args) {
40
+ if (_logLevel === "debug" || _logLevel === "info" || _logLevel === "warn") {
41
+ _loggerHandlers.warn(...args);
42
+ }
43
+ },
44
+ error(...args) {
45
+ _loggerHandlers.error(...args);
46
+ }
47
+ };
48
+ var _logLevel = "info";
49
+ function setLogger(logger) {
50
+ _loggerHandlers = logger;
51
+ return _logger;
52
+ }
53
+ function setLogLevel(logLevel) {
54
+ _logLevel = logLevel;
55
+ return _logger;
56
+ }
57
+ function useLogger() {
58
+ return _logger;
59
+ }
13
60
 
14
- // src/utils/filter-object-undefined.ts
61
+ // src/lib/utils/filter-object-undefined.ts
15
62
  function filterObjectUndefined(object) {
16
63
  return Object.fromEntries(
17
64
  Object.entries(object).filter(([_, value]) => value !== void 0)
18
65
  );
19
66
  }
20
67
 
21
- // src/utils/get-dirname.ts
68
+ // src/lib/utils/get-dirname.ts
22
69
  import { fileURLToPath } from "url";
23
70
  function getDirname(importMetaUrl) {
24
71
  return fileURLToPath(new URL(".", importMetaUrl));
25
72
  }
26
73
 
27
- // src/utils/get-error-message.ts
74
+ // src/lib/utils/get-error-message.ts
28
75
  function getErrorMessage(error) {
29
76
  return toErrorWithMessage(error).message;
30
77
  }
@@ -41,28 +88,28 @@ function toErrorWithMessage(maybeError) {
41
88
  }
42
89
  }
43
90
 
44
- // src/utils/get-relative-path.ts
45
- function getRootRelativePath(path16, rootPath) {
46
- const strippedPath = path16.replace(rootPath, "");
91
+ // src/lib/utils/get-relative-path.ts
92
+ function getRootRelativePath(path18, rootPath) {
93
+ const strippedPath = path18.replace(rootPath, "");
47
94
  return strippedPath.startsWith("/") ? `(root)${strippedPath}` : `(root)/${strippedPath}`;
48
95
  }
49
- function getIsolateRelativePath(path16, isolatePath) {
50
- const strippedPath = path16.replace(isolatePath, "");
96
+ function getIsolateRelativePath(path18, isolatePath) {
97
+ const strippedPath = path18.replace(isolatePath, "");
51
98
  return strippedPath.startsWith("/") ? `(isolate)${strippedPath}` : `(isolate)/${strippedPath}`;
52
99
  }
53
100
 
54
- // src/utils/inspect-value.ts
101
+ // src/lib/utils/inspect-value.ts
55
102
  import { inspect } from "node:util";
56
103
  function inspectValue(value) {
57
104
  return inspect(value, false, 4, true);
58
105
  }
59
106
 
60
- // src/utils/is-present.ts
107
+ // src/lib/utils/is-present.ts
61
108
  function isDefined(t) {
62
109
  return t !== void 0;
63
110
  }
64
111
 
65
- // src/utils/json.ts
112
+ // src/lib/utils/json.ts
66
113
  import fs from "fs-extra";
67
114
  import stripJsonComments from "strip-json-comments";
68
115
  function readTypedJsonSync(filePath) {
@@ -88,56 +135,7 @@ async function readTypedJson(filePath) {
88
135
  }
89
136
  }
90
137
 
91
- // src/utils/logger.ts
92
- import chalk from "chalk";
93
- var _loggerHandlers = {
94
- debug(...args) {
95
- console.log(chalk.blue("debug"), ...args);
96
- },
97
- info(...args) {
98
- console.log(chalk.green("info"), ...args);
99
- },
100
- warn(...args) {
101
- console.log(chalk.yellow("warning"), ...args);
102
- },
103
- error(...args) {
104
- console.log(chalk.red("error"), ...args);
105
- }
106
- };
107
- var _logger = {
108
- debug(...args) {
109
- if (_logLevel === "debug") {
110
- _loggerHandlers.debug(...args);
111
- }
112
- },
113
- info(...args) {
114
- if (_logLevel === "debug" || _logLevel === "info") {
115
- _loggerHandlers.info(...args);
116
- }
117
- },
118
- warn(...args) {
119
- if (_logLevel === "debug" || _logLevel === "info" || _logLevel === "warn") {
120
- _loggerHandlers.warn(...args);
121
- }
122
- },
123
- error(...args) {
124
- _loggerHandlers.error(...args);
125
- }
126
- };
127
- var _logLevel = "info";
128
- function setLogger(logger) {
129
- _loggerHandlers = logger;
130
- return _logger;
131
- }
132
- function setLogLevel(logLevel) {
133
- _logLevel = logLevel;
134
- return _logger;
135
- }
136
- function useLogger() {
137
- return _logger;
138
- }
139
-
140
- // src/utils/pack.ts
138
+ // src/lib/utils/pack.ts
141
139
  import fs2 from "fs-extra";
142
140
  import { exec } from "node:child_process";
143
141
  import path from "node:path";
@@ -185,7 +183,7 @@ async function pack(srcDir, dstDir, usePnpmPack = false) {
185
183
  return filePath;
186
184
  }
187
185
 
188
- // src/utils/unpack.ts
186
+ // src/lib/utils/unpack.ts
189
187
  import fs3 from "fs-extra";
190
188
  import tar from "tar-fs";
191
189
  import { createGunzip } from "zlib";
@@ -195,7 +193,7 @@ async function unpack(filePath, unpackDir) {
195
193
  });
196
194
  }
197
195
 
198
- // src/utils/yaml.ts
196
+ // src/lib/utils/yaml.ts
199
197
  import fs4 from "fs-extra";
200
198
  import yaml from "yaml";
201
199
  function readTypedYamlSync(filePath) {
@@ -210,54 +208,7 @@ function readTypedYamlSync(filePath) {
210
208
  }
211
209
  }
212
210
 
213
- // src/helpers/patch-internal-entries.ts
214
- import path2 from "node:path";
215
- function patchInternalEntries(dependencies, packagesRegistry, parentRootRelativeDir) {
216
- const log = useLogger();
217
- const allWorkspacePackageNames = Object.keys(packagesRegistry);
218
- return Object.fromEntries(
219
- Object.entries(dependencies).map(([key, value]) => {
220
- if (allWorkspacePackageNames.includes(key)) {
221
- const def = packagesRegistry[key];
222
- const relativePath = parentRootRelativeDir ? path2.relative(parentRootRelativeDir, `./${def.rootRelativeDir}`) : `./${def.rootRelativeDir}`;
223
- const linkPath = `file:${relativePath}`;
224
- log.debug(`Linking dependency ${key} to ${linkPath}`);
225
- return [key, linkPath];
226
- } else {
227
- return [key, value];
228
- }
229
- })
230
- );
231
- }
232
-
233
- // src/helpers/adapt-manifest-internal-deps.ts
234
- function adaptManifestInternalDeps({
235
- manifest,
236
- packagesRegistry,
237
- parentRootRelativeDir
238
- }, opts = {}) {
239
- return Object.assign(
240
- omit(["devDependencies"], manifest),
241
- filterObjectUndefined({
242
- dependencies: manifest.dependencies ? patchInternalEntries(
243
- manifest.dependencies,
244
- packagesRegistry,
245
- parentRootRelativeDir
246
- ) : void 0,
247
- devDependencies: opts.includeDevDependencies && manifest.devDependencies ? patchInternalEntries(
248
- manifest.devDependencies,
249
- packagesRegistry,
250
- parentRootRelativeDir
251
- ) : void 0
252
- })
253
- );
254
- }
255
-
256
- // src/helpers/config.ts
257
- import fs5 from "fs-extra";
258
- import assert from "node:assert";
259
- import path3 from "node:path";
260
- import { isEmpty } from "ramda";
211
+ // src/lib/config.ts
261
212
  var configDefaults = {
262
213
  buildDirName: void 0,
263
214
  includeDevDependencies: false,
@@ -293,7 +244,7 @@ function resolveConfig() {
293
244
  }
294
245
  setLogLevel(process.env.DEBUG_ISOLATE_CONFIG ? "debug" : "info");
295
246
  const log = useLogger();
296
- const configFilePath = path3.join(process.cwd(), CONFIG_FILE_NAME);
247
+ const configFilePath = path2.join(process.cwd(), CONFIG_FILE_NAME);
297
248
  if (_user_defined_config) {
298
249
  log.debug(`Using user defined config:`, inspectValue(_user_defined_config));
299
250
  } else {
@@ -323,47 +274,138 @@ function getUserDefinedConfig() {
323
274
  return _user_defined_config;
324
275
  }
325
276
 
326
- // src/helpers/detect-package-manager.ts
327
- import fs8 from "fs-extra";
328
- import assert3 from "node:assert";
329
- import { execSync } from "node:child_process";
277
+ // src/lib/lockfile/process-lockfile.ts
278
+ import fs9 from "fs-extra";
330
279
  import path7 from "node:path";
280
+ import { mapObjIndexed } from "ramda";
281
+
282
+ // src/lib/package-manager/helpers/infer-from-files.ts
283
+ import fs6 from "fs-extra";
284
+ import { execSync } from "node:child_process";
285
+ import path3 from "node:path";
286
+
287
+ // src/lib/package-manager/names.ts
288
+ var supportedPackageManagerNames = ["pnpm", "yarn", "npm"];
289
+ function getLockfileFileName(name) {
290
+ switch (name) {
291
+ case "pnpm":
292
+ return "pnpm-lock.yaml";
293
+ case "yarn":
294
+ return "yarn.lock";
295
+ case "npm":
296
+ return "package-lock.json";
297
+ }
298
+ }
299
+
300
+ // src/lib/package-manager/helpers/infer-from-files.ts
301
+ function inferFromFiles(workspaceRoot) {
302
+ for (const name of supportedPackageManagerNames) {
303
+ const lockfileName = getLockfileFileName(name);
304
+ if (fs6.existsSync(path3.join(workspaceRoot, lockfileName))) {
305
+ return { name, version: getVersion(name) };
306
+ }
307
+ }
308
+ if (fs6.existsSync(path3.join(workspaceRoot, "npm-shrinkwrap.json"))) {
309
+ return { name: "npm", version: getVersion("npm") };
310
+ }
311
+ throw new Error(`Failed to detect package manager`);
312
+ }
313
+ function getVersion(packageManagerName) {
314
+ const buffer = execSync(`${packageManagerName} --version`);
315
+ return buffer.toString().trim();
316
+ }
331
317
 
332
- // src/helpers/process-lockfile.ts
318
+ // src/lib/package-manager/helpers/infer-from-manifest.ts
333
319
  import fs7 from "fs-extra";
334
- import path6 from "node:path";
335
- import { mapObjIndexed } from "ramda";
320
+ import assert2 from "node:assert";
321
+ import path4 from "node:path";
322
+ function inferFromManifest(workspaceRoot) {
323
+ const log = useLogger();
324
+ const rootManifest = readTypedJsonSync(
325
+ path4.join(workspaceRoot, "package.json")
326
+ );
327
+ if (!rootManifest.packageManager) {
328
+ log.debug("No packageManager field found in root manifest");
329
+ return;
330
+ }
331
+ const [name, version = "*"] = rootManifest.packageManager.split("@");
332
+ assert2(
333
+ supportedPackageManagerNames.includes(name),
334
+ `Package manager "${name}" is not currently supported`
335
+ );
336
+ const lockfileName = getLockfileFileName(name);
337
+ assert2(
338
+ fs7.existsSync(path4.join(workspaceRoot, lockfileName)),
339
+ `Manifest declares ${name} to be the packageManager, but failed to find ${lockfileName} in workspace root`
340
+ );
341
+ return { name, version };
342
+ }
343
+
344
+ // src/lib/package-manager/index.ts
345
+ var packageManager;
346
+ function usePackageManager() {
347
+ if (!packageManager) {
348
+ throw Error(
349
+ "No package manager detected. Make sure to call detectPackageManager() before usePackageManager()"
350
+ );
351
+ }
352
+ return packageManager;
353
+ }
354
+ function detectPackageManager(workspaceRoot) {
355
+ packageManager = inferFromManifest(workspaceRoot) ?? inferFromFiles(workspaceRoot);
356
+ return packageManager;
357
+ }
336
358
 
337
- // src/helpers/generate-npm-lockfile.ts
359
+ // src/lib/lockfile/helpers/generate-npm-lockfile.ts
338
360
  import Arborist from "@npmcli/arborist";
339
- import fs6 from "node:fs/promises";
340
- import path4 from "node:path";
361
+ import fs8 from "fs-extra";
362
+ import path5 from "node:path";
341
363
  async function generateNpmLockfile({
342
- targetPackageDir,
343
- packagesRegistry,
344
- isolateDir
364
+ workspaceRootDir,
365
+ isolateDir,
366
+ packagesRegistry
345
367
  }) {
346
368
  const log = useLogger();
347
- log.debug("Generating NPM lockfile...");
348
- const internalPackageNames = Object.keys(packagesRegistry);
349
- const arborist = new Arborist({ path: targetPackageDir });
350
- const { meta } = await arborist.buildIdealTree({
351
- // rm: internalPackageNames,
352
- });
353
- meta?.commit();
354
- const lockfilePath = path4.join(isolateDir, "package-lock.json");
355
- await fs6.writeFile(lockfilePath, String(meta));
356
- log.debug("Created lockfile at", lockfilePath);
369
+ log.info("Generating NPM lockfile...");
370
+ const origRootNodeModulesPath = path5.join(workspaceRootDir, "node_modules");
371
+ const tempRootNodeModulesPath = path5.join(isolateDir, "node_modules");
372
+ if (!fs8.existsSync(origRootNodeModulesPath)) {
373
+ throw new Error(
374
+ `Failed to find node_modules at ${origRootNodeModulesPath}`
375
+ );
376
+ }
377
+ log.debug(`Temporarily moving node_modules to the isolate output`);
378
+ let hasMovedNodeModules = false;
379
+ try {
380
+ await fs8.move(origRootNodeModulesPath, tempRootNodeModulesPath);
381
+ hasMovedNodeModules = true;
382
+ const internalPackageNames = Object.keys(packagesRegistry);
383
+ const arborist = new Arborist({ path: isolateDir });
384
+ const { meta } = await arborist.buildIdealTree({
385
+ rm: internalPackageNames
386
+ });
387
+ meta?.commit();
388
+ const lockfilePath = path5.join(isolateDir, "package-lock.json");
389
+ await fs8.writeFile(lockfilePath, String(meta));
390
+ log.debug("Created lockfile at", lockfilePath);
391
+ } catch (err) {
392
+ log.error(getErrorMessage(err));
393
+ } finally {
394
+ if (hasMovedNodeModules) {
395
+ log.debug(`Restoring node_modules to the workspace root`);
396
+ await fs8.move(tempRootNodeModulesPath, origRootNodeModulesPath);
397
+ }
398
+ }
357
399
  }
358
400
 
359
- // src/helpers/generate-pnpm-lockfile.ts
401
+ // src/lib/lockfile/helpers/generate-pnpm-lockfile.ts
360
402
  import {
361
403
  getLockfileImporterId,
362
404
  readWantedLockfile,
363
405
  writeWantedLockfile
364
406
  } from "@pnpm/lockfile-file";
365
- import assert2 from "node:assert";
366
- import path5 from "node:path";
407
+ import assert3 from "node:assert";
408
+ import path6 from "node:path";
367
409
  import { pick } from "ramda";
368
410
  async function generatePnpmLockfile({
369
411
  workspaceRootDir,
@@ -374,11 +416,11 @@ async function generatePnpmLockfile({
374
416
  }) {
375
417
  const { includeDevDependencies } = useConfig();
376
418
  const log = useLogger();
377
- log.debug("Generating PNPM lockfile...");
419
+ log.info("Generating PNPM lockfile...");
378
420
  const lockfile = await readWantedLockfile(workspaceRootDir, {
379
421
  ignoreIncompatible: false
380
422
  });
381
- assert2(lockfile, `No input lockfile found at ${workspaceRootDir}`);
423
+ assert3(lockfile, `No input lockfile found at ${workspaceRootDir}`);
382
424
  const targetImporterId = getLockfileImporterId(
383
425
  workspaceRootDir,
384
426
  targetPackageDir
@@ -386,7 +428,7 @@ async function generatePnpmLockfile({
386
428
  const directoryByPackageName = Object.fromEntries(
387
429
  internalDepPackageNames.map((name) => {
388
430
  const pkg = packagesRegistry[name];
389
- assert2(pkg, `Package ${name} not found in packages registry`);
431
+ assert3(pkg, `Package ${name} not found in packages registry`);
390
432
  return [name, pkg.rootRelativeDir];
391
433
  })
392
434
  );
@@ -424,20 +466,10 @@ async function generatePnpmLockfile({
424
466
  )
425
467
  );
426
468
  await writeWantedLockfile(isolateDir, lockfile);
427
- log.debug("Created lockfile at", path5.join(isolateDir, "pnpm-lock.yaml"));
469
+ log.debug("Created lockfile at", path6.join(isolateDir, "pnpm-lock.yaml"));
428
470
  }
429
471
 
430
- // src/helpers/process-lockfile.ts
431
- function getLockfileFileName(name) {
432
- switch (name) {
433
- case "pnpm":
434
- return "pnpm-lock.yaml";
435
- case "yarn":
436
- return "yarn.lock";
437
- case "npm":
438
- return "package-lock.json";
439
- }
440
- }
472
+ // src/lib/lockfile/process-lockfile.ts
441
473
  function pnpmMapImporter({ dependencies, devDependencies, ...rest }, {
442
474
  includeDevDependencies,
443
475
  directoryByPackageName
@@ -467,16 +499,16 @@ async function processLockfile({
467
499
  switch (name) {
468
500
  case "npm": {
469
501
  await generateNpmLockfile({
470
- targetPackageDir,
502
+ workspaceRootDir,
471
503
  isolateDir,
472
504
  packagesRegistry
473
505
  });
474
506
  break;
475
507
  }
476
508
  case "yarn": {
477
- const lockfileSrcPath = path6.join(workspaceRootDir, fileName);
478
- const lockfileDstPath = path6.join(isolateDir, fileName);
479
- fs7.copyFileSync(lockfileSrcPath, lockfileDstPath);
509
+ const lockfileSrcPath = path7.join(workspaceRootDir, fileName);
510
+ const lockfileDstPath = path7.join(isolateDir, fileName);
511
+ fs9.copyFileSync(lockfileSrcPath, lockfileDstPath);
480
512
  log.debug("Copied lockfile to", lockfileDstPath);
481
513
  break;
482
514
  }
@@ -495,60 +527,63 @@ async function processLockfile({
495
527
  }
496
528
  }
497
529
 
498
- // src/helpers/detect-package-manager.ts
499
- var supportedPackageManagerNames = ["pnpm", "yarn", "npm"];
500
- var packageManager;
501
- function detectPackageManager(workspaceRoot) {
502
- packageManager = inferFromManifest(workspaceRoot) ?? inferFromFiles(workspaceRoot);
503
- return packageManager;
504
- }
505
- function inferFromManifest(workspaceRoot) {
530
+ // src/lib/manifest/adapt-target-package-manifest.ts
531
+ import fs11 from "fs-extra";
532
+ import path10 from "node:path";
533
+ import { omit as omit3 } from "ramda";
534
+
535
+ // src/lib/manifest/helpers/adapt-internal-package-manifests.ts
536
+ import fs10 from "fs-extra";
537
+ import path9 from "node:path";
538
+ import { omit as omit2 } from "ramda";
539
+
540
+ // src/lib/manifest/helpers/adapt-manifest-internal-deps.ts
541
+ import { omit } from "ramda";
542
+
543
+ // src/lib/manifest/helpers/patch-internal-entries.ts
544
+ import path8 from "node:path";
545
+ function patchInternalEntries(dependencies, packagesRegistry, parentRootRelativeDir) {
506
546
  const log = useLogger();
507
- const rootManifest = readTypedJsonSync(
508
- path7.join(workspaceRoot, "package.json")
509
- );
510
- if (!rootManifest.packageManager) {
511
- log.debug("No packageManager field found in root manifest");
512
- return;
513
- }
514
- const [name, version = "*"] = rootManifest.packageManager.split("@");
515
- assert3(
516
- supportedPackageManagerNames.includes(name),
517
- `Package manager "${name}" is not currently supported`
518
- );
519
- const lockfileName = getLockfileFileName(name);
520
- assert3(
521
- fs8.existsSync(path7.join(workspaceRoot, lockfileName)),
522
- `Manifest declares ${name} to be the packageManager, but failed to find ${lockfileName} in workspace root`
547
+ const allWorkspacePackageNames = Object.keys(packagesRegistry);
548
+ return Object.fromEntries(
549
+ Object.entries(dependencies).map(([key, value]) => {
550
+ if (allWorkspacePackageNames.includes(key)) {
551
+ const def = packagesRegistry[key];
552
+ const relativePath = parentRootRelativeDir ? path8.relative(parentRootRelativeDir, `./${def.rootRelativeDir}`) : `./${def.rootRelativeDir}`;
553
+ const linkPath = `file:${relativePath}`;
554
+ log.debug(`Linking dependency ${key} to ${linkPath}`);
555
+ return [key, linkPath];
556
+ } else {
557
+ return [key, value];
558
+ }
559
+ })
523
560
  );
524
- return { name, version };
525
- }
526
- function inferFromFiles(workspaceRoot) {
527
- for (const name of supportedPackageManagerNames) {
528
- const lockfileName = getLockfileFileName(name);
529
- if (fs8.existsSync(path7.join(workspaceRoot, lockfileName))) {
530
- return { name, version: getVersion(name) };
531
- }
532
- }
533
- if (fs8.existsSync(path7.join(workspaceRoot, "npm-shrinkwrap.json"))) {
534
- return { name: "npm", version: getVersion("npm") };
535
- }
536
- throw new Error(`Failed to detect package manager`);
537
561
  }
538
- function getVersion(packageManagerName) {
539
- const buffer = execSync(`${packageManagerName} --version`);
540
- return buffer.toString().trim();
541
- }
542
- function usePackageManager() {
543
- if (!packageManager) {
544
- throw Error(
545
- "No package manager detected. Make sure to call detectPackageManager() before usePackageManager()"
546
- );
547
- }
548
- return packageManager;
562
+
563
+ // src/lib/manifest/helpers/adapt-manifest-internal-deps.ts
564
+ function adaptManifestInternalDeps({
565
+ manifest,
566
+ packagesRegistry,
567
+ parentRootRelativeDir
568
+ }, opts = {}) {
569
+ return Object.assign(
570
+ omit(["devDependencies"], manifest),
571
+ filterObjectUndefined({
572
+ dependencies: manifest.dependencies ? patchInternalEntries(
573
+ manifest.dependencies,
574
+ packagesRegistry,
575
+ parentRootRelativeDir
576
+ ) : void 0,
577
+ devDependencies: opts.includeDevDependencies && manifest.devDependencies ? patchInternalEntries(
578
+ manifest.devDependencies,
579
+ packagesRegistry,
580
+ parentRootRelativeDir
581
+ ) : void 0
582
+ })
583
+ );
549
584
  }
550
585
 
551
- // src/helpers/adapt-internal-package-manifests.ts
586
+ // src/lib/manifest/helpers/adapt-internal-package-manifests.ts
552
587
  async function adaptInternalPackageManifests(internalPackageNames, packagesRegistry, isolateDir) {
553
588
  const packageManager2 = usePackageManager();
554
589
  const { includeDevDependencies } = useConfig();
@@ -576,126 +611,46 @@ async function adaptInternalPackageManifests(internalPackageNames, packagesRegis
576
611
  },
577
612
  { includeDevDependencies }
578
613
  );
579
- await fs9.writeFile(
580
- path8.join(isolateDir, rootRelativeDir, "package.json"),
614
+ await fs10.writeFile(
615
+ path9.join(isolateDir, rootRelativeDir, "package.json"),
581
616
  JSON.stringify(outputManifest, null, 2)
582
617
  );
583
618
  })
584
619
  );
585
620
  }
586
621
 
587
- // src/helpers/adapt-target-package-manifest.ts
588
- import fs10 from "fs-extra";
589
- import path9 from "node:path";
590
- import { omit as omit3 } from "ramda";
622
+ // src/lib/manifest/adapt-target-package-manifest.ts
591
623
  async function adaptTargetPackageManifest(manifest, packagesRegistry, isolateDir) {
592
624
  const packageManager2 = usePackageManager();
593
625
  const { includeDevDependencies } = useConfig();
594
- const outputManifest = packageManager2.name === "pnpm" ? Object.assign(omit3(["devDependencies", "scripts"], manifest), {
595
- devDependencies: includeDevDependencies ? manifest.devDependencies : void 0
596
- }) : adaptManifestInternalDeps(
597
- {
598
- manifest,
599
- packagesRegistry
600
- },
601
- { includeDevDependencies }
626
+ const outputManifest = packageManager2.name === "pnpm" ? (
627
+ /**
628
+ * For PNPM the output itself is a workspace so we can preserve the specifiers
629
+ * with "workspace:*" in the output manifest.
630
+ */
631
+ Object.assign(omit3(["devDependencies", "scripts"], manifest), {
632
+ devDependencies: includeDevDependencies ? manifest.devDependencies : void 0
633
+ })
634
+ ) : (
635
+ /** For other package managers we replace the links to internal dependencies */
636
+ adaptManifestInternalDeps(
637
+ {
638
+ manifest,
639
+ packagesRegistry
640
+ },
641
+ { includeDevDependencies }
642
+ )
602
643
  );
603
- await fs10.writeFile(
604
- path9.join(isolateDir, "package.json"),
644
+ await fs11.writeFile(
645
+ path10.join(isolateDir, "package.json"),
605
646
  JSON.stringify(outputManifest, null, 2)
606
647
  );
607
648
  }
608
649
 
609
- // src/helpers/create-packages-registry.ts
610
- import fs11 from "fs-extra";
611
- import { globSync } from "glob";
650
+ // src/lib/manifest/import-manifest.ts
612
651
  import path11 from "node:path";
613
652
 
614
- // src/helpers/find-packages-globs.ts
615
- import assert4 from "node:assert";
616
- import path10 from "node:path";
617
- function findPackagesGlobs(workspaceRootDir) {
618
- const log = useLogger();
619
- const packageManager2 = usePackageManager();
620
- switch (packageManager2.name) {
621
- case "pnpm": {
622
- const { packages: globs } = readTypedYamlSync(
623
- path10.join(workspaceRootDir, "pnpm-workspace.yaml")
624
- );
625
- log.debug("Detected pnpm packages globs:", inspectValue(globs));
626
- return globs;
627
- }
628
- case "yarn":
629
- case "npm": {
630
- const workspaceRootManifestPath = path10.join(
631
- workspaceRootDir,
632
- "package.json"
633
- );
634
- const { workspaces } = readTypedJsonSync(
635
- workspaceRootManifestPath
636
- );
637
- if (!workspaces) {
638
- throw new Error(
639
- `No workspaces field found in ${workspaceRootManifestPath}`
640
- );
641
- }
642
- if (Array.isArray(workspaces)) {
643
- return workspaces;
644
- } else {
645
- const workspacesObject = workspaces;
646
- assert4(
647
- workspacesObject.packages,
648
- "workspaces.packages must be an array"
649
- );
650
- return workspacesObject.packages;
651
- }
652
- }
653
- }
654
- }
655
-
656
- // src/helpers/create-packages-registry.ts
657
- async function createPackagesRegistry(workspaceRootDir, workspacePackagesOverride) {
658
- const log = useLogger();
659
- if (workspacePackagesOverride) {
660
- log.debug(
661
- `Override workspace packages via config: ${workspacePackagesOverride}`
662
- );
663
- }
664
- const packagesGlobs = workspacePackagesOverride ?? findPackagesGlobs(workspaceRootDir);
665
- const cwd = process.cwd();
666
- process.chdir(workspaceRootDir);
667
- const allPackages = packagesGlobs.flatMap((glob) => globSync(glob)).filter((dir) => fs11.lstatSync(dir).isDirectory());
668
- const registry = (await Promise.all(
669
- allPackages.map(async (rootRelativeDir) => {
670
- const manifestPath = path11.join(rootRelativeDir, "package.json");
671
- if (!fs11.existsSync(manifestPath)) {
672
- log.warn(
673
- `Ignoring directory ./${rootRelativeDir} because it does not contain a package.json file`
674
- );
675
- return;
676
- } else {
677
- log.debug(`Registering package ./${rootRelativeDir}`);
678
- const manifest = await readTypedJson(
679
- path11.join(rootRelativeDir, "package.json")
680
- );
681
- return {
682
- manifest,
683
- rootRelativeDir,
684
- absoluteDir: path11.join(workspaceRootDir, rootRelativeDir)
685
- };
686
- }
687
- })
688
- )).reduce((acc, info) => {
689
- if (info) {
690
- acc[info.manifest.name] = info;
691
- }
692
- return acc;
693
- }, {});
694
- process.chdir(cwd);
695
- return registry;
696
- }
697
-
698
- // src/helpers/get-build-output-dir.ts
653
+ // src/lib/output/get-build-output-dir.ts
699
654
  import fs12 from "fs-extra";
700
655
  import path12 from "node:path";
701
656
  import outdent from "outdent";
@@ -726,26 +681,8 @@ async function getBuildOutputDir(targetPackageDir) {
726
681
  }
727
682
  }
728
683
 
729
- // src/helpers/list-internal-packages.ts
730
- import { uniq } from "ramda";
731
- function listInternalPackages(manifest, packagesRegistry, { includeDevDependencies = false } = {}) {
732
- const allWorkspacePackageNames = Object.keys(packagesRegistry);
733
- const internalPackageNames = (includeDevDependencies ? [
734
- ...Object.keys(manifest.dependencies ?? {}),
735
- ...Object.keys(manifest.devDependencies ?? {})
736
- ] : Object.keys(manifest.dependencies ?? {})).filter((name) => allWorkspacePackageNames.includes(name));
737
- const nestedInternalPackageNames = internalPackageNames.flatMap(
738
- (packageName) => listInternalPackages(
739
- packagesRegistry[packageName].manifest,
740
- packagesRegistry,
741
- { includeDevDependencies }
742
- )
743
- );
744
- return uniq(internalPackageNames.concat(nestedInternalPackageNames));
745
- }
746
-
747
- // src/helpers/pack-dependencies.ts
748
- import assert5 from "node:assert";
684
+ // src/lib/output/pack-dependencies.ts
685
+ import assert4 from "node:assert";
749
686
  async function packDependencies({
750
687
  /** All packages found in the monorepo by workspaces declaration */
751
688
  packagesRegistry,
@@ -770,7 +707,7 @@ async function packDependencies({
770
707
  }
771
708
  for (const dependency of internalPackageNames) {
772
709
  const def = packagesRegistry[dependency];
773
- assert5(dependency, `Failed to find package definition for ${dependency}`);
710
+ assert4(dependency, `Failed to find package definition for ${dependency}`);
774
711
  const { name: name2 } = def.manifest;
775
712
  if (packedFileByName[name2]) {
776
713
  log.debug(`Skipping ${name2} because it has already been packed`);
@@ -785,7 +722,7 @@ async function packDependencies({
785
722
  return packedFileByName;
786
723
  }
787
724
 
788
- // src/helpers/process-build-output-files.ts
725
+ // src/lib/output/process-build-output-files.ts
789
726
  import fs13 from "fs-extra";
790
727
  import path13 from "node:path";
791
728
  var TIMEOUT_MS = 5e3;
@@ -810,7 +747,7 @@ async function processBuildOutputFiles({
810
747
  await fs13.copy(path13.join(unpackDir, "package"), isolateDir);
811
748
  }
812
749
 
813
- // src/helpers/unpack-dependencies.ts
750
+ // src/lib/output/unpack-dependencies.ts
814
751
  import fs14 from "fs-extra";
815
752
  import path14, { join } from "node:path";
816
753
  async function unpackDependencies(packedFilesByName, packagesRegistry, tmpDir, isolateDir) {
@@ -836,6 +773,113 @@ async function unpackDependencies(packedFilesByName, packagesRegistry, tmpDir, i
836
773
  );
837
774
  }
838
775
 
776
+ // src/lib/registry/create-packages-registry.ts
777
+ import fs15 from "fs-extra";
778
+ import { globSync } from "glob";
779
+ import path16 from "node:path";
780
+
781
+ // src/lib/registry/helpers/find-packages-globs.ts
782
+ import assert5 from "node:assert";
783
+ import path15 from "node:path";
784
+ function findPackagesGlobs(workspaceRootDir) {
785
+ const log = useLogger();
786
+ const packageManager2 = usePackageManager();
787
+ switch (packageManager2.name) {
788
+ case "pnpm": {
789
+ const { packages: globs } = readTypedYamlSync(
790
+ path15.join(workspaceRootDir, "pnpm-workspace.yaml")
791
+ );
792
+ log.debug("Detected pnpm packages globs:", inspectValue(globs));
793
+ return globs;
794
+ }
795
+ case "yarn":
796
+ case "npm": {
797
+ const workspaceRootManifestPath = path15.join(
798
+ workspaceRootDir,
799
+ "package.json"
800
+ );
801
+ const { workspaces } = readTypedJsonSync(
802
+ workspaceRootManifestPath
803
+ );
804
+ if (!workspaces) {
805
+ throw new Error(
806
+ `No workspaces field found in ${workspaceRootManifestPath}`
807
+ );
808
+ }
809
+ if (Array.isArray(workspaces)) {
810
+ return workspaces;
811
+ } else {
812
+ const workspacesObject = workspaces;
813
+ assert5(
814
+ workspacesObject.packages,
815
+ "workspaces.packages must be an array"
816
+ );
817
+ return workspacesObject.packages;
818
+ }
819
+ }
820
+ }
821
+ }
822
+
823
+ // src/lib/registry/create-packages-registry.ts
824
+ async function createPackagesRegistry(workspaceRootDir, workspacePackagesOverride) {
825
+ const log = useLogger();
826
+ if (workspacePackagesOverride) {
827
+ log.debug(
828
+ `Override workspace packages via config: ${workspacePackagesOverride}`
829
+ );
830
+ }
831
+ const packagesGlobs = workspacePackagesOverride ?? findPackagesGlobs(workspaceRootDir);
832
+ const cwd = process.cwd();
833
+ process.chdir(workspaceRootDir);
834
+ const allPackages = packagesGlobs.flatMap((glob) => globSync(glob)).filter((dir) => fs15.lstatSync(dir).isDirectory());
835
+ const registry = (await Promise.all(
836
+ allPackages.map(async (rootRelativeDir) => {
837
+ const manifestPath = path16.join(rootRelativeDir, "package.json");
838
+ if (!fs15.existsSync(manifestPath)) {
839
+ log.warn(
840
+ `Ignoring directory ./${rootRelativeDir} because it does not contain a package.json file`
841
+ );
842
+ return;
843
+ } else {
844
+ log.debug(`Registering package ./${rootRelativeDir}`);
845
+ const manifest = await readTypedJson(
846
+ path16.join(rootRelativeDir, "package.json")
847
+ );
848
+ return {
849
+ manifest,
850
+ rootRelativeDir,
851
+ absoluteDir: path16.join(workspaceRootDir, rootRelativeDir)
852
+ };
853
+ }
854
+ })
855
+ )).reduce((acc, info) => {
856
+ if (info) {
857
+ acc[info.manifest.name] = info;
858
+ }
859
+ return acc;
860
+ }, {});
861
+ process.chdir(cwd);
862
+ return registry;
863
+ }
864
+
865
+ // src/lib/registry/list-internal-packages.ts
866
+ import { uniq } from "ramda";
867
+ function listInternalPackages(manifest, packagesRegistry, { includeDevDependencies = false } = {}) {
868
+ const allWorkspacePackageNames = Object.keys(packagesRegistry);
869
+ const internalPackageNames = (includeDevDependencies ? [
870
+ ...Object.keys(manifest.dependencies ?? {}),
871
+ ...Object.keys(manifest.devDependencies ?? {})
872
+ ] : Object.keys(manifest.dependencies ?? {})).filter((name) => allWorkspacePackageNames.includes(name));
873
+ const nestedInternalPackageNames = internalPackageNames.flatMap(
874
+ (packageName) => listInternalPackages(
875
+ packagesRegistry[packageName].manifest,
876
+ packagesRegistry,
877
+ { includeDevDependencies }
878
+ )
879
+ );
880
+ return uniq(internalPackageNames.concat(nestedInternalPackageNames));
881
+ }
882
+
839
883
  // src/isolate.ts
840
884
  var __dirname = getDirname(import.meta.url);
841
885
  async function isolate(options = {}) {
@@ -849,14 +893,14 @@ async function isolate(options = {}) {
849
893
  setLogLevel(config.logLevel);
850
894
  const log = useLogger();
851
895
  const thisPackageManifest = await readTypedJson(
852
- path15.join(path15.join(__dirname, "..", "package.json"))
896
+ path17.join(path17.join(__dirname, "..", "package.json"))
853
897
  );
854
898
  log.debug("Using isolate-package version", thisPackageManifest.version);
855
- const targetPackageDir = config.targetPackagePath ? path15.join(process.cwd(), config.targetPackagePath) : process.cwd();
856
- const workspaceRootDir = config.targetPackagePath ? process.cwd() : path15.join(targetPackageDir, config.workspaceRoot);
899
+ const targetPackageDir = config.targetPackagePath ? path17.join(process.cwd(), config.targetPackagePath) : process.cwd();
900
+ const workspaceRootDir = config.targetPackagePath ? process.cwd() : path17.join(targetPackageDir, config.workspaceRoot);
857
901
  const buildOutputDir = await getBuildOutputDir(targetPackageDir);
858
902
  assert6(
859
- fs15.existsSync(buildOutputDir),
903
+ fs16.existsSync(buildOutputDir),
860
904
  `Failed to find build output path at ${buildOutputDir}. Please make sure you build the source before isolating it.`
861
905
  );
862
906
  log.debug("Workspace root resolved to", workspaceRootDir);
@@ -864,20 +908,20 @@ async function isolate(options = {}) {
864
908
  "Isolate target package",
865
909
  getRootRelativePath(targetPackageDir, workspaceRootDir)
866
910
  );
867
- const isolateDir = path15.join(targetPackageDir, config.isolateDirName);
911
+ const isolateDir = path17.join(targetPackageDir, config.isolateDirName);
868
912
  log.debug(
869
913
  "Isolate output directory",
870
914
  getRootRelativePath(isolateDir, workspaceRootDir)
871
915
  );
872
- if (fs15.existsSync(isolateDir)) {
873
- await fs15.remove(isolateDir);
916
+ if (fs16.existsSync(isolateDir)) {
917
+ await fs16.remove(isolateDir);
874
918
  log.debug("Cleaned the existing isolate output directory");
875
919
  }
876
- await fs15.ensureDir(isolateDir);
877
- const tmpDir = path15.join(isolateDir, "__tmp");
878
- await fs15.ensureDir(tmpDir);
920
+ await fs16.ensureDir(isolateDir);
921
+ const tmpDir = path17.join(isolateDir, "__tmp");
922
+ await fs16.ensureDir(tmpDir);
879
923
  const targetPackageManifest = await readTypedJson(
880
- path15.join(targetPackageDir, "package.json")
924
+ path17.join(targetPackageDir, "package.json")
881
925
  );
882
926
  const packageManager2 = detectPackageManager(workspaceRootDir);
883
927
  log.debug(
@@ -941,21 +985,21 @@ async function isolate(options = {}) {
941
985
  });
942
986
  }
943
987
  if (packageManager2.name === "pnpm") {
944
- fs15.copyFileSync(
945
- path15.join(workspaceRootDir, "pnpm-workspace.yaml"),
946
- path15.join(isolateDir, "pnpm-workspace.yaml")
988
+ fs16.copyFileSync(
989
+ path17.join(workspaceRootDir, "pnpm-workspace.yaml"),
990
+ path17.join(isolateDir, "pnpm-workspace.yaml")
947
991
  );
948
992
  }
949
- const npmrcPath = path15.join(workspaceRootDir, ".npmrc");
950
- if (fs15.existsSync(npmrcPath)) {
951
- fs15.copyFileSync(npmrcPath, path15.join(isolateDir, ".npmrc"));
993
+ const npmrcPath = path17.join(workspaceRootDir, ".npmrc");
994
+ if (fs16.existsSync(npmrcPath)) {
995
+ fs16.copyFileSync(npmrcPath, path17.join(isolateDir, ".npmrc"));
952
996
  log.debug("Copied .npmrc file to the isolate output");
953
997
  }
954
998
  log.debug(
955
999
  "Deleting temp directory",
956
1000
  getRootRelativePath(tmpDir, workspaceRootDir)
957
1001
  );
958
- await fs15.remove(tmpDir);
1002
+ await fs16.remove(tmpDir);
959
1003
  log.info("Isolate completed at", isolateDir);
960
1004
  return isolateDir;
961
1005
  }