Files
webshell/src/program/Rm.ts
2026-05-19 23:19:36 +02:00

40 lines
1.1 KiB
TypeScript

import { Item } from '../fs/Item'
import type { SimpleStream } from '../utils/SimpleStream'
import { Program } from './Program'
export class Rm extends Program {
constructor() {
super()
}
async Exec(_: SimpleStream<string>, stdout: SimpleStream<string>, workdir: Item, args: string[]): Promise<number> {
if (!args[1]) {
stdout.emit("rm: error: missing first argument\n")
return 1
}
let item: Item
try {
item = await Item.openDir(args[1].startsWith('/')
? args[1]
: `${workdir.GetPath()}${workdir.GetPath().endsWith('/') ? '' : '/'}${args[1]}`)
} catch {
item = await Item.open(args[1].startsWith('/')
? args[1]
: `${workdir.GetPath()}${workdir.GetPath().endsWith('/') ? '' : '/'}${args[1]}`)
}
if (!(await item.Exists())) {
stdout.emit(`rm: error: item ${item.GetPath()} doesn't exist.\n`)
return 1
}
stdout.emit(`-> removing: '${item.GetPath()}'\n`)
await item.Delete()
return 0
}
}