snapshot of https://github.com/mountain-loop/yaak.git (main, 2026-09-23; blobs > 131072B omitted)
This commit is contained in:
121
scripts/run-dev.mjs
Normal file
121
scripts/run-dev.mjs
Normal file
@@ -0,0 +1,121 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
/**
|
||||
* Script to run a Tauri app dev server with dynamic port configuration.
|
||||
*/
|
||||
|
||||
import { spawnSync } from "child_process";
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
import { fileURLToPath } from "url";
|
||||
|
||||
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
||||
const rootDir = path.join(__dirname, "..");
|
||||
|
||||
// Load .env.local if it exists
|
||||
const envLocalPath = path.join(rootDir, ".env.local");
|
||||
if (fs.existsSync(envLocalPath)) {
|
||||
const envContent = fs.readFileSync(envLocalPath, "utf8");
|
||||
const envVars = envContent
|
||||
.split("\n")
|
||||
.filter((line) => line && !line.startsWith("#"))
|
||||
.reduce((acc, line) => {
|
||||
const [key, value] = line.split("=");
|
||||
if (key && value) {
|
||||
acc[key.trim()] = value.trim();
|
||||
}
|
||||
return acc;
|
||||
}, {});
|
||||
|
||||
Object.assign(process.env, envVars);
|
||||
}
|
||||
|
||||
const [appName = "client", ...additionalArgs] = process.argv.slice(2);
|
||||
const appConfigs = {
|
||||
client: {
|
||||
appDir: "crates-tauri/yaak-app-client",
|
||||
devPortEnv: "YAAK_CLIENT_DEV_PORT",
|
||||
fallbackDevPortEnv: "YAAK_DEV_PORT",
|
||||
defaultPort: "1420",
|
||||
tauriConfig: "tauri.conf.json",
|
||||
tauriDevConfig: "tauri.development.conf.json",
|
||||
tauriWorktreeConfig: "tauri.worktree.conf.json",
|
||||
},
|
||||
proxy: {
|
||||
appDir: "crates-tauri/yaak-app-proxy",
|
||||
devPortEnv: "YAAK_PROXY_DEV_PORT",
|
||||
fallbackDevPortEnv: null,
|
||||
defaultPort: "2420",
|
||||
tauriConfig: "tauri.conf.json",
|
||||
tauriDevConfig: "tauri.development.conf.json",
|
||||
tauriWorktreeConfig: "tauri.worktree.conf.json",
|
||||
},
|
||||
};
|
||||
|
||||
const appConfig = appConfigs[appName];
|
||||
if (appConfig == null) {
|
||||
console.error(`Unknown Tauri app "${appName}"`);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const port =
|
||||
process.env[appConfig.devPortEnv] ||
|
||||
(appConfig.fallbackDevPortEnv ? process.env[appConfig.fallbackDevPortEnv] : undefined) ||
|
||||
appConfig.defaultPort;
|
||||
const config = JSON.stringify({
|
||||
build: { devUrl: `http://localhost:${port}` },
|
||||
});
|
||||
|
||||
// Normalize extra `--config` path args to absolute paths from repo root so
|
||||
// callers can keep passing root-relative config files.
|
||||
const normalizedAdditionalArgs = [];
|
||||
for (let i = 0; i < additionalArgs.length; i++) {
|
||||
const arg = additionalArgs[i];
|
||||
if (arg === "--") {
|
||||
normalizedAdditionalArgs.push(arg, ...additionalArgs.slice(i + 1));
|
||||
break;
|
||||
}
|
||||
if (arg === "--config" && i + 1 < additionalArgs.length) {
|
||||
const value = additionalArgs[i + 1];
|
||||
const isInlineJson = value.trimStart().startsWith("{");
|
||||
normalizedAdditionalArgs.push("--config");
|
||||
normalizedAdditionalArgs.push(
|
||||
!isInlineJson && !path.isAbsolute(value) ? path.join(rootDir, value) : value,
|
||||
);
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
normalizedAdditionalArgs.push(arg);
|
||||
}
|
||||
|
||||
// Tauri only auto-merges platform-specific config names, so the per-worktree config
|
||||
// written by the post-checkout hook has to be passed explicitly. It's gitignored, so
|
||||
// it's normally absent in the main checkout.
|
||||
const worktreeConfigArgs = [];
|
||||
if (fs.existsSync(path.join(rootDir, appConfig.appDir, appConfig.tauriWorktreeConfig))) {
|
||||
worktreeConfigArgs.push("--config", appConfig.tauriWorktreeConfig);
|
||||
}
|
||||
|
||||
const args = [
|
||||
"dev",
|
||||
"--no-watch",
|
||||
"--config",
|
||||
appConfig.tauriConfig,
|
||||
"--config",
|
||||
appConfig.tauriDevConfig,
|
||||
...worktreeConfigArgs,
|
||||
"--config",
|
||||
config,
|
||||
...normalizedAdditionalArgs,
|
||||
];
|
||||
|
||||
// Invoke the tauri CLI JS entry point directly via node to avoid shell escaping issues on Windows
|
||||
const tauriJs = path.join(rootDir, "node_modules", "@tauri-apps", "cli", "tauri.js");
|
||||
|
||||
const result = spawnSync(process.execPath, [tauriJs, ...args], {
|
||||
cwd: path.join(rootDir, appConfig.appDir),
|
||||
stdio: "inherit",
|
||||
env: process.env,
|
||||
});
|
||||
|
||||
process.exit(result.status || 0);
|
||||
Reference in New Issue
Block a user