-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
137 lines (122 loc) · 3.54 KB
/
index.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
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
132
133
134
135
136
137
#!/usr/bin/env node
import * as lf from "@yarnpkg/lockfile";
import * as process from "process";
import * as path from "path";
import * as fs from "fs";
import * as cp from "child_process";
import { keyBy, pick } from "lodash";
import * as sv from "semver";
import { unparse } from "./argv";
export type AuditEntry = {
data: {
advisory: {
module_name: string;
vulnerable_versions: string;
patched_versions: string;
};
};
};
export type LockfileObject = {
[versionInfo: string]: {
version: string;
resolved: string;
integrity: string;
dependencies: string[];
};
};
export type IFixOptions = {
cwd?: string;
dryRun?: boolean;
force?: boolean;
groups?: string | string[];
level?: string;
verbose?: boolean;
};
export const run = (opts: IFixOptions): void => {
const { cwd = process.cwd() } = opts;
const yarnlockPath = path.resolve(cwd, "yarn.lock");
const yarnAuditAllowedOpts = ["cwd", "groups", "level", "verbose"];
const yarnAuditCmd = unparse(
{ ...pick(opts, yarnAuditAllowedOpts), json: true, _: [] },
{ command: "yarn audit" }
).join(" ");
const spawnOptions = {
shell: true,
maxBuffer: 128 * 1024 * 1024,
};
let data = lf.parse(fs.readFileSync(yarnlockPath, "utf-8"));
if (data.type != "success") {
console.error("Merge conflict in yarn lockfile, aborting");
process.exit(1);
}
console.log("Downloading audit...");
let audit = cp.spawnSync(yarnAuditCmd, spawnOptions);
if (audit.error) {
console.error("Error retrieving audit: ", audit.error.message);
process.exit(1);
}
function attempt<T>(f: () => T): T | null {
try {
return f();
} catch {
return null;
}
}
let auditDict = keyBy(
audit.stdout
.toString()
.split("\n")
.map((item) => attempt(() => JSON.parse(item)) as AuditEntry)
.map((item) => item?.data?.advisory)
.filter((item) => item != null)
.map((item) => ({
module_name: item.module_name,
vulnerable_versions: item.vulnerable_versions,
patched_versions: item.patched_versions,
})),
(item) => item.module_name
);
if (Object.keys(auditDict).length < 1) {
console.log("Audit check found no issues");
process.exit(0);
}
let lockfile = data.object as LockfileObject;
let upgradeVersions = [];
for (let depSpec of Object.keys(lockfile)) {
// console.log("Testing depspec", depSpec);
let [pkgName, desiredRange] = depSpec.split("@");
let pkgAudit = auditDict[pkgName];
if (!pkgAudit) continue;
let pkgSpec = lockfile[depSpec];
if (sv.satisfies(pkgSpec.version, pkgAudit.vulnerable_versions)) {
let fix = sv.minVersion(pkgAudit.patched_versions)?.format();
if (fix == null) {
console.error(
"Can't find satisfactory version for",
pkgAudit.module_name,
pkgAudit.patched_versions
);
continue;
}
if (!sv.satisfies(fix, desiredRange) && !opts.force) {
console.error(
"Cant find patched version that satisfies",
depSpec,
"in",
pkgAudit.patched_versions
);
continue;
}
upgradeVersions.push(`${pkgName}@${fix}`);
pkgSpec.version = fix;
pkgSpec.dependencies = [];
pkgSpec.integrity = "";
pkgSpec.resolved = "";
}
}
if (!opts.dryRun) {
fs.writeFileSync(yarnlockPath, lf.stringify(lockfile));
}
console.log("Installing upgrades:", upgradeVersions.join(", "));
cp.spawnSync(`yarn install --update-checksums --cwd ${cwd}`, spawnOptions);
};