Skip to content

Commit 1c93c44

Browse files
authored
feat: Add --cpu and --os option to override platform specific install (#6755)
1 parent 7bf2374 commit 1c93c44

File tree

10 files changed

+163
-4
lines changed

10 files changed

+163
-4
lines changed

lib/commands/install.js

+2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ class Install extends ArboristWorkspaceCmd {
3434
'bin-links',
3535
'fund',
3636
'dry-run',
37+
'cpu',
38+
'os',
3739
...super.params,
3840
]
3941

tap-snapshots/test/lib/commands/config.js.test.cjs

+4
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ exports[`test/lib/commands/config.js TAP config list --json > output matches sna
3333
"cidr": null,
3434
"color": true,
3535
"commit-hooks": true,
36+
"cpu": null,
37+
"os": null,
3638
"depth": null,
3739
"description": true,
3840
"dev": false,
@@ -187,6 +189,7 @@ cert = null
187189
cidr = null
188190
color = true
189191
commit-hooks = true
192+
cpu = null
190193
depth = null
191194
description = true
192195
dev = false
@@ -258,6 +261,7 @@ omit = []
258261
omit-lockfile-registry-resolved = false
259262
only = null
260263
optional = null
264+
os = null
261265
otp = null
262266
pack-destination = "."
263267
package = []

tap-snapshots/test/lib/docs.js.test.cjs

+32-2
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,16 @@ Run git commit hooks when using the \`npm version\` command.
392392
393393
394394
395+
#### \`cpu\`
396+
397+
* Default: null
398+
* Type: null or String
399+
400+
Override CPU architecture of native modules to install. Acceptable values
401+
are same as \`cpu\` field of package.json, which comes from \`process.arch\`.
402+
403+
404+
395405
#### \`depth\`
396406
397407
* Default: \`Infinity\` if \`--all\` is set, otherwise \`1\`
@@ -1085,6 +1095,16 @@ time.
10851095
10861096
10871097
1098+
#### \`os\`
1099+
1100+
* Default: null
1101+
* Type: null or String
1102+
1103+
Override OS of native modules to install. Acceptable values are same as \`os\`
1104+
field of package.json, which comes from \`process.platform\`.
1105+
1106+
1107+
10881108
#### \`otp\`
10891109
10901110
* Default: null
@@ -2006,6 +2026,8 @@ Array [
20062026
"cidr",
20072027
"color",
20082028
"commit-hooks",
2029+
"cpu",
2030+
"os",
20092031
"depth",
20102032
"description",
20112033
"dev",
@@ -2159,6 +2181,8 @@ Array [
21592181
"cidr",
21602182
"color",
21612183
"commit-hooks",
2184+
"cpu",
2185+
"os",
21622186
"depth",
21632187
"description",
21642188
"dev",
@@ -2313,6 +2337,7 @@ Object {
23132337
"cidr": null,
23142338
"color": false,
23152339
"commitHooks": true,
2340+
"cpu": null,
23162341
"defaultTag": "latest",
23172342
"depth": null,
23182343
"diff": Array [],
@@ -2361,6 +2386,7 @@ Object {
23612386
"offline": false,
23622387
"omit": Array [],
23632388
"omitLockfileRegistryResolved": false,
2389+
"os": null,
23642390
"otp": null,
23652391
"package": Array [],
23662392
"packageLock": true,
@@ -3170,7 +3196,7 @@ Options:
31703196
[--global-style] [--omit <dev|optional|peer> [--omit <dev|optional|peer> ...]]
31713197
[--strict-peer-deps] [--prefer-dedupe] [--no-package-lock] [--package-lock-only]
31723198
[--foreground-scripts] [--ignore-scripts] [--no-audit] [--no-bin-links]
3173-
[--no-fund] [--dry-run]
3199+
[--no-fund] [--dry-run] [--cpu <cpu>] [--os <os>]
31743200
[-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]]
31753201
[-ws|--workspaces] [--include-workspace-root] [--install-links]
31763202
@@ -3201,6 +3227,8 @@ aliases: add, i, in, ins, inst, insta, instal, isnt, isnta, isntal, isntall
32013227
#### \`bin-links\`
32023228
#### \`fund\`
32033229
#### \`dry-run\`
3230+
#### \`cpu\`
3231+
#### \`os\`
32043232
#### \`workspace\`
32053233
#### \`workspaces\`
32063234
#### \`include-workspace-root\`
@@ -3261,7 +3289,7 @@ Options:
32613289
[--global-style] [--omit <dev|optional|peer> [--omit <dev|optional|peer> ...]]
32623290
[--strict-peer-deps] [--prefer-dedupe] [--no-package-lock] [--package-lock-only]
32633291
[--foreground-scripts] [--ignore-scripts] [--no-audit] [--no-bin-links]
3264-
[--no-fund] [--dry-run]
3292+
[--no-fund] [--dry-run] [--cpu <cpu>] [--os <os>]
32653293
[-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]]
32663294
[-ws|--workspaces] [--include-workspace-root] [--install-links]
32673295
@@ -3292,6 +3320,8 @@ alias: it
32923320
#### \`bin-links\`
32933321
#### \`fund\`
32943322
#### \`dry-run\`
3323+
#### \`cpu\`
3324+
#### \`os\`
32953325
#### \`workspace\`
32963326
#### \`workspaces\`
32973327
#### \`include-workspace-root\`

workspaces/arborist/lib/arborist/reify.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,7 @@ module.exports = cls => class Reifier extends cls {
628628
process.emit('time', timer)
629629
this.addTracker('reify', node.name, node.location)
630630

631-
const { npmVersion, nodeVersion } = this.options
631+
const { npmVersion, nodeVersion, cpu, os } = this.options
632632
const p = Promise.resolve().then(async () => {
633633
// when we reify an optional node, check the engine and platform
634634
// first. be sure to ignore the --force and --engine-strict flags,
@@ -638,7 +638,7 @@ module.exports = cls => class Reifier extends cls {
638638
// eslint-disable-next-line promise/always-return
639639
if (node.optional) {
640640
checkEngine(node.package, npmVersion, nodeVersion, false)
641-
checkPlatform(node.package, false)
641+
checkPlatform(node.package, false, { cpu, os })
642642
}
643643
await this[_checkBins](node)
644644
await this[_extractOrLink](node)

workspaces/arborist/tap-snapshots/test/arborist/reify.js.test.cjs

+75
Original file line numberDiff line numberDiff line change
@@ -3161,6 +3161,44 @@ ArboristNode {
31613161
}
31623162
`
31633163

3164+
exports[`test/arborist/reify.js TAP fail to install optional deps with matched os and mismatched cpu with os and cpu options > expect resolving Promise 1`] = `
3165+
ArboristNode {
3166+
"edgesOut": Map {
3167+
"platform-specifying-test-package" => EdgeOut {
3168+
"name": "platform-specifying-test-package",
3169+
"spec": "1.0.0",
3170+
"to": null,
3171+
"type": "optional",
3172+
},
3173+
},
3174+
"isProjectRoot": true,
3175+
"location": "",
3176+
"name": "tap-testdir-reify-fail-to-install-optional-deps-with-matched-os-and-mismatched-cpu-with-os-and-cpu-options",
3177+
"packageName": "platform-test",
3178+
"path": "{CWD}/test/arborist/tap-testdir-reify-fail-to-install-optional-deps-with-matched-os-and-mismatched-cpu-with-os-and-cpu-options",
3179+
"version": "1.0.0",
3180+
}
3181+
`
3182+
3183+
exports[`test/arborist/reify.js TAP fail to install optional deps with mismatched os and matched cpu with os and cpu options > expect resolving Promise 1`] = `
3184+
ArboristNode {
3185+
"edgesOut": Map {
3186+
"platform-specifying-test-package" => EdgeOut {
3187+
"name": "platform-specifying-test-package",
3188+
"spec": "1.0.0",
3189+
"to": null,
3190+
"type": "optional",
3191+
},
3192+
},
3193+
"isProjectRoot": true,
3194+
"location": "",
3195+
"name": "tap-testdir-reify-fail-to-install-optional-deps-with-mismatched-os-and-matched-cpu-with-os-and-cpu-options",
3196+
"packageName": "platform-test",
3197+
"path": "{CWD}/test/arborist/tap-testdir-reify-fail-to-install-optional-deps-with-mismatched-os-and-matched-cpu-with-os-and-cpu-options",
3198+
"version": "1.0.0",
3199+
}
3200+
`
3201+
31643202
exports[`test/arborist/reify.js TAP failing script means install failure, unless ignoreScripts prod-dep-allinstall-fail --ignore-scripts > expect resolving Promise 1`] = `
31653203
ArboristNode {
31663204
"children": Map {
@@ -32993,6 +33031,43 @@ exports[`test/arborist/reify.js TAP store files with a custom indenting > must m
3299333031

3299433032
`
3299533033

33034+
exports[`test/arborist/reify.js TAP success to install optional deps with matched platform specifications with os and cpu options > expect resolving Promise 1`] = `
33035+
ArboristNode {
33036+
"children": Map {
33037+
"platform-specifying-test-package" => ArboristNode {
33038+
"edgesIn": Set {
33039+
EdgeIn {
33040+
"from": "",
33041+
"name": "platform-specifying-test-package",
33042+
"spec": "1.0.0",
33043+
"type": "optional",
33044+
},
33045+
},
33046+
"location": "node_modules/platform-specifying-test-package",
33047+
"name": "platform-specifying-test-package",
33048+
"optional": true,
33049+
"path": "{CWD}/test/arborist/tap-testdir-reify-success-to-install-optional-deps-with-matched-platform-specifications-with-os-and-cpu-options/node_modules/platform-specifying-test-package",
33050+
"resolved": "https://registry.npmjs.org/platform-specifying-test-package/-/platform-specifying-test-package-1.0.0.tgz",
33051+
"version": "1.0.0",
33052+
},
33053+
},
33054+
"edgesOut": Map {
33055+
"platform-specifying-test-package" => EdgeOut {
33056+
"name": "platform-specifying-test-package",
33057+
"spec": "1.0.0",
33058+
"to": "node_modules/platform-specifying-test-package",
33059+
"type": "optional",
33060+
},
33061+
},
33062+
"isProjectRoot": true,
33063+
"location": "",
33064+
"name": "tap-testdir-reify-success-to-install-optional-deps-with-matched-platform-specifications-with-os-and-cpu-options",
33065+
"packageName": "platform-test",
33066+
"path": "{CWD}/test/arborist/tap-testdir-reify-success-to-install-optional-deps-with-matched-platform-specifications-with-os-and-cpu-options",
33067+
"version": "1.0.0",
33068+
}
33069+
`
33070+
3299633071
exports[`test/arborist/reify.js TAP tarball deps with transitive tarball deps > expect resolving Promise 1`] = `
3299733072
ArboristNode {
3299833073
"children": Map {

workspaces/arborist/test/arborist/reify.js

+12
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,18 @@ t.test('still do not install optional deps with mismatched platform specificatio
465465
t.test('fail to install deps with mismatched platform specifications', t =>
466466
t.rejects(printReified(fixture(t, 'platform-specification')), { code: 'EBADPLATFORM' }))
467467

468+
t.test('success to install optional deps with matched platform specifications with os and cpu options', t =>
469+
t.resolveMatchSnapshot(printReified(
470+
fixture(t, 'optional-platform-specification'), { os: 'not-your-os', cpu: 'not-your-cpu' })))
471+
472+
t.test('fail to install optional deps with matched os and mismatched cpu with os and cpu options', t =>
473+
t.resolveMatchSnapshot(printReified(
474+
fixture(t, 'optional-platform-specification'), { os: 'not-your-os', cpu: 'another-cpu' })))
475+
476+
t.test('fail to install optional deps with mismatched os and matched cpu with os and cpu options', t =>
477+
t.resolveMatchSnapshot(printReified(
478+
fixture(t, 'optional-platform-specification'), { os: 'another-os', cpu: 'not-your-cpu' })))
479+
468480
t.test('dry run, do not get anything wet', async t => {
469481
const cases = [
470482
'shrinkwrapped-dep-with-lock-empty',

workspaces/arborist/test/fixtures/registry-mocks/content/platform-specifying-test-package.json

+3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
"os": [
2222
"not-your-os"
2323
],
24+
"cpu": [
25+
"not-your-cpu"
26+
],
2427
2528
"_nodeVersion": "12.18.4",
2629
"_npmVersion": "6.14.6",

workspaces/arborist/test/fixtures/registry-mocks/content/platform-specifying-test-package.min.json

+3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
},
1616
"os": [
1717
"not-your-os"
18+
],
19+
"cpu": [
20+
"not-your-cpu"
1821
]
1922
}
2023
},

workspaces/config/lib/definitions/definitions.js

+22
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,28 @@ define('commit-hooks', {
472472
flatten,
473473
})
474474

475+
define('cpu', {
476+
default: null,
477+
type: [null, String],
478+
description: `
479+
Override CPU architecture of native modules to install.
480+
Acceptable values are same as \`cpu\` field of package.json,
481+
which comes from \`process.arch\`.
482+
`,
483+
flatten,
484+
})
485+
486+
define('os', {
487+
default: null,
488+
type: [null, String],
489+
description: `
490+
Override OS of native modules to install.
491+
Acceptable values are same as \`os\` field of package.json,
492+
which comes from \`process.platform\`.
493+
`,
494+
flatten,
495+
})
496+
475497
define('depth', {
476498
default: null,
477499
defaultDescription: `

workspaces/config/tap-snapshots/test/type-description.js.test.cjs

+8
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ Object {
9191
"commit-hooks": Array [
9292
"boolean value (true or false)",
9393
],
94+
"cpu": Array [
95+
null,
96+
Function String(),
97+
],
9498
"depth": Array [
9599
null,
96100
"numeric value",
@@ -335,6 +339,10 @@ Object {
335339
null,
336340
"boolean value (true or false)",
337341
],
342+
"os": Array [
343+
null,
344+
Function String(),
345+
],
338346
"otp": Array [
339347
null,
340348
Function String(),

0 commit comments

Comments
 (0)