New utils
This commit is contained in:
+33
-1
@@ -89,12 +89,13 @@ function bareOsEmitRaw(ctx, chunk) {
|
||||
|
||||
/**
|
||||
* rm — remove files or directories.
|
||||
* Flags: -r -R --recursive, -f --force, -- ; bundled e.g. -rf
|
||||
* Flags: -r -R --recursive, -f --force, -d --dir, -- ; bundled e.g. -rf
|
||||
*/
|
||||
async function run(ctx, argv) {
|
||||
const vfs = ctx.vfs
|
||||
let recursive = false
|
||||
let force = false
|
||||
let dirEmptyOnly = false
|
||||
const files = []
|
||||
let dash = false
|
||||
for (let i = 1; i < argv.length; i++) {
|
||||
@@ -115,11 +116,16 @@ async function run(ctx, argv) {
|
||||
force = true
|
||||
continue
|
||||
}
|
||||
if (a === '--dir' || a === '-d') {
|
||||
dirEmptyOnly = true
|
||||
continue
|
||||
}
|
||||
if (a.startsWith('-') && a.length > 1) {
|
||||
for (let j = 1; j < a.length; j++) {
|
||||
const c = a[j]
|
||||
if (c === 'r' || c === 'R') recursive = true
|
||||
else if (c === 'f') force = true
|
||||
else if (c === 'd') dirEmptyOnly = true
|
||||
}
|
||||
continue
|
||||
}
|
||||
@@ -130,6 +136,11 @@ async function run(ctx, argv) {
|
||||
ctx.exitCode = 1
|
||||
return
|
||||
}
|
||||
if (dirEmptyOnly && recursive) {
|
||||
ctx.console.error('rm: cannot combine -d and -r')
|
||||
ctx.exitCode = 1
|
||||
return
|
||||
}
|
||||
const doRm =
|
||||
vfs && typeof vfs.rm === 'function'
|
||||
? (p) => vfs.rm(p, { recursive, force })
|
||||
@@ -139,6 +150,27 @@ async function run(ctx, argv) {
|
||||
}
|
||||
for (const f of files) {
|
||||
try {
|
||||
if (dirEmptyOnly) {
|
||||
const st = await vfs.lstat(f)
|
||||
if (!st) {
|
||||
if (!force) {
|
||||
ctx.console.error('rm: ' + f + ': No such file')
|
||||
ctx.exitCode = 1
|
||||
}
|
||||
continue
|
||||
}
|
||||
if (st.type !== 'directory') {
|
||||
ctx.console.error('rm: cannot remove ' + f + ': Not a directory')
|
||||
ctx.exitCode = 1
|
||||
continue
|
||||
}
|
||||
if (typeof vfs.rmdir === 'function') await vfs.rmdir(f)
|
||||
else {
|
||||
ctx.console.error('rm: rmdir not supported by this VFS')
|
||||
ctx.exitCode = 1
|
||||
}
|
||||
continue
|
||||
}
|
||||
await doRm(f)
|
||||
} catch (e) {
|
||||
if (force) continue
|
||||
|
||||
Reference in New Issue
Block a user