Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow checkPlatform to override execution environment #65

Merged
merged 5 commits into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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