cleanApp.js
1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import fs from "node:fs";
import path from "node:path";
// Usage:
// node scripts/cleanApp.js vue <name> [--dir=apps/frontend/vue] [--subdir=playground]
// node scripts/cleanApp.js react <name> [--dir=apps/frontend/react] [--subdir=playground]
// node scripts/cleanApp.js path --path=apps/frontend/vue/my-app-vue
// node scripts/cleanApp.js <template> <name> [--dir=<relative base>] [--subdir=<sub path>]
const args = process.argv.slice(2);
const template = (args[0] || "vue").toLowerCase();
const flags = args.filter((a) => typeof a === "string" && a.startsWith("--"));
const positionals = args.filter((a) => typeof a === "string" && !a.startsWith("--")).slice(1); // drop template
const dirArg = flags.find((a) => a.startsWith("--dir="));
const subdirArg = flags.find((a) => a.startsWith("--subdir="));
const pathArg = flags.find((a) => a.startsWith("--path="));
const defaultBase = template === "react" ? "apps/frontend/react" : "apps/frontend/vue";
let target;
if (pathArg) {
const rel = pathArg.slice("--path=".length);
target = path.resolve(process.cwd(), rel);
} else {
const name = positionals[0];
if (!name) {
console.error("请提供要清理的项目名。例如: pnpm run clean:vue my-app-vue");
process.exit(1);
}
let base = dirArg ? dirArg.slice("--dir=".length) : defaultBase;
if (subdirArg) {
base = path.join(base, subdirArg.slice("--subdir=".length));
}
target = path.resolve(process.cwd(), base, name);
}
try {
if (!fs.existsSync(target)) {
console.log(`目标目录不存在,已跳过: ${target}`);
process.exit(0);
}
fs.rmSync(target, { recursive: true, force: true });
console.log(`已清理: ${target}`);
process.exit(0);
} catch (error) {
console.error(`清理失败: ${error?.message || error}`);
process.exit(1);
}