// `mollusc dev`: run the shell on the prebuilt Electron with the app directory // as its argument. The shell itself does the watching and reloading. import { spawn } from 'node:child_process'; import { resolve } from 'node:path'; import { loadConfig } from '../shell/config.js'; import { electronVersion, runBefore, shellDir } from './builder.js'; import { devExecutable } from './dev-app.js'; export async function runDev (appDir, { args = [], env = {}, log = console.log } = {}) { appDir = resolve(appDir); // Fail here, with a readable message, rather than inside Electron. const { config, warnings } = await loadConfig(appDir); for (const w of warnings) log(`warning: ${w}`); // A first build of what the page needs (a bundle, say), so the window does // not open on a stale one while the watchers in dev.serve catch up. for (const command of config.dev.before) await runBefore(command, appDir, log); log(`${config.name} ${config.version}: starting`); // On macOS, the app's own named copy of Electron.app (see dev-app.js). const electron = await devExecutable({ electron: (await import('electron')).default, config, appDir, electronVersion: await electronVersion(), log, }); const childEnv = { ...process.env, ...env }; // Set, this makes the Electron binary a plain Node and no window ever opens. delete childEnv.ELECTRON_RUN_AS_NODE; const child = spawn(electron, [shellDir, '--app', appDir, ...args], { stdio: 'inherit', env: childEnv }); for (const signal of ['SIGINT', 'SIGTERM']) process.on(signal, () => child.kill(signal)); return new Promise((res, rej) => { child.on('error', rej); child.on('exit', (code, signal) => res(code ?? (signal ? 1 : 0))); }); }