"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.forkProcess=void 0;const tslib_1=require("tslib"),child_process_1=require("child_process"),debug_1=require("../debug"),path_1=tslib_1.__importDefault(require("path")),env_1=require("../env"),PROCESS_READY_TIMEOUT=2e4;async function forkProcess(e,s,o,r,t=8891){const c=s,i={stdio:["pipe","pipe","pipe","ipc"],cwd:o,env:Object.assign(Object.assign({},process.env),{cpprocessEnv:"childprocess",nativeProcess:"1",timeout:global.TEST_COMPILER_PROCESS_TIMEOUT})};if((0,debug_1.shouldInspectCompiler)()&&(i.execArgv=["--inspect-brk="+t],i.env.__MINIPROGRAM_CI_TEST__="true"),i.env.isDevtools=env_1.isDevtools,env_1.isDevtools){let e=path_1.default.join(path_1.default.dirname(process.execPath),"node");"darwin"!==process.platform&&(e+=".exe"),i.execPath=e}return new Promise((s,o)=>{const t=(0,child_process_1.fork)(c,["--expose-gc"],i),n=setTimeout(()=>{e.destroy(),o(new Error("fork process timeout"))},2e4);return e._cpPromise={timer:n,resolve:()=>{s(t)},reject:o},t.stdout.setEncoding("utf8"),t.stdout.on("data",e=>{console.log("child process stdout: "+e)}),t.stderr.on("data",e=>{console.error("child process stderr: "+e)}),t.on("exit",(s,r)=>{e.process=void 0,e._checkReadyTask=void 0,env_1.isDevtools&&console.error(`child process exit: code(${s}), signal(${r})`),0!==s&&o(new Error(`native child process exit: code(${s}), signal(${r})`))}),t.on("message",e.onMessage),t.unref(),e.taskId+=1,t.send(r),t})}exports.forkProcess=forkProcess;