@@ -16,6 +16,7 @@ const npmAuditReport = require('npm-audit-report')
1616const { readTree : getFundingInfo } = require ( 'libnpmfund' )
1717const { trustedDisplay } = require ( '@npmcli/arborist/lib/script-allowed.js' )
1818const auditError = require ( './audit-error.js' )
19+ const { configSetAllowScripts } = require ( './allow-scripts-remediation.js' )
1920
2021const reifyOutput = ( npm , arb , extras = { } ) => {
2122 const { diff, actualTree } = arb
@@ -243,10 +244,12 @@ const unreviewedScriptsMessage = (npm, unreviewedScripts) => {
243244 const pkg = count === 1 ? 'package has' : 'packages have'
244245 const header = `${ count } ${ pkg } install scripts not yet covered by allowScripts:`
245246
247+ const names = [ ]
246248 const lines = unreviewedScripts . map ( ( { node, scripts } ) => {
247249 const { name, version } = trustedDisplay ( node )
248250 /* istanbul ignore next: every test node has a name */
249251 const display = name || '<unknown>'
252+ names . push ( display )
250253 const ver = version ? `@${ version } ` : ''
251254 const events = Object . entries ( scripts )
252255 . map ( ( [ event , cmd ] ) => `${ event } : ${ cmd } ` )
@@ -260,9 +263,28 @@ const unreviewedScriptsMessage = (npm, unreviewedScripts) => {
260263 header ,
261264 ...lines ,
262265 '' ,
263- 'Run ` npm approve-scripts --allow-scripts-pending` to review, or `npm approve-scripts <pkg>` to allow.' ,
266+ ... remediationLines ( npm , names ) ,
264267 ] . join ( '\n' )
265268 )
266269}
267270
271+ // `npm approve-scripts` writes to a project package.json, which doesn't
272+ // exist for global installs (it throws EGLOBAL). For those, point users at
273+ // the mechanism that does work globally: the `--allow-scripts` flag for a
274+ // one-off, or `npm config set allow-scripts` to persist it.
275+ const remediationLines = ( npm , names ) => {
276+ if ( npm . global ) {
277+ const list = names . join ( ',' )
278+ return [
279+ `Run \`npm install -g --allow-scripts=${ list } \` to allow these scripts ` +
280+ `once, or \`${ configSetAllowScripts ( names ) } \` to allow them for ` +
281+ 'all global installs.' ,
282+ ]
283+ }
284+ return [
285+ 'Run `npm approve-scripts --allow-scripts-pending` to review, ' +
286+ 'or `npm approve-scripts <pkg>` to allow.' ,
287+ ]
288+ }
289+
268290module . exports = reifyOutput
0 commit comments