/
checkEngine.ts
41 lines (36 loc) · 1.23 KB
/
checkEngine.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
import { PnpmError } from '@pnpm/error'
import semver from 'semver'
export class UnsupportedEngineError extends PnpmError {
public wanted: WantedEngine
public current: Engine
public packageId: string
constructor (packageId: string, wanted: WantedEngine, current: Engine) {
super('UNSUPPORTED_ENGINE', `Unsupported engine for ${packageId}: wanted: ${JSON.stringify(wanted)} (current: ${JSON.stringify(current)})`)
this.packageId = packageId
this.wanted = wanted
this.current = current
}
}
export function checkEngine (
packageId: string,
wantedEngine: WantedEngine,
currentEngine: Engine
) {
if (!wantedEngine) return null
const unsatisfiedWanted: WantedEngine = {}
if (wantedEngine.node && !semver.satisfies(currentEngine.node, wantedEngine.node)) {
unsatisfiedWanted.node = wantedEngine.node
}
if (currentEngine.pnpm && wantedEngine.pnpm && !semver.satisfies(currentEngine.pnpm, wantedEngine.pnpm)) {
unsatisfiedWanted.pnpm = wantedEngine.pnpm
}
if (Object.keys(unsatisfiedWanted).length > 0) {
return new UnsupportedEngineError(packageId, unsatisfiedWanted, currentEngine)
}
return null
}
export interface Engine {
node: string
pnpm?: string
}
export type WantedEngine = Partial<Engine>