|
| 1 | +import * as cp from "child_process" |
| 2 | +import * as path from "path" |
| 3 | +import { RebuildOptions } from "@electron/rebuild" |
| 4 | +import { log } from "builder-util" |
| 5 | + |
| 6 | +export const rebuild = async (options: RebuildOptions): Promise<void> => { |
| 7 | + const { arch } = options |
| 8 | + log.info({ arch }, `installing native dependencies`) |
| 9 | + |
| 10 | + const child = cp.fork(path.resolve(__dirname, "remote-rebuild.js"), [JSON.stringify(options)], { |
| 11 | + stdio: ["pipe", "pipe", "pipe", "ipc"], |
| 12 | + }) |
| 13 | + |
| 14 | + let pendingError: Error |
| 15 | + |
| 16 | + child.stdout?.on("data", chunk => { |
| 17 | + log.info(chunk.toString()) |
| 18 | + }) |
| 19 | + child.stderr?.on("data", chunk => { |
| 20 | + log.error(chunk.toString()) |
| 21 | + }) |
| 22 | + |
| 23 | + child.on("message", (message: { msg: string; moduleName: string; err: { message: string; stack: string } }) => { |
| 24 | + const { moduleName, msg } = message |
| 25 | + switch (msg) { |
| 26 | + case "module-found": { |
| 27 | + log.info({ moduleName, arch }, "preparing") |
| 28 | + break |
| 29 | + } |
| 30 | + case "module-done": { |
| 31 | + log.info({ moduleName, arch }, "finished") |
| 32 | + break |
| 33 | + } |
| 34 | + case "module-skip": { |
| 35 | + log.debug?.({ moduleName, arch }, "skipped. set ENV=electron-rebuild to determine why") |
| 36 | + break |
| 37 | + } |
| 38 | + case "rebuild-error": { |
| 39 | + pendingError = new Error(message.err.message) |
| 40 | + pendingError.stack = message.err.stack |
| 41 | + break |
| 42 | + } |
| 43 | + case "rebuild-done": { |
| 44 | + log.info("completed installing native dependencies") |
| 45 | + break |
| 46 | + } |
| 47 | + } |
| 48 | + }) |
| 49 | + |
| 50 | + await new Promise<void>((resolve, reject) => { |
| 51 | + child.on("exit", code => { |
| 52 | + if (code === 0 && !pendingError) { |
| 53 | + resolve() |
| 54 | + } else { |
| 55 | + reject(pendingError || new Error(`Rebuilder failed with exit code: ${code}`)) |
| 56 | + } |
| 57 | + }) |
| 58 | + }) |
| 59 | +} |
0 commit comments