|
1 | | -const isScriptAllowed = require('@npmcli/arborist/lib/script-allowed.js') |
2 | | -const getInstallScripts = require('@npmcli/arborist/lib/install-scripts.js') |
| 1 | +const { collectUnreviewedScripts } = require('@npmcli/arborist/lib/unreviewed-scripts.js') |
3 | 2 |
|
4 | | -// Walks arb.actualTree.inventory and returns the list of dep nodes that |
5 | | -// have install-relevant lifecycle scripts and are not yet covered (or |
6 | | -// explicitly denied) by the allowScripts policy. |
| 3 | +// Walks a tree's inventory and returns the list of dep nodes that have |
| 4 | +// install-relevant lifecycle scripts and are not yet covered (or explicitly |
| 5 | +// denied) by the allowScripts policy. |
| 6 | +// |
| 7 | +// Thin wrapper around arborist's shared `collectUnreviewedScripts`, mapping |
| 8 | +// the CLI's `({ arb, npm, tree })` shape onto the shared walk. Defaults to |
| 9 | +// `arb.actualTree` (post-reify) but accepts an explicit tree so callers can |
| 10 | +// pre-flight against the idealTree before scripts run. |
7 | 11 | // |
8 | 12 | // Returns an array of `{ node, scripts }` entries. `scripts` is an object |
9 | 13 | // describing the relevant lifecycle scripts that would run. |
10 | | - |
11 | | -const checkAllowScripts = async ({ arb, npm, tree, includeWhenIgnored = false }) => { |
12 | | - const ignoreScripts = !!arb.options?.ignoreScripts |
13 | | - const dangerouslyAllowAll = !!npm?.flatOptions?.dangerouslyAllowAllScripts |
14 | | - |
15 | | - // With ignore-scripts set, no scripts run, so execution callers |
16 | | - // (install, rebuild, strict preflight) bail out here. approve/deny pass |
17 | | - // includeWhenIgnored so they keep listing unreviewed packages, which is |
18 | | - // what you need to move from a blanket ignore-scripts to an allowlist. |
19 | | - // Listing never runs anything. |
20 | | - if ((ignoreScripts && !includeWhenIgnored) || dangerouslyAllowAll) { |
21 | | - return [] |
22 | | - } |
23 | | - |
24 | | - // Defaults to actualTree (post-reify) but accepts an explicit tree so |
25 | | - // callers can pre-flight against the idealTree before scripts run. |
26 | | - const targetTree = tree || arb.actualTree |
27 | | - if (!targetTree?.inventory) { |
28 | | - return [] |
29 | | - } |
30 | | - |
31 | | - const policy = arb.options?.allowScripts || null |
32 | | - |
33 | | - const unreviewed = [] |
34 | | - for (const node of targetTree.inventory.values()) { |
35 | | - if (node.isProjectRoot || node.isWorkspace) { |
36 | | - continue |
37 | | - } |
38 | | - if (node.isLink) { |
39 | | - // Linked workspace dependencies are managed by the workspace owner. |
40 | | - continue |
41 | | - } |
42 | | - |
43 | | - const verdict = isScriptAllowed(node, policy) |
44 | | - if (verdict === true || verdict === false) { |
45 | | - continue |
46 | | - } |
47 | | - |
48 | | - const scripts = await getInstallScripts(node) |
49 | | - if (Object.keys(scripts).length === 0) { |
50 | | - continue |
51 | | - } |
52 | | - |
53 | | - unreviewed.push({ node, scripts }) |
54 | | - } |
55 | | - |
56 | | - return unreviewed |
57 | | -} |
| 14 | +// |
| 15 | +// `includeWhenIgnored` keeps listing unreviewed packages even when |
| 16 | +// ignore-scripts is set, so approve/deny can show what you'd move from a |
| 17 | +// blanket ignore-scripts to an allowlist. Execution callers leave it false. |
| 18 | +const checkAllowScripts = async ({ arb, npm, tree, includeWhenIgnored = false }) => |
| 19 | + collectUnreviewedScripts({ |
| 20 | + tree: tree || arb.actualTree, |
| 21 | + policy: arb.options?.allowScripts || null, |
| 22 | + ignoreScripts: !!arb.options?.ignoreScripts, |
| 23 | + dangerouslyAllowAllScripts: !!npm?.flatOptions?.dangerouslyAllowAllScripts, |
| 24 | + includeWhenIgnored, |
| 25 | + }) |
58 | 26 |
|
59 | 27 | module.exports = checkAllowScripts |
0 commit comments