File size: 5,475 Bytes
b92316a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | '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
}
|