-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclean.js
More file actions
60 lines (51 loc) · 1.36 KB
/
clean.js
File metadata and controls
60 lines (51 loc) · 1.36 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
54
55
56
57
58
59
60
#!/usr/bin/env node
/**
* 跨平台清理脚本
* 自动检测并删除指定的目录
*/
import { rmSync } from 'fs'
import { join, dirname } from 'path'
import { fileURLToPath } from 'url'
const __dirname = dirname(fileURLToPath(import.meta.url))
// 要清理的目录列表
const dirsToClean = {
dist: join(__dirname, 'dist'),
distSsr: join(__dirname, 'dist-ssr'),
nodeModules: join(__dirname, 'node_modules'),
}
// 检查并删除目录
function cleanDirectory(dirPath, dirName) {
try {
rmSync(dirPath, { recursive: true, force: true })
console.log(`✅ Cleaned: ${dirName}`)
return true
} catch (error) {
console.log(`ℹ️ Skipped: ${dirName} (not found or not removable)`)
return false
}
}
// 主函数
function main() {
const args = process.argv.slice(2)
let cleanedCount = 0
console.log('🧹 Cleaning build artifacts...\n')
if (args.includes('--all')) {
// 清理所有
Object.entries(dirsToClean).forEach(([key, path]) => {
if (cleanDirectory(path, key)) {
cleanedCount++
}
})
} else {
// 默认清理 dist 目录
if (cleanDirectory(dirsToClean.dist, 'dist')) {
cleanedCount++
}
if (cleanDirectory(dirsToClean.distSsr, 'dist-ssr')) {
cleanedCount++
}
}
console.log(`\n✨ Clean complete! (${cleanedCount} directories removed)`)
}
// 运行主函数
main()