|
| 1 | +import type { CmdContext, CmdResult, Command, CommandRegistry } from '../types' |
| 2 | +import { collect, emptyIter, lines, stringIter } from '../types' |
| 3 | + |
| 4 | +function ok(stdout: AsyncIterable<string>, exitCode = 0): CmdResult { |
| 5 | + return { stdout, exitCode } |
| 6 | +} |
| 7 | + |
| 8 | +function err(message: string, exitCode = 2): CmdResult { |
| 9 | + return { stdout: emptyIter(), exitCode, stderr: message } |
| 10 | +} |
| 11 | + |
| 12 | +const echo: Command = async (ctx) => { |
| 13 | + const args = ctx.argv.slice(1) |
| 14 | + let newline = true |
| 15 | + if (args[0] === '-n') { |
| 16 | + newline = false |
| 17 | + args.shift() |
| 18 | + } |
| 19 | + const text = args.join(' ') + (newline ? '\n' : '') |
| 20 | + return ok(stringIter(text)) |
| 21 | +} |
| 22 | + |
| 23 | +const cat: Command = async (ctx) => { |
| 24 | + const paths = ctx.argv.slice(1) |
| 25 | + if (paths.length === 0) return ok(ctx.stdin) |
| 26 | + async function* gen(): AsyncIterable<string> { |
| 27 | + for (const p of paths) { |
| 28 | + yield await ctx.vfs.read(p) |
| 29 | + } |
| 30 | + } |
| 31 | + return ok(gen()) |
| 32 | +} |
| 33 | + |
| 34 | +const ls: Command = async (ctx) => { |
| 35 | + const path = ctx.argv[1] ?? ctx.cwd |
| 36 | + const entries = await ctx.vfs.list(path) |
| 37 | + const out = |
| 38 | + entries.map((e) => (e.type === 'dir' ? e.name + '/' : e.name)).join('\n') + |
| 39 | + (entries.length > 0 ? '\n' : '') |
| 40 | + return ok(stringIter(out)) |
| 41 | +} |
| 42 | + |
| 43 | +const pwd: Command = async (ctx) => ok(stringIter(ctx.cwd + '\n')) |
| 44 | + |
| 45 | +const wc: Command = async (ctx) => { |
| 46 | + const data = await collect(ctx.stdin) |
| 47 | + const bytes = data.length |
| 48 | + const lineCount = |
| 49 | + data === '' ? 0 : data.split('\n').length - (data.endsWith('\n') ? 1 : 0) |
| 50 | + const words = data.split(/\s+/).filter((w) => w.length > 0).length |
| 51 | + return ok(stringIter(`${lineCount} ${words} ${bytes}\n`)) |
| 52 | +} |
| 53 | + |
| 54 | +function parseNFlag( |
| 55 | + argv: string[], |
| 56 | + defaultN: number |
| 57 | +): { n: number; rest: string[] } { |
| 58 | + const rest = argv.slice(1) |
| 59 | + let n = defaultN |
| 60 | + if (rest[0] === '-n') { |
| 61 | + const parsed = Number(rest[1]) |
| 62 | + if (!Number.isFinite(parsed)) throw new Error('invalid -n value') |
| 63 | + n = parsed |
| 64 | + rest.splice(0, 2) |
| 65 | + } else if (rest[0]?.startsWith('-n')) { |
| 66 | + const parsed = Number(rest[0].slice(2)) |
| 67 | + if (!Number.isFinite(parsed)) throw new Error('invalid -n value') |
| 68 | + n = parsed |
| 69 | + rest.shift() |
| 70 | + } |
| 71 | + return { n, rest } |
| 72 | +} |
| 73 | + |
| 74 | +const head: Command = async (ctx) => { |
| 75 | + let n: number |
| 76 | + try { |
| 77 | + ;({ n } = parseNFlag(ctx.argv, 10)) |
| 78 | + } catch (e) { |
| 79 | + return err('usage: head [-n N]') |
| 80 | + } |
| 81 | + async function* gen(): AsyncIterable<string> { |
| 82 | + let i = 0 |
| 83 | + for await (const line of lines(ctx.stdin)) { |
| 84 | + if (i >= n) break |
| 85 | + yield line + '\n' |
| 86 | + i++ |
| 87 | + } |
| 88 | + } |
| 89 | + return ok(gen()) |
| 90 | +} |
| 91 | + |
| 92 | +const tail: Command = async (ctx) => { |
| 93 | + let n: number |
| 94 | + try { |
| 95 | + ;({ n } = parseNFlag(ctx.argv, 10)) |
| 96 | + } catch (e) { |
| 97 | + return err('usage: tail [-n N]') |
| 98 | + } |
| 99 | + const buf: string[] = [] |
| 100 | + for await (const line of lines(ctx.stdin)) { |
| 101 | + buf.push(line) |
| 102 | + if (buf.length > n) buf.shift() |
| 103 | + } |
| 104 | + const out = buf.length > 0 ? buf.join('\n') + '\n' : '' |
| 105 | + return ok(stringIter(out)) |
| 106 | +} |
| 107 | + |
| 108 | +const grep: Command = async (ctx) => { |
| 109 | + const pattern = ctx.argv[1] |
| 110 | + if (!pattern) return err('usage: grep <pattern>') |
| 111 | + const re = new RegExp(pattern) |
| 112 | + async function* gen(): AsyncIterable<string> { |
| 113 | + let matched = false |
| 114 | + for await (const line of lines(ctx.stdin)) { |
| 115 | + if (re.test(line)) { |
| 116 | + yield line + '\n' |
| 117 | + matched = true |
| 118 | + } |
| 119 | + } |
| 120 | + void matched |
| 121 | + } |
| 122 | + return ok(gen()) |
| 123 | +} |
| 124 | + |
| 125 | +const trueCmd: Command = async () => ok(emptyIter(), 0) |
| 126 | +const falseCmd: Command = async () => ok(emptyIter(), 1) |
| 127 | + |
| 128 | +export function registerCoreutils(registry: CommandRegistry): void { |
| 129 | + registry.register('echo', echo) |
| 130 | + registry.register('cat', cat) |
| 131 | + registry.register('ls', ls) |
| 132 | + registry.register('pwd', pwd) |
| 133 | + registry.register('wc', wc) |
| 134 | + registry.register('head', head) |
| 135 | + registry.register('tail', tail) |
| 136 | + registry.register('grep', grep) |
| 137 | + registry.register('true', trueCmd) |
| 138 | + registry.register('false', falseCmd) |
| 139 | +} |
| 140 | + |
| 141 | +export const coreutils = { |
| 142 | + echo, |
| 143 | + cat, |
| 144 | + ls, |
| 145 | + pwd, |
| 146 | + wc, |
| 147 | + head, |
| 148 | + tail, |
| 149 | + grep, |
| 150 | + true: trueCmd, |
| 151 | + false: falseCmd |
| 152 | +} satisfies Record<string, (ctx: CmdContext) => Promise<CmdResult>> |
0 commit comments