|
| 1 | +import { execSync } from 'child_process'; |
| 2 | +import { fileURLToPath } from 'url'; |
| 3 | +import { dirname, join } from 'path'; |
| 4 | +import fs from 'fs'; |
| 5 | +import os from 'os'; |
| 6 | +import chalk from 'chalk'; |
| 7 | +import { getPlatformDetails, patchTauriConfigWithMetricsHTML } from './utils.js'; |
| 8 | + |
| 9 | +const __filename = fileURLToPath(import.meta.url); |
| 10 | +const __dirname = dirname(__filename); |
| 11 | +const projectRoot = join(__dirname, '..'); |
| 12 | + |
| 13 | +// Parse command line args |
| 14 | +const args = process.argv.slice(2); |
| 15 | +const isBundle = args.includes('--bundle'); |
| 16 | +const isDebug = args.includes('--debug'); |
| 17 | +const { platform } = getPlatformDetails(); |
| 18 | + |
| 19 | +// Print build info |
| 20 | +console.log(chalk.cyan('\n=== Phoenix Code Dist Release Build ===\n')); |
| 21 | +console.log(`Platform: ${platform}, Bundle: ${isBundle}, Debug: ${isDebug}\n`); |
| 22 | + |
| 23 | +function run(cmd, options = {}) { |
| 24 | + console.log(chalk.blue(`> ${cmd}`)); |
| 25 | + execSync(cmd, { stdio: 'inherit', ...options }); |
| 26 | +} |
| 27 | + |
| 28 | +function setupSrcNode() { |
| 29 | + console.log(chalk.cyan('\n=== Setting up src-node ===\n')); |
| 30 | + const srcNodeSource = join(projectRoot, '..', 'phoenix', 'src-node'); |
| 31 | + const destPath = join(projectRoot, 'src-tauri', 'src-node'); |
| 32 | + |
| 33 | + console.log(`Setting up ${destPath}...`); |
| 34 | + run(`shx rm -rf "${destPath}"`); |
| 35 | + run(`shx cp -r "${srcNodeSource}" "${destPath}"`); |
| 36 | + console.log('Installing production dependencies...'); |
| 37 | + execSync('npm ci --production', { cwd: destPath, stdio: 'inherit' }); |
| 38 | + // Remove unsupported musl binaries |
| 39 | + execSync(`shx rm -f "${destPath}/node_modules/@msgpackr-extract/msgpackr-extract-linux-*/*.musl.node"`, { stdio: 'pipe' }); |
| 40 | + execSync(`shx rm -f "${destPath}/node_modules/@lmdb/lmdb-linux-*/*.musl.node"`, { stdio: 'pipe' }); |
| 41 | +} |
| 42 | + |
| 43 | +function createDistConfig() { |
| 44 | + console.log(chalk.cyan('\n=== Creating Tauri Config ===\n')); |
| 45 | + const tauriConfigPath = join(projectRoot, 'src-tauri', 'tauri.conf.json'); |
| 46 | + const tauriLocalConfigPath = join(projectRoot, 'src-tauri', 'tauri-local.conf.json'); |
| 47 | + |
| 48 | + console.log('Reading Tauri config file:', tauriConfigPath); |
| 49 | + let configJson = JSON.parse(fs.readFileSync(tauriConfigPath)); |
| 50 | + |
| 51 | + if (isBundle) { |
| 52 | + console.log(chalk.cyan('\n!Updates and signing is disabled while creating msi, appimage and dmg installers. If you want to sign, use tauri build commands directly.\n')); |
| 53 | + configJson.tauri.bundle.active = true; |
| 54 | + configJson.tauri.updater.active = false; |
| 55 | + } else { |
| 56 | + console.log(chalk.cyan('\n!Only creating executables. Creating msi, appimage and dmg installers are disabled in this build. If you want to create an installer, use: npm run releaseDistBundle\n')); |
| 57 | + configJson.tauri.bundle.active = false; |
| 58 | + } |
| 59 | + |
| 60 | + configJson.build.distDir = '../../phoenix/dist/'; |
| 61 | + |
| 62 | + const phoenixVersion = configJson.package.version; |
| 63 | + if (os.platform() === 'win32') { |
| 64 | + configJson.tauri.windows[0].url = `https://phtauri.localhost/v${phoenixVersion}/`; |
| 65 | + configJson.tauri.windows[2].url = `https://phtauri.localhost/v${phoenixVersion}/drop-files.html`; |
| 66 | + } else { |
| 67 | + configJson.tauri.windows[0].url = `phtauri://localhost/v${phoenixVersion}/`; |
| 68 | + configJson.tauri.windows[2].url = `phtauri://localhost/v${phoenixVersion}/drop-files.html`; |
| 69 | + } |
| 70 | + |
| 71 | + if (os.platform() === 'darwin') { |
| 72 | + // inject macos icons |
| 73 | + configJson.tauri.bundle.icon = [ |
| 74 | + "icons-mac/32x32.png", |
| 75 | + "icons-mac/128x128.png", |
| 76 | + "icons-mac/128x128@2x.png", |
| 77 | + "icons-mac/icon.icns", |
| 78 | + "icons-mac/icon.ico" |
| 79 | + ]; |
| 80 | + } |
| 81 | + |
| 82 | + patchTauriConfigWithMetricsHTML(configJson); |
| 83 | + console.log('Window Boot url:', configJson.tauri.windows[0].url); |
| 84 | + console.log('Writing new local config json:', tauriLocalConfigPath); |
| 85 | + fs.writeFileSync(tauriLocalConfigPath, JSON.stringify(configJson, null, 4)); |
| 86 | +} |
| 87 | + |
| 88 | +function buildTauri() { |
| 89 | + console.log(chalk.cyan('\n=== Building Tauri ===\n')); |
| 90 | + const debugFlags = isDebug ? '--debug --verbose' : ''; |
| 91 | + const verboseFlag = isBundle && !isDebug ? '--verbose' : ''; |
| 92 | + run(`tauri build --config ./src-tauri/tauri-local.conf.json ${debugFlags} ${verboseFlag}`.trim().replace(/\s+/g, ' ')); |
| 93 | +} |
| 94 | + |
| 95 | +async function main() { |
| 96 | + setupSrcNode(); |
| 97 | + createDistConfig(); |
| 98 | + buildTauri(); |
| 99 | + console.log(chalk.green('\n=== Dist release build complete! ===\n')); |
| 100 | +} |
| 101 | + |
| 102 | +main().catch(err => { |
| 103 | + console.error(chalk.red('Build failed:'), err); |
| 104 | + process.exit(1); |
| 105 | +}); |
0 commit comments