Skip to content

Commit

Permalink
Adopt pnpm overrides from root package manifest (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
0x80 authored Mar 30, 2024
1 parent 32a555e commit f4c6c46
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 11 deletions.
9 changes: 5 additions & 4 deletions src/isolate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,12 @@ export async function isolate(
* Copy the target manifest file to the isolate location and adapt its
* workspace dependencies to point to the isolated packages.
*/
await adaptTargetPackageManifest(
targetPackageManifest,
await adaptTargetPackageManifest({
manifest: targetPackageManifest,
packagesRegistry,
isolateDir
);
isolateDir,
workspaceRootDir,
});

/** Generate an isolated lockfile based on the original one */
const usedFallbackToNpm = await processLockfile({
Expand Down
22 changes: 15 additions & 7 deletions src/lib/manifest/adapt-target-package-manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useConfig } from "../config";
import { usePackageManager } from "../package-manager";
import type { PackageManifest, PackagesRegistry } from "../types";
import { adaptManifestInternalDeps } from "./helpers";
import { adoptPnpmFieldsFromRoot } from "./helpers/adopt-pnpm-fields-from-root";
import { writeManifest } from "./io";

/**
Expand All @@ -12,11 +13,17 @@ import { writeManifest } from "./io";
* - The devDependencies are possibly removed
* - Scripts are picked or omitted and otherwise removed
*/
export async function adaptTargetPackageManifest(
manifest: PackageManifest,
packagesRegistry: PackagesRegistry,
isolateDir: string
) {
export async function adaptTargetPackageManifest({
manifest,
packagesRegistry,
isolateDir,
workspaceRootDir,
}: {
manifest: PackageManifest;
packagesRegistry: PackagesRegistry;
isolateDir: string;
workspaceRootDir: string;
}) {
const packageManager = usePackageManager();
const { includeDevDependencies, forceNpm, pickFromScripts, omitFromScripts } =
useConfig();
Expand All @@ -30,9 +37,10 @@ export async function adaptTargetPackageManifest(
packageManager.name === "pnpm" && !forceNpm
? /**
* For PNPM the output itself is a workspace so we can preserve the specifiers
* with "workspace:*" in the output manifest.
* with "workspace:*" in the output manifest, but we do want to adopt the
* pnpm.overrides field from the root package.json.
*/
inputManifest
await adoptPnpmFieldsFromRoot(inputManifest, workspaceRootDir)
: /** For other package managers we replace the links to internal dependencies */
adaptManifestInternalDeps({
manifest: inputManifest,
Expand Down
31 changes: 31 additions & 0 deletions src/lib/manifest/helpers/adopt-pnpm-fields-from-root.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import type { ProjectManifest } from "@pnpm/types";
import path from "path";
import type { PackageManifest } from "~/lib/types";
import { readTypedJson } from "~/lib/utils";

/**
* Adopts the `pnpm` fields from the root package manifest. Currently it only
* takes overrides, because I don't know if any of the others are useful or
* desired.
*/
export async function adoptPnpmFieldsFromRoot(
targetPackageManifest: PackageManifest,
workspaceRootDir: string
) {
const rootPackageManifest = await readTypedJson<ProjectManifest>(
path.join(workspaceRootDir, "package.json")
);

const overrides = rootPackageManifest.pnpm?.overrides;

if (!overrides) {
return targetPackageManifest;
}

return {
...targetPackageManifest,
pnpm: {
overrides,
},
};
}

0 comments on commit f4c6c46

Please sign in to comment.