MCPcopy Create free account
hub / github.com/npm/cli / runPrune

Method runPrune

lib/utils/allow-scripts-cmd.js:333–381  ·  view source on GitHub ↗
(args)

Source from the content-addressed store, hash-verified

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) {

Callers 1

runModeMethod · 0.95

Calls 10

printPruneSummaryMethod · 0.95
getInstallScriptsFunction · 0.85
classifyUnusedEntriesFunction · 0.85
usageErrorMethod · 0.80
loadActualMethod · 0.80
pushMethod · 0.80
updateMethod · 0.80
getMethod · 0.45
loadMethod · 0.45
saveMethod · 0.45

Tested by

no test coverage detected