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