节点在线、应用在线、配置在线使用令牌查询
大石头 authored at 2021-12-16 19:49:30
2.56 KiB
Stardust
#!/usr/bin/env node
/**
 * 一键打包前后端(由根 package.json 的 `pnpm build:all` 调用)。
 *
 * 流程:
 *   1. pnpm run build   —— 重建 Cube 工作区包(dist 为 gitignore 产物)+ vue-tsc 类型检查 + vite build
 *   2. sync-wwwroot.mjs —— dist 整树同步到 Stardust.WebApi/wwwroot(嵌入源,已 gitignore)
 *   3. dotnet publish   —— Release 发布 Stardust.WebApi 到 Bin/WebApi/publish,前端产物嵌入 DLL
 *
 * TEMP 重定向(本机环境绕行,仅作用于步骤 1):本机常驻杀软(QQPCRTP/火绒 HipsTray 等)
 * 实时扫描 %TEMP% 下新建文件并短暂持句柄,esbuild 服务进程压缩大体积 CSS 时写入
 * %TEMP%\esbuild-* 再删除会稳定报 "Access is denied";仓库内目录(.forgeself 已 gitignore)
 * 无此问题。对本机之外的环境无副作用。根治办法是把仓库目录加入杀软白名单或卸载常驻杀软。
 */
import { spawnSync } from 'node:child_process';
import { mkdirSync, rmSync } from 'node:fs';
import { dirname, join, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';

const scriptDir = dirname(fileURLToPath(import.meta.url));
const repoRoot = resolve(scriptDir, '..', '..');

const steps = [
  {
    cmd: 'pnpm run build',
    label: '构建前端(Cube 包 + vue-tsc + vite build)',
    tempRedirect: true,
  },
  {
    cmd: 'node Stardust.Web.Vue/scripts/sync-wwwroot.mjs',
    label: '同步 dist -> Stardust.WebApi/wwwroot',
    tempRedirect: false,
  },
  {
    cmd: 'dotnet publish Stardust.WebApi/Stardust.WebApi.csproj --nologo -c Release -o Bin/WebApi/publish',
    label: '发布后端(前端产物嵌入 DLL)',
    tempRedirect: false,
  },
];

// 清掉上一轮残留的 esbuild 临时目录(杀软句柄可能未释放,失败不阻断)
const tempDir = join(repoRoot, '.forgeself', 'tmp', 'esbuild-temp');
try {
  rmSync(tempDir, { recursive: true, force: true });
} catch {}
mkdirSync(tempDir, { recursive: true });
const envRedirect = { ...process.env, TEMP: tempDir, TMP: tempDir };

for (const step of steps) {
  console.log(`[build-all] ${step.label} ...`);
  const r = spawnSync(step.cmd, {
    shell: true,
    stdio: 'inherit',
    cwd: repoRoot,
    env: step.tempRedirect ? envRedirect : process.env,
  });
  if (r.status !== 0) {
    console.error(`[build-all] 步骤失败(exit ${r.status ?? 'signal'}):${step.cmd}`);
    process.exit(r.status ?? 1);
  }
}

console.log('[build-all] 完成:前端已构建并同步到 Stardust.WebApi/wwwroot,后端已发布到 Bin/WebApi/publish(前端嵌入 DLL)。');