-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsync-package-lock-workspaces.mjs
More file actions
62 lines (53 loc) · 1.77 KB
/
Copy pathsync-package-lock-workspaces.mjs
File metadata and controls
62 lines (53 loc) · 1.77 KB
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
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env node
import { readFileSync, writeFileSync } from "node:fs";
import { resolve } from "node:path";
const root = resolve(
process.argv[2] ?? new URL("..", import.meta.url).pathname,
);
const readJson = (path) => JSON.parse(readFileSync(path, "utf8"));
const rootManifest = readJson(resolve(root, "package.json"));
const lockPath = resolve(root, "package-lock.json");
const lock = readJson(lockPath);
if (lock.lockfileVersion !== 3 || typeof lock.packages !== "object") {
throw new Error("Expected an npm lockfileVersion 3 package-lock.json");
}
for (const workspacePath of rootManifest.workspaces ?? []) {
if (workspacePath.includes("*")) {
throw new Error(`Workspace globs are not supported: ${workspacePath}`);
}
const manifest = readJson(resolve(root, workspacePath, "package.json"));
const lockedWorkspace = lock.packages[workspacePath];
if (!lockedWorkspace) {
throw new Error(
`package-lock.json has no entry for workspace ${workspacePath}`,
);
}
lockedWorkspace.version = manifest.version;
for (
const field of [
"dependencies",
"devDependencies",
"optionalDependencies",
"peerDependencies",
]
) {
if (
manifest[field] === undefined ||
Object.keys(manifest[field]).length === 0
) {
delete lockedWorkspace[field];
} else {
lockedWorkspace[field] = manifest[field];
}
}
for (
const [name, version] of Object.entries(manifest.optionalDependencies ?? {})
) {
const nestedPath = `${workspacePath}/node_modules/${name}`;
const nestedPackage = lock.packages[nestedPath];
if (nestedPackage && /^\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?$/.test(version)) {
nestedPackage.version = version;
}
}
}
writeFileSync(lockPath, `${JSON.stringify(lock, null, 2)}\n`);