|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +const { spawnSync } = require('child_process'); |
| 4 | +const path = require('path'); |
| 5 | + |
| 6 | +function parsePositiveInt(value, fallback) { |
| 7 | + const parsed = Number.parseInt(String(value || ''), 10); |
| 8 | + return Number.isFinite(parsed) && parsed > 0 ? parsed : fallback; |
| 9 | +} |
| 10 | + |
| 11 | +function appendRustflag(existingRustflags, flag) { |
| 12 | + const base = String(existingRustflags || '').trim(); |
| 13 | + if (base.includes(flag)) { |
| 14 | + return base; |
| 15 | + } |
| 16 | + return `${base} ${flag}`.trim(); |
| 17 | +} |
| 18 | + |
| 19 | +function buildLowMemoryRustflags(existingRustflags) { |
| 20 | + let rustflags = String(existingRustflags || '').trim(); |
| 21 | + rustflags = appendRustflag(rustflags, '-C debuginfo=0'); |
| 22 | + return rustflags; |
| 23 | +} |
| 24 | + |
| 25 | +function resolveDefaultTargetDir() { |
| 26 | + return path.resolve(__dirname, '..', 'src-tauri', 'target-dev-lowmem'); |
| 27 | +} |
| 28 | + |
| 29 | +function main() { |
| 30 | + const cargoTargetDir = String(process.env.CARGO_TARGET_DIR || '').trim() || resolveDefaultTargetDir(); |
| 31 | + const cargoBuildJobs = parsePositiveInt(process.env.CARGO_BUILD_JOBS, 1); |
| 32 | + const cargoIncremental = String(process.env.CARGO_INCREMENTAL || '0'); |
| 33 | + const cargoProfileDevDebug = String(process.env.CARGO_PROFILE_DEV_DEBUG || '0'); |
| 34 | + const rustflags = buildLowMemoryRustflags(process.env.RUSTFLAGS); |
| 35 | + const isWindows = process.platform === 'win32'; |
| 36 | + const tauriArgs = ['tauri', 'dev', ...process.argv.slice(2)]; |
| 37 | + const execCommand = isWindows ? 'cmd.exe' : 'npx'; |
| 38 | + const execArgs = isWindows ? ['/d', '/s', '/c', 'npx', ...tauriArgs] : tauriArgs; |
| 39 | + |
| 40 | + console.log(`[Tauri Dev Runner] Cargo target dir: ${cargoTargetDir}`); |
| 41 | + console.log(`[Tauri Dev Runner] Cargo jobs: ${cargoBuildJobs}`); |
| 42 | + console.log(`[Tauri Dev Runner] Cargo incremental: ${cargoIncremental}`); |
| 43 | + console.log(`[Tauri Dev Runner] Cargo dev debug: ${cargoProfileDevDebug}`); |
| 44 | + console.log(`[Tauri Dev Runner] RUSTFLAGS: ${rustflags || '(empty)'}`); |
| 45 | + console.log(`[Tauri Dev Runner] Executing: npx ${tauriArgs.join(' ')}`); |
| 46 | + |
| 47 | + const result = spawnSync(execCommand, execArgs, { |
| 48 | + stdio: 'inherit', |
| 49 | + env: { |
| 50 | + ...process.env, |
| 51 | + CARGO_TARGET_DIR: cargoTargetDir, |
| 52 | + CARGO_BUILD_JOBS: String(cargoBuildJobs), |
| 53 | + CARGO_INCREMENTAL: cargoIncremental, |
| 54 | + CARGO_PROFILE_DEV_DEBUG: cargoProfileDevDebug, |
| 55 | + RUSTFLAGS: rustflags, |
| 56 | + } |
| 57 | + }); |
| 58 | + |
| 59 | + if (result.error) { |
| 60 | + console.error(`[Tauri Dev Runner] Failed to start command: ${result.error.message}`); |
| 61 | + } |
| 62 | + if (result.signal) { |
| 63 | + console.error(`[Tauri Dev Runner] Command terminated by signal: ${result.signal}`); |
| 64 | + } |
| 65 | + |
| 66 | + process.exit(result.status === null ? 1 : result.status); |
| 67 | +} |
| 68 | + |
| 69 | +main(); |
0 commit comments