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

59
src/program/Cp.ts Normal file
View File

@@ -0,0 +1,59 @@
import { Item } from '../fs/Item'
import type { SimpleStream } from '../utils/SimpleStream'
import { Program } from './Program'
export class Cp extends Program {
constructor() {
super()
}
async Exec(_: SimpleStream<string>, stdout: SimpleStream<string>, workdir: Item, args: string[]): Promise<number> {
if (args.length < 3) {
stdout.emit("cp: error: missing the first and/or second path arguments\n")
return 1
}
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]}`))
} 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(`cp: error: source item ${item1.GetPath()} does not exist.\n`)
return 2
}
if (await item2.Exists() && !destIsDir) {
stdout.emit(`cp: error: destination item ${item2.GetPath()} already exists.\n`)
return 2
}
// either copy the item into a destination directory or create a new copy
if (destIsDir) {
const destChild = await Item.open(Item.NormalizePath(`${item2.GetPath()}/${item1.GetName()}`))
await item1.Copy(destChild)
stdout.emit(`-> copied ${item1.GetPath()} -> ${destChild.GetPath()}\n`)
} else {
await item2.Create()
await item1.Copy(item2)
stdout.emit(`-> copied ${item1.GetPath()} -> ${item2.GetPath()}\n`)
}
return 0
}
}