feat: working filesystem
This commit is contained in:
28
src/program/Cat.ts
Normal file
28
src/program/Cat.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import { Item } from '../fs/Item'
|
||||
import type { SimpleStream } from '../utils/SimpleStream'
|
||||
import { Program } from './Program'
|
||||
|
||||
export class Cat extends Program {
|
||||
constructor() {
|
||||
super()
|
||||
}
|
||||
|
||||
async Exec(_: SimpleStream<string>, stdout: SimpleStream<string>, __: Item, args: string[]): Promise<number> {
|
||||
let item: Item = await Item.open(args[1])
|
||||
|
||||
if (!(await item.Exists())) {
|
||||
stdout.emit(`cat: error: item ${item.GetPath()} doesn't exist.\n`)
|
||||
return 1
|
||||
}
|
||||
|
||||
if (item.IsDirectory()) {
|
||||
stdout.emit(`cat: error: can't read data from a directory; item ${item.GetPath()} is a directory.\n`)
|
||||
return 2
|
||||
}
|
||||
|
||||
stdout.emit(`${item.ReadData()}`)
|
||||
stdout.emit('[ EOF ]\n')
|
||||
|
||||
return 0
|
||||
}
|
||||
}
|
||||
27
src/program/Echo.ts
Normal file
27
src/program/Echo.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import { Item } from '../fs/Item'
|
||||
import type { SimpleStream } from '../utils/SimpleStream'
|
||||
import { Program } from './Program'
|
||||
|
||||
export class Echo extends Program {
|
||||
constructor() {
|
||||
super()
|
||||
}
|
||||
|
||||
async Exec(_: SimpleStream<string>, stdout: SimpleStream<string>, ___: Item, args: string[]): Promise<number> {
|
||||
let item: Item = await Item.open(args[1])
|
||||
|
||||
if (!(await item.Exists())) {
|
||||
stdout.emit(`echo: error: item ${item.GetPath()} doesn't exist.\n`)
|
||||
return 1
|
||||
}
|
||||
|
||||
if (item.IsDirectory()) {
|
||||
stdout.emit(`echo: error: can't write data to a directory; item ${item.GetPath()} is a directory.\n`)
|
||||
return 2
|
||||
}
|
||||
|
||||
await item.Write(args.slice(2).join(' '))
|
||||
|
||||
return 0
|
||||
}
|
||||
}
|
||||
@@ -8,15 +8,24 @@ export class Ls extends Program {
|
||||
}
|
||||
|
||||
async Exec(_: SimpleStream<string>, stdout: SimpleStream<string>, workdir: Item, args: string[]): Promise<number> {
|
||||
if (!(new Item(args[1]).IsDirectory())) {
|
||||
stdout.emit("ls: error: the provided path is not a directory")
|
||||
let item: Item = args[1] ? await Item.openDir(args[1]) : workdir
|
||||
if (args[1] && !item.IsDirectory()) {
|
||||
stdout.emit("ls: error: the provided path is not a directory\n")
|
||||
return 1
|
||||
}
|
||||
|
||||
stdout.emit(`item: '${workdir.GetPath()}'\n`)
|
||||
stdout.emit(`index attr name\n`)
|
||||
workdir.List().forEach((entry, i) => {
|
||||
stdout.emit(`${i.toString().padEnd(8, ' ')} ${''.padEnd(4, '-').padEnd(8, ' ')} '${entry.GetName()}'\n`)
|
||||
if (!(await item.Exists())) {
|
||||
stdout.emit(`ls: error: path ${item.GetPath()} doesn't exist\n`)
|
||||
return 2
|
||||
}
|
||||
|
||||
console.log(item)
|
||||
|
||||
stdout.emit(`-> Listing contents of item: '${item.GetPath()}'\n`)
|
||||
stdout.emit(` [ attr name ]\n`)
|
||||
const items = await item.List()
|
||||
items.forEach((entry, i) => {
|
||||
stdout.emit(` | ${''.padEnd(4, '-').padEnd(8, ' ')} ${entry.GetName()}\n`)
|
||||
})
|
||||
|
||||
return 0
|
||||
|
||||
@@ -12,8 +12,12 @@ export class Lsprg extends Program {
|
||||
}
|
||||
|
||||
async Exec(_: SimpleStream<string>, stdout: SimpleStream<string>, __: Item, ___: string[]): Promise<number> {
|
||||
const programs = this.wush.GetPrograms()
|
||||
|
||||
stdout.emit(`-> ${Object.keys(programs).length} loaded programs:\n`)
|
||||
|
||||
for (const program in this.wush.GetPrograms())
|
||||
stdout.emit(`${program}\n`)
|
||||
stdout.emit(` | - ${program}\n`)
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
23
src/program/Mkdir.ts
Normal file
23
src/program/Mkdir.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { Item } from '../fs/Item'
|
||||
import type { SimpleStream } from '../utils/SimpleStream'
|
||||
import { Program } from './Program'
|
||||
|
||||
export class Mkdir extends Program {
|
||||
constructor() {
|
||||
super()
|
||||
}
|
||||
|
||||
async Exec(_: SimpleStream<string>, stdout: SimpleStream<string>, workdir: Item, args: string[]): Promise<number> {
|
||||
const item = await Item.openDir(args[1])
|
||||
stdout.emit(`does ${args[1]} exist? ${await item.Exists()}\n`)
|
||||
|
||||
if (await item.Exists()) {
|
||||
stdout.emit(`mkdir: directory ${item.GetPath()} already exists.\n`)
|
||||
return 1
|
||||
}
|
||||
|
||||
item.Create()
|
||||
|
||||
return 0
|
||||
}
|
||||
}
|
||||
44
src/program/ResetIndexedDb.ts
Normal file
44
src/program/ResetIndexedDb.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import { GetWebfsDatabase } from '../app'
|
||||
import { Item } from '../fs/Item'
|
||||
import type { SimpleStream } from '../utils/SimpleStream'
|
||||
import { Program } from './Program'
|
||||
|
||||
export class ResetIndexedDb extends Program {
|
||||
constructor() {
|
||||
super()
|
||||
}
|
||||
|
||||
async Exec(_: SimpleStream<string>, stdout: SimpleStream<string>, ___: Item, ____: string[]): Promise<number> {
|
||||
const db = GetWebfsDatabase()
|
||||
|
||||
if (!db) {
|
||||
stdout.emit("rsindb: error: GetWebfsDatabase returned null")
|
||||
return 1
|
||||
}
|
||||
|
||||
return new Promise<number>(resolve => {
|
||||
const request = indexedDB.deleteDatabase(db.name)
|
||||
|
||||
request.onerror = () => {
|
||||
stdout.emit("rsindb: error: IndexedDB deletion request has failed\n")
|
||||
resolve(2)
|
||||
}
|
||||
|
||||
request.onblocked = () => {
|
||||
stdout.emit("rsindb: debug: database open, closing connection\n")
|
||||
db.close()
|
||||
}
|
||||
|
||||
request.onupgradeneeded = () =>
|
||||
stdout.emit("rsindb: debug: request upgrade needed\n")
|
||||
|
||||
request.onsuccess = () => {
|
||||
stdout.emit("success\n")
|
||||
|
||||
resolve(0)
|
||||
|
||||
location.reload()
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
15
src/program/Rl.ts
Normal file
15
src/program/Rl.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import { Item } from '../fs/Item'
|
||||
import type { SimpleStream } from '../utils/SimpleStream'
|
||||
import { Program } from './Program'
|
||||
|
||||
export class Rl extends Program {
|
||||
constructor() {
|
||||
super()
|
||||
}
|
||||
|
||||
async Exec(_: SimpleStream<string>, __: SimpleStream<string>, ___: Item, ____: string[]): Promise<number> {
|
||||
location.reload()
|
||||
|
||||
return 0
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
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])
|
||||
} catch {
|
||||
item = await Item.open(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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,15 +10,15 @@ export class Touch extends Program {
|
||||
async Exec(_: SimpleStream<string>, stdout: SimpleStream<string>, workdir: Item, args: string[]): Promise<number> {
|
||||
stdout.emit(`touching children, please wait...\n`)
|
||||
|
||||
const file = new Item(args[1])
|
||||
stdout.emit(`does ${args[1]} exist? ${file.Exists()}\n`)
|
||||
const item = await Item.open(args[1])
|
||||
stdout.emit(`does ${args[1]} exist? ${await item.Exists()}\n`)
|
||||
|
||||
if (file.Exists()) {
|
||||
if (await item.Exists()) {
|
||||
stdout.emit("touch: the file already exists.\n")
|
||||
return 1
|
||||
}
|
||||
|
||||
file.Create()
|
||||
item.Create()
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user