File size: 2,896 Bytes
e5425d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
'use strict'

const fs = require('fs')
const path = require('path')
const { spawn } = require('child_process')
const bytenode = require('bytenode')

const FILES_TO_COMPILE = ['main.js']
const backups = new Map()
const generatedJsc = new Set()
let restoring = false

async function backupAndCompile() {
  for (const file of FILES_TO_COMPILE) {
    const absPath = path.join(process.cwd(), file)
    if (!fs.existsSync(absPath)) continue

    const backupPath = `${absPath}.backup`
    fs.copyFileSync(absPath, backupPath)
    backups.set(absPath, backupPath)

    const parsed = path.parse(absPath)
    const jscPath = path.join(parsed.dir, `${parsed.name}.jsc`)
    const existedBefore = fs.existsSync(jscPath)

    await bytenode.compileFile({
      filename: absPath,
      output: jscPath,
      electron: true,
      compileAsModule: true
    })

    if (!existedBefore) generatedJsc.add(jscPath)
    console.log(`[V8 Builder] Compiled ${file} -> ${path.basename(jscPath)}`)

    const relBase = parsed.name
    const requireLine = file === 'main.js'
      ? `module.exports = require('./${relBase}.jsc')`
      : `require('./${relBase}.jsc')`
    fs.writeFileSync(absPath, ["'use strict'", "require('bytenode')", requireLine, ''].join('\n'), 'utf8')
  }
}

function restoreFiles() {
  if (restoring) return
  restoring = true
  console.log('\n[V8 Builder] Restoring original source files...')
  for (const [absPath, backupPath] of backups) {
    try {
      if (fs.existsSync(backupPath)) {
        fs.copyFileSync(backupPath, absPath)
        fs.unlinkSync(backupPath)
        console.log(`[V8 Builder] Restored ${path.basename(absPath)}`)
      }
    } catch (err) {
      console.error(`[V8 Builder] restore failed for ${path.basename(absPath)}: ${err.message}`)
    }
  }
  for (const jscPath of generatedJsc) {
    try { if (fs.existsSync(jscPath)) fs.unlinkSync(jscPath) } catch {}
  }
}

process.on('SIGINT', () => { restoreFiles(); process.exit(1) })
process.on('uncaughtException', (err) => { console.error(err); restoreFiles(); process.exit(1) })

async function run() {
  try {
    console.log('=== Starting V8 Compilation and Build ===')
    await backupAndCompile()

    const builderCli = require.resolve('electron-builder/out/cli/cli.js')
    const extraArgs = process.argv.slice(2)
    const args = [builderCli, ...extraArgs]

    const child = spawn(process.execPath, args, { stdio: 'inherit', windowsHide: true, shell: false, env: { ...process.env } })
    child.on('exit', (code) => { restoreFiles(); process.exit(code ?? 1) })
    child.on('error', (err) => { console.error(`[V8 Builder] failed: ${err.message}`); restoreFiles(); process.exit(1) })
  } catch (err) {
    console.error(`[V8 Builder] prebuild failed: ${err.message}`)
    restoreFiles()
    process.exit(1)
  }
}

run()