11import { spawn } from 'child_process' ;
22import fsPromises from 'fs/promises' ;
3+ import { existsSync } from 'fs'
34
45import {
56 PREFIX_RESPONSE_BODY_DATA ,
@@ -40,10 +41,14 @@ export async function runner (
4041 await fsPromises . writeFile ( mainFileAbsolutePath , injectedCodes , { flag : "a" } )
4142 await fsPromises . appendFile ( mainFileAbsolutePath , "// @ts-nocheck" )
4243
43- const childProcess = spawn ( args [ 0 ] , args . slice ( 1 ) , { stdio : [ "inherit" , "pipe" , "inherit" ] } ) ;
44+ const childProcess = spawn ( args [ 0 ] , args . slice ( 1 ) , {
45+ detached : true ,
46+ stdio : [ "inherit" , "pipe" , "inherit" ]
47+ } ) ;
4448
4549 childProcess . stdout . on ( 'data' , ( data ) => {
4650 const dataStr = data . toString ( )
51+
4752 if ( dataStr . startsWith ( PREFIX_RESPONSE_BODY_DATA ) ) {
4853 try {
4954 const res = JSON . parse ( dataStr . substr ( PREFIX_RESPONSE_BODY_DATA . length ) )
@@ -79,8 +84,11 @@ export async function runner (
7984 } )
8085
8186 childProcess . on ( 'close' , async ( code ) => {
82- await fsPromises . copyFile ( projectCWD + "/outdoc_tmp_file" , mainFileAbsolutePath )
83- await fsPromises . rm ( projectCWD + "/outdoc_tmp_file" )
87+ const tmpFilePath = projectCWD + "/outdoc_tmp_file"
88+ if ( existsSync ( tmpFilePath ) ) {
89+ await fsPromises . copyFile ( projectCWD + "/outdoc_tmp_file" , mainFileAbsolutePath )
90+ await fsPromises . rm ( projectCWD + "/outdoc_tmp_file" )
91+ }
8492
8593 if ( code === 0 ) {
8694 try {
@@ -101,4 +109,12 @@ export async function runner (
101109 }
102110 }
103111 } ) ;
112+
113+ process . on ( 'SIGINT' , async ( ) => {
114+ const tmpFilePath = projectCWD + "/outdoc_tmp_file"
115+ if ( existsSync ( tmpFilePath ) ) {
116+ await fsPromises . copyFile ( tmpFilePath , mainFileAbsolutePath )
117+ await fsPromises . rm ( tmpFilePath )
118+ }
119+ } ) ;
104120}
0 commit comments