Files
trackgo-fe/apps/desktop/scripts/copy-web-dist.ts
T

71 lines
2.9 KiB
TypeScript

/**
* copy-web-dist.ts
*
* Prebuild script for apps/desktop.
* Copies the build output of the target web app into ./web-dist/
* so electron-builder can bundle it into the packaged application.
*
* Reads DESKTOP_TARGET_APP from .env (default: "web").
*
* Directory layout:
* monorepo-root/
* apps/
* desktop/
* scripts/ ← this file lives here
* web-dist/ ← destination
* web/
* dist/ ← source
*/
import { cpSync, existsSync, mkdirSync, rmSync, readFileSync } from 'fs';
import { resolve } from 'path';
import { fileURLToPath } from 'url';
// ── Resolve paths ──────────────────────────────────────────────
// Use import.meta.url for ESM compatibility (works across Node versions)
const SCRIPT_DIR = resolve(fileURLToPath(import.meta.url), '..');
const DESKTOP_DIR = resolve(SCRIPT_DIR, '..');
const MONOREPO_ROOT = resolve(DESKTOP_DIR, '..', '..');
const DEST_DIR = resolve(DESKTOP_DIR, 'web-dist');
// ── Read .env for target app name ──────────────────────────────
function loadTargetApp(): string {
const envPath = resolve(DESKTOP_DIR, '.env');
if (existsSync(envPath)) {
const content = readFileSync(envPath, 'utf-8');
const match = content.match(/^DESKTOP_TARGET_APP=(.+)$/m);
if (match) return match[1].trim();
}
return 'web';
}
const targetApp = process.env.DESKTOP_TARGET_APP || loadTargetApp();
const SOURCE_DIR = resolve(MONOREPO_ROOT, 'apps', targetApp, 'dist');
// ── Debug: print resolved paths ────────────────────────────────
console.log(`\n📦 copy-web-dist`);
console.log(` Target app : ${targetApp}`);
console.log(` Monorepo root: ${MONOREPO_ROOT}`);
console.log(` Source : ${SOURCE_DIR}`);
console.log(` Destination : ${DEST_DIR}`);
// ── Validate ───────────────────────────────────────────────────
if (!existsSync(SOURCE_DIR)) {
console.error(
`\n❌ Build output not found at: ${SOURCE_DIR}\n` +
` Run "pnpm build --filter=${targetApp}" first.\n`,
);
process.exit(1);
}
// ── Clean destination ──────────────────────────────────────────
if (existsSync(DEST_DIR)) {
rmSync(DEST_DIR, { recursive: true, force: true });
}
mkdirSync(DEST_DIR, { recursive: true });
// ── Copy ───────────────────────────────────────────────────────
cpSync(SOURCE_DIR, DEST_DIR, { recursive: true });
console.log(`✅ Copied ${targetApp} build → web-dist/\n`);