-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
Copy pathcheck-versions.js
66 lines (56 loc) · 1.72 KB
/
check-versions.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
const { writeFileSync } = require(`fs`)
const yargs = require(`yargs`)
const { getPackages } = require(`@lerna/project`)
const PackageGraph = require(`@lerna/package-graph`)
const semver = require(`semver`)
let warned = false
const argv = yargs
.option(`fix`, {
default: false,
describe: `Fixes outdated dependencies`,
})
.option(`allow-next`, {
default: false,
describe: `Allow using "next" versions. Use this only for alpha/beta releases`,
}).argv
getPackages(process.cwd()).then(packages => {
const graph = new PackageGraph(packages, `allDependencies`, true)
graph.forEach((pkgNode, name) => {
let outdated = Array.from(pkgNode.localDependencies.values()).filter(
localDep =>
!semver.satisfies(graph.get(localDep.name).version, localDep.fetchSpec)
)
if (argv[`allow-next`]) {
outdated = outdated.filter(localDep => localDep.fetchSpec !== `next`)
}
if (!outdated.length) return
const msg = outdated
.map(
p =>
` Depends on "${p.name}@${p.fetchSpec}" \n` +
` instead of "${p.name}@${graph.get(p.name).version}". \n`
)
.join(`\n`)
console.error(`${pkgNode.name}: \n${msg}`)
warned = true
if (argv.fix) {
const pkg = pkgNode.pkg
const next = pkg.toJSON()
const depTypes = [`dependencies`, `devDependencies`, `peerDependencies`]
outdated.forEach(p => {
depTypes.forEach(depKey => {
if (pkg[depKey] && pkg[depKey][p.name]) {
next[depKey][p.name] = `^${graph.get(p.name).version}`
}
})
})
writeFileSync(
`${pkg.location}/package.json`,
JSON.stringify(next, null, 2)
)
}
})
if (warned) {
process.exit(1)
}
})