diff --git a/package.json b/package.json index bc7ea27..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", @@ -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..4c46c12 100644 --- a/src/lib/lockfile/helpers/generate-npm-lockfile.ts +++ b/src/lib/lockfile/helpers/generate-npm-lockfile.ts @@ -21,11 +21,14 @@ export async function generateNpmLockfile({ log.debug("Generating NPM lockfile..."); - const nodeModulesPath = path.join(workspaceRootDir, "node_modules"); + const originalLockfilePath = 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(originalLockfilePath)) { + throw new Error( + `Failed to find package-lock.json at ${originalLockfilePath}` + ); } const config = await loadNpmConfig({ npmPath: workspaceRootDir }); @@ -35,15 +38,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(originalLockfilePath, 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;