// macOS names an app in the menu bar, the Dock and the app switcher after the // bundle its executable lives in, and nothing a running process can do // changes that. `mollusc dev` runs the prebuilt Electron, so every app under // development was "Electron" with Electron's icon. So on macOS dev runs a // copy of Electron.app that carries the app's own name and icon instead: a // copy-on-write clone (instant, and no disk, on APFS) kept in the user's // cache, one per app, remade when the Electron version, the name or the icon // changes. The shell still sets the Dock icon at run time; this is what makes // the name right and the icon right before the app is up. // // MOLLUSC_STOCK_ELECTRON=1 opts out. Any failure falls back to the stock // Electron: a wrong name is better than no dev loop. import { execFile } from 'node:child_process'; import { createHash } from 'node:crypto'; import { existsSync } from 'node:fs'; import { mkdir, readFile, rm, stat, writeFile } from 'node:fs/promises'; import { homedir, tmpdir } from 'node:os'; import { basename, dirname, join, resolve } from 'node:path'; import { promisify } from 'node:util'; const run = promisify(execFile); /** The name macOS should show: the product name a build would have. */ export function devProductName (config) { return config.build?.productName || config.name; } /** Where an app's dev bundle lives, and what it is made from. Pure. */ export function devAppPlan ({ config, appDir, electronApp, electronVersion, cacheRoot }) { const name = devProductName(config); // Bundle names are file names: keep what a file name may hold. const safe = name.replace(/[/:\0]/g, '-'); const dir = join(cacheRoot, config.id); const icon = config.icon && /\.png$/i.test(config.icon) ? resolve(appDir, config.icon) : null; return { name, dir, app: join(dir, `${safe}.app`), executable: join(dir, `${safe}.app`, 'Contents', 'MacOS', basename(electronApp, '.app')), stampFile: join(dir, 'stamp'), icon, bundleId: `${config.id}.dev`, stampFor: (iconMtime) => createHash('sha256').update(JSON.stringify([1, electronVersion, electronApp, name, icon, iconMtime])).digest('hex'), }; } // An .icns from one PNG, with the system's own tools. async function makeIcns (png, out) { const set = join(tmpdir(), `mollusc-${process.pid}.iconset`); await rm(set, { recursive: true, force: true }); await mkdir(set); try { for (const size of [16, 32, 128, 256, 512]) { await run('sips', ['-z', String(size), String(size), png, '--out', join(set, `icon_${size}x${size}.png`)]); await run('sips', ['-z', String(size * 2), String(size * 2), png, '--out', join(set, `icon_${size}x${size}@2x.png`)]); } await run('iconutil', ['-c', 'icns', set, '-o', out]); } finally { await rm(set, { recursive: true, force: true }); } } /** * The executable `mollusc dev` should run for this app: on macOS, the one in * the app's own named copy of Electron.app (made or refreshed here); anywhere * else, and on any failure, `electron` as given. */ export async function devExecutable ({ electron, config, appDir, electronVersion, env = process.env, log = () => {}, platform = process.platform }) { if (platform !== 'darwin' || env.MOLLUSC_STOCK_ELECTRON) return electron; // …/Electron.app/Contents/MacOS/Electron const electronApp = dirname(dirname(dirname(electron))); if (!electronApp.endsWith('.app')) return electron; try { const plan = devAppPlan({ config, appDir, electronApp, electronVersion, cacheRoot: join(homedir(), 'Library', 'Caches', 'mollusc', 'dev'), }); const iconMtime = plan.icon && existsSync(plan.icon) ? (await stat(plan.icon)).mtimeMs : null; const stamp = plan.stampFor(iconMtime); const current = existsSync(plan.stampFile) && existsSync(plan.executable) && (await readFile(plan.stampFile, 'utf8')) === stamp; if (current) return plan.executable; await rm(plan.dir, { recursive: true, force: true }); await mkdir(plan.dir, { recursive: true }); // -c clones on APFS; a plain copy where cloning is not to be had. await run('cp', ['-c', '-R', electronApp, plan.app]).catch(() => run('cp', ['-R', electronApp, plan.app])); const plist = join(plan.app, 'Contents', 'Info.plist'); const set = (key, value) => run('/usr/libexec/PlistBuddy', ['-c', `Set :${key} ${JSON.stringify(value)}`, plist]) .catch(() => run('/usr/libexec/PlistBuddy', ['-c', `Add :${key} string ${JSON.stringify(value)}`, plist])); await set('CFBundleName', plan.name); await set('CFBundleDisplayName', plan.name); await set('CFBundleIdentifier', plan.bundleId); if (iconMtime !== null) { const { stdout } = await run('/usr/libexec/PlistBuddy', ['-c', 'Print :CFBundleIconFile', plist]); const icns = join(plan.app, 'Contents', 'Resources', stdout.trim().replace(/(\.icns)?$/, '.icns')); await makeIcns(plan.icon, icns); } // The plist is sealed into the signature; sign again, ad hoc, so the // bundle is whole. Launching does not depend on it, so failing is fine. await run('codesign', ['--force', '--deep', '--sign', '-', plan.app]).catch(() => {}); await writeFile(plan.stampFile, stamp); log(`${plan.name}: dev runs as "${plan.name}" (${plan.app})`); return plan.executable; } catch (err) { log(`warning: could not name the dev app (${err.message}); it will show as Electron`); return electron; } }