Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make NPM output adhere to lockfile versions #113

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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",
Expand Down
28 changes: 21 additions & 7 deletions src/lib/lockfile/helpers/generate-npm-lockfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand All @@ -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;
Expand Down