Files
2026-10-11 07:55:36 -07:00

73 lines
2.8 KiB
JavaScript
Executable File

// Run a build command while mirroring ALL of its output — stdout AND stderr, every
// line timestamped — to a log file under build-logs/, so a silent/hung packaging
// step is actually visible. A 15s heartbeat keeps writing "…still running…" so the
// LAST real line before the heartbeats tells you exactly which step is stuck.
//
// It also flips on verbose logging for the whole Electron toolchain (forge,
// @electron/packager, @electron/universal, osx-sign) via DEBUG, unless you already
// set DEBUG yourself.
//
// Usage:
// node scripts/build-log.js <logname> -- <command …>
// e.g. node scripts/build-log.js mac -- npm run mac
const { spawn } = require('node:child_process')
const fs = require('node:fs')
const path = require('node:path')
const argv = process.argv.slice(2)
const sep = argv.indexOf('--')
const logName = sep > 0 ? argv[0] : 'build'
const cmdParts = sep >= 0 ? argv.slice(sep + 1) : argv
if (!cmdParts.length) {
console.error('usage: node scripts/build-log.js <logname> -- <command …>')
process.exit(1)
}
const command = cmdParts.join(' ')
const logDir = path.join(__dirname, '..', 'build-logs')
fs.mkdirSync(logDir, { recursive: true })
const stamp = new Date().toISOString().replace(/[:.]/g, '-')
const logFile = path.join(logDir, `${logName}-${stamp}.log`)
const out = fs.createWriteStream(logFile, { flags: 'a' })
const ts = () => new Date().toISOString()
function mirror(chunk) {
const text = chunk.toString()
process.stdout.write(text) // live console, unchanged
out.write(text.replace(/^/gm, `[${ts()}] `)) // file copy, line-timestamped
}
console.log(`\n=== build-log → ${logFile} ===\n`)
out.write(`=== build-log started ${ts()} ===\ncmd: ${command}\ncwd: ${process.cwd()}\n\n`)
// Verbose logging across the Electron packaging toolchain unless the caller set DEBUG.
const env = { ...process.env }
if (!env.DEBUG) env.DEBUG = 'electron-*,@electron/*,electron-forge:*,electron-osx-sign*,electron-notarize*'
// Force color off so the log file isn't full of escape codes.
env.FORCE_COLOR = '0'
const child = spawn(command, { env, shell: true, stdio: ['inherit', 'pipe', 'pipe'] })
child.stdout.on('data', mirror)
child.stderr.on('data', mirror)
// Heartbeat: if the child goes quiet, these lines show the log is still live and
// pinpoint when output stopped (= the hung step).
const hb = setInterval(() => out.write(`[${ts()}] …still running (no output)…\n`), 15000)
child.on('exit', (code, signal) => {
clearInterval(hb)
const msg = `\n=== exited code=${code} signal=${signal} at ${ts()} ===\n`
process.stdout.write(msg)
out.write(msg)
out.end()
process.exit(code == null ? 1 : code)
})
child.on('error', (e) => {
clearInterval(hb)
const msg = `\n=== spawn error: ${e.stack || e.message} ===\n`
process.stdout.write(msg)
out.write(msg)
out.end()
process.exit(1)
})