@movk/core 1.2.1 → 1.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/clean.mjs +53 -46
- package/package.json +1 -1
package/bin/clean.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import { rm } from 'node:fs/promises'
|
|
3
|
-
import { relative, resolve } from 'node:path'
|
|
2
|
+
import { lstat, rm } from 'node:fs/promises'
|
|
3
|
+
import { basename, relative, resolve } from 'node:path'
|
|
4
4
|
import process from 'node:process'
|
|
5
5
|
import { glob } from 'tinyglobby'
|
|
6
6
|
|
|
@@ -11,68 +11,75 @@ const DEFAULT_TARGETS = [
|
|
|
11
11
|
'.output',
|
|
12
12
|
'.cache',
|
|
13
13
|
'dist',
|
|
14
|
-
'dist.zip'
|
|
14
|
+
'dist.zip',
|
|
15
15
|
]
|
|
16
|
-
const
|
|
17
|
-
|
|
18
|
-
async function removePath(path) {
|
|
19
|
-
try {
|
|
20
|
-
await rm(path, { recursive: true, force: true, maxRetries: 3 })
|
|
21
|
-
return { path, success: true }
|
|
22
|
-
}
|
|
23
|
-
catch (e) {
|
|
24
|
-
if (e.code === 'ENOENT')
|
|
25
|
-
return { path, success: true }
|
|
26
|
-
return { path, success: false, error: e.message }
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
async function processBatch(paths) {
|
|
31
|
-
const results = []
|
|
32
|
-
for (let i = 0; i < paths.length; i += BATCH_SIZE) {
|
|
33
|
-
const batch = paths.slice(i, i + BATCH_SIZE)
|
|
34
|
-
results.push(...await Promise.all(batch.map(removePath)))
|
|
35
|
-
}
|
|
36
|
-
return results
|
|
37
|
-
}
|
|
16
|
+
const RE_PATH_SEP = /[/\\]/
|
|
17
|
+
const RE_TRAILING_SLASH = /\/+$/
|
|
38
18
|
|
|
39
19
|
async function clean() {
|
|
40
20
|
const start = Date.now()
|
|
41
21
|
const args = process.argv.slice(2)
|
|
42
|
-
const targets = args.length > 0 ? args : DEFAULT_TARGETS
|
|
22
|
+
const targets = (args.length > 0 ? args : DEFAULT_TARGETS)
|
|
23
|
+
.filter(t => t && !RE_PATH_SEP.test(t) && t !== '.' && t !== '..')
|
|
43
24
|
const root = resolve(process.cwd())
|
|
25
|
+
const targetSet = new Set(targets)
|
|
26
|
+
|
|
27
|
+
if (targets.length === 0) {
|
|
28
|
+
console.log('没有有效的清理目标')
|
|
29
|
+
return
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const rawPaths = await glob(targets.map(t => `**/${t}`), {
|
|
33
|
+
cwd: root,
|
|
34
|
+
onlyFiles: false,
|
|
35
|
+
dot: true,
|
|
36
|
+
absolute: true,
|
|
37
|
+
followSymbolicLinks: false,
|
|
38
|
+
ignore: ['**/.git/**'],
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
const matched = Array.from(new Set(rawPaths), p => p.replace(RE_TRAILING_SLASH, ''))
|
|
42
|
+
.filter(p => p !== root && p.startsWith(`${root}/`) && targetSet.has(basename(p)))
|
|
43
|
+
.sort((a, b) => a.length - b.length)
|
|
44
44
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
onlyFiles: false,
|
|
50
|
-
dot: true,
|
|
51
|
-
absolute: true,
|
|
52
|
-
ignore: ['**/node_modules/**/node_modules/**'],
|
|
53
|
-
})
|
|
45
|
+
const deduped = []
|
|
46
|
+
for (const p of matched) {
|
|
47
|
+
if (!deduped.some(parent => p.startsWith(`${parent}/`)))
|
|
48
|
+
deduped.push(p)
|
|
54
49
|
}
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
50
|
+
|
|
51
|
+
const paths = []
|
|
52
|
+
for (const p of deduped) {
|
|
53
|
+
try {
|
|
54
|
+
if ((await lstat(p)).isSymbolicLink())
|
|
55
|
+
continue
|
|
56
|
+
}
|
|
57
|
+
catch { continue }
|
|
58
|
+
paths.push(p)
|
|
58
59
|
}
|
|
59
60
|
|
|
60
|
-
if (
|
|
61
|
+
if (paths.length === 0) {
|
|
61
62
|
console.log('未找到需要清理的目标')
|
|
62
63
|
return
|
|
63
64
|
}
|
|
64
65
|
|
|
65
|
-
paths
|
|
66
|
+
paths.forEach(p => console.log(` ${relative(root, p)}`))
|
|
66
67
|
|
|
67
|
-
const results = await
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
68
|
+
const results = await Promise.all(paths.map(async (p) => {
|
|
69
|
+
try {
|
|
70
|
+
await rm(p, { recursive: true, force: true, maxRetries: 3 })
|
|
71
|
+
return null
|
|
72
|
+
}
|
|
73
|
+
catch (e) {
|
|
74
|
+
return e.code === 'ENOENT' ? null : { path: p, error: e.message }
|
|
75
|
+
}
|
|
76
|
+
}))
|
|
71
77
|
|
|
72
|
-
|
|
78
|
+
const failed = results.filter(Boolean)
|
|
79
|
+
const duration = ((Date.now() - start) / 1000).toFixed(2)
|
|
80
|
+
console.log(`已清理 ${paths.length - failed.length}/${paths.length} 项,耗时 ${duration}s`)
|
|
73
81
|
|
|
74
82
|
if (failed.length) {
|
|
75
|
-
console.warn(`\n${failed.length} 项清理失败:`)
|
|
76
83
|
failed.forEach(f => console.warn(` ${relative(root, f.path)}: ${f.error}`))
|
|
77
84
|
process.exit(1)
|
|
78
85
|
}
|