This repository has been archived on 2026-08-27. You can view files and clone it, but cannot push or open issues or pull requests.
Files
RECS-WEB-MODLE-REACT/scripts/start-local.mjs
2026-08-27 20:15:24 +08:00

51 lines
1.5 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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));
}