Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support change out dir #7

Merged
merged 2 commits into from
Jan 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 18 additions & 16 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,31 @@ function run() {
const [runCmd, tscArgs, runArgs, isCleanDirectory] = parseArgs(process.argv);
const cwd = process.cwd();

let outDir;
const projectIndex = tscArgs.findIndex(arg => arg === '--project');
if (projectIndex !== -1) {
const projectPath = tscArgs[projectIndex + 1];
const tsconfig = readJSONFile(path.resolve(cwd, projectPath));
outDir = tsconfig.compilerOptions.outDir;
} else {
const outDirIndex = tscArgs.findIndex(arg => arg === '--outDir');
if (outDirIndex !== -1) {
outDir = tscArgs[outDirIndex + 1];
} else {
const tsconfig = readJSONFile(path.resolve(cwd, 'tsconfig.json'));
outDir = tsconfig.compilerOptions.outDir;
}
}

runArgs.push('--baseDir', path.resolve(cwd, outDir));

if (isCleanDirectory) {
/**
* 删除编译目录,有几种情况
* 1、tscArgs 中有 --outDir 参数,直接删除
* 2、tscArgs 中没有 --outDir 参数,从 tsconfig.json 中读取 outDir 参数
* 3、如果 tscArgs 中包含 --project 参数,那么就以 --project 参数指定的 tsconfig.json 为准
*/
let outDir;
const projectIndex = tscArgs.findIndex(arg => arg === '--project');
if (projectIndex !== -1) {
const projectPath = tscArgs[projectIndex + 1];
const tsconfig = readJSONFile(path.resolve(cwd, projectPath));
outDir = tsconfig.compilerOptions.outDir;
} else {
const outDirIndex = tscArgs.findIndex(arg => arg === '--outDir');
if (outDirIndex !== -1) {
outDir = tscArgs[outDirIndex + 1];
} else {
const tsconfig = readJSONFile(path.resolve(cwd, 'tsconfig.json'));
outDir = tsconfig.compilerOptions.outDir;
}
}

deleteFolderRecursive(path.resolve(cwd, outDir));
}

Expand Down