46 lines
1.6 KiB
TypeScript
46 lines
1.6 KiB
TypeScript
import { Item } from '../fs/Item'
|
|
import type { SimpleStream } from '../utils/SimpleStream'
|
|
import { Program } from './Program'
|
|
|
|
export class Mv extends Program {
|
|
constructor() {
|
|
super()
|
|
}
|
|
|
|
async Exec(_: SimpleStream<string>, stdout: SimpleStream<string>, workdir: Item, args: string[]): Promise<number> {
|
|
if (args.length < 3) {
|
|
stdout.emit("mv: error: missing the first and/or second path arguments\n")
|
|
return 1
|
|
}
|
|
|
|
let item1: Item
|
|
let item2: Item
|
|
|
|
try {
|
|
item1 = await Item.openDir(Item.NormalizePath(args[1].startsWith('/') ? args[1] : `${workdir.GetPath()}/${args[1]}`))
|
|
item2 = await Item.openDir(Item.NormalizePath(args[2].startsWith('/') ? args[2] : `${workdir.GetPath()}/${args[2]}`))
|
|
} catch {
|
|
item1 = await Item.open(Item.NormalizePath(args[1].startsWith('/') ? args[1] : `${workdir.GetPath()}/${args[1]}`))
|
|
item2 = await Item.open(Item.NormalizePath(args[2].startsWith('/') ? args[2] : `${workdir.GetPath()}/${args[2]}`))
|
|
}
|
|
|
|
if (await item1.Exists()) {
|
|
stdout.emit(`mv: error: source directory ${item1.GetPath()} does not exist.\n`)
|
|
return 2
|
|
}
|
|
|
|
if (await item2.Exists()) {
|
|
stdout.emit(`mv: error: destination directory ${item2.GetPath()} already exists.\n`)
|
|
return 2
|
|
}
|
|
|
|
await item2.Create()
|
|
await item1.Copy(item2)
|
|
await item1.Delete()
|
|
|
|
stdout.emit(`-> moved ${item1.GetPath()} -> ${item2.GetPath()}\n`)
|
|
|
|
return 0
|
|
}
|
|
}
|