From b77d02488eeace34ace52a1f7c2b41c0a995ff84 Mon Sep 17 00:00:00 2001 From: Thijs Koerselman Date: Sun, 17 Nov 2024 12:28:06 +0100 Subject: [PATCH 1/3] Use loadVirtual on original lockfile --- package.json | 2 +- .../lockfile/helpers/generate-npm-lockfile.ts | 32 ++++++++++++++----- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/package.json b/package.json index bc7ea27..d06b3c3 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,7 @@ "lint": "eslint . --max-warnings 0", "lint:format": "prettier --check .", "compile": "tsc --noEmit", - "prepare": "pnpm run compile && pnpm run build" + "?prepare": "pnpm run compile && pnpm run build" }, "dependencies": { "@npmcli/arborist": "^7.5.4", diff --git a/src/lib/lockfile/helpers/generate-npm-lockfile.ts b/src/lib/lockfile/helpers/generate-npm-lockfile.ts index 11584c1..21c64a7 100644 --- a/src/lib/lockfile/helpers/generate-npm-lockfile.ts +++ b/src/lib/lockfile/helpers/generate-npm-lockfile.ts @@ -19,13 +19,18 @@ export async function generateNpmLockfile({ }) { const log = useLogger(); - log.debug("Generating NPM lockfile..."); + log.debug("Generating NPM lockfile the new way..."); - const nodeModulesPath = path.join(workspaceRootDir, "node_modules"); + // const nodeModulesPath = path.join(workspaceRootDir, "node_modules"); + + const origLockfilePath = path.join(workspaceRootDir, "package-lock.json"); + const isolatedLockfilePath = path.join(isolateDir, "package-lock.json"); try { - if (!fs.existsSync(nodeModulesPath)) { - throw new Error(`Failed to find node_modules at ${nodeModulesPath}`); + if (!fs.existsSync(origLockfilePath)) { + throw new Error( + `Failed to find package-lock.json at ${origLockfilePath}` + ); } const config = await loadNpmConfig({ npmPath: workspaceRootDir }); @@ -35,15 +40,26 @@ export async function generateNpmLockfile({ ...config.flat, }); + /** + * One way to get NPM to match the lockfile versions seems to be to copy the + * original lockfile to the isolate directory and run loadVirtual before + * buildIdealTree + */ + await fs.copy(origLockfilePath, isolatedLockfilePath, { + overwrite: true, + }); + + log.debug("Load virtual tree"); + await arborist.loadVirtual(); + + log.debug("Build ideal tree"); const { meta } = await arborist.buildIdealTree(); meta?.commit(); - const lockfilePath = path.join(isolateDir, "package-lock.json"); - - await fs.writeFile(lockfilePath, String(meta)); + await fs.writeFile(isolatedLockfilePath, String(meta)); - log.debug("Created lockfile at", lockfilePath); + log.debug("Created lockfile at", isolatedLockfilePath); } catch (err) { log.error(`Failed to generate lockfile: ${getErrorMessage(err)}`); throw err; From fbbdb288abff33b6d706e6277e43ab2ff1aecd04 Mon Sep 17 00:00:00 2001 From: Thijs Koerselman Date: Sun, 17 Nov 2024 12:28:23 +0100 Subject: [PATCH 2/3] 1.20.0-2 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index d06b3c3..7d16f0a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "isolate-package", - "version": "1.20.0-1", + "version": "1.20.0-2", "description": "Isolate a monorepo package with its shared dependencies to form a self-contained directory, compatible with Firebase deploy", "author": "Thijs Koerselman", "license": "MIT", From b3bc2d29dcd4974f75f7e6ceae8192731a4ad696 Mon Sep 17 00:00:00 2001 From: Thijs Koerselman Date: Sun, 17 Nov 2024 12:39:21 +0100 Subject: [PATCH 3/3] Clean up --- src/lib/lockfile/helpers/generate-npm-lockfile.ts | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/lib/lockfile/helpers/generate-npm-lockfile.ts b/src/lib/lockfile/helpers/generate-npm-lockfile.ts index 21c64a7..4c46c12 100644 --- a/src/lib/lockfile/helpers/generate-npm-lockfile.ts +++ b/src/lib/lockfile/helpers/generate-npm-lockfile.ts @@ -19,17 +19,15 @@ export async function generateNpmLockfile({ }) { const log = useLogger(); - log.debug("Generating NPM lockfile the new way..."); + log.debug("Generating NPM lockfile..."); - // const nodeModulesPath = path.join(workspaceRootDir, "node_modules"); - - const origLockfilePath = path.join(workspaceRootDir, "package-lock.json"); + const originalLockfilePath = path.join(workspaceRootDir, "package-lock.json"); const isolatedLockfilePath = path.join(isolateDir, "package-lock.json"); try { - if (!fs.existsSync(origLockfilePath)) { + if (!fs.existsSync(originalLockfilePath)) { throw new Error( - `Failed to find package-lock.json at ${origLockfilePath}` + `Failed to find package-lock.json at ${originalLockfilePath}` ); } @@ -45,7 +43,7 @@ export async function generateNpmLockfile({ * original lockfile to the isolate directory and run loadVirtual before * buildIdealTree */ - await fs.copy(origLockfilePath, isolatedLockfilePath, { + await fs.copy(originalLockfilePath, isolatedLockfilePath, { overwrite: true, });