@@ -3,13 +3,15 @@ const npa = require('npm-package-arg')
33const semver = require ( 'semver' )
44const pkgJson = require ( '@npmcli/package-json' )
55const { trustedDisplay } = require ( '@npmcli/arborist/lib/script-allowed.js' )
6+ const getInstallScripts = require ( '@npmcli/arborist/lib/install-scripts.js' )
67const checkAllowScripts = require ( './check-allow-scripts.js' )
78const resolveAllowScripts = require ( './resolve-allow-scripts.js' )
89const {
910 applyApprovalForPackage,
1011 applyDenyForPackage,
1112 nameKeyFor,
1213} = require ( './allow-scripts-writer.js' )
14+ const { classifyUnusedEntries } = require ( './allow-scripts-prune.js' )
1315const BaseCommand = require ( '../base-cmd.js' )
1416
1517// Parse a positional arg into a name and an optional version range. A bare
@@ -50,7 +52,7 @@ class AllowScriptsCmd extends BaseCommand {
5052 static ignoreImplicitWorkspace = false
5153
5254 // Mode of the current run, set by runMode.
53- // One of 'approve', 'deny', or 'list '.
55+ // One of 'approve', 'deny', 'list', or 'prune '.
5456 #mode = null
5557
5658 // verb drives the writers and summaries, which only run in the two write modes, so it is never read while listing.
@@ -73,6 +75,12 @@ class AllowScriptsCmd extends BaseCommand {
7375 )
7476 }
7577
78+ // `prune` has its own flow: it reads the literal package.json#allowScripts
79+ // map, not the resolved policy.
80+ if ( mode === 'prune' ) {
81+ return this . runPrune ( args )
82+ }
83+
7684 // `--allow-scripts-pending` is only honored by commands that declare it; the namespace lists via `ls` instead.
7785 const pending = this . constructor . params . includes ( 'allow-scripts-pending' ) &&
7886 ! ! this . npm . config . get ( 'allow-scripts-pending' )
@@ -318,6 +326,78 @@ class AllowScriptsCmd extends BaseCommand {
318326 output . standard ( `Nothing to ${ this . verb } ; allowScripts unchanged.` )
319327 }
320328 }
329+
330+ // `npm install-scripts prune`: drop package.json#allowScripts entries that no
331+ // longer match an installed package with an install script. Edits only
332+ // package.json (never `.npmrc`/CLI policy); `--dry-run` reports without writing.
333+ async runPrune ( args ) {
334+ const all = ! ! this . npm . config . get ( 'all' )
335+ if ( args . length > 0 || all ) {
336+ throw this . usageError (
337+ '`npm install-scripts prune` cannot be combined with positional arguments or `--all`.'
338+ )
339+ }
340+
341+ const dryRun = ! ! this . npm . config . get ( 'dry-run' )
342+ const pkg = await pkgJson . load ( this . npm . prefix )
343+ const existing = pkg . content . allowScripts && typeof pkg . content . allowScripts === 'object'
344+ ? pkg . content . allowScripts
345+ : { }
346+
347+ let removed = [ ]
348+ if ( Object . keys ( existing ) . length > 0 ) {
349+ const Arborist = require ( '@npmcli/arborist' )
350+ const arb = new Arborist ( {
351+ ...this . npm . flatOptions ,
352+ path : this . npm . prefix ,
353+ } )
354+ await arb . loadActual ( )
355+
356+ // Candidate install nodes (mirrors collectUnreviewedScripts), tagged with
357+ // whether each has install scripts so the classifier can tell "gone" from
358+ // "no longer has scripts".
359+ const nodes = [ ]
360+ for ( const node of arb . actualTree . inventory . values ( ) ) {
361+ if ( node . isProjectRoot || node . isWorkspace || node . isLink || node . inBundle || node . inert ) {
362+ continue
363+ }
364+ const scripts = await getInstallScripts ( node )
365+ nodes . push ( { node, hasScripts : Object . keys ( scripts ) . length > 0 } )
366+ }
367+
368+ const { remaining, removed : unused } = classifyUnusedEntries ( existing , nodes )
369+ removed = unused
370+
371+ if ( removed . length > 0 && ! dryRun ) {
372+ // Drop the field entirely when nothing is left rather than leaving `{}`.
373+ pkg . update ( {
374+ allowScripts : Object . keys ( remaining ) . length > 0 ? remaining : undefined ,
375+ } )
376+ await pkg . save ( )
377+ }
378+ }
379+
380+ this . printPruneSummary ( { removed, dryRun } )
381+ }
382+
383+ printPruneSummary ( { removed, dryRun } ) {
384+ if ( this . npm . flatOptions . json ) {
385+ output . buffer ( { allowScripts : { removed, dryRun } } )
386+ return
387+ }
388+ if ( removed . length === 0 ) {
389+ output . standard ( 'No unused allowScripts entries.' )
390+ return
391+ }
392+ const entry = removed . length === 1 ? 'entry' : 'entries'
393+ output . standard (
394+ `${ dryRun ? 'Would remove' : 'Removed' } ${ removed . length } unused allowScripts ${ entry } :`
395+ )
396+ for ( const { key, reason } of removed ) {
397+ const text = reason === 'not-installed' ? 'package not installed' : 'no install scripts'
398+ output . standard ( ` ${ key } (${ text } )` )
399+ }
400+ }
321401}
322402
323403module . exports = AllowScriptsCmd
0 commit comments