Skip to content

Commit

Permalink
Prune pnpm lockfile before writing to disk (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
0x80 authored Mar 30, 2024
1 parent 733724b commit 4e76b5b
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 11 deletions.
6 changes: 4 additions & 2 deletions src/isolate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,13 +156,14 @@ 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({
const outputManifest = await adaptTargetPackageManifest({
manifest: targetPackageManifest,
packagesRegistry,
isolateDir,
workspaceRootDir,
});

await writeManifest(isolateDir, outputManifest);

/** Generate an isolated lockfile based on the original one */
const usedFallbackToNpm = await processLockfile({
workspaceRootDir,
Expand All @@ -171,6 +172,7 @@ export async function isolate(
internalDepPackageNames: internalPackageNames,
targetPackageDir,
targetPackageName: targetPackageManifest.name,
targetPackageManifest: outputManifest,
});

if (usedFallbackToNpm) {
Expand Down
14 changes: 12 additions & 2 deletions src/lib/lockfile/helpers/generate-pnpm-lockfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ import {
readWantedLockfile,
writeWantedLockfile,
} from "@pnpm/lockfile-file";
import { pruneLockfile } from "@pnpm/prune-lockfile";
import assert from "node:assert";
import path from "node:path";
import { pick } from "remeda";
import { useConfig } from "~/lib/config";
import { useLogger } from "~/lib/logger";
import type { PackagesRegistry } from "~/lib/types";
import type { PackageManifest, PackagesRegistry } from "~/lib/types";
import { getErrorMessage } from "~/lib/utils";
import { pnpmMapImporter } from "./pnpm-map-importer";

Expand All @@ -18,12 +19,14 @@ export async function generatePnpmLockfile({
isolateDir,
internalDepPackageNames,
packagesRegistry,
targetPackageManifest,
}: {
workspaceRootDir: string;
targetPackageDir: string;
isolateDir: string;
internalDepPackageNames: string[];
packagesRegistry: PackagesRegistry;
targetPackageManifest: PackageManifest;
}) {
const { includeDevDependencies, includePatchedDependencies } = useConfig();
const log = useLogger();
Expand Down Expand Up @@ -91,8 +94,15 @@ export async function generatePnpmLockfile({
)
);

log.debug("Pruning the lockfile");
const prunedLockfile = await pruneLockfile(
lockfile,
targetPackageManifest,
"."
);

await writeWantedLockfile(isolateDir, {
...lockfile,
...prunedLockfile,
/**
* Don't know how to map the patched dependencies yet, so we just include
* them but I don't think it would work like this. The important thing for
Expand Down
5 changes: 4 additions & 1 deletion src/lib/lockfile/process-lockfile.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useConfig } from "../config";
import { useLogger } from "../logger";
import { usePackageManager } from "../package-manager";
import type { PackagesRegistry } from "../types";
import type { PackageManifest, PackagesRegistry } from "../types";
import {
generateNpmLockfile,
generatePnpmLockfile,
Expand All @@ -21,13 +21,15 @@ export async function processLockfile({
isolateDir,
internalDepPackageNames,
targetPackageDir,
targetPackageManifest,
}: {
workspaceRootDir: string;
packagesRegistry: PackagesRegistry;
isolateDir: string;
internalDepPackageNames: string[];
targetPackageDir: string;
targetPackageName: string;
targetPackageManifest: PackageManifest;
}) {
const log = useLogger();

Expand Down Expand Up @@ -86,6 +88,7 @@ export async function processLockfile({
isolateDir,
internalDepPackageNames,
packagesRegistry,
targetPackageManifest,
});
break;
}
Expand Down
7 changes: 1 addition & 6 deletions src/lib/manifest/adapt-target-package-manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { useConfig } from "../config";
import { usePackageManager } from "../package-manager";
import type { PackageManifest, PackagesRegistry } from "../types";
import { adaptManifestInternalDeps, adoptPnpmFieldsFromRoot } from "./helpers";
import { writeManifest } from "./io";

/**
* Adapt the output package manifest, so that:
Expand All @@ -15,12 +14,10 @@ import { writeManifest } from "./io";
export async function adaptTargetPackageManifest({
manifest,
packagesRegistry,
isolateDir,
workspaceRootDir,
}: {
manifest: PackageManifest;
packagesRegistry: PackagesRegistry;
isolateDir: string;
workspaceRootDir: string;
}) {
const packageManager = usePackageManager();
Expand All @@ -46,7 +43,7 @@ export async function adaptTargetPackageManifest({
packagesRegistry,
});

const outputManifest = {
return {
...adaptedManifest,
/**
* Scripts are removed by default if not explicitly picked or omitted via
Expand All @@ -58,6 +55,4 @@ export async function adaptTargetPackageManifest({
? omit(manifest.scripts ?? {}, omitFromScripts)
: undefined,
};

await writeManifest(isolateDir, outputManifest);
}

0 comments on commit 4e76b5b

Please sign in to comment.