|
| 1 | +const fs = require('node:fs'); |
| 2 | +const path = require('node:path'); |
| 3 | + |
| 4 | +const target = path.join( |
| 5 | + process.cwd(), |
| 6 | + 'node_modules', |
| 7 | + 'vite', |
| 8 | + 'dist', |
| 9 | + 'node', |
| 10 | + 'chunks', |
| 11 | + 'config.js' |
| 12 | +); |
| 13 | + |
| 14 | +if (!fs.existsSync(target)) { |
| 15 | + console.log('[patch-vite-windows-eperm] Skip: vite config chunk not found'); |
| 16 | + process.exit(0); |
| 17 | +} |
| 18 | + |
| 19 | +const source = fs.readFileSync(target, 'utf8'); |
| 20 | +const marker = 'Some locked-down Windows environments block child_process spawn.'; |
| 21 | +if (source.includes(marker)) { |
| 22 | + console.log('[patch-vite-windows-eperm] Already patched'); |
| 23 | + process.exit(0); |
| 24 | +} |
| 25 | + |
| 26 | +const before = `\texec("net use", (error$1, stdout) => { |
| 27 | +\t\tif (error$1) return; |
| 28 | +\t\tconst lines = stdout.split("\\n"); |
| 29 | +\t\tfor (const line of lines) { |
| 30 | +\t\t\tconst m = parseNetUseRE.exec(line); |
| 31 | +\t\t\tif (m) windowsNetworkMap.set(m[2], m[1]); |
| 32 | +\t\t} |
| 33 | +\t\tif (windowsNetworkMap.size === 0) safeRealpathSync = fs.realpathSync.native; |
| 34 | +\t\telse safeRealpathSync = windowsMappedRealpathSync; |
| 35 | +\t});`; |
| 36 | + |
| 37 | +const after = `\ttry { |
| 38 | +\t\texec("net use", (error$1, stdout) => { |
| 39 | +\t\t\tif (error$1) return; |
| 40 | +\t\t\tconst lines = stdout.split("\\n"); |
| 41 | +\t\t\tfor (const line of lines) { |
| 42 | +\t\t\t\tconst m = parseNetUseRE.exec(line); |
| 43 | +\t\t\t\tif (m) windowsNetworkMap.set(m[2], m[1]); |
| 44 | +\t\t\t} |
| 45 | +\t\t\tif (windowsNetworkMap.size === 0) safeRealpathSync = fs.realpathSync.native; |
| 46 | +\t\t\telse safeRealpathSync = windowsMappedRealpathSync; |
| 47 | +\t\t}); |
| 48 | +\t} catch { |
| 49 | +\t\t// Some locked-down Windows environments block child_process spawn. |
| 50 | +\t\t// Fallback to native realpath to avoid crashing during config load. |
| 51 | +\t\tsafeRealpathSync = fs.realpathSync.native; |
| 52 | +\t}`; |
| 53 | + |
| 54 | +if (!source.includes(before)) { |
| 55 | + console.log('[patch-vite-windows-eperm] Pattern not found, nothing changed'); |
| 56 | + process.exit(0); |
| 57 | +} |
| 58 | + |
| 59 | +fs.writeFileSync(target, source.replace(before, after), 'utf8'); |
| 60 | +console.log('[patch-vite-windows-eperm] Patch applied'); |
0 commit comments