@ecmaos/coreutils 0.1.5 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +1 -1
- package/CHANGELOG.md +45 -0
- package/dist/commands/basename.d.ts +4 -0
- package/dist/commands/basename.d.ts.map +1 -0
- package/dist/commands/basename.js +78 -0
- package/dist/commands/basename.js.map +1 -0
- package/dist/commands/cal.d.ts +4 -0
- package/dist/commands/cal.d.ts.map +1 -0
- package/dist/commands/cal.js +105 -0
- package/dist/commands/cal.js.map +1 -0
- package/dist/commands/cat.d.ts.map +1 -1
- package/dist/commands/cat.js +33 -25
- package/dist/commands/cat.js.map +1 -1
- package/dist/commands/cd.d.ts.map +1 -1
- package/dist/commands/cd.js +38 -10
- package/dist/commands/cd.js.map +1 -1
- package/dist/commands/chmod.d.ts.map +1 -1
- package/dist/commands/chmod.js +31 -9
- package/dist/commands/chmod.js.map +1 -1
- package/dist/commands/comm.d.ts +4 -0
- package/dist/commands/comm.d.ts.map +1 -0
- package/dist/commands/comm.js +162 -0
- package/dist/commands/comm.js.map +1 -0
- package/dist/commands/cp.d.ts.map +1 -1
- package/dist/commands/cp.js +61 -12
- package/dist/commands/cp.js.map +1 -1
- package/dist/commands/cut.d.ts +4 -0
- package/dist/commands/cut.d.ts.map +1 -0
- package/dist/commands/cut.js +208 -0
- package/dist/commands/cut.js.map +1 -0
- package/dist/commands/date.d.ts +4 -0
- package/dist/commands/date.d.ts.map +1 -0
- package/dist/commands/date.js +100 -0
- package/dist/commands/date.js.map +1 -0
- package/dist/commands/diff.d.ts +4 -0
- package/dist/commands/diff.d.ts.map +1 -0
- package/dist/commands/diff.js +194 -0
- package/dist/commands/diff.js.map +1 -0
- package/dist/commands/dirname.d.ts +4 -0
- package/dist/commands/dirname.d.ts.map +1 -0
- package/dist/commands/dirname.js +50 -0
- package/dist/commands/dirname.js.map +1 -0
- package/dist/commands/echo.d.ts.map +1 -1
- package/dist/commands/echo.js +49 -10
- package/dist/commands/echo.js.map +1 -1
- package/dist/commands/false.d.ts +4 -0
- package/dist/commands/false.d.ts.map +1 -0
- package/dist/commands/false.js +27 -0
- package/dist/commands/false.js.map +1 -0
- package/dist/commands/find.d.ts +4 -0
- package/dist/commands/find.d.ts.map +1 -0
- package/dist/commands/find.js +181 -0
- package/dist/commands/find.js.map +1 -0
- package/dist/commands/grep.d.ts.map +1 -1
- package/dist/commands/grep.js +45 -10
- package/dist/commands/grep.js.map +1 -1
- package/dist/commands/head.d.ts.map +1 -1
- package/dist/commands/head.js +46 -9
- package/dist/commands/head.js.map +1 -1
- package/dist/commands/hex.d.ts.map +1 -1
- package/dist/commands/hex.js +16 -6
- package/dist/commands/hex.js.map +1 -1
- package/dist/commands/id.d.ts +4 -0
- package/dist/commands/id.d.ts.map +1 -0
- package/dist/commands/id.js +97 -0
- package/dist/commands/id.js.map +1 -0
- package/dist/commands/join.d.ts +4 -0
- package/dist/commands/join.d.ts.map +1 -0
- package/dist/commands/join.js +152 -0
- package/dist/commands/join.js.map +1 -0
- package/dist/commands/less.d.ts.map +1 -1
- package/dist/commands/less.js +16 -7
- package/dist/commands/less.js.map +1 -1
- package/dist/commands/ln.d.ts.map +1 -1
- package/dist/commands/ln.js +54 -12
- package/dist/commands/ln.js.map +1 -1
- package/dist/commands/ls.d.ts.map +1 -1
- package/dist/commands/ls.js +96 -91
- package/dist/commands/ls.js.map +1 -1
- package/dist/commands/mkdir.d.ts.map +1 -1
- package/dist/commands/mkdir.js +34 -9
- package/dist/commands/mkdir.js.map +1 -1
- package/dist/commands/mv.d.ts.map +1 -1
- package/dist/commands/mv.js +25 -7
- package/dist/commands/mv.js.map +1 -1
- package/dist/commands/nc.d.ts +4 -0
- package/dist/commands/nc.d.ts.map +1 -0
- package/dist/commands/nc.js +451 -0
- package/dist/commands/nc.js.map +1 -0
- package/dist/commands/nl.d.ts +4 -0
- package/dist/commands/nl.d.ts.map +1 -0
- package/dist/commands/nl.js +208 -0
- package/dist/commands/nl.js.map +1 -0
- package/dist/commands/passkey.d.ts.map +1 -1
- package/dist/commands/passkey.js +44 -18
- package/dist/commands/passkey.js.map +1 -1
- package/dist/commands/paste.d.ts +4 -0
- package/dist/commands/paste.d.ts.map +1 -0
- package/dist/commands/paste.js +161 -0
- package/dist/commands/paste.js.map +1 -0
- package/dist/commands/pwd.d.ts.map +1 -1
- package/dist/commands/pwd.js +13 -4
- package/dist/commands/pwd.js.map +1 -1
- package/dist/commands/rm.d.ts.map +1 -1
- package/dist/commands/rm.js +105 -12
- package/dist/commands/rm.js.map +1 -1
- package/dist/commands/rmdir.d.ts.map +1 -1
- package/dist/commands/rmdir.js +34 -9
- package/dist/commands/rmdir.js.map +1 -1
- package/dist/commands/sed.d.ts.map +1 -1
- package/dist/commands/sed.js +73 -28
- package/dist/commands/sed.js.map +1 -1
- package/dist/commands/seq.d.ts +4 -0
- package/dist/commands/seq.d.ts.map +1 -0
- package/dist/commands/seq.js +148 -0
- package/dist/commands/seq.js.map +1 -0
- package/dist/commands/sockets.d.ts +4 -0
- package/dist/commands/sockets.d.ts.map +1 -0
- package/dist/commands/sockets.js +239 -0
- package/dist/commands/sockets.js.map +1 -0
- package/dist/commands/sort.d.ts +4 -0
- package/dist/commands/sort.d.ts.map +1 -0
- package/dist/commands/sort.js +175 -0
- package/dist/commands/sort.js.map +1 -0
- package/dist/commands/split.d.ts +4 -0
- package/dist/commands/split.d.ts.map +1 -0
- package/dist/commands/split.js +147 -0
- package/dist/commands/split.js.map +1 -0
- package/dist/commands/stat.d.ts.map +1 -1
- package/dist/commands/stat.js +14 -6
- package/dist/commands/stat.js.map +1 -1
- package/dist/commands/tail.d.ts.map +1 -1
- package/dist/commands/tail.js +46 -9
- package/dist/commands/tail.js.map +1 -1
- package/dist/commands/tee.d.ts.map +1 -1
- package/dist/commands/tee.js +45 -10
- package/dist/commands/tee.js.map +1 -1
- package/dist/commands/test.d.ts +4 -0
- package/dist/commands/test.d.ts.map +1 -0
- package/dist/commands/test.js +99 -0
- package/dist/commands/test.js.map +1 -0
- package/dist/commands/touch.d.ts.map +1 -1
- package/dist/commands/touch.js +34 -9
- package/dist/commands/touch.js.map +1 -1
- package/dist/commands/tr.d.ts +4 -0
- package/dist/commands/tr.d.ts.map +1 -0
- package/dist/commands/tr.js +162 -0
- package/dist/commands/tr.js.map +1 -0
- package/dist/commands/true.d.ts +4 -0
- package/dist/commands/true.d.ts.map +1 -0
- package/dist/commands/true.js +27 -0
- package/dist/commands/true.js.map +1 -0
- package/dist/commands/uniq.d.ts +4 -0
- package/dist/commands/uniq.d.ts.map +1 -0
- package/dist/commands/uniq.js +187 -0
- package/dist/commands/uniq.js.map +1 -0
- package/dist/commands/user.d.ts +4 -0
- package/dist/commands/user.d.ts.map +1 -0
- package/dist/commands/user.js +427 -0
- package/dist/commands/user.js.map +1 -0
- package/dist/commands/wc.d.ts +4 -0
- package/dist/commands/wc.d.ts.map +1 -0
- package/dist/commands/wc.js +178 -0
- package/dist/commands/wc.js.map +1 -0
- package/dist/commands/which.d.ts +4 -0
- package/dist/commands/which.d.ts.map +1 -0
- package/dist/commands/which.js +78 -0
- package/dist/commands/which.js.map +1 -0
- package/dist/commands/whoami.d.ts +4 -0
- package/dist/commands/whoami.d.ts.map +1 -0
- package/dist/commands/whoami.js +28 -0
- package/dist/commands/whoami.js.map +1 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +67 -1
- package/dist/index.js.map +1 -1
- package/dist/shared/terminal-command.d.ts +8 -2
- package/dist/shared/terminal-command.d.ts.map +1 -1
- package/dist/shared/terminal-command.js +42 -17
- package/dist/shared/terminal-command.js.map +1 -1
- package/package.json +4 -2
- package/src/commands/basename.ts +84 -0
- package/src/commands/cal.ts +111 -0
- package/src/commands/cat.ts +37 -27
- package/src/commands/cd.ts +40 -12
- package/src/commands/chmod.ts +37 -11
- package/src/commands/comm.ts +169 -0
- package/src/commands/cp.ts +73 -15
- package/src/commands/cut.ts +214 -0
- package/src/commands/date.ts +97 -0
- package/src/commands/diff.ts +204 -0
- package/src/commands/dirname.ts +57 -0
- package/src/commands/echo.ts +51 -11
- package/src/commands/false.ts +31 -0
- package/src/commands/find.ts +184 -0
- package/src/commands/grep.ts +44 -11
- package/src/commands/head.ts +46 -10
- package/src/commands/hex.ts +19 -7
- package/src/commands/id.ts +94 -0
- package/src/commands/join.ts +162 -0
- package/src/commands/less.ts +20 -8
- package/src/commands/ln.ts +50 -13
- package/src/commands/ls.ts +110 -87
- package/src/commands/mkdir.ts +41 -12
- package/src/commands/mv.ts +31 -9
- package/src/commands/nc.ts +499 -0
- package/src/commands/nl.ts +201 -0
- package/src/commands/passkey.ts +46 -19
- package/src/commands/paste.ts +172 -0
- package/src/commands/pwd.ts +16 -4
- package/src/commands/rm.ts +118 -13
- package/src/commands/rmdir.ts +41 -12
- package/src/commands/sed.ts +64 -30
- package/src/commands/seq.ts +147 -0
- package/src/commands/sockets.ts +283 -0
- package/src/commands/sort.ts +175 -0
- package/src/commands/split.ts +154 -0
- package/src/commands/stat.ts +17 -8
- package/src/commands/tail.ts +46 -10
- package/src/commands/tee.ts +43 -11
- package/src/commands/test.ts +116 -0
- package/src/commands/touch.ts +41 -12
- package/src/commands/tr.ts +170 -0
- package/src/commands/true.ts +31 -0
- package/src/commands/uniq.ts +189 -0
- package/src/commands/user.ts +436 -0
- package/src/commands/wc.ts +181 -0
- package/src/commands/which.ts +89 -0
- package/src/commands/whoami.ts +32 -0
- package/src/index.ts +67 -1
- package/src/shared/terminal-command.ts +43 -16
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import path from 'path'
|
|
2
|
+
import type { Kernel, Process, Shell, Terminal } from '@ecmaos/types'
|
|
3
|
+
import { TerminalCommand } from '../shared/terminal-command.js'
|
|
4
|
+
import { writelnStderr } from '../shared/helpers.js'
|
|
5
|
+
|
|
6
|
+
function printUsage(process: Process | undefined, terminal: Terminal): void {
|
|
7
|
+
const usage = `Usage: which [COMMAND]...
|
|
8
|
+
Locate a command.
|
|
9
|
+
|
|
10
|
+
COMMAND the command(s) to locate
|
|
11
|
+
--help display this help and exit`
|
|
12
|
+
writelnStderr(process, terminal, usage)
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function createCommand(kernel: Kernel, shell: Shell, terminal: Terminal): TerminalCommand {
|
|
16
|
+
return new TerminalCommand({
|
|
17
|
+
command: 'which',
|
|
18
|
+
description: 'Locate a command',
|
|
19
|
+
kernel,
|
|
20
|
+
shell,
|
|
21
|
+
terminal,
|
|
22
|
+
run: async (pid: number, argv: string[]) => {
|
|
23
|
+
const process = kernel.processes.get(pid) as Process | undefined
|
|
24
|
+
|
|
25
|
+
if (!process) return 1
|
|
26
|
+
|
|
27
|
+
if (argv.length > 0 && (argv[0] === '--help' || argv[0] === '-h')) {
|
|
28
|
+
printUsage(process, terminal)
|
|
29
|
+
return 0
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const commands: string[] = []
|
|
33
|
+
for (const arg of argv) {
|
|
34
|
+
if (arg !== '--help' && arg !== '-h' && !arg.startsWith('-')) {
|
|
35
|
+
commands.push(arg)
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (commands.length === 0) {
|
|
40
|
+
await writelnStderr(process, terminal, 'which: missing command name')
|
|
41
|
+
return 1
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const writer = process.stdout.getWriter()
|
|
45
|
+
let exitCode = 0
|
|
46
|
+
|
|
47
|
+
const resolveCommand = async (command: string): Promise<string | undefined> => {
|
|
48
|
+
if (command.startsWith('./')) {
|
|
49
|
+
const cwdCommand = path.join(shell.cwd, command.slice(2))
|
|
50
|
+
if (await shell.context.fs.promises.exists(cwdCommand)) {
|
|
51
|
+
return cwdCommand
|
|
52
|
+
}
|
|
53
|
+
return undefined
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const paths = shell.env.get('PATH')?.split(':') || ['/bin', '/usr/bin', '/usr/local/bin']
|
|
57
|
+
const resolvedCommand = path.resolve(command)
|
|
58
|
+
|
|
59
|
+
if (await shell.context.fs.promises.exists(resolvedCommand)) {
|
|
60
|
+
return resolvedCommand
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
for (const pathDir of paths) {
|
|
64
|
+
const expandedPath = pathDir.replace(/\$([A-Z_]+)/g, (_, name) => shell.env.get(name) || '')
|
|
65
|
+
const fullPath = `${expandedPath}/${command}`
|
|
66
|
+
if (await shell.context.fs.promises.exists(fullPath)) return fullPath
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return undefined
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
try {
|
|
73
|
+
for (const cmd of commands) {
|
|
74
|
+
const commandPath = await resolveCommand(cmd)
|
|
75
|
+
|
|
76
|
+
if (commandPath) {
|
|
77
|
+
await writer.write(new TextEncoder().encode(commandPath + '\n'))
|
|
78
|
+
} else {
|
|
79
|
+
exitCode = 1
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return exitCode
|
|
84
|
+
} finally {
|
|
85
|
+
writer.releaseLock()
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
})
|
|
89
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { Kernel, Process, Shell, Terminal } from '@ecmaos/types'
|
|
2
|
+
import { TerminalCommand } from '../shared/terminal-command.js'
|
|
3
|
+
import { writelnStdout } from '../shared/helpers.js'
|
|
4
|
+
|
|
5
|
+
function printUsage(process: Process | undefined, terminal: Terminal): void {
|
|
6
|
+
const usage = `Usage: whoami
|
|
7
|
+
Print effective user ID.
|
|
8
|
+
|
|
9
|
+
--help display this help and exit`
|
|
10
|
+
writelnStdout(process, terminal, usage)
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function createCommand(kernel: Kernel, shell: Shell, terminal: Terminal): TerminalCommand {
|
|
14
|
+
return new TerminalCommand({
|
|
15
|
+
command: 'whoami',
|
|
16
|
+
description: 'Print effective user ID',
|
|
17
|
+
kernel,
|
|
18
|
+
shell,
|
|
19
|
+
terminal,
|
|
20
|
+
run: async (pid: number, argv: string[]) => {
|
|
21
|
+
const process = kernel.processes.get(pid) as Process | undefined
|
|
22
|
+
|
|
23
|
+
if (argv.length > 0 && (argv[0] === '--help' || argv[0] === '-h')) {
|
|
24
|
+
printUsage(process, terminal)
|
|
25
|
+
return 0
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
await writelnStdout(process, terminal, shell.username)
|
|
29
|
+
return 0
|
|
30
|
+
}
|
|
31
|
+
})
|
|
32
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -18,7 +18,9 @@ import { createCommand as createLn } from './commands/ln.js'
|
|
|
18
18
|
import { createCommand as createLs } from './commands/ls.js'
|
|
19
19
|
import { createCommand as createMkdir } from './commands/mkdir.js'
|
|
20
20
|
import { createCommand as createMv } from './commands/mv.js'
|
|
21
|
+
import { createCommand as createNc } from './commands/nc.js'
|
|
21
22
|
import { createCommand as createPwd } from './commands/pwd.js'
|
|
23
|
+
import { createCommand as createSockets } from './commands/sockets.js'
|
|
22
24
|
import { createCommand as createRm } from './commands/rm.js'
|
|
23
25
|
import { createCommand as createRmdir } from './commands/rmdir.js'
|
|
24
26
|
import { createCommand as createStat } from './commands/stat.js'
|
|
@@ -29,6 +31,30 @@ import { createCommand as createPasskey } from './commands/passkey.js'
|
|
|
29
31
|
import { createCommand as createSed } from './commands/sed.js'
|
|
30
32
|
import { createCommand as createTee } from './commands/tee.js'
|
|
31
33
|
import { createCommand as createTail } from './commands/tail.js'
|
|
34
|
+
import { createCommand as createBasename } from './commands/basename.js'
|
|
35
|
+
import { createCommand as createCal } from './commands/cal.js'
|
|
36
|
+
import { createCommand as createComm } from './commands/comm.js'
|
|
37
|
+
import { createCommand as createCut } from './commands/cut.js'
|
|
38
|
+
import { createCommand as createDate } from './commands/date.js'
|
|
39
|
+
import { createCommand as createDiff } from './commands/diff.js'
|
|
40
|
+
import { createCommand as createDirname } from './commands/dirname.js'
|
|
41
|
+
import { createCommand as createFalse } from './commands/false.js'
|
|
42
|
+
import { createCommand as createFind } from './commands/find.js'
|
|
43
|
+
import { createCommand as createId } from './commands/id.js'
|
|
44
|
+
import { createCommand as createJoin } from './commands/join.js'
|
|
45
|
+
import { createCommand as createNl } from './commands/nl.js'
|
|
46
|
+
import { createCommand as createPaste } from './commands/paste.js'
|
|
47
|
+
import { createCommand as createSeq } from './commands/seq.js'
|
|
48
|
+
import { createCommand as createSort } from './commands/sort.js'
|
|
49
|
+
import { createCommand as createSplit } from './commands/split.js'
|
|
50
|
+
import { createCommand as createTest } from './commands/test.js'
|
|
51
|
+
import { createCommand as createTr } from './commands/tr.js'
|
|
52
|
+
import { createCommand as createTrue } from './commands/true.js'
|
|
53
|
+
import { createCommand as createUniq } from './commands/uniq.js'
|
|
54
|
+
import { createCommand as createUser } from './commands/user.js'
|
|
55
|
+
import { createCommand as createWc } from './commands/wc.js'
|
|
56
|
+
import { createCommand as createWhich } from './commands/which.js'
|
|
57
|
+
import { createCommand as createWhoami } from './commands/whoami.js'
|
|
32
58
|
|
|
33
59
|
// Export individual command factories
|
|
34
60
|
export { createCommand as createCat } from './commands/cat.js'
|
|
@@ -51,6 +77,20 @@ export { createCommand as createLess } from './commands/less.js'
|
|
|
51
77
|
export { createCommand as createSed } from './commands/sed.js'
|
|
52
78
|
export { createCommand as createTee } from './commands/tee.js'
|
|
53
79
|
export { createCommand as createTail } from './commands/tail.js'
|
|
80
|
+
export { createCommand as createBasename } from './commands/basename.js'
|
|
81
|
+
export { createCommand as createCut } from './commands/cut.js'
|
|
82
|
+
export { createCommand as createDate } from './commands/date.js'
|
|
83
|
+
export { createCommand as createDiff } from './commands/diff.js'
|
|
84
|
+
export { createCommand as createDirname } from './commands/dirname.js'
|
|
85
|
+
export { createCommand as createFind } from './commands/find.js'
|
|
86
|
+
export { createCommand as createSort } from './commands/sort.js'
|
|
87
|
+
export { createCommand as createTest } from './commands/test.js'
|
|
88
|
+
export { createCommand as createTr } from './commands/tr.js'
|
|
89
|
+
export { createCommand as createUniq } from './commands/uniq.js'
|
|
90
|
+
export { createCommand as createUser } from './commands/user.js'
|
|
91
|
+
export { createCommand as createWc } from './commands/wc.js'
|
|
92
|
+
export { createCommand as createWhich } from './commands/which.js'
|
|
93
|
+
export { createCommand as createSockets } from './commands/sockets.js'
|
|
54
94
|
|
|
55
95
|
/**
|
|
56
96
|
* Creates all coreutils commands.
|
|
@@ -70,6 +110,8 @@ export function createAllCommands(kernel: Kernel, shell: Shell, terminal: Termin
|
|
|
70
110
|
mkdir: createMkdir(kernel, shell, terminal),
|
|
71
111
|
mv: createMv(kernel, shell, terminal),
|
|
72
112
|
pwd: createPwd(kernel, shell, terminal),
|
|
113
|
+
nc: createNc(kernel, shell, terminal),
|
|
114
|
+
sockets: createSockets(kernel, shell, terminal),
|
|
73
115
|
rm: createRm(kernel, shell, terminal),
|
|
74
116
|
rmdir: createRmdir(kernel, shell, terminal),
|
|
75
117
|
stat: createStat(kernel, shell, terminal),
|
|
@@ -79,7 +121,31 @@ export function createAllCommands(kernel: Kernel, shell: Shell, terminal: Termin
|
|
|
79
121
|
passkey: createPasskey(kernel, shell, terminal),
|
|
80
122
|
sed: createSed(kernel, shell, terminal),
|
|
81
123
|
tail: createTail(kernel, shell, terminal),
|
|
82
|
-
tee: createTee(kernel, shell, terminal)
|
|
124
|
+
tee: createTee(kernel, shell, terminal),
|
|
125
|
+
basename: createBasename(kernel, shell, terminal),
|
|
126
|
+
cal: createCal(kernel, shell, terminal),
|
|
127
|
+
comm: createComm(kernel, shell, terminal),
|
|
128
|
+
cut: createCut(kernel, shell, terminal),
|
|
129
|
+
date: createDate(kernel, shell, terminal),
|
|
130
|
+
diff: createDiff(kernel, shell, terminal),
|
|
131
|
+
dirname: createDirname(kernel, shell, terminal),
|
|
132
|
+
false: createFalse(kernel, shell, terminal),
|
|
133
|
+
find: createFind(kernel, shell, terminal),
|
|
134
|
+
id: createId(kernel, shell, terminal),
|
|
135
|
+
join: createJoin(kernel, shell, terminal),
|
|
136
|
+
nl: createNl(kernel, shell, terminal),
|
|
137
|
+
paste: createPaste(kernel, shell, terminal),
|
|
138
|
+
seq: createSeq(kernel, shell, terminal),
|
|
139
|
+
sort: createSort(kernel, shell, terminal),
|
|
140
|
+
split: createSplit(kernel, shell, terminal),
|
|
141
|
+
test: createTest(kernel, shell, terminal),
|
|
142
|
+
tr: createTr(kernel, shell, terminal),
|
|
143
|
+
true: createTrue(kernel, shell, terminal),
|
|
144
|
+
uniq: createUniq(kernel, shell, terminal),
|
|
145
|
+
user: createUser(kernel, shell, terminal),
|
|
146
|
+
wc: createWc(kernel, shell, terminal),
|
|
147
|
+
which: createWhich(kernel, shell, terminal),
|
|
148
|
+
whoami: createWhoami(kernel, shell, terminal)
|
|
83
149
|
}
|
|
84
150
|
}
|
|
85
151
|
|
|
@@ -5,8 +5,14 @@ import type { TerminalCommand as ITerminalCommand } from '@ecmaos/types'
|
|
|
5
5
|
import type { Kernel, Process, Shell, Terminal } from '@ecmaos/types'
|
|
6
6
|
import { writelnStdout, writelnStderr } from './helpers.js'
|
|
7
7
|
|
|
8
|
+
type UnifiedParserRun = (argv: CommandLineOptions, process?: Process, rawArgv?: string[]) => Promise<number | void>
|
|
9
|
+
type RawArgvRun = (pid: number, argv: string[]) => Promise<number | void>
|
|
10
|
+
|
|
8
11
|
/**
|
|
9
12
|
* The TerminalCommand class sets up a common interface for builtin terminal commands
|
|
13
|
+
* Supports two modes:
|
|
14
|
+
* - Unified parser mode: When options are provided, uses command-line-args (for kernel commands)
|
|
15
|
+
* - Raw argv mode: When options are not provided, passes raw argv directly (for coreutils commands)
|
|
10
16
|
*/
|
|
11
17
|
export class TerminalCommand implements ITerminalCommand {
|
|
12
18
|
command: string = ''
|
|
@@ -24,8 +30,8 @@ export class TerminalCommand implements ITerminalCommand {
|
|
|
24
30
|
command: string
|
|
25
31
|
description: string
|
|
26
32
|
kernel: Kernel
|
|
27
|
-
options
|
|
28
|
-
run:
|
|
33
|
+
options?: parseUsage.OptionDefinition[]
|
|
34
|
+
run: UnifiedParserRun | RawArgvRun
|
|
29
35
|
shell: Shell
|
|
30
36
|
terminal: Terminal
|
|
31
37
|
stdin?: ReadableStream<Uint8Array>
|
|
@@ -35,32 +41,50 @@ export class TerminalCommand implements ITerminalCommand {
|
|
|
35
41
|
this.command = command
|
|
36
42
|
this.description = description
|
|
37
43
|
this.kernel = kernel
|
|
38
|
-
this.options = options
|
|
44
|
+
this.options = options || []
|
|
39
45
|
this.shell = shell
|
|
40
46
|
this.terminal = terminal
|
|
41
47
|
this.stdin = stdin
|
|
42
48
|
this.stdout = stdout
|
|
43
49
|
this.stderr = stderr
|
|
44
50
|
|
|
45
|
-
|
|
46
|
-
if (argv === null) return 1
|
|
47
|
-
const process = this.kernel.processes.get(pid) as Process | undefined
|
|
48
|
-
try {
|
|
49
|
-
const parsed = parseArgs(this.options, { argv })
|
|
50
|
-
if (parsed.help) {
|
|
51
|
-
await writelnStdout(process, this.terminal, this.usage)
|
|
52
|
-
return 0
|
|
53
|
-
}
|
|
51
|
+
const useUnifiedParser = this.options.length > 0
|
|
54
52
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
return 1
|
|
53
|
+
if (useUnifiedParser) {
|
|
54
|
+
const unifiedRun = run as UnifiedParserRun
|
|
55
|
+
this.run = async (pid: number, argv: string[]) => {
|
|
56
|
+
if (argv === null) return 1
|
|
57
|
+
const process = this.kernel.processes.get(pid) as Process | undefined
|
|
58
|
+
try {
|
|
59
|
+
const parsed = parseArgs(this.options, { argv, stopAtFirstUnknown: true })
|
|
60
|
+
if (parsed.help) {
|
|
61
|
+
await writelnStdout(process, this.terminal, this.usage)
|
|
62
|
+
return 0
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return await unifiedRun(parsed, process, argv)
|
|
66
|
+
} catch (error) {
|
|
67
|
+
const errorMessage = error instanceof Error ? error.message : String(error)
|
|
68
|
+
if (errorMessage.includes('UNKNOWN_OPTION') || errorMessage.includes('Unknown option')) {
|
|
69
|
+
return await unifiedRun({} as CommandLineOptions, process, argv)
|
|
70
|
+
}
|
|
71
|
+
await writelnStderr(process, this.terminal, chalk.red(errorMessage))
|
|
72
|
+
return 1
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
} else {
|
|
76
|
+
const rawRun = run as RawArgvRun
|
|
77
|
+
this.run = async (pid: number, argv: string[]) => {
|
|
78
|
+
if (argv === null) return 1
|
|
79
|
+
return await rawRun(pid, argv)
|
|
59
80
|
}
|
|
60
81
|
}
|
|
61
82
|
}
|
|
62
83
|
|
|
63
84
|
get usage() {
|
|
85
|
+
if (this.options.length === 0) {
|
|
86
|
+
return ''
|
|
87
|
+
}
|
|
64
88
|
return parseUsage([
|
|
65
89
|
{ header: this.command, content: this.description },
|
|
66
90
|
{ header: 'Usage', content: this.usageContent },
|
|
@@ -69,6 +93,9 @@ export class TerminalCommand implements ITerminalCommand {
|
|
|
69
93
|
}
|
|
70
94
|
|
|
71
95
|
get usageContent() {
|
|
96
|
+
if (this.options.length === 0) {
|
|
97
|
+
return ''
|
|
98
|
+
}
|
|
72
99
|
return `${this.command} ${this.options.map(option => {
|
|
73
100
|
let optionStr = option.name
|
|
74
101
|
if (option.type === Boolean) optionStr = `[--${option.name}]`
|