75 lines
2.5 KiB
JavaScript
75 lines
2.5 KiB
JavaScript
|
|
/**
|
|||
|
|
* @description 通用 Vite 开发启动脚本,先构建 Monaco DTS,再按指定 mode 启动 Vite。
|
|||
|
|
*/
|
|||
|
|
import { spawn } from 'node:child_process';
|
|||
|
|
import { basename, dirname, resolve } from 'node:path';
|
|||
|
|
import process from 'node:process';
|
|||
|
|
import { fileURLToPath } from 'node:url';
|
|||
|
|
|
|||
|
|
const __filename = fileURLToPath(import.meta.url);
|
|||
|
|
const __dirname = dirname(__filename);
|
|||
|
|
const pkgRoot = resolve(__dirname, '..');
|
|||
|
|
const cliArgs = process.argv.slice(2);
|
|||
|
|
const modeFlagIndex = cliArgs.findIndex(arg => arg === '--mode');
|
|||
|
|
const modeArg = modeFlagIndex >= 0 ? String(cliArgs[modeFlagIndex + 1] || '').trim() : '';
|
|||
|
|
const mode = modeArg || String(process.env.BUSINESS_RULES_MODE || 'dev').trim() || 'dev';
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @description 以继承输出的方式执行命令,并等待结束。
|
|||
|
|
* @param {string} command 命令。
|
|||
|
|
* @param {string[]} args 参数。
|
|||
|
|
* @param {NodeJS.ProcessEnv} env 环境变量。
|
|||
|
|
* @returns {Promise<void>}
|
|||
|
|
*/
|
|||
|
|
function runCommand(command, args, env) {
|
|||
|
|
return new Promise((resolvePromise, rejectPromise) => {
|
|||
|
|
const child = spawn(command, args, {
|
|||
|
|
cwd: pkgRoot,
|
|||
|
|
env,
|
|||
|
|
stdio: 'inherit',
|
|||
|
|
});
|
|||
|
|
child.on('error', rejectPromise);
|
|||
|
|
child.on('exit', (code, signal) => {
|
|||
|
|
if (signal) {
|
|||
|
|
rejectPromise(new Error(`${command} exited with signal ${signal}`));
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
if (code !== 0) {
|
|||
|
|
rejectPromise(new Error(`${command} ${args.join(' ')} exited with code ${code}`));
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
resolvePromise();
|
|||
|
|
});
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @description 解析当前 pnpm runner。Windows 下裸 spawn('pnpm') 不会稳定命中 pnpm.cmd,
|
|||
|
|
* 优先复用 pnpm 注入的 npm_execpath,保证 Windows/macOS 都走同一个包管理器入口。
|
|||
|
|
* @param {NodeJS.ProcessEnv} env 环境变量。
|
|||
|
|
* @returns {{ command: string, args: string[] }} 可执行命令与预置参数。
|
|||
|
|
*/
|
|||
|
|
function resolvePnpmRunner(env) {
|
|||
|
|
const npmExecPath = String(env.npm_execpath || '').trim();
|
|||
|
|
const npmExecName = basename(npmExecPath).toLowerCase();
|
|||
|
|
if (npmExecPath && npmExecName.includes('pnpm')) {
|
|||
|
|
return {
|
|||
|
|
command: process.execPath,
|
|||
|
|
args: [npmExecPath],
|
|||
|
|
};
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return {
|
|||
|
|
command: process.platform === 'win32' ? 'pnpm.cmd' : 'pnpm',
|
|||
|
|
args: [],
|
|||
|
|
};
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
async function runPnpm(args, env) {
|
|||
|
|
const runner = resolvePnpmRunner(env);
|
|||
|
|
await runCommand(runner.command, [...runner.args, ...args], env);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
await runCommand(process.execPath, ['./scripts/BuildMonacoDts.mjs'], process.env);
|
|||
|
|
await runPnpm(['vite', '--mode', mode, '--force'], process.env);
|