@@ -139,83 +139,86 @@ function cleanTaskArg (arg) {
139
139
*/
140
140
module . exports = function runTask ( task , options ) {
141
141
let cp = null
142
- const promise = new Promise ( ( resolve , reject ) => {
143
- ansiStylesPromise . then ( ( { default : ansiStyles } ) => {
144
- const stdin = options . stdin
145
- const stdout = wrapLabeling ( task , options . stdout , options . labelState , ansiStyles )
146
- const stderr = wrapLabeling ( task , options . stderr , options . labelState , ansiStyles )
147
- const stdinKind = detectStreamKind ( stdin , process . stdin )
148
- const stdoutKind = detectStreamKind ( stdout , process . stdout )
149
- const stderrKind = detectStreamKind ( stderr , process . stderr )
150
- const spawnOptions = { stdio : [ stdinKind , stdoutKind , stderrKind ] }
151
-
152
- // Print task name.
153
- if ( options . printName && stdout != null ) {
154
- stdout . write ( createHeader (
155
- task ,
156
- options . packageInfo ,
157
- options . stdout . isTTY ,
158
- ansiStyles
159
- ) )
160
- }
161
142
162
- // Execute.
163
- let npmPath = options . npmPath
164
- if ( ! npmPath && process . env . npm_execpath ) {
165
- const basename = path . basename ( process . env . npm_execpath )
166
- let newBasename = basename
167
- if ( basename . startsWith ( 'npx' ) ) {
168
- newBasename = basename . replace ( 'npx' , 'npm' ) // eslint-disable-line no-process-env
169
- } else if ( basename . startsWith ( 'pnpx' ) ) {
170
- newBasename = basename . replace ( 'pnpx' , 'pnpm' ) // eslint-disable-line no-process-env
171
- }
172
-
173
- npmPath = newBasename === basename
174
- ? path . join ( path . dirname ( process . env . npm_execpath ) , newBasename )
175
- : process . env . npm_execpath // eslint-disable-line no-process-env
143
+ async function asyncRunTask ( ) {
144
+ const { default : ansiStyles } = await ansiStylesPromise
145
+
146
+ const stdin = options . stdin
147
+ const stdout = wrapLabeling ( task , options . stdout , options . labelState , ansiStyles )
148
+ const stderr = wrapLabeling ( task , options . stderr , options . labelState , ansiStyles )
149
+ const stdinKind = detectStreamKind ( stdin , process . stdin )
150
+ const stdoutKind = detectStreamKind ( stdout , process . stdout )
151
+ const stderrKind = detectStreamKind ( stderr , process . stderr )
152
+ const spawnOptions = { stdio : [ stdinKind , stdoutKind , stderrKind ] }
153
+
154
+ // Print task name.
155
+ if ( options . printName && stdout != null ) {
156
+ stdout . write ( createHeader (
157
+ task ,
158
+ options . packageInfo ,
159
+ options . stdout . isTTY ,
160
+ ansiStyles
161
+ ) )
162
+ }
163
+
164
+ // Execute.
165
+ let npmPath = options . npmPath
166
+ if ( ! npmPath && process . env . npm_execpath ) {
167
+ const basename = path . basename ( process . env . npm_execpath )
168
+ let newBasename = basename
169
+ if ( basename . startsWith ( 'npx' ) ) {
170
+ newBasename = basename . replace ( 'npx' , 'npm' ) // eslint-disable-line no-process-env
171
+ } else if ( basename . startsWith ( 'pnpx' ) ) {
172
+ newBasename = basename . replace ( 'pnpx' , 'pnpm' ) // eslint-disable-line no-process-env
176
173
}
177
174
178
- const npmPathIsJs = typeof npmPath === 'string' && / \. ( c | m ) ? j s / . test ( path . extname ( npmPath ) )
179
- let execPath = ( npmPathIsJs ? process . execPath : npmPath || 'npm' )
180
-
181
- if ( ! npmPath && ! process . env . npm_execpath ) {
182
- // When a script is being run via pnpm, npmPath and npm_execpath will be null or undefined
183
- // Attempt to figure out whether we're running via pnpm
184
- const projectRoot = path . dirname ( options . packageInfo . path )
185
- const hasPnpmLockfile = fs . existsSync ( path . join ( projectRoot , 'pnpm-lock.yaml' ) )
186
- const { status : pnpmFound , output } = spawn . sync ( 'which' , 'pnpm' , { silent : true } )
187
- if ( hasPnpmLockfile && __dirname . split ( path . delimiter ) . includes ( '.pnpm' ) && pnpmFound ) {
188
- execPath = output
189
- }
175
+ npmPath = newBasename === basename
176
+ ? path . join ( path . dirname ( process . env . npm_execpath ) , newBasename )
177
+ : process . env . npm_execpath // eslint-disable-line no-process-env
178
+ }
179
+
180
+ const npmPathIsJs = typeof npmPath === 'string' && / \. ( c | m ) ? j s / . test ( path . extname ( npmPath ) )
181
+ let execPath = ( npmPathIsJs ? process . execPath : npmPath || 'npm' )
182
+
183
+ if ( ! npmPath && ! process . env . npm_execpath ) {
184
+ // When a script is being run via pnpm, npmPath and npm_execpath will be null or undefined
185
+ // Attempt to figure out whether we're running via pnpm
186
+ const projectRoot = path . dirname ( options . packageInfo . path )
187
+ const hasPnpmLockfile = fs . existsSync ( path . join ( projectRoot , 'pnpm-lock.yaml' ) )
188
+ const { status : pnpmFound , output : pnpmWhichOutput } = await spawn ( 'which' , 'pnpm' , { silent : true } )
189
+ if ( hasPnpmLockfile && __dirname . split ( path . delimiter ) . includes ( '.pnpm' ) && pnpmFound ) {
190
+ execPath = pnpmWhichOutput
190
191
}
192
+ }
191
193
192
- const isYarn = process . env . npm_config_user_agent && process . env . npm_config_user_agent . startsWith ( 'yarn' ) // eslint-disable-line no-process-env
194
+ const isYarn = process . env . npm_config_user_agent && process . env . npm_config_user_agent . startsWith ( 'yarn' ) // eslint-disable-line no-process-env
193
195
194
- const spawnArgs = [ 'run' ]
196
+ const spawnArgs = [ 'run' ]
195
197
196
- if ( npmPathIsJs ) {
197
- spawnArgs . unshift ( npmPath )
198
- }
199
- if ( ! isYarn ) {
200
- Array . prototype . push . apply ( spawnArgs , options . prefixOptions )
201
- } else if ( options . prefixOptions . indexOf ( '--silent' ) !== - 1 ) {
202
- spawnArgs . push ( '--silent' )
203
- }
204
- Array . prototype . push . apply ( spawnArgs , parseArgs ( task ) . map ( cleanTaskArg ) )
198
+ if ( npmPathIsJs ) {
199
+ spawnArgs . unshift ( npmPath )
200
+ }
201
+ if ( ! isYarn ) {
202
+ Array . prototype . push . apply ( spawnArgs , options . prefixOptions )
203
+ } else if ( options . prefixOptions . indexOf ( '--silent' ) !== - 1 ) {
204
+ spawnArgs . push ( '--silent' )
205
+ }
206
+ Array . prototype . push . apply ( spawnArgs , parseArgs ( task ) . map ( cleanTaskArg ) )
205
207
206
- cp = spawn ( execPath , spawnArgs , spawnOptions )
208
+ cp = spawn ( execPath , spawnArgs , spawnOptions )
207
209
208
- // Piping stdio.
209
- if ( stdinKind === 'pipe' ) {
210
- stdin . pipe ( cp . stdin )
211
- }
212
- if ( stdoutKind === 'pipe' ) {
213
- cp . stdout . pipe ( stdout , { end : false } )
214
- }
215
- if ( stderrKind === 'pipe' ) {
216
- cp . stderr . pipe ( stderr , { end : false } )
217
- }
210
+ // Piping stdio.
211
+ if ( stdinKind === 'pipe' ) {
212
+ stdin . pipe ( cp . stdin )
213
+ }
214
+ if ( stdoutKind === 'pipe' ) {
215
+ cp . stdout . pipe ( stdout , { end : false } )
216
+ }
217
+ if ( stderrKind === 'pipe' ) {
218
+ cp . stderr . pipe ( stderr , { end : false } )
219
+ }
218
220
221
+ return new Promise ( ( resolve , reject ) => {
219
222
// Register
220
223
cp . on ( 'error' , ( err ) => {
221
224
cp = null
@@ -226,7 +229,9 @@ module.exports = function runTask (task, options) {
226
229
resolve ( { task, code, signal } )
227
230
} )
228
231
} )
229
- } )
232
+ }
233
+
234
+ const promise = asyncRunTask ( )
230
235
231
236
promise . abort = function abort ( ) {
232
237
if ( cp != null ) {
0 commit comments