-
Notifications
You must be signed in to change notification settings - Fork 89
/
validate-lock-file.js
52 lines (48 loc) · 1.3 KB
/
validate-lock-file.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
const fs = require('fs')
const path = require('path')
const url = require('url')
const lockfile = require('@yarnpkg/lockfile')
const errorsSymbol = Symbol('errors')
const packageRegistryShouldBeGlobal = (entry) => {
const { resolved, [errorsSymbol]: errors } = entry
const link = url.parse(resolved)
if (link.host !== 'registry.yarnpkg.com') {
errors.push(
`package registry should be 'registry.yarnpkg.com' but locked to ${link.host}`
)
}
return entry
}
const mapYarnLockEntries = (...callbacks) => {
const contents = fs.readFileSync(path.resolve('yarn.lock'), 'utf-8')
const yarnLock = lockfile.parse(contents)
return Object.keys(yarnLock.object).map((key) => {
const entry = {
...yarnLock.object[key],
name: key,
[errorsSymbol]: [],
}
callbacks.map((cb) => cb(entry))
return entry
})
}
try {
const errors = mapYarnLockEntries(packageRegistryShouldBeGlobal).reduce(
(output, entry) => {
entry[errorsSymbol].map((error) =>
output.push(`[${entry.name}]: ${error}`)
)
return output
},
[]
)
if (errors.length) {
// eslint-disable-next-line
errors.map((error) => console.error(error))
process.exit(1)
}
} catch (error) {
// eslint-disable-next-line
console.error(error.message)
process.exit(1)
}