-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvalidate-lock-file.ts
58 lines (53 loc) · 1.51 KB
/
validate-lock-file.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
import { readFileSync } from 'fs'
import * as path from 'path'
import { URL } from 'url'
import { parse as parseLockFile } from '@yarnpkg/lockfile'
const errorsSymbol = Symbol('errors')
type LockFileEntry = {
name: string
resolved: string
[errorsSymbol]: Array<string>
}
type EntryProcessor = (entry: LockFileEntry) => void
const packageRegistryShouldBeGlobal: EntryProcessor = (entry) => {
const { resolved, [errorsSymbol]: errors } = entry
const link = new URL(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: Array<EntryProcessor>
): Array<LockFileEntry> => {
const contents = readFileSync(path.resolve('yarn.lock'), 'utf-8')
const yarnLock = parseLockFile(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<
Array<string>
>((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)
}