|
1 | 1 | /** @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' |
9 | 4 |
|
10 | 5 | /**
|
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. |
12 | 7 | * First attempts to move items to trash for recoverability. If trash fails
|
13 | 8 | * (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(). |
16 | 11 | */
|
17 |
| -export async function safeRemove(paths, options) { |
| 12 | +export async function trash(paths, options) { |
18 | 13 | const pathArray = Array.isArray(paths) ? paths : [paths]
|
19 | 14 | if (pathArray.length === 0) {
|
20 | 15 | return
|
21 | 16 | }
|
22 | 17 |
|
23 | 18 | try {
|
24 |
| - await trash(pathArray) |
| 19 | + await trashPkg(pathArray) |
25 | 20 | } 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 | + }) |
48 | 27 | }
|
49 | 28 | }
|
0 commit comments