1- import { spawn } from "child_process" ;
1+ import { spawn , SpawnOptions } from "child_process" ;
22import fs from "fs" ;
33import Serverless from "serverless" ;
44import configConstants from "../config" ;
@@ -29,7 +29,7 @@ export class OfflineService extends BaseService {
2929 await this . packageService . createBindings ( ) ;
3030 const filenames = Object . keys ( this . localFiles ) ;
3131 for ( const filename of filenames ) {
32- if ( ! fs . existsSync ( filename ) ) {
32+ if ( ! fs . existsSync ( filename ) ) {
3333 fs . writeFileSync (
3434 filename ,
3535 this . localFiles [ filename ]
@@ -44,7 +44,7 @@ export class OfflineService extends BaseService {
4444 await this . packageService . cleanUp ( ) ;
4545 const filenames = Object . keys ( this . localFiles ) ;
4646 for ( const filename of filenames ) {
47- if ( fs . existsSync ( filename ) ) {
47+ if ( fs . existsSync ( filename ) ) {
4848 this . log ( `Removing file '${ filename } '` ) ;
4949 fs . unlinkSync ( filename )
5050 }
@@ -62,59 +62,30 @@ export class OfflineService extends BaseService {
6262 /**
6363 * Spawn a Node child process with predefined environment variables
6464 * @param command CLI Command - NO ARGS
65- * @param args Array of arguments for CLI command
66- * @param env Additional environment variables to be set in addition to
67- * predefined variables in `serverless.yml`
65+ * @param spawnArgs Array of arguments for CLI command
6866 */
69- private spawn ( command : string , args ?: string [ ] , env ?: any ) : Promise < void > {
70- env = {
67+ private spawn ( command : string , spawnArgs ?: string [ ] ) : Promise < void > {
68+ if ( process . platform === "win32" ) {
69+ command += ".cmd" ;
70+ }
71+
72+ const env = {
73+ // Inherit environment from current process, most importantly, the PATH
74+ ...process . env ,
75+ // Environment variables from serverless config are king
7176 ...this . serverless . service . provider [ "environment" ] ,
72- ...env
7377 }
74- this . log ( `Spawning process '${ command } ${ args . join ( " " ) } '` ) ;
78+ this . log ( `Spawning process '${ command } ${ spawnArgs . join ( " " ) } '` ) ;
7579 return new Promise ( ( resolve , reject ) => {
76- const childProcess = spawn ( command , args , { env} ) ;
77-
78- childProcess . stdout . on ( "data" , ( data ) => {
79- this . log ( data , {
80- color : configConstants . funcConsoleColor ,
81- } , command ) ;
82- } ) ;
83-
84- childProcess . stderr . on ( "data" , ( data ) => {
85- this . log ( data , {
86- color : "red" ,
87- } , command ) ;
88- } )
89-
90- childProcess . on ( "message" , ( message ) => {
91- this . log ( message , {
92- color : configConstants . funcConsoleColor ,
93- } , command ) ;
94- } ) ;
95-
96- childProcess . on ( "error" , ( err ) => {
97- this . log ( `${ err } ` , {
98- color : "red"
99- } , command ) ;
100- reject ( err ) ;
101- } ) ;
80+ const spawnOptions : SpawnOptions = { env, stdio : "inherit" } ;
81+ const childProcess = spawn ( command , spawnArgs , spawnOptions ) ;
10282
10383 childProcess . on ( "exit" , ( code ) => {
104- this . log ( `Exited with code: ${ code } ` , {
105- color : ( code === 0 ) ? "green" : "red" ,
106- } , command ) ;
107- } ) ;
108-
109- childProcess . on ( "close" , ( code ) => {
110- this . log ( `Closed with code: ${ code } ` , {
111- color : ( code === 0 ) ? "green" : "red" ,
112- } , command ) ;
113- resolve ( ) ;
114- } ) ;
115-
116- childProcess . on ( "disconnect" , ( ) => {
117- this . log ( "Process disconnected" ) ;
84+ if ( code === 0 ) {
85+ resolve ( ) ;
86+ } else {
87+ reject ( ) ;
88+ }
11889 } ) ;
11990 } ) ;
12091 }
0 commit comments