isolate-package 1.10.1 → 1.11.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(
@@ -647,17 +651,17 @@ async function adaptInternalPackageManifests(internalPackageNames, packagesRegis
647
651
  await Promise.all(
648
652
  internalPackageNames.map(async (packageName) => {
649
653
  const { manifest, rootRelativeDir } = packagesRegistry[packageName];
650
- const inputManifest = omit(manifest, ["scripts", "devDependencies"]);
654
+ const strippedManifest = omit(manifest, ["scripts", "devDependencies"]);
651
655
  const outputManifest = packageManager2.name === "pnpm" && !forceNpm ? (
652
656
  /**
653
657
  * For PNPM the output itself is a workspace so we can preserve the specifiers
654
658
  * with "workspace:*" in the output manifest.
655
659
  */
656
- inputManifest
660
+ strippedManifest
657
661
  ) : (
658
662
  /** For other package managers we replace the links to internal dependencies */
659
663
  adaptManifestInternalDeps({
660
- manifest: inputManifest,
664
+ manifest: strippedManifest,
661
665
  packagesRegistry,
662
666
  parentRootRelativeDir: rootRelativeDir
663
667
  })
@@ -670,17 +674,41 @@ async function adaptInternalPackageManifests(internalPackageNames, packagesRegis
670
674
  );
671
675
  }
672
676
 
677
+ // src/lib/manifest/helpers/adopt-pnpm-fields-from-root.ts
678
+ import path11 from "path";
679
+ async function adoptPnpmFieldsFromRoot(targetPackageManifest, workspaceRootDir) {
680
+ const rootPackageManifest = await readTypedJson(
681
+ path11.join(workspaceRootDir, "package.json")
682
+ );
683
+ const overrides = rootPackageManifest.pnpm?.overrides;
684
+ if (!overrides) {
685
+ return targetPackageManifest;
686
+ }
687
+ return {
688
+ ...targetPackageManifest,
689
+ pnpm: {
690
+ overrides
691
+ }
692
+ };
693
+ }
694
+
673
695
  // src/lib/manifest/adapt-target-package-manifest.ts
674
- async function adaptTargetPackageManifest(manifest, packagesRegistry, isolateDir) {
696
+ async function adaptTargetPackageManifest({
697
+ manifest,
698
+ packagesRegistry,
699
+ isolateDir,
700
+ workspaceRootDir
701
+ }) {
675
702
  const packageManager2 = usePackageManager();
676
703
  const { includeDevDependencies, forceNpm, pickFromScripts, omitFromScripts } = useConfig();
677
704
  const inputManifest = includeDevDependencies ? manifest : omit2(manifest, ["devDependencies"]);
678
705
  const adaptedManifest = packageManager2.name === "pnpm" && !forceNpm ? (
679
706
  /**
680
707
  * For PNPM the output itself is a workspace so we can preserve the specifiers
681
- * with "workspace:*" in the output manifest.
708
+ * with "workspace:*" in the output manifest, but we do want to adopt the
709
+ * pnpm.overrides field from the root package.json.
682
710
  */
683
- inputManifest
711
+ await adoptPnpmFieldsFromRoot(inputManifest, workspaceRootDir)
684
712
  ) : (
685
713
  /** For other package managers we replace the links to internal dependencies */
686
714
  adaptManifestInternalDeps({
@@ -701,22 +729,22 @@ async function adaptTargetPackageManifest(manifest, packagesRegistry, isolateDir
701
729
 
702
730
  // src/lib/output/get-build-output-dir.ts
703
731
  import fs11 from "fs-extra";
704
- import path11 from "node:path";
732
+ import path12 from "node:path";
705
733
  import outdent from "outdent";
706
734
  async function getBuildOutputDir(targetPackageDir) {
707
735
  const config = useConfig();
708
736
  const log = useLogger();
709
737
  if (config.buildDirName) {
710
738
  log.debug("Using buildDirName from config:", config.buildDirName);
711
- return path11.join(targetPackageDir, config.buildDirName);
739
+ return path12.join(targetPackageDir, config.buildDirName);
712
740
  }
713
- const tsconfigPath = path11.join(targetPackageDir, config.tsconfigPath);
741
+ const tsconfigPath = path12.join(targetPackageDir, config.tsconfigPath);
714
742
  if (fs11.existsSync(tsconfigPath)) {
715
743
  log.debug("Found tsconfig at:", config.tsconfigPath);
716
744
  const tsconfig = await readTypedJson(tsconfigPath);
717
745
  const outDir = tsconfig.compilerOptions?.outDir;
718
746
  if (outDir) {
719
- return path11.join(targetPackageDir, outDir);
747
+ return path12.join(targetPackageDir, outDir);
720
748
  } else {
721
749
  throw new Error(outdent`
722
750
  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 +800,7 @@ async function packDependencies({
772
800
 
773
801
  // src/lib/output/process-build-output-files.ts
774
802
  import fs12 from "fs-extra";
775
- import path12 from "node:path";
803
+ import path13 from "node:path";
776
804
  var TIMEOUT_MS = 5e3;
777
805
  async function processBuildOutputFiles({
778
806
  targetPackageDir,
@@ -781,7 +809,7 @@ async function processBuildOutputFiles({
781
809
  }) {
782
810
  const log = useLogger();
783
811
  const packedFilePath = await pack(targetPackageDir, tmpDir);
784
- const unpackDir = path12.join(tmpDir, "target");
812
+ const unpackDir = path13.join(tmpDir, "target");
785
813
  const now = Date.now();
786
814
  let isWaitingYet = false;
787
815
  while (!fs12.existsSync(packedFilePath) && Date.now() - now < TIMEOUT_MS) {
@@ -792,19 +820,19 @@ async function processBuildOutputFiles({
792
820
  await new Promise((resolve) => setTimeout(resolve, 100));
793
821
  }
794
822
  await unpack(packedFilePath, unpackDir);
795
- await fs12.copy(path12.join(unpackDir, "package"), isolateDir);
823
+ await fs12.copy(path13.join(unpackDir, "package"), isolateDir);
796
824
  }
797
825
 
798
826
  // src/lib/output/unpack-dependencies.ts
799
827
  import fs13 from "fs-extra";
800
- import path13, { join } from "node:path";
828
+ import path14, { join } from "node:path";
801
829
  async function unpackDependencies(packedFilesByName, packagesRegistry, tmpDir, isolateDir) {
802
830
  const log = useLogger();
803
831
  await Promise.all(
804
832
  Object.entries(packedFilesByName).map(async ([packageName, filePath]) => {
805
833
  const dir = packagesRegistry[packageName].rootRelativeDir;
806
834
  const unpackDir = join(tmpDir, dir);
807
- log.debug("Unpacking", `(temp)/${path13.basename(filePath)}`);
835
+ log.debug("Unpacking", `(temp)/${path14.basename(filePath)}`);
808
836
  await unpack(filePath, unpackDir);
809
837
  const destinationDir = join(isolateDir, dir);
810
838
  await fs13.ensureDir(destinationDir);
@@ -824,25 +852,25 @@ async function unpackDependencies(packedFilesByName, packagesRegistry, tmpDir, i
824
852
  // src/lib/registry/create-packages-registry.ts
825
853
  import fs14 from "fs-extra";
826
854
  import { globSync } from "glob";
827
- import path15 from "node:path";
855
+ import path16 from "node:path";
828
856
 
829
857
  // src/lib/registry/helpers/find-packages-globs.ts
830
858
  import assert5 from "node:assert";
831
- import path14 from "node:path";
859
+ import path15 from "node:path";
832
860
  function findPackagesGlobs(workspaceRootDir) {
833
861
  const log = useLogger();
834
862
  const packageManager2 = usePackageManager();
835
863
  switch (packageManager2.name) {
836
864
  case "pnpm": {
837
865
  const { packages: globs } = readTypedYamlSync(
838
- path14.join(workspaceRootDir, "pnpm-workspace.yaml")
866
+ path15.join(workspaceRootDir, "pnpm-workspace.yaml")
839
867
  );
840
868
  log.debug("Detected pnpm packages globs:", inspectValue(globs));
841
869
  return globs;
842
870
  }
843
871
  case "yarn":
844
872
  case "npm": {
845
- const workspaceRootManifestPath = path14.join(
873
+ const workspaceRootManifestPath = path15.join(
846
874
  workspaceRootDir,
847
875
  "package.json"
848
876
  );
@@ -882,7 +910,7 @@ async function createPackagesRegistry(workspaceRootDir, workspacePackagesOverrid
882
910
  const allPackages = packagesGlobs.flatMap((glob) => globSync(glob)).filter((dir) => fs14.lstatSync(dir).isDirectory());
883
911
  const registry = (await Promise.all(
884
912
  allPackages.map(async (rootRelativeDir) => {
885
- const manifestPath = path15.join(rootRelativeDir, "package.json");
913
+ const manifestPath = path16.join(rootRelativeDir, "package.json");
886
914
  if (!fs14.existsSync(manifestPath)) {
887
915
  log.warn(
888
916
  `Ignoring directory ./${rootRelativeDir} because it does not contain a package.json file`
@@ -891,12 +919,12 @@ async function createPackagesRegistry(workspaceRootDir, workspacePackagesOverrid
891
919
  } else {
892
920
  log.debug(`Registering package ./${rootRelativeDir}`);
893
921
  const manifest = await readTypedJson(
894
- path15.join(rootRelativeDir, "package.json")
922
+ path16.join(rootRelativeDir, "package.json")
895
923
  );
896
924
  return {
897
925
  manifest,
898
926
  rootRelativeDir,
899
- absoluteDir: path15.join(workspaceRootDir, rootRelativeDir)
927
+ absoluteDir: path16.join(workspaceRootDir, rootRelativeDir)
900
928
  };
901
929
  }
902
930
  })
@@ -941,11 +969,11 @@ async function isolate(options = {}) {
941
969
  setLogLevel(config.logLevel);
942
970
  const log = useLogger();
943
971
  const thisPackageManifest = await readTypedJson(
944
- path16.join(path16.join(__dirname, "..", "package.json"))
972
+ path17.join(path17.join(__dirname, "..", "package.json"))
945
973
  );
946
974
  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);
975
+ const targetPackageDir = config.targetPackagePath ? path17.join(process.cwd(), config.targetPackagePath) : process.cwd();
976
+ const workspaceRootDir = config.targetPackagePath ? process.cwd() : path17.join(targetPackageDir, config.workspaceRoot);
949
977
  const buildOutputDir = await getBuildOutputDir(targetPackageDir);
950
978
  assert6(
951
979
  fs15.existsSync(buildOutputDir),
@@ -956,7 +984,7 @@ async function isolate(options = {}) {
956
984
  "Isolate target package",
957
985
  getRootRelativePath(targetPackageDir, workspaceRootDir)
958
986
  );
959
- const isolateDir = path16.join(targetPackageDir, config.isolateDirName);
987
+ const isolateDir = path17.join(targetPackageDir, config.isolateDirName);
960
988
  log.debug(
961
989
  "Isolate output directory",
962
990
  getRootRelativePath(isolateDir, workspaceRootDir)
@@ -966,10 +994,10 @@ async function isolate(options = {}) {
966
994
  log.debug("Cleaned the existing isolate output directory");
967
995
  }
968
996
  await fs15.ensureDir(isolateDir);
969
- const tmpDir = path16.join(isolateDir, "__tmp");
997
+ const tmpDir = path17.join(isolateDir, "__tmp");
970
998
  await fs15.ensureDir(tmpDir);
971
999
  const targetPackageManifest = await readTypedJson(
972
- path16.join(targetPackageDir, "package.json")
1000
+ path17.join(targetPackageDir, "package.json")
973
1001
  );
974
1002
  const packageManager2 = detectPackageManager(workspaceRootDir);
975
1003
  log.debug(
@@ -1009,11 +1037,12 @@ async function isolate(options = {}) {
1009
1037
  tmpDir,
1010
1038
  isolateDir
1011
1039
  });
1012
- await adaptTargetPackageManifest(
1013
- targetPackageManifest,
1040
+ await adaptTargetPackageManifest({
1041
+ manifest: targetPackageManifest,
1014
1042
  packagesRegistry,
1015
- isolateDir
1016
- );
1043
+ isolateDir,
1044
+ workspaceRootDir
1045
+ });
1017
1046
  const usedFallbackToNpm = await processLockfile({
1018
1047
  workspaceRootDir,
1019
1048
  isolateDir,
@@ -1030,13 +1059,13 @@ async function isolate(options = {}) {
1030
1059
  }
1031
1060
  if (packageManager2.name === "pnpm" && !config.forceNpm) {
1032
1061
  fs15.copyFileSync(
1033
- path16.join(workspaceRootDir, "pnpm-workspace.yaml"),
1034
- path16.join(isolateDir, "pnpm-workspace.yaml")
1062
+ path17.join(workspaceRootDir, "pnpm-workspace.yaml"),
1063
+ path17.join(isolateDir, "pnpm-workspace.yaml")
1035
1064
  );
1036
1065
  }
1037
- const npmrcPath = path16.join(workspaceRootDir, ".npmrc");
1066
+ const npmrcPath = path17.join(workspaceRootDir, ".npmrc");
1038
1067
  if (fs15.existsSync(npmrcPath)) {
1039
- fs15.copyFileSync(npmrcPath, path16.join(isolateDir, ".npmrc"));
1068
+ fs15.copyFileSync(npmrcPath, path17.join(isolateDir, ".npmrc"));
1040
1069
  log.debug("Copied .npmrc file to the isolate output");
1041
1070
  }
1042
1071
  log.debug(