forked from tnobody/lerna-audit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
executable file
·131 lines (107 loc) · 3.89 KB
/
main.js
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/env node
const { restorePackageJson } = require("./lib/restore-package-json.function")
const { restoreOriginalPackageJson } = require("./lib/restore-original-package-json.function")
const { analyzeJson } = require("./lib/analyzeJson")
const { writeJson } = require("./lib/writeJson")
const { spawnSync } = require("child_process")
const { promises } = require("fs")
const { join } = require("path")
// const { argv } = require("yargs").boolean("fix").default("fix", false)
let packagePaths
process.on("unhandledRejection", (error) => {
console.error(error)
process.exit(1)
})
async function dieGracefully() {
await restoreOriginalPackageJson(packagePaths)
process.exit(1)
}
process.on("SIGINT", async () => await dieGracefully())
process.on("SIGTERM", async () => await dieGracefully())
function getLernaPackages() {
const result = spawnSync("npx", ["lerna", "ls", "--all", "--json", "--loglevel=silent"], {
stdio: ["pipe", "pipe", "inherit"],
shell: true,
})
if (result.status === 0) {
return JSON.parse(result.stdout)
}
return []
}
function getPackageFilePaths(path) {
return {
backupPath: join(path, "original-package.json"),
originalPath: join(path, "package.json"),
}
}
function packageInJson(packageNames, name) {
return packageNames.some((n) => n === name)
}
function filterInternalLernaDeps(deps, packageNames) {
return packageFilter(deps, (name) => packageInJson(packageNames, name))
}
function filterExternalDeps(deps, packageNames) {
return packageFilter(deps, (name) => !packageInJson(packageNames, name))
}
function packageFilter(originalDepenencies, filter) {
return Object.entries(originalDepenencies || {})
.filter(([name]) => {
return filter(name)
})
.reduce(
(filteredDependencies, [name, version]) => ({
...filteredDependencies,
[name]: version,
}),
{}
)
}
async function lernaAudit() {
const lernaPackages = getLernaPackages()
const lernaPackageNames = lernaPackages.map((p) => p.name)
for (let lernaPackage of lernaPackages) {
packagePaths = getPackageFilePaths(lernaPackage.location)
console.log(`Running ${lernaPackage.name}`)
const jsonOpts = analyzeJson(await promises.readFile(packagePaths.originalPath, "utf8"))
const packageJson = require(packagePaths.originalPath)
await promises.rename(packagePaths.originalPath, packagePaths.backupPath)
const internalLernaDependencies = {
dependencies: filterInternalLernaDeps(packageJson.dependencies, lernaPackageNames),
devDependencies: filterInternalLernaDeps(packageJson.devDependencies, lernaPackageNames),
}
try {
const newPackageJson = {
...packageJson,
dependencies: filterExternalDeps(packageJson.dependencies, lernaPackageNames),
devDependencies: filterExternalDeps(packageJson.devDependencies, lernaPackageNames),
}
await writeJson(packagePaths.originalPath, newPackageJson, jsonOpts)
console.log(`Run audit in ${lernaPackage.location}`)
const betterNpmAuditArgs = process.argv.slice(2)
//const auditResult =
spawnSync("npx", ["better-npm-audit", "audit"].concat(betterNpmAuditArgs), {
cwd: lernaPackage.location,
stdio: "inherit",
shell: true,
})
// fixup disabled
// if (auditResult.status !== 0 && argv.fix) {
// console.log("We will fix this for you")
// spawnSync("npm", ["audit", "fix"], {
// cwd: lernaPackage.location,
// stdio: "inherit",
// shell: true,
// })
// }
const restoredPackageJson = restorePackageJson(packagePaths, internalLernaDependencies)
await writeJson(packagePaths.originalPath, restoredPackageJson, jsonOpts)
await promises.unlink(packagePaths.backupPath)
} catch (e) {
console.error(e)
await restoreOriginalPackageJson(packagePaths)
}
}
}
;(async () => {
await lernaAudit()
})()