Skip to content

Commit

Permalink
feat: allow checkPlatform to override execution environment (#65)
Browse files Browse the repository at this point in the history
This PR allows `checkPlatform` to override execution environment which
comes from `process.platform` and `process.arch` by default.

This will be required to achieve npm/rfcs#612
  • Loading branch information
yukukotani committed Aug 7, 2023
1 parent 5965ee3 commit 7a3f5ed
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
4 changes: 3 additions & 1 deletion README.md
Expand Up @@ -19,10 +19,12 @@ npm is unable to install the package properly for some reason.

Error code: 'EBADENGINE'

### .checkPlatform(pkg, force)
### .checkPlatform(pkg, force, environment)

Check if a package's `os`, `cpu` and `libc` match the running system.

`force` argument skips all checks.

`environment` overrides the execution environment which comes from `process.platform` and `process.arch` by default. `environment.os` and `environment.cpu` are available.

Error code: 'EBADPLATFORM'
6 changes: 3 additions & 3 deletions lib/index.js
Expand Up @@ -22,13 +22,13 @@ const checkEngine = (target, npmVer, nodeVer, force = false) => {

const isMusl = (file) => file.includes('libc.musl-') || file.includes('ld-musl-')

const checkPlatform = (target, force = false) => {
const checkPlatform = (target, force = false, environment = {}) => {
if (force) {
return
}

const platform = process.platform
const arch = process.arch
const platform = environment.os || process.platform
const arch = environment.cpu || process.arch
const osOk = target.os ? checkList(platform, target.os) : true
const cpuOk = target.cpu ? checkList(arch, target.cpu) : true

Expand Down
32 changes: 32 additions & 0 deletions test/check-platform.js
Expand Up @@ -43,6 +43,38 @@ t.test('os wrong (negation)', async t =>
t.test('nothing wrong (negation)', async t =>
checkPlatform({ cpu: '!enten-cpu', os: '!enten-os' }))

t.test('nothing wrong with overridden os', async t =>
checkPlatform({
cpu: 'any',
os: 'enten-os',
}, false, {
os: 'enten-os',
}))

t.test('nothing wrong with overridden cpu', async t =>
checkPlatform({
cpu: 'enten-cpu',
os: 'any',
}, false, {
cpu: 'enten-cpu',
}))

t.test('wrong os with overridden os', async t =>
t.throws(() => checkPlatform({
cpu: 'any',
os: 'enten-os',
}, false, {
os: 'another-os',
}), { code: 'EBADPLATFORM' }))

t.test('wrong cpu with overridden cpu', async t =>
t.throws(() => checkPlatform({
cpu: 'enten-cpu',
os: 'any',
}, false, {
cpu: 'another-cpu',
}), { code: 'EBADPLATFORM' }))

t.test('libc', (t) => {
let PLATFORM = ''

Expand Down

0 comments on commit 7a3f5ed

Please sign in to comment.