( subprocessName: string[], dslJSON: DangerDSLJSONType, exec: Executor, config: RunnerConfig )
| 38 | |
| 39 | // Runs the Danger process |
| 40 | export const runDangerSubprocess = ( |
| 41 | subprocessName: string[], |
| 42 | dslJSON: DangerDSLJSONType, |
| 43 | exec: Executor, |
| 44 | config: RunnerConfig |
| 45 | ) => { |
| 46 | let processName = subprocessName[0] |
| 47 | let args = subprocessName |
| 48 | let results = null as any |
| 49 | args.shift() // mutate and remove the first element |
| 50 | |
| 51 | const processDisplayName = basename(processName) |
| 52 | const dslJSONString = prepareDangerDSL(dslJSON) |
| 53 | d(`Running sub-process: ${processDisplayName} - ${args}`) |
| 54 | const child = spawn(processName, args, { env: { ...process.env, ...config.additionalEnvVars } }) |
| 55 | |
| 56 | const sendDSLToSubprocess = () => { |
| 57 | if (exec.options.passURLForDSL) { |
| 58 | const filename = `danger-dsl-${randomBytes(4).toString("hex")}.json` |
| 59 | const resultsPath = join(tmpdir(), filename) |
| 60 | writeFileSync(resultsPath, dslJSONString, "utf8") |
| 61 | const url = `danger://dsl/${resultsPath}` |
| 62 | d(`Started passing in STDIN via the URL: ${url}`) |
| 63 | child.stdin.write(url) |
| 64 | child.stdin.end() |
| 65 | } else { |
| 66 | d(`Started passing in STDIN`) |
| 67 | child.stdin.write(dslJSONString) |
| 68 | child.stdin.end() |
| 69 | } |
| 70 | d(`Passed DSL in via STDIN`) |
| 71 | } |
| 72 | |
| 73 | // Initial sending of the DSL |
| 74 | sendDSLToSubprocess() |
| 75 | |
| 76 | let allLogs = "" |
| 77 | child.stdout.on("data", async (data) => { |
| 78 | const stdout: string = data.toString() |
| 79 | allLogs += stdout |
| 80 | |
| 81 | // Provide a way for a process to request the DSL |
| 82 | // if they missed the first go. |
| 83 | if (stdout.includes(messageToSendDSL)) { |
| 84 | sendDSLToSubprocess() |
| 85 | } |
| 86 | |
| 87 | // There are two checks for a response |
| 88 | const maybeJSON = getJSONFromSTDOUT(stdout) |
| 89 | const maybeJSONURL = getJSONURLFromSTDOUT(stdout) |
| 90 | |
| 91 | // Remove message sent back to danger-js |
| 92 | const withoutURLs: string = data |
| 93 | .toString() |
| 94 | .replace(maybeJSON, "") |
| 95 | .replace(maybeJSONURL + "\n", "") |
| 96 | .replace(maybeJSONURL, "") |
| 97 | .replace(messageToSendDSL + "\n", "") |
no test coverage detected