Skip to content

Commit 39983ab

Browse files
committed
refactor: rename safeRemove function to trash
1 parent c5d7d2c commit 39983ab

File tree

1 file changed

+13
-34
lines changed

1 file changed

+13
-34
lines changed

scripts/utils/trash.mjs

Lines changed: 13 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,28 @@
11
/** @fileoverview Safe file removal utility with trash fallback. */
2-
import { promises as fs } from 'node:fs'
3-
4-
import { pEach } from '@socketsecurity/registry/lib/promises'
5-
import trash from 'trash'
6-
7-
// Max concurrent fs.rm operations when trash fails.
8-
const DEFAULT_CONCURRENCY = 10
2+
import { remove } from '@socketsecurity/registry/lib/fs'
3+
import trashPkg from 'trash'
94

105
/**
11-
* Remove files or directories safely using trash with fs.rm fallback.
6+
* Remove files or directories safely using trash with registry's remove() fallback.
127
* First attempts to move items to trash for recoverability. If trash fails
138
* (e.g., on CI systems or when trash binary is unavailable), falls back to
14-
* permanent deletion using fs.rm with error handling.
15-
* @throws {Error} Never throws; logs warnings for non-ENOENT errors via spinner if provided.
9+
* permanent deletion using registry's remove() method.
10+
* @throws {Error} Never throws on trash failure; falls back to remove().
1611
*/
17-
export async function safeRemove(paths, options) {
12+
export async function trash(paths, options) {
1813
const pathArray = Array.isArray(paths) ? paths : [paths]
1914
if (pathArray.length === 0) {
2015
return
2116
}
2217

2318
try {
24-
await trash(pathArray)
19+
await trashPkg(pathArray)
2520
} catch {
26-
// If trash fails, fallback to fs.rm.
27-
const {
28-
concurrency = DEFAULT_CONCURRENCY,
29-
spinner,
30-
...rmOptions
31-
} = { __proto__: null, ...options }
32-
const defaultRmOptions = { force: true, recursive: true, ...rmOptions }
33-
34-
await pEach(
35-
pathArray,
36-
async p => {
37-
try {
38-
await fs.rm(p, defaultRmOptions)
39-
} catch (rmError) {
40-
// Only warn about non-ENOENT errors if a spinner is provided.
41-
if (spinner && rmError.code !== 'ENOENT') {
42-
spinner.warn(`Failed to remove ${p}: ${rmError.message}`)
43-
}
44-
}
45-
},
46-
{ concurrency },
47-
)
21+
// If trash fails, fallback to registry's remove().
22+
await remove(pathArray, {
23+
force: true,
24+
recursive: true,
25+
...options,
26+
})
4827
}
4928
}

0 commit comments

Comments
 (0)