@@ -295,6 +295,17 @@ module.exports = EventEmitter;
|
||||
// regardless of how the runtime normalizes the key.
|
||||
const hasWin32 = hosts.some((h) => h.startsWith('win32'))
|
||||
let jsonFixed = 0
|
||||
// Resolve a normalized key to a disk path. Tries native-host first; on CI/Linux
|
||||
// deps may be hoisted to repo root, so fall back to ROOT for node_modules paths.
|
||||
function resolveJsonDiskPath(keyNorm) {
|
||||
const inNativeHost = path.join(NATIVE_HOST_DIR, keyNorm)
|
||||
if (fs.existsSync(inNativeHost)) return inNativeHost
|
||||
if (keyNorm.startsWith('node_modules' + path.sep) || keyNorm.startsWith('node_modules/')) {
|
||||
const inRoot = path.join(ROOT, keyNorm.replace(/\//g, path.sep))
|
||||
if (fs.existsSync(inRoot)) return inRoot
|
||||
}
|
||||
return null
|
||||
}
|
||||
let keysToProcess = typeof bundle.keys === 'function' ? [...bundle.keys()] : Object.keys(bundle.files || {})
|
||||
for (const key of keysToProcess) {
|
||||
if (!key.endsWith('.json')) continue
|
||||
@@ -308,8 +319,9 @@ module.exports = EventEmitter;
|
||||
const isEmpty = !content || content.length === 0
|
||||
const invalidJson = content && content.length > 0 && (() => { try { JSON.parse(content.toString()); return false } catch (_) { return true } })()
|
||||
if (isEmpty || invalidJson) {
|
||||
const diskPath = path.join(NATIVE_HOST_DIR, key.replace(/^\/+/, '').replace(/\//g, path.sep))
|
||||
if (fs.existsSync(diskPath)) {
|
||||
const keyNorm = key.replace(/^\/+/, '').replace(/\//g, path.sep)
|
||||
const diskPath = resolveJsonDiskPath(keyNorm)
|
||||
if (diskPath) {
|
||||
content = fs.readFileSync(diskPath)
|
||||
bundle.write(key, content)
|
||||
jsonFixed++
|
||||
@@ -349,6 +361,7 @@ module.exports = EventEmitter;
|
||||
// Verification pass: ensure every .json key in the bundle has valid JSON content
|
||||
// (catches keys we added or any the runtime might use with different normalization)
|
||||
keysToProcess = typeof bundle.keys === 'function' ? [...bundle.keys()] : Object.keys(bundle.files || {})
|
||||
const missingJson = []
|
||||
for (const key of keysToProcess) {
|
||||
if (!key.endsWith('.json')) continue
|
||||
let content = bundle.read(key)
|
||||
@@ -356,14 +369,19 @@ module.exports = EventEmitter;
|
||||
if (!valid) {
|
||||
// Normalize key to disk path: strip leading . and slashes, then use path.sep
|
||||
const keyNorm = key.replace(/^\.?[/\\]+/, '').replace(/^[/\\]+/, '').replace(/[/\\]/g, path.sep)
|
||||
const diskPath = path.join(NATIVE_HOST_DIR, keyNorm)
|
||||
if (fs.existsSync(diskPath)) {
|
||||
const diskPath = resolveJsonDiskPath(keyNorm)
|
||||
if (diskPath) {
|
||||
content = fs.readFileSync(diskPath)
|
||||
bundle.write(key, content)
|
||||
jsonFixed++
|
||||
} else if (hasWin32) {
|
||||
missingJson.push(key)
|
||||
}
|
||||
}
|
||||
}
|
||||
if (missingJson.length > 0) {
|
||||
console.warn(' Warning: could not refill .json from disk (Windows exe may fail at runtime):', missingJson.slice(0, 5).join(', ') + (missingJson.length > 5 ? ' ...' : ''))
|
||||
}
|
||||
if (jsonFixed > 0) console.log(` Patched ${jsonFixed} empty/invalid .json entries`)
|
||||
if (hasWin32 && keysToProcess.some((k) => k.endsWith('.json'))) console.log(' Added Windows backslash path variants for .json entries')
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user