'use strict' /** * platform-win32.js — Windows-specific runtime adapter * Loaded exclusively when process.platform === 'win32' */ const path = require('path') const os = require('os') const { spawnSync } = require('child_process') const PLATFORM = 'win32' // ── Writable path candidates for app userData ───────────────────────────────── function getWritableCandidates(appDirName) { const candidates = [] const localAppData = process.env.LOCALAPPDATA if (localAppData) { candidates.push(path.join(localAppData, 'Chahuadev', appDirName)) } candidates.push(path.join(os.tmpdir(), appDirName)) return candidates } // ── Command name normalization ──────────────────────────────────────────────── // Windows requires .cmd suffix for package manager shims in PATH. function normalizeExecName(cmd) { const c = String(cmd || '').trim() if (!c) return c const lc = c.toLowerCase() if (lc === 'npm') return 'npm.cmd' if (lc === 'npx') return 'npx.cmd' if (lc === 'pnpm') return 'pnpm.cmd' if (lc === 'yarn') return 'yarn.cmd' return c } // ── Shell wrapping (for .bat / .cmd files) ──────────────────────────────────── function shouldUseShell(cmd) { const c = String(cmd || '').toLowerCase() return c.endsWith('.bat') || c.endsWith('.cmd') } function quoteArg(arg) { const s = String(arg || '') if (!s) return '""' if (!/[\s"]/g.test(s)) return s return `"${s.replace(/(\\*)"/g, '$1$1\\"').replace(/(\\+)$/g, '$1$1')}"` } function buildCmdLine(cmd, args) { return [cmd, ...(args || [])].map(quoteArg).join(' ') } // Returns { file, args } ready for node-pty.spawn() function buildShellSpawn(cmd, args) { const comspec = process.env.COMSPEC || 'cmd.exe' return { file: comspec, args: ['/d', '/s', '/c', buildCmdLine(cmd, args)] } } // ── node-pty spawn extra options ────────────────────────────────────────────── // useConpty enables Windows 10+ ConPTY backend — required on Windows for // proper ANSI colour support in cmd.exe / PowerShell. function getPtySpawnOptions() { return { useConpty: true } } // ── Node.js runtime path ────────────────────────────────────────────────────── function resolveNodeRuntime() { try { const r = spawnSync('where', ['node'], { windowsHide: true, encoding: 'utf8', shell: false }) if ((r.status ?? 1) === 0) { const first = String(r.stdout || '') .split(/\r?\n/) .map(x => x.trim()) .find(Boolean) if (first) return first } } catch (_e) { /* where.exe failed or node not found; fall through to default */ } return 'node.exe' } // ── Linux build command rewrite (via WSL) ───────────────────────────────────── function _quoteBash(s) { return `'${String(s || '').replace(/'/g, `'"'"'`)}'` } function windowsPathToWsl(inputPath) { const raw = String(inputPath || '') const win = raw.replace(/\//g, '\\') const m = win.match(/^([a-zA-Z]):\\(.*)$/) if (!m) return raw.replace(/\\/g, '/') const drive = String(m[1] || '').toLowerCase() const tail = String(m[2] || '').replace(/\\/g, '/') return `/mnt/${drive}/${tail}` } function rewriteLinuxBuildCommand(cwd) { const wslTarget = windowsPathToWsl(cwd) const script = [ 'set -euo pipefail', `cd ${_quoteBash(wslTarget)}`, 'npm install', 'if npm run | grep -q "build:linux"; then npm run build:linux; else npx electron-builder --linux AppImage --x64 --publish never; fi' ].join('; ') return { cmd: 'wsl.exe', args: ['bash', '-lc', script], rewriteInfo: { applied: true, kind: 'launcher-linux-build-inline-wsl', targetProjectPath: cwd } } } // ── Protected OS path bases ─────────────────────────────────────────────────── function getProtectedPathBases() { const homeDir = os.homedir() const systemDrive = String(process.env.SystemDrive || 'C:').toUpperCase() const driveRoot = `${systemDrive}\\` return [ path.join(driveRoot, 'Windows'), path.join(driveRoot, 'Program Files'), path.join(driveRoot, 'Program Files (x86)'), path.join(driveRoot, 'ProgramData'), path.join(driveRoot, 'Users'), path.join(driveRoot, 'PerfLogs'), path.join(driveRoot, 'Recovery'), path.join(driveRoot, 'System Volume Information'), path.join(driveRoot, '$Recycle.Bin'), path.join(homeDir, 'AppData'), path.join(homeDir, 'Local Settings') ] } module.exports = { PLATFORM, getWritableCandidates, normalizeExecName, shouldUseShell, buildShellSpawn, getPtySpawnOptions, resolveNodeRuntime, rewriteLinuxBuildCommand, getProtectedPathBases, // Exported for tests / diagnostics windowsPathToWsl, quoteArg, buildCmdLine }