51 lines
1.5 KiB
JavaScript
51 lines
1.5 KiB
JavaScript
import { spawn, spawnSync } from "node:child_process";
|
||
import { existsSync, readFileSync, writeFileSync } from "node:fs";
|
||
import { dirname, resolve } from "node:path";
|
||
import { fileURLToPath } from "node:url";
|
||
|
||
const projectRoot = resolve(dirname(fileURLToPath(import.meta.url)), "..");
|
||
const npmCommand = process.platform === "win32" ? "npm.cmd" : "npm";
|
||
const cacheFile = resolve(
|
||
projectRoot,
|
||
"node_modules/vinext/dist/server/static-file-cache.js",
|
||
);
|
||
const originalLine = 'const pathname = "/" + relativePath;';
|
||
const windowsSafeLine =
|
||
'const pathname = "/" + relativePath.replaceAll("\\\\", "/");';
|
||
|
||
if (!existsSync(cacheFile)) {
|
||
console.error("未找到项目依赖,请先执行 npm install。");
|
||
process.exit(1);
|
||
}
|
||
|
||
const cacheSource = readFileSync(cacheFile, "utf8");
|
||
if (cacheSource.includes(originalLine)) {
|
||
writeFileSync(
|
||
cacheFile,
|
||
cacheSource.replace(originalLine, windowsSafeLine),
|
||
"utf8",
|
||
);
|
||
console.log("已应用 Windows 静态资源路径兼容处理。");
|
||
}
|
||
|
||
const build = spawnSync(npmCommand, ["run", "build"], {
|
||
cwd: projectRoot,
|
||
stdio: "inherit",
|
||
});
|
||
|
||
if (build.status !== 0) {
|
||
process.exit(build.status ?? 1);
|
||
}
|
||
|
||
console.log("\n整个模板项目正在启动:http://localhost:3000/\n");
|
||
const server = spawn(npmCommand, ["run", "start", "--", "--port", "3000"], {
|
||
cwd: projectRoot,
|
||
stdio: "inherit",
|
||
});
|
||
|
||
server.on("exit", (code) => process.exit(code ?? 0));
|
||
|
||
for (const signal of ["SIGINT", "SIGTERM"]) {
|
||
process.on(signal, () => server.kill(signal));
|
||
}
|