Skip to content

Commit 66ef765

Browse files
authored
feat: Add --cpu and --os option to override platform specific install (#6776)
1 parent 1ddf976 commit 66ef765

File tree

10 files changed

+163
-4
lines changed

10 files changed

+163
-4
lines changed

lib/commands/install.js

Lines changed: 2 additions & 0 deletions
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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ exports[`test/lib/commands/config.js TAP config list --json > output matches sna
3434
"cidr": null,
3535
"color": true,
3636
"commit-hooks": true,
37+
"cpu": null,
38+
"os": null,
3739
"depth": null,
3840
"description": true,
3941
"dev": false,
@@ -191,6 +193,7 @@ ci-name = null
191193
cidr = null
192194
color = true
193195
commit-hooks = true
196+
cpu = null
194197
depth = null
195198
description = true
196199
dev = false
@@ -263,6 +266,7 @@ omit = []
263266
omit-lockfile-registry-resolved = false
264267
only = null
265268
optional = null
269+
os = null
266270
otp = null
267271
pack-destination = "."
268272
package = []

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

Lines changed: 32 additions & 2 deletions
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
@@ -2035,6 +2055,8 @@ Array [
20352055
"cidr",
20362056
"color",
20372057
"commit-hooks",
2058+
"cpu",
2059+
"os",
20382060
"depth",
20392061
"description",
20402062
"dev",
@@ -2190,6 +2212,8 @@ Array [
21902212
"cidr",
21912213
"color",
21922214
"commit-hooks",
2215+
"cpu",
2216+
"os",
21932217
"depth",
21942218
"description",
21952219
"dev",
@@ -2346,6 +2370,7 @@ Object {
23462370
"ciName": "{ci}",
23472371
"color": false,
23482372
"commitHooks": true,
2373+
"cpu": null,
23492374
"defaultTag": "latest",
23502375
"depth": null,
23512376
"diff": Array [],
@@ -2395,6 +2420,7 @@ Object {
23952420
"offline": false,
23962421
"omit": Array [],
23972422
"omitLockfileRegistryResolved": false,
2423+
"os": null,
23982424
"otp": null,
23992425
"package": Array [],
24002426
"packageLock": true,
@@ -3204,7 +3230,7 @@ Options:
32043230
[--global-style] [--omit <dev|optional|peer> [--omit <dev|optional|peer> ...]]
32053231
[--strict-peer-deps] [--prefer-dedupe] [--no-package-lock] [--package-lock-only]
32063232
[--foreground-scripts] [--ignore-scripts] [--no-audit] [--no-bin-links]
3207-
[--no-fund] [--dry-run]
3233+
[--no-fund] [--dry-run] [--cpu <cpu>] [--os <os>]
32083234
[-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]]
32093235
[-ws|--workspaces] [--include-workspace-root] [--install-links]
32103236
@@ -3235,6 +3261,8 @@ aliases: add, i, in, ins, inst, insta, instal, isnt, isnta, isntal, isntall
32353261
#### \`bin-links\`
32363262
#### \`fund\`
32373263
#### \`dry-run\`
3264+
#### \`cpu\`
3265+
#### \`os\`
32383266
#### \`workspace\`
32393267
#### \`workspaces\`
32403268
#### \`include-workspace-root\`
@@ -3295,7 +3323,7 @@ Options:
32953323
[--global-style] [--omit <dev|optional|peer> [--omit <dev|optional|peer> ...]]
32963324
[--strict-peer-deps] [--prefer-dedupe] [--no-package-lock] [--package-lock-only]
32973325
[--foreground-scripts] [--ignore-scripts] [--no-audit] [--no-bin-links]
3298-
[--no-fund] [--dry-run]
3326+
[--no-fund] [--dry-run] [--cpu <cpu>] [--os <os>]
32993327
[-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]]
33003328
[-ws|--workspaces] [--include-workspace-root] [--install-links]
33013329
@@ -3326,6 +3354,8 @@ alias: it
33263354
#### \`bin-links\`
33273355
#### \`fund\`
33283356
#### \`dry-run\`
3357+
#### \`cpu\`
3358+
#### \`os\`
33293359
#### \`workspace\`
33303360
#### \`workspaces\`
33313361
#### \`include-workspace-root\`

workspaces/arborist/lib/arborist/reify.js

Lines changed: 2 additions & 2 deletions
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

Lines changed: 75 additions & 0 deletions
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

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

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

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

Lines changed: 3 additions & 0 deletions
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

Lines changed: 3 additions & 0 deletions
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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,28 @@ define('commit-hooks', {
490490
flatten,
491491
})
492492

493+
define('cpu', {
494+
default: null,
495+
type: [null, String],
496+
description: `
497+
Override CPU architecture of native modules to install.
498+
Acceptable values are same as \`cpu\` field of package.json,
499+
which comes from \`process.arch\`.
500+
`,
501+
flatten,
502+
})
503+
504+
define('os', {
505+
default: null,
506+
type: [null, String],
507+
description: `
508+
Override OS of native modules to install.
509+
Acceptable values are same as \`os\` field of package.json,
510+
which comes from \`process.platform\`.
511+
`,
512+
flatten,
513+
})
514+
493515
define('depth', {
494516
default: null,
495517
defaultDescription: `

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ Object {
9595
"commit-hooks": Array [
9696
"boolean value (true or false)",
9797
],
98+
"cpu": Array [
99+
null,
100+
Function String(),
101+
],
98102
"depth": Array [
99103
null,
100104
"numeric value",
@@ -339,6 +343,10 @@ Object {
339343
null,
340344
"boolean value (true or false)",
341345
],
346+
"os": Array [
347+
null,
348+
Function String(),
349+
],
342350
"otp": Array [
343351
null,
344352
Function String(),

0 commit comments

Comments
 (0)