Files
Pake/script/build_with_pake_cli.js
2025-08-20 17:00:23 +08:00

141 lines
4.3 KiB
JavaScript
Vendored

import fs from "fs";
import path from "path";
import { execa } from "execa";
// Configuration logging
const logConfiguration = () => {
console.log("Welcome to use pake-cli to build app");
console.log("Node.js info in your localhost:", process.version);
console.log("\n=======================\n");
console.log("Pake parameters:");
console.log("url:", process.env.URL);
console.log("name:", process.env.NAME);
console.log("icon:", process.env.ICON);
console.log("height:", process.env.HEIGHT);
console.log("width:", process.env.WIDTH);
console.log("fullscreen:", process.env.FULLSCREEN);
console.log("hide-title-bar:", process.env.HIDE_TITLE_BAR);
console.log("is multi arch? only for Mac:", process.env.MULTI_ARCH);
console.log("targets type? only for Linux:", process.env.TARGETS);
console.log("===========================\n");
};
// Main execution
const main = async () => {
try {
logConfiguration();
const cliPath = path.join(process.cwd(), "node_modules/pake-cli");
// Check if pake-cli directory exists
if (!fs.existsSync(cliPath)) {
console.error("Error: pake-cli not found at", cliPath);
console.error("Please make sure pake-cli is installed: npm install pake-cli");
process.exit(1);
}
process.chdir(cliPath);
// Clean up any previous configuration to ensure fresh build
const pakeDirPath = path.join("src-tauri", ".pake");
// Remove .pake directory to force fresh config generation
if (fs.existsSync(pakeDirPath)) {
fs.rmSync(pakeDirPath, { recursive: true, force: true });
console.log("Cleaned previous .pake directory");
}
// Fix hardcoded default config in npm package
const defaultConfigPath = path.join("src-tauri", "tauri.conf.json");
if (fs.existsSync(defaultConfigPath)) {
const defaultConfig = JSON.parse(fs.readFileSync(defaultConfigPath, 'utf8'));
defaultConfig.productName = process.env.NAME;
defaultConfig.identifier = `com.pake.${process.env.NAME.toLowerCase()}`;
fs.writeFileSync(defaultConfigPath, JSON.stringify(defaultConfig, null, 2));
console.log(`Fixed default config: productName -> ${process.env.NAME}`);
}
// Build CLI parameters
let params = [
"dist/cli.js",
process.env.URL,
"--name",
process.env.NAME,
"--height",
process.env.HEIGHT,
"--width",
process.env.WIDTH,
];
if (
process.env.HIDE_TITLE_BAR === "true" &&
process.platform === "darwin"
) {
params.push("--hide-title-bar");
}
if (process.env.FULLSCREEN === "true") {
params.push("--fullscreen");
}
if (process.env.MULTI_ARCH === "true" && process.platform === "darwin") {
params.push("--multi-arch");
}
if (process.env.TARGETS && process.platform === "linux") {
params.push("--targets", process.env.TARGETS);
}
if (process.platform === "win32" || process.platform === "linux") {
params.push("--show-system-tray");
}
// Add icon parameter if provided - CLI will handle download and conversion
if (process.env.ICON && process.env.ICON !== "") {
params.push("--icon", process.env.ICON);
} else {
console.log(
"Won't use icon as ICON environment variable is not defined!",
);
}
console.log("Pake parameters:", params.join(" "));
console.log("Expected app name:", process.env.NAME);
console.log("Compiling....");
// Execute the CLI command
await execa("node", params, { stdio: "inherit" });
// Create output directory and move built files
if (!fs.existsSync("output")) {
fs.mkdirSync("output");
}
// pake-cli outputs files to current directory with pattern: {name}.{extension}
const files = fs.readdirSync(".");
const namePattern = new RegExp(`^${process.env.NAME}\\..*$`);
let foundFiles = false;
for (const file of files) {
if (namePattern.test(file)) {
const destPath = path.join("output", file);
fs.renameSync(file, destPath);
console.log(`Moved: ${file} -> output/${file}`);
foundFiles = true;
}
}
if (!foundFiles) {
console.log("Warning: No output files found matching pattern");
}
console.log("Build Success");
process.chdir("../..");
} catch (error) {
console.error("Build failed:", error);
process.exit(1);
}
};
main();