feat: 编译脚本添加-p参数的支持

This commit is contained in:
Pan Qiancheng 2026-01-01 21:01:45 +08:00
parent b9b904d230
commit e5b005b067
2 changed files with 46 additions and 2 deletions

View File

@ -6,7 +6,7 @@
},
"scripts": {
"make:domain": "ts-node scripts/make.ts",
"build": "node --stack-size=4096 ./scripts/build.js",
"build": "node --stack-size=4096 ./scripts/build.js -p ./tsconfig.json",
"prebuild": "npm run make:domain",
"test": "ts-node test/test.ts"
},

View File

@ -3,6 +3,26 @@ const path = require('path');
const fs = require('fs');
const { cwd } = require('process');
// 解析命令行参数
function parseArgs() {
const args = process.argv.slice(2);
let configPath = 'tsconfig.json';
for (let i = 0; i < args.length; i++) {
if (args[i] === '-p' || args[i] === '--project') {
if (i + 1 < args.length) {
configPath = args[i + 1];
break;
} else {
console.error('error: option \'-p, --project\' argument missing');
process.exit(1);
}
}
}
return configPath;
}
// ANSI 颜色代码
const colors = {
reset: '\x1b[0m',
@ -137,7 +157,31 @@ function compile(configPath) {
}
// 执行编译
const configPath = path.resolve(cwd(), 'tsconfig.json');
const configPathArg = parseArgs();
let configPath;
// 判断参数是目录还是文件
if (fs.existsSync(configPathArg)) {
const stat = fs.statSync(configPathArg);
if (stat.isDirectory()) {
// 如果是目录,拼接 tsconfig.json
configPath = path.resolve(configPathArg, 'tsconfig.json');
} else {
// 如果是文件,直接使用
configPath = path.resolve(configPathArg);
}
} else {
// 尝试作为相对路径解析
configPath = path.resolve(cwd(), configPathArg);
if (!fs.existsSync(configPath)) {
// 如果还是不存在,尝试添加 tsconfig.json
const dirPath = path.resolve(cwd(), configPathArg);
if (fs.existsSync(dirPath) && fs.statSync(dirPath).isDirectory()) {
configPath = path.join(dirPath, 'tsconfig.json');
}
}
}
if (!fs.existsSync(configPath)) {
console.error(`error TS5058: The specified path does not exist: '${configPath}'.`);
process.exit(1);