-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathgenerate-npm-lockfile.ts
51 lines (40 loc) · 1.33 KB
/
generate-npm-lockfile.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import Arborist from "@npmcli/arborist";
import fs from "fs-extra";
import path from "node:path";
import { useLogger } from "~/lib/logger";
import { getErrorMessage } from "~/lib/utils";
import { loadNpmConfig } from "./load-npm-config";
/**
* Generate an isolated / pruned lockfile, based on the contents of installed
* node_modules from the monorepo root plus the adapted package manifest in the
* isolate directory.
*/
export async function generateNpmLockfile({
workspaceRootDir,
isolateDir,
}: {
workspaceRootDir: string;
isolateDir: string;
}) {
const log = useLogger();
log.debug("Generating NPM lockfile...");
const nodeModulesPath = path.join(workspaceRootDir, "node_modules");
try {
if (!fs.existsSync(nodeModulesPath)) {
throw new Error(`Failed to find node_modules at ${nodeModulesPath}`);
}
const config = await loadNpmConfig({ npmPath: workspaceRootDir });
const arborist = new Arborist({
path: isolateDir,
...config.flat,
});
const { meta } = await arborist.buildIdealTree();
meta?.commit();
const lockfilePath = path.join(isolateDir, "package-lock.json");
await fs.writeFile(lockfilePath, String(meta));
log.debug("Created lockfile at", lockfilePath);
} catch (err) {
log.error(`Failed to generate lockfile: ${getErrorMessage(err)}`);
throw err;
}
}