[6a3a178] | 1 | const {format} = require('util')
|
---|
| 2 | const semver = require('semver')
|
---|
| 3 |
|
---|
| 4 | const checkEngine = (target, npmVer, nodeVer, force = false) => {
|
---|
| 5 | const nodev = force ? null : nodeVer
|
---|
| 6 | const eng = target.engines
|
---|
| 7 | const opt = { includePrerelease: true }
|
---|
| 8 | if (!eng) {
|
---|
| 9 | return
|
---|
| 10 | }
|
---|
| 11 |
|
---|
| 12 | const nodeFail = nodev && eng.node && !semver.satisfies(nodev, eng.node, opt)
|
---|
| 13 | const npmFail = npmVer && eng.npm && !semver.satisfies(npmVer, eng.npm, opt)
|
---|
| 14 | if (nodeFail || npmFail) {
|
---|
| 15 | throw Object.assign(new Error('Unsupported engine'), {
|
---|
| 16 | pkgid: target._id,
|
---|
| 17 | current: { node: nodeVer, npm: npmVer },
|
---|
| 18 | required: eng,
|
---|
| 19 | code: 'EBADENGINE'
|
---|
| 20 | })
|
---|
| 21 | }
|
---|
| 22 | }
|
---|
| 23 |
|
---|
| 24 | const checkPlatform = (target, force = false) => {
|
---|
| 25 | if (force) {
|
---|
| 26 | return
|
---|
| 27 | }
|
---|
| 28 |
|
---|
| 29 | const platform = process.platform
|
---|
| 30 | const arch = process.arch
|
---|
| 31 | const osOk = target.os ? checkList(platform, target.os) : true
|
---|
| 32 | const cpuOk = target.cpu ? checkList(arch, target.cpu) : true
|
---|
| 33 |
|
---|
| 34 | if (!osOk || !cpuOk) {
|
---|
| 35 | throw Object.assign(new Error('Unsupported platform'), {
|
---|
| 36 | pkgid: target._id,
|
---|
| 37 | current: {
|
---|
| 38 | os: platform,
|
---|
| 39 | cpu: arch
|
---|
| 40 | },
|
---|
| 41 | required: {
|
---|
| 42 | os: target.os,
|
---|
| 43 | cpu: target.cpu
|
---|
| 44 | },
|
---|
| 45 | code: 'EBADPLATFORM'
|
---|
| 46 | })
|
---|
| 47 | }
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | const checkList = (value, list) => {
|
---|
| 51 | if (typeof list === 'string') {
|
---|
| 52 | list = [list]
|
---|
| 53 | }
|
---|
| 54 | if (list.length === 1 && list[0] === 'any') {
|
---|
| 55 | return true
|
---|
| 56 | }
|
---|
| 57 | // match none of the negated values, and at least one of the
|
---|
| 58 | // non-negated values, if any are present.
|
---|
| 59 | let negated = 0
|
---|
| 60 | let match = false
|
---|
| 61 | for (const entry of list) {
|
---|
| 62 | const negate = entry.charAt(0) === '!'
|
---|
| 63 | const test = negate ? entry.slice(1) : entry
|
---|
| 64 | if (negate) {
|
---|
| 65 | negated ++
|
---|
| 66 | if (value === test) {
|
---|
| 67 | return false
|
---|
| 68 | }
|
---|
| 69 | } else {
|
---|
| 70 | match = match || value === test
|
---|
| 71 | }
|
---|
| 72 | }
|
---|
| 73 | return match || negated === list.length
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | module.exports = {
|
---|
| 77 | checkEngine,
|
---|
| 78 | checkPlatform
|
---|
| 79 | }
|
---|