11const spawn = require ( 'cross-spawn' )
2+ const tmp = require ( 'tmp' )
3+ const fs = require ( 'fs' )
24const report = require ( './helpers/report' )
35
46/**
@@ -10,18 +12,22 @@ const report = require('./helpers/report')
1012 */
1113const cmd = ( socket , topic , req ) => {
1214 return new Promise ( ( resolve , reject ) => {
13-
15+
16+ // Store code in tmp file
17+ const previousfile = tmp . tmpNameSync ( )
18+ const commandFile = tmp . tmpNameSync ( )
19+ fs . writeFileSync ( commandFile , req . command )
20+
1421 // Given the command to execute, get the program's name and its parameters
1522 // in order to pass them to child_process.spawn
16- let parameters = req . command . split ( ' ' )
17- const command = parameters . shift ( )
23+ const command = process . platform === 'win32' ? 'sh' : 'bash'
24+ let parameters = [ commandFile ]
1825
1926 // Check if the command is attaching the output from the previous flow's step
2027 if ( req . previous ) {
21- [
22- '--tf_previous' ,
23- req . previous
24- ] . map ( p => parameters . push ( p ) )
28+ fs . writeFileSync ( previousfile , req . previous )
29+ parameters . push ( '--tf_previous_file' )
30+ parameters . push ( previousfile )
2531 }
2632
2733 // Execute the command in a child process so that stds can be monitored
@@ -39,6 +45,8 @@ const cmd = (socket, topic, req) => {
3945
4046 // Report Exit code
4147 sp . on ( 'exit' , code => {
48+ fs . unlinkSync ( previousfile )
49+ fs . unlinkSync ( commandFile )
4250 report . result ( socket , topic , req ,
4351 {
4452 stderr : code ? `EXIT CODE ${ code } ` : null ,
@@ -51,6 +59,8 @@ const cmd = (socket, topic, req) => {
5159
5260 // Report an exception
5361 sp . on ( 'error' , ( error ) => {
62+ fs . unlinkSync ( previousfile )
63+ fs . unlinkSync ( commandFile )
5464 report . exception ( socket , topic , req , error . toString ( ) )
5565 return reject ( error )
5666 } )
0 commit comments