chore: fix bugs & qol

This commit is contained in:
binekrasik
2026-05-21 23:42:14 +02:00
parent 255cc6a858
commit 646a9f6b7a
7 changed files with 383 additions and 13 deletions

View File

@@ -16,30 +16,46 @@ export class Mv extends Program {
let item1: Item
let item2: Item
let destIsDir = false
// figure out if the items are files or directories
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]}`))
}
try {
item2 = await Item.open(Item.NormalizePath(args[2].startsWith('/') ? args[2] : `${workdir.GetPath()}/${args[2]}`))
} catch {
item2 = await Item.openDir(Item.NormalizePath(args[2].startsWith('/') ? args[2] : `${workdir.GetPath()}/${args[2]}`))
destIsDir = true
}
if (await item1.Exists()) {
stdout.emit(`mv: error: source directory ${item1.GetPath()} does not exist.\n`)
if (!await item1.Exists()) {
stdout.emit(`mv: error: source item ${item1.GetPath()} does not exist.\n`)
return 2
}
if (await item2.Exists()) {
stdout.emit(`mv: error: destination directory ${item2.GetPath()} already exists.\n`)
if (await item2.Exists() && !destIsDir) {
stdout.emit(`mv: error: destination item ${item2.GetPath()} already exists.\n`)
return 2
}
await item2.Create()
await item1.Copy(item2)
// either move the file into a destination directory or move it to a new path
if (destIsDir) {
const destChild = await Item.open(Item.NormalizePath(`${item2.GetPath()}/${item1.GetName()}`))
await item1.Copy(destChild)
stdout.emit(`-> moved ${item1.GetPath()} -> ${destChild.GetPath()}\n`)
} else {
await item2.Create()
await item1.Copy(item2)
stdout.emit(`-> moved ${item1.GetPath()} -> ${item2.GetPath()}\n`)
}
await item1.Delete()
stdout.emit(`-> moved ${item1.GetPath()} -> ${item2.GetPath()}\n`)
return 0
}
}