| 85 | } |
| 86 | |
| 87 | async #run ([event, ...args], { path, pkg, workspace }) { |
| 88 | const runScript = require('@npmcli/run-script') |
| 89 | |
| 90 | pkg ??= await pkgJson.normalize(path).then(p => p.content) |
| 91 | |
| 92 | const { scripts = {} } = pkg |
| 93 | |
| 94 | if (event === 'restart' && !scripts.restart) { |
| 95 | scripts.restart = 'npm stop --if-present && npm start' |
| 96 | } else if (event === 'env' && !scripts.env) { |
| 97 | const { isWindowsShell } = require('../utils/is-windows.js') |
| 98 | scripts.env = isWindowsShell ? 'SET' : 'env' |
| 99 | } |
| 100 | |
| 101 | pkg.scripts = scripts |
| 102 | |
| 103 | if ( |
| 104 | !Object.prototype.hasOwnProperty.call(scripts, event) && |
| 105 | !(event === 'start' && (await runScript.isServerPackage(path))) |
| 106 | ) { |
| 107 | if (this.npm.config.get('if-present')) { |
| 108 | return |
| 109 | } |
| 110 | |
| 111 | const suggestions = require('../utils/did-you-mean.js')(pkg, event) |
| 112 | const wsArg = workspace && path !== this.npm.localPrefix |
| 113 | ? ` --workspace=${pkg._id || pkg.name}` |
| 114 | : '' |
| 115 | throw new Error([ |
| 116 | `Missing script: "${event}"${suggestions}`, |
| 117 | '', |
| 118 | 'To see a list of scripts, run:', |
| 119 | ` npm run${wsArg}`, |
| 120 | ].join('\n')) |
| 121 | } |
| 122 | |
| 123 | // positional args only added to the main event, not pre/post |
| 124 | const events = [[event, args]] |
| 125 | if (!this.npm.config.get('ignore-scripts')) { |
| 126 | if (scripts[`pre${event}`]) { |
| 127 | events.unshift([`pre${event}`, []]) |
| 128 | } |
| 129 | |
| 130 | if (scripts[`post${event}`]) { |
| 131 | events.push([`post${event}`, []]) |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | for (const [ev, evArgs] of events) { |
| 136 | await runScript({ |
| 137 | args: evArgs, |
| 138 | event: ev, |
| 139 | nodeGyp: this.npm.config.get('node-gyp'), |
| 140 | path, |
| 141 | pkg, |
| 142 | // || undefined is because runScript will be unhappy with the default null value |
| 143 | scriptShell: this.npm.config.get('script-shell') || undefined, |
| 144 | stdio: 'inherit', |