1+ import { exec as cp_exec } from 'child_process' ;
2+ import { promisify } from 'util' ;
3+
14import shellEnv from 'shell-env' ;
25
3- // Singleton: We don't want to do this more than once.
4- let _shellPathCalled = false ;
6+ /**
7+ * On macOS & Linux, we need to fix the $PATH environment variable
8+ * so that we can call `npm`.
9+ *
10+ * @returns {Promise<void> }
11+ */
12+ export const maybeFixPath = ( ( ) => {
13+ // Singleton: We don't want to do this more than once.
14+ let _shellPathCalled = false ;
15+
16+ return async ( ) : Promise < void > => {
17+ if ( _shellPathCalled ) {
18+ return ;
19+ }
20+
21+ if ( process . platform !== 'win32' ) {
22+ const { PATH } = await shellEnv ( ) ;
23+ if ( PATH ) {
24+ process . env . PATH = PATH ;
25+ }
26+ }
27+
28+ _shellPathCalled = true ;
29+ } ;
30+ } ) ( ) ;
531
632/**
733 * Execute a command in a directory.
@@ -11,40 +37,12 @@ let _shellPathCalled = false;
1137 * @returns {Promise<string> }
1238 */
1339export async function exec ( dir : string , cliArgs : string ) : Promise < string > {
14- return new Promise < string > ( async ( resolve , reject ) => {
15- await maybeFixPath ( ) ;
16-
17- const { exec } = await import ( 'child_process' ) ;
18- exec (
19- cliArgs ,
20- {
21- cwd : dir ,
22- maxBuffer : 200 * 1024 * 100 , // 100 times the default
23- } ,
24- ( error , result ) => {
25- if ( error ) {
26- reject ( error ) ;
27- }
28-
29- resolve ( typeof result === 'string' ? result : `${ result } ` ) ;
30- } ,
31- ) ;
32- } ) ;
33- }
40+ await maybeFixPath ( ) ;
3441
35- /**
36- * On macOS & Linux, we need to fix the $PATH environment variable
37- * so that we can call `npm`.
38- *
39- * @returns {Promise<void> }
40- */
41- export async function maybeFixPath ( ) : Promise < void > {
42- if ( ! _shellPathCalled && process . platform !== 'win32' ) {
43- const { PATH } = await shellEnv ( ) ;
44- if ( PATH ) {
45- process . env . PATH = PATH ;
46- }
47- }
42+ const { stdout } = await promisify ( cp_exec ) ( cliArgs , {
43+ cwd : dir ,
44+ maxBuffer : 200 * 1024 * 100 , // 100 times the default
45+ } ) ;
4846
49- _shellPathCalled = true ;
47+ return stdout . trim ( ) ;
5048}
0 commit comments