isolate-package 1.10.1 → 1.12.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,7 +1,7 @@
1
1
  // src/isolate.ts
2
2
  import fs15 from "fs-extra";
3
3
  import assert6 from "node:assert";
4
- import path16 from "node:path";
4
+ import path17 from "node:path";
5
5
 
6
6
  // src/lib/config.ts
7
7
  import fs5 from "fs-extra";
@@ -82,12 +82,12 @@ function toErrorWithMessage(maybeError) {
82
82
  }
83
83
 
84
84
  // src/lib/utils/get-relative-path.ts
85
- function getRootRelativePath(path17, rootPath) {
86
- const strippedPath = path17.replace(rootPath, "");
85
+ function getRootRelativePath(path18, rootPath) {
86
+ const strippedPath = path18.replace(rootPath, "");
87
87
  return strippedPath.startsWith("/") ? `(root)${strippedPath}` : `(root)/${strippedPath}`;
88
88
  }
89
- function getIsolateRelativePath(path17, isolatePath) {
90
- const strippedPath = path17.replace(isolatePath, "");
89
+ function getIsolateRelativePath(path18, isolatePath) {
90
+ const strippedPath = path18.replace(isolatePath, "");
91
91
  return strippedPath.startsWith("/") ? `(isolate)${strippedPath}` : `(isolate)/${strippedPath}`;
92
92
  }
93
93
 
@@ -103,7 +103,9 @@ import stripJsonComments from "strip-json-comments";
103
103
  function readTypedJsonSync(filePath) {
104
104
  try {
105
105
  const rawContent = fs.readFileSync(filePath, "utf-8");
106
- const data = JSON.parse(stripJsonComments(rawContent));
106
+ const data = JSON.parse(
107
+ stripJsonComments(rawContent, { trailingCommas: true })
108
+ );
107
109
  return data;
108
110
  } catch (err) {
109
111
  throw new Error(
@@ -114,7 +116,9 @@ function readTypedJsonSync(filePath) {
114
116
  async function readTypedJson(filePath) {
115
117
  try {
116
118
  const rawContent = await fs.readFile(filePath, "utf-8");
117
- const data = JSON.parse(rawContent);
119
+ const data = JSON.parse(
120
+ stripJsonComments(rawContent, { trailingCommas: true })
121
+ );
118
122
  return data;
119
123
  } catch (err) {
120
124
  throw new Error(
@@ -257,9 +261,6 @@ function resolveConfig() {
257
261
  return config;
258
262
  }
259
263
 
260
- // src/lib/lockfile/process-lockfile.ts
261
- import { mapValues } from "remeda";
262
-
263
264
  // src/lib/package-manager/helpers/infer-from-files.ts
264
265
  import fs6 from "fs-extra";
265
266
  import { execSync } from "node:child_process";
@@ -388,15 +389,38 @@ import {
388
389
  readWantedLockfile,
389
390
  writeWantedLockfile
390
391
  } from "@pnpm/lockfile-file";
392
+ import { pruneLockfile } from "@pnpm/prune-lockfile";
391
393
  import assert3 from "node:assert";
392
394
  import path6 from "node:path";
393
395
  import { pick } from "remeda";
396
+
397
+ // src/lib/lockfile/helpers/pnpm-map-importer.ts
398
+ import { mapValues } from "remeda";
399
+ function pnpmMapImporter({ dependencies, devDependencies, ...rest }, {
400
+ includeDevDependencies,
401
+ directoryByPackageName
402
+ }) {
403
+ return {
404
+ dependencies: dependencies ? pnpmMapDependenciesLinks(dependencies, directoryByPackageName) : void 0,
405
+ devDependencies: includeDevDependencies && devDependencies ? pnpmMapDependenciesLinks(devDependencies, directoryByPackageName) : void 0,
406
+ ...rest
407
+ };
408
+ }
409
+ function pnpmMapDependenciesLinks(def, directoryByPackageName) {
410
+ return mapValues(
411
+ def,
412
+ (value, key) => value.startsWith("link:") ? `link:./${directoryByPackageName[key]}` : value
413
+ );
414
+ }
415
+
416
+ // src/lib/lockfile/helpers/generate-pnpm-lockfile.ts
394
417
  async function generatePnpmLockfile({
395
418
  workspaceRootDir,
396
419
  targetPackageDir,
397
420
  isolateDir,
398
421
  internalDepPackageNames,
399
- packagesRegistry
422
+ packagesRegistry,
423
+ targetPackageManifest
400
424
  }) {
401
425
  const { includeDevDependencies, includePatchedDependencies } = useConfig();
402
426
  const log = useLogger();
@@ -452,8 +476,14 @@ async function generatePnpmLockfile({
452
476
  }
453
477
  )
454
478
  );
479
+ log.debug("Pruning the lockfile");
480
+ const prunedLockfile = await pruneLockfile(
481
+ lockfile,
482
+ targetPackageManifest,
483
+ "."
484
+ );
455
485
  await writeWantedLockfile(isolateDir, {
456
- ...lockfile,
486
+ ...prunedLockfile,
457
487
  /**
458
488
  * Don't know how to map the patched dependencies yet, so we just include
459
489
  * them but I don't think it would work like this. The important thing for
@@ -495,28 +525,13 @@ async function generateYarnLockfile({
495
525
  }
496
526
 
497
527
  // src/lib/lockfile/process-lockfile.ts
498
- function pnpmMapImporter({ dependencies, devDependencies, ...rest }, {
499
- includeDevDependencies,
500
- directoryByPackageName
501
- }) {
502
- return {
503
- dependencies: dependencies ? pnpmMapDependenciesLinks(dependencies, directoryByPackageName) : void 0,
504
- devDependencies: includeDevDependencies && devDependencies ? pnpmMapDependenciesLinks(devDependencies, directoryByPackageName) : void 0,
505
- ...rest
506
- };
507
- }
508
- function pnpmMapDependenciesLinks(def, directoryByPackageName) {
509
- return mapValues(
510
- def,
511
- (value, key) => value.startsWith("link:") ? `link:./${directoryByPackageName[key]}` : value
512
- );
513
- }
514
528
  async function processLockfile({
515
529
  workspaceRootDir,
516
530
  packagesRegistry,
517
531
  isolateDir,
518
532
  internalDepPackageNames,
519
- targetPackageDir
533
+ targetPackageDir,
534
+ targetPackageManifest
520
535
  }) {
521
536
  const log = useLogger();
522
537
  const { forceNpm } = useConfig();
@@ -563,7 +578,8 @@ async function processLockfile({
563
578
  targetPackageDir,
564
579
  isolateDir,
565
580
  internalDepPackageNames,
566
- packagesRegistry
581
+ packagesRegistry,
582
+ targetPackageManifest
567
583
  });
568
584
  break;
569
585
  }
@@ -647,17 +663,17 @@ async function adaptInternalPackageManifests(internalPackageNames, packagesRegis
647
663
  await Promise.all(
648
664
  internalPackageNames.map(async (packageName) => {
649
665
  const { manifest, rootRelativeDir } = packagesRegistry[packageName];
650
- const inputManifest = omit(manifest, ["scripts", "devDependencies"]);
666
+ const strippedManifest = omit(manifest, ["scripts", "devDependencies"]);
651
667
  const outputManifest = packageManager2.name === "pnpm" && !forceNpm ? (
652
668
  /**
653
669
  * For PNPM the output itself is a workspace so we can preserve the specifiers
654
670
  * with "workspace:*" in the output manifest.
655
671
  */
656
- inputManifest
672
+ strippedManifest
657
673
  ) : (
658
674
  /** For other package managers we replace the links to internal dependencies */
659
675
  adaptManifestInternalDeps({
660
- manifest: inputManifest,
676
+ manifest: strippedManifest,
661
677
  packagesRegistry,
662
678
  parentRootRelativeDir: rootRelativeDir
663
679
  })
@@ -670,17 +686,40 @@ async function adaptInternalPackageManifests(internalPackageNames, packagesRegis
670
686
  );
671
687
  }
672
688
 
689
+ // src/lib/manifest/helpers/adopt-pnpm-fields-from-root.ts
690
+ import path11 from "path";
691
+ async function adoptPnpmFieldsFromRoot(targetPackageManifest, workspaceRootDir) {
692
+ const rootPackageManifest = await readTypedJson(
693
+ path11.join(workspaceRootDir, "package.json")
694
+ );
695
+ const overrides = rootPackageManifest.pnpm?.overrides;
696
+ if (!overrides) {
697
+ return targetPackageManifest;
698
+ }
699
+ return {
700
+ ...targetPackageManifest,
701
+ pnpm: {
702
+ overrides
703
+ }
704
+ };
705
+ }
706
+
673
707
  // src/lib/manifest/adapt-target-package-manifest.ts
674
- async function adaptTargetPackageManifest(manifest, packagesRegistry, isolateDir) {
708
+ async function adaptTargetPackageManifest({
709
+ manifest,
710
+ packagesRegistry,
711
+ workspaceRootDir
712
+ }) {
675
713
  const packageManager2 = usePackageManager();
676
714
  const { includeDevDependencies, forceNpm, pickFromScripts, omitFromScripts } = useConfig();
677
715
  const inputManifest = includeDevDependencies ? manifest : omit2(manifest, ["devDependencies"]);
678
716
  const adaptedManifest = packageManager2.name === "pnpm" && !forceNpm ? (
679
717
  /**
680
718
  * For PNPM the output itself is a workspace so we can preserve the specifiers
681
- * with "workspace:*" in the output manifest.
719
+ * with "workspace:*" in the output manifest, but we do want to adopt the
720
+ * pnpm.overrides field from the root package.json.
682
721
  */
683
- inputManifest
722
+ await adoptPnpmFieldsFromRoot(inputManifest, workspaceRootDir)
684
723
  ) : (
685
724
  /** For other package managers we replace the links to internal dependencies */
686
725
  adaptManifestInternalDeps({
@@ -688,7 +727,7 @@ async function adaptTargetPackageManifest(manifest, packagesRegistry, isolateDir
688
727
  packagesRegistry
689
728
  })
690
729
  );
691
- const outputManifest = {
730
+ return {
692
731
  ...adaptedManifest,
693
732
  /**
694
733
  * Scripts are removed by default if not explicitly picked or omitted via
@@ -696,27 +735,26 @@ async function adaptTargetPackageManifest(manifest, packagesRegistry, isolateDir
696
735
  */
697
736
  scripts: pickFromScripts ? pick2(manifest.scripts ?? {}, pickFromScripts) : omitFromScripts ? omit2(manifest.scripts ?? {}, omitFromScripts) : void 0
698
737
  };
699
- await writeManifest(isolateDir, outputManifest);
700
738
  }
701
739
 
702
740
  // src/lib/output/get-build-output-dir.ts
703
741
  import fs11 from "fs-extra";
704
- import path11 from "node:path";
742
+ import path12 from "node:path";
705
743
  import outdent from "outdent";
706
744
  async function getBuildOutputDir(targetPackageDir) {
707
745
  const config = useConfig();
708
746
  const log = useLogger();
709
747
  if (config.buildDirName) {
710
748
  log.debug("Using buildDirName from config:", config.buildDirName);
711
- return path11.join(targetPackageDir, config.buildDirName);
749
+ return path12.join(targetPackageDir, config.buildDirName);
712
750
  }
713
- const tsconfigPath = path11.join(targetPackageDir, config.tsconfigPath);
751
+ const tsconfigPath = path12.join(targetPackageDir, config.tsconfigPath);
714
752
  if (fs11.existsSync(tsconfigPath)) {
715
753
  log.debug("Found tsconfig at:", config.tsconfigPath);
716
754
  const tsconfig = await readTypedJson(tsconfigPath);
717
755
  const outDir = tsconfig.compilerOptions?.outDir;
718
756
  if (outDir) {
719
- return path11.join(targetPackageDir, outDir);
757
+ return path12.join(targetPackageDir, outDir);
720
758
  } else {
721
759
  throw new Error(outdent`
722
760
  Failed to find outDir in tsconfig. If you are executing isolate from the root of a monorepo you should specify the buildDirName in isolate.config.json.
@@ -772,7 +810,7 @@ async function packDependencies({
772
810
 
773
811
  // src/lib/output/process-build-output-files.ts
774
812
  import fs12 from "fs-extra";
775
- import path12 from "node:path";
813
+ import path13 from "node:path";
776
814
  var TIMEOUT_MS = 5e3;
777
815
  async function processBuildOutputFiles({
778
816
  targetPackageDir,
@@ -781,7 +819,7 @@ async function processBuildOutputFiles({
781
819
  }) {
782
820
  const log = useLogger();
783
821
  const packedFilePath = await pack(targetPackageDir, tmpDir);
784
- const unpackDir = path12.join(tmpDir, "target");
822
+ const unpackDir = path13.join(tmpDir, "target");
785
823
  const now = Date.now();
786
824
  let isWaitingYet = false;
787
825
  while (!fs12.existsSync(packedFilePath) && Date.now() - now < TIMEOUT_MS) {
@@ -792,19 +830,19 @@ async function processBuildOutputFiles({
792
830
  await new Promise((resolve) => setTimeout(resolve, 100));
793
831
  }
794
832
  await unpack(packedFilePath, unpackDir);
795
- await fs12.copy(path12.join(unpackDir, "package"), isolateDir);
833
+ await fs12.copy(path13.join(unpackDir, "package"), isolateDir);
796
834
  }
797
835
 
798
836
  // src/lib/output/unpack-dependencies.ts
799
837
  import fs13 from "fs-extra";
800
- import path13, { join } from "node:path";
838
+ import path14, { join } from "node:path";
801
839
  async function unpackDependencies(packedFilesByName, packagesRegistry, tmpDir, isolateDir) {
802
840
  const log = useLogger();
803
841
  await Promise.all(
804
842
  Object.entries(packedFilesByName).map(async ([packageName, filePath]) => {
805
843
  const dir = packagesRegistry[packageName].rootRelativeDir;
806
844
  const unpackDir = join(tmpDir, dir);
807
- log.debug("Unpacking", `(temp)/${path13.basename(filePath)}`);
845
+ log.debug("Unpacking", `(temp)/${path14.basename(filePath)}`);
808
846
  await unpack(filePath, unpackDir);
809
847
  const destinationDir = join(isolateDir, dir);
810
848
  await fs13.ensureDir(destinationDir);
@@ -824,25 +862,25 @@ async function unpackDependencies(packedFilesByName, packagesRegistry, tmpDir, i
824
862
  // src/lib/registry/create-packages-registry.ts
825
863
  import fs14 from "fs-extra";
826
864
  import { globSync } from "glob";
827
- import path15 from "node:path";
865
+ import path16 from "node:path";
828
866
 
829
867
  // src/lib/registry/helpers/find-packages-globs.ts
830
868
  import assert5 from "node:assert";
831
- import path14 from "node:path";
869
+ import path15 from "node:path";
832
870
  function findPackagesGlobs(workspaceRootDir) {
833
871
  const log = useLogger();
834
872
  const packageManager2 = usePackageManager();
835
873
  switch (packageManager2.name) {
836
874
  case "pnpm": {
837
875
  const { packages: globs } = readTypedYamlSync(
838
- path14.join(workspaceRootDir, "pnpm-workspace.yaml")
876
+ path15.join(workspaceRootDir, "pnpm-workspace.yaml")
839
877
  );
840
878
  log.debug("Detected pnpm packages globs:", inspectValue(globs));
841
879
  return globs;
842
880
  }
843
881
  case "yarn":
844
882
  case "npm": {
845
- const workspaceRootManifestPath = path14.join(
883
+ const workspaceRootManifestPath = path15.join(
846
884
  workspaceRootDir,
847
885
  "package.json"
848
886
  );
@@ -882,7 +920,7 @@ async function createPackagesRegistry(workspaceRootDir, workspacePackagesOverrid
882
920
  const allPackages = packagesGlobs.flatMap((glob) => globSync(glob)).filter((dir) => fs14.lstatSync(dir).isDirectory());
883
921
  const registry = (await Promise.all(
884
922
  allPackages.map(async (rootRelativeDir) => {
885
- const manifestPath = path15.join(rootRelativeDir, "package.json");
923
+ const manifestPath = path16.join(rootRelativeDir, "package.json");
886
924
  if (!fs14.existsSync(manifestPath)) {
887
925
  log.warn(
888
926
  `Ignoring directory ./${rootRelativeDir} because it does not contain a package.json file`
@@ -891,12 +929,12 @@ async function createPackagesRegistry(workspaceRootDir, workspacePackagesOverrid
891
929
  } else {
892
930
  log.debug(`Registering package ./${rootRelativeDir}`);
893
931
  const manifest = await readTypedJson(
894
- path15.join(rootRelativeDir, "package.json")
932
+ path16.join(rootRelativeDir, "package.json")
895
933
  );
896
934
  return {
897
935
  manifest,
898
936
  rootRelativeDir,
899
- absoluteDir: path15.join(workspaceRootDir, rootRelativeDir)
937
+ absoluteDir: path16.join(workspaceRootDir, rootRelativeDir)
900
938
  };
901
939
  }
902
940
  })
@@ -941,11 +979,11 @@ async function isolate(options = {}) {
941
979
  setLogLevel(config.logLevel);
942
980
  const log = useLogger();
943
981
  const thisPackageManifest = await readTypedJson(
944
- path16.join(path16.join(__dirname, "..", "package.json"))
982
+ path17.join(path17.join(__dirname, "..", "package.json"))
945
983
  );
946
984
  log.debug("Using isolate-package version", thisPackageManifest.version);
947
- const targetPackageDir = config.targetPackagePath ? path16.join(process.cwd(), config.targetPackagePath) : process.cwd();
948
- const workspaceRootDir = config.targetPackagePath ? process.cwd() : path16.join(targetPackageDir, config.workspaceRoot);
985
+ const targetPackageDir = config.targetPackagePath ? path17.join(process.cwd(), config.targetPackagePath) : process.cwd();
986
+ const workspaceRootDir = config.targetPackagePath ? process.cwd() : path17.join(targetPackageDir, config.workspaceRoot);
949
987
  const buildOutputDir = await getBuildOutputDir(targetPackageDir);
950
988
  assert6(
951
989
  fs15.existsSync(buildOutputDir),
@@ -956,7 +994,7 @@ async function isolate(options = {}) {
956
994
  "Isolate target package",
957
995
  getRootRelativePath(targetPackageDir, workspaceRootDir)
958
996
  );
959
- const isolateDir = path16.join(targetPackageDir, config.isolateDirName);
997
+ const isolateDir = path17.join(targetPackageDir, config.isolateDirName);
960
998
  log.debug(
961
999
  "Isolate output directory",
962
1000
  getRootRelativePath(isolateDir, workspaceRootDir)
@@ -966,10 +1004,10 @@ async function isolate(options = {}) {
966
1004
  log.debug("Cleaned the existing isolate output directory");
967
1005
  }
968
1006
  await fs15.ensureDir(isolateDir);
969
- const tmpDir = path16.join(isolateDir, "__tmp");
1007
+ const tmpDir = path17.join(isolateDir, "__tmp");
970
1008
  await fs15.ensureDir(tmpDir);
971
1009
  const targetPackageManifest = await readTypedJson(
972
- path16.join(targetPackageDir, "package.json")
1010
+ path17.join(targetPackageDir, "package.json")
973
1011
  );
974
1012
  const packageManager2 = detectPackageManager(workspaceRootDir);
975
1013
  log.debug(
@@ -1009,18 +1047,20 @@ async function isolate(options = {}) {
1009
1047
  tmpDir,
1010
1048
  isolateDir
1011
1049
  });
1012
- await adaptTargetPackageManifest(
1013
- targetPackageManifest,
1050
+ const outputManifest = await adaptTargetPackageManifest({
1051
+ manifest: targetPackageManifest,
1014
1052
  packagesRegistry,
1015
- isolateDir
1016
- );
1053
+ workspaceRootDir
1054
+ });
1055
+ await writeManifest(isolateDir, outputManifest);
1017
1056
  const usedFallbackToNpm = await processLockfile({
1018
1057
  workspaceRootDir,
1019
1058
  isolateDir,
1020
1059
  packagesRegistry,
1021
1060
  internalDepPackageNames: internalPackageNames,
1022
1061
  targetPackageDir,
1023
- targetPackageName: targetPackageManifest.name
1062
+ targetPackageName: targetPackageManifest.name,
1063
+ targetPackageManifest: outputManifest
1024
1064
  });
1025
1065
  if (usedFallbackToNpm) {
1026
1066
  const manifest = await readManifest(isolateDir);
@@ -1030,13 +1070,13 @@ async function isolate(options = {}) {
1030
1070
  }
1031
1071
  if (packageManager2.name === "pnpm" && !config.forceNpm) {
1032
1072
  fs15.copyFileSync(
1033
- path16.join(workspaceRootDir, "pnpm-workspace.yaml"),
1034
- path16.join(isolateDir, "pnpm-workspace.yaml")
1073
+ path17.join(workspaceRootDir, "pnpm-workspace.yaml"),
1074
+ path17.join(isolateDir, "pnpm-workspace.yaml")
1035
1075
  );
1036
1076
  }
1037
- const npmrcPath = path16.join(workspaceRootDir, ".npmrc");
1077
+ const npmrcPath = path17.join(workspaceRootDir, ".npmrc");
1038
1078
  if (fs15.existsSync(npmrcPath)) {
1039
- fs15.copyFileSync(npmrcPath, path16.join(isolateDir, ".npmrc"));
1079
+ fs15.copyFileSync(npmrcPath, path17.join(isolateDir, ".npmrc"));
1040
1080
  log.debug("Copied .npmrc file to the isolate output");
1041
1081
  }
1042
1082
  log.debug(