ide-plugins/scripts/run-workspace-task.js

127 lines
3.2 KiB
JavaScript

const { spawnSync } = require("child_process");
const fs = require("fs");
const path = require("path");
const repoRoot = path.resolve(__dirname, "..");
const packagesRoot = path.join(repoRoot, "packages");
const task = process.argv[2];
const orderedPackages = [
"dsp-core",
"dsp-language",
"dsp-mod-ai",
"dsp-mod-build",
"dsp-mod-debug",
"dsp-mod-project",
"dsp-suite"
];
function fail(message) {
console.error(message);
process.exit(1);
}
function resolveNpmCommand() {
if (process.platform === "win32") {
if (process.env.NVM_SYMLINK) {
const candidate = path.join(process.env.NVM_SYMLINK, "npm.cmd");
if (fs.existsSync(candidate)) {
return candidate;
}
}
if (process.env.npm_execpath && process.env.npm_execpath.toLowerCase().endsWith(".cmd")) {
return process.env.npm_execpath;
}
return "npm.cmd";
}
if (process.env.npm_execpath) {
return process.env.npm_execpath;
}
return "npm";
}
function runCommand(command, args, cwd) {
const result = spawnSync(command, args, {
cwd,
stdio: "inherit",
shell: false
});
if (typeof result.status === "number" && result.status !== 0) {
process.exit(result.status);
}
if (result.error) {
throw result.error;
}
}
function hasScript(packageName, scriptName) {
const packageJsonPath = path.join(packagesRoot, packageName, "package.json");
if (!fs.existsSync(packageJsonPath)) {
return false;
}
const manifest = JSON.parse(fs.readFileSync(packageJsonPath, "utf8"));
return Boolean(manifest?.scripts?.[scriptName]);
}
function runSuiteBuild() {
const suiteRoot = path.join(packagesRoot, "dsp-suite");
runCommand(process.execPath, [".\\scripts\\prepare-bundle.js"], suiteRoot);
const tscScriptPath = path.join(repoRoot, "node_modules", "typescript", "bin", "tsc");
runCommand(process.execPath, [tscScriptPath, "-p", ".\\tsconfig.json"], suiteRoot);
}
function runSuitePackage() {
const suiteRoot = path.join(packagesRoot, "dsp-suite");
const vsceScriptPath = path.join(repoRoot, "node_modules", "vsce", "vsce");
runCommand(process.execPath, [vsceScriptPath, "package", "--no-dependencies", "--allow-missing-repository"], suiteRoot);
}
function runSuiteClean() {
const distDir = path.join(packagesRoot, "dsp-suite", "dist");
if (fs.existsSync(distDir)) {
fs.rmSync(distDir, { recursive: true, force: true });
}
}
function runTaskForPackage(packageName, taskName, npmCommand) {
if (!hasScript(packageName, taskName)) {
return;
}
const packageRoot = path.join(packagesRoot, packageName);
console.log(`[ide-plugins] ${taskName} ${packageName}`);
if (packageName === "dsp-suite") {
if (taskName === "build") {
runSuiteBuild();
return;
}
if (taskName === "package") {
runSuitePackage();
return;
}
if (taskName === "clean") {
runSuiteClean();
return;
}
}
runCommand(npmCommand, ["run", taskName], packageRoot);
}
if (!task || !["build", "package", "clean"].includes(task)) {
fail("用法: node ./scripts/run-workspace-task.js <build|package|clean>");
}
const npmCommand = resolveNpmCommand();
for (const packageName of orderedPackages) {
runTaskForPackage(packageName, task, npmCommand);
}