forked from trufflesuite/ganache
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfilter-shrinkwrap.ts
57 lines (49 loc) · 1.39 KB
/
filter-shrinkwrap.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
52
53
54
55
56
57
import { writeFileSync } from "fs-extra";
type Leaf = {
dev?: boolean;
dependencies?: {
[name: string]: Leaf;
};
};
type Root = {
name: string;
lockfileVersion: number;
requires?: boolean;
} & Leaf;
const shrinkwrapFile = process.argv[2];
if (!shrinkwrapFile || !shrinkwrapFile.endsWith("npm-shrinkwrap.json")) {
throw new Error(
"Usage: ts-node filter-shrinkwrap.json ./path/to/npm-shrinkwrap.json"
);
}
console.log(`loading ${shrinkwrapFile}`);
const shrinkwrap: Root = require(shrinkwrapFile);
let removeCount = 0;
function walk(leaf: Leaf, mutate: (leaf: Leaf) => void) {
const dependencies = leaf.dependencies;
if (dependencies) {
mutate(leaf);
Object.entries(dependencies).forEach(([name, entry]) => {
walk(entry, mutate);
});
}
}
function deleteDevDepsFromLeaf(leaf: Leaf) {
const dependencies = leaf.dependencies;
if (dependencies) {
Object.entries(dependencies).forEach(([name, entry]) => {
if (entry.dev) {
console.log(`removing ${name} from shrinkwrap`);
removeCount++;
delete dependencies[name];
}
});
}
}
console.log(`removing development dependencies`);
walk(shrinkwrap, deleteDevDepsFromLeaf);
console.log(
`removed ${removeCount} dependenc${removeCount === 1 ? "y" : "ies"}`
);
console.log(`writing updated shrinkwrap to ${shrinkwrapFile}`);
writeFileSync(shrinkwrapFile, JSON.stringify(shrinkwrap));