@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.
Files changed (231) hide show
  1. package/.turbo/turbo-build.log +1 -1
  2. package/CHANGELOG.md +45 -0
  3. package/dist/commands/basename.d.ts +4 -0
  4. package/dist/commands/basename.d.ts.map +1 -0
  5. package/dist/commands/basename.js +78 -0
  6. package/dist/commands/basename.js.map +1 -0
  7. package/dist/commands/cal.d.ts +4 -0
  8. package/dist/commands/cal.d.ts.map +1 -0
  9. package/dist/commands/cal.js +105 -0
  10. package/dist/commands/cal.js.map +1 -0
  11. package/dist/commands/cat.d.ts.map +1 -1
  12. package/dist/commands/cat.js +33 -25
  13. package/dist/commands/cat.js.map +1 -1
  14. package/dist/commands/cd.d.ts.map +1 -1
  15. package/dist/commands/cd.js +38 -10
  16. package/dist/commands/cd.js.map +1 -1
  17. package/dist/commands/chmod.d.ts.map +1 -1
  18. package/dist/commands/chmod.js +31 -9
  19. package/dist/commands/chmod.js.map +1 -1
  20. package/dist/commands/comm.d.ts +4 -0
  21. package/dist/commands/comm.d.ts.map +1 -0
  22. package/dist/commands/comm.js +162 -0
  23. package/dist/commands/comm.js.map +1 -0
  24. package/dist/commands/cp.d.ts.map +1 -1
  25. package/dist/commands/cp.js +61 -12
  26. package/dist/commands/cp.js.map +1 -1
  27. package/dist/commands/cut.d.ts +4 -0
  28. package/dist/commands/cut.d.ts.map +1 -0
  29. package/dist/commands/cut.js +208 -0
  30. package/dist/commands/cut.js.map +1 -0
  31. package/dist/commands/date.d.ts +4 -0
  32. package/dist/commands/date.d.ts.map +1 -0
  33. package/dist/commands/date.js +100 -0
  34. package/dist/commands/date.js.map +1 -0
  35. package/dist/commands/diff.d.ts +4 -0
  36. package/dist/commands/diff.d.ts.map +1 -0
  37. package/dist/commands/diff.js +194 -0
  38. package/dist/commands/diff.js.map +1 -0
  39. package/dist/commands/dirname.d.ts +4 -0
  40. package/dist/commands/dirname.d.ts.map +1 -0
  41. package/dist/commands/dirname.js +50 -0
  42. package/dist/commands/dirname.js.map +1 -0
  43. package/dist/commands/echo.d.ts.map +1 -1
  44. package/dist/commands/echo.js +49 -10
  45. package/dist/commands/echo.js.map +1 -1
  46. package/dist/commands/false.d.ts +4 -0
  47. package/dist/commands/false.d.ts.map +1 -0
  48. package/dist/commands/false.js +27 -0
  49. package/dist/commands/false.js.map +1 -0
  50. package/dist/commands/find.d.ts +4 -0
  51. package/dist/commands/find.d.ts.map +1 -0
  52. package/dist/commands/find.js +181 -0
  53. package/dist/commands/find.js.map +1 -0
  54. package/dist/commands/grep.d.ts.map +1 -1
  55. package/dist/commands/grep.js +45 -10
  56. package/dist/commands/grep.js.map +1 -1
  57. package/dist/commands/head.d.ts.map +1 -1
  58. package/dist/commands/head.js +46 -9
  59. package/dist/commands/head.js.map +1 -1
  60. package/dist/commands/hex.d.ts.map +1 -1
  61. package/dist/commands/hex.js +16 -6
  62. package/dist/commands/hex.js.map +1 -1
  63. package/dist/commands/id.d.ts +4 -0
  64. package/dist/commands/id.d.ts.map +1 -0
  65. package/dist/commands/id.js +97 -0
  66. package/dist/commands/id.js.map +1 -0
  67. package/dist/commands/join.d.ts +4 -0
  68. package/dist/commands/join.d.ts.map +1 -0
  69. package/dist/commands/join.js +152 -0
  70. package/dist/commands/join.js.map +1 -0
  71. package/dist/commands/less.d.ts.map +1 -1
  72. package/dist/commands/less.js +16 -7
  73. package/dist/commands/less.js.map +1 -1
  74. package/dist/commands/ln.d.ts.map +1 -1
  75. package/dist/commands/ln.js +54 -12
  76. package/dist/commands/ln.js.map +1 -1
  77. package/dist/commands/ls.d.ts.map +1 -1
  78. package/dist/commands/ls.js +96 -91
  79. package/dist/commands/ls.js.map +1 -1
  80. package/dist/commands/mkdir.d.ts.map +1 -1
  81. package/dist/commands/mkdir.js +34 -9
  82. package/dist/commands/mkdir.js.map +1 -1
  83. package/dist/commands/mv.d.ts.map +1 -1
  84. package/dist/commands/mv.js +25 -7
  85. package/dist/commands/mv.js.map +1 -1
  86. package/dist/commands/nc.d.ts +4 -0
  87. package/dist/commands/nc.d.ts.map +1 -0
  88. package/dist/commands/nc.js +451 -0
  89. package/dist/commands/nc.js.map +1 -0
  90. package/dist/commands/nl.d.ts +4 -0
  91. package/dist/commands/nl.d.ts.map +1 -0
  92. package/dist/commands/nl.js +208 -0
  93. package/dist/commands/nl.js.map +1 -0
  94. package/dist/commands/passkey.d.ts.map +1 -1
  95. package/dist/commands/passkey.js +44 -18
  96. package/dist/commands/passkey.js.map +1 -1
  97. package/dist/commands/paste.d.ts +4 -0
  98. package/dist/commands/paste.d.ts.map +1 -0
  99. package/dist/commands/paste.js +161 -0
  100. package/dist/commands/paste.js.map +1 -0
  101. package/dist/commands/pwd.d.ts.map +1 -1
  102. package/dist/commands/pwd.js +13 -4
  103. package/dist/commands/pwd.js.map +1 -1
  104. package/dist/commands/rm.d.ts.map +1 -1
  105. package/dist/commands/rm.js +105 -12
  106. package/dist/commands/rm.js.map +1 -1
  107. package/dist/commands/rmdir.d.ts.map +1 -1
  108. package/dist/commands/rmdir.js +34 -9
  109. package/dist/commands/rmdir.js.map +1 -1
  110. package/dist/commands/sed.d.ts.map +1 -1
  111. package/dist/commands/sed.js +73 -28
  112. package/dist/commands/sed.js.map +1 -1
  113. package/dist/commands/seq.d.ts +4 -0
  114. package/dist/commands/seq.d.ts.map +1 -0
  115. package/dist/commands/seq.js +148 -0
  116. package/dist/commands/seq.js.map +1 -0
  117. package/dist/commands/sockets.d.ts +4 -0
  118. package/dist/commands/sockets.d.ts.map +1 -0
  119. package/dist/commands/sockets.js +239 -0
  120. package/dist/commands/sockets.js.map +1 -0
  121. package/dist/commands/sort.d.ts +4 -0
  122. package/dist/commands/sort.d.ts.map +1 -0
  123. package/dist/commands/sort.js +175 -0
  124. package/dist/commands/sort.js.map +1 -0
  125. package/dist/commands/split.d.ts +4 -0
  126. package/dist/commands/split.d.ts.map +1 -0
  127. package/dist/commands/split.js +147 -0
  128. package/dist/commands/split.js.map +1 -0
  129. package/dist/commands/stat.d.ts.map +1 -1
  130. package/dist/commands/stat.js +14 -6
  131. package/dist/commands/stat.js.map +1 -1
  132. package/dist/commands/tail.d.ts.map +1 -1
  133. package/dist/commands/tail.js +46 -9
  134. package/dist/commands/tail.js.map +1 -1
  135. package/dist/commands/tee.d.ts.map +1 -1
  136. package/dist/commands/tee.js +45 -10
  137. package/dist/commands/tee.js.map +1 -1
  138. package/dist/commands/test.d.ts +4 -0
  139. package/dist/commands/test.d.ts.map +1 -0
  140. package/dist/commands/test.js +99 -0
  141. package/dist/commands/test.js.map +1 -0
  142. package/dist/commands/touch.d.ts.map +1 -1
  143. package/dist/commands/touch.js +34 -9
  144. package/dist/commands/touch.js.map +1 -1
  145. package/dist/commands/tr.d.ts +4 -0
  146. package/dist/commands/tr.d.ts.map +1 -0
  147. package/dist/commands/tr.js +162 -0
  148. package/dist/commands/tr.js.map +1 -0
  149. package/dist/commands/true.d.ts +4 -0
  150. package/dist/commands/true.d.ts.map +1 -0
  151. package/dist/commands/true.js +27 -0
  152. package/dist/commands/true.js.map +1 -0
  153. package/dist/commands/uniq.d.ts +4 -0
  154. package/dist/commands/uniq.d.ts.map +1 -0
  155. package/dist/commands/uniq.js +187 -0
  156. package/dist/commands/uniq.js.map +1 -0
  157. package/dist/commands/user.d.ts +4 -0
  158. package/dist/commands/user.d.ts.map +1 -0
  159. package/dist/commands/user.js +427 -0
  160. package/dist/commands/user.js.map +1 -0
  161. package/dist/commands/wc.d.ts +4 -0
  162. package/dist/commands/wc.d.ts.map +1 -0
  163. package/dist/commands/wc.js +178 -0
  164. package/dist/commands/wc.js.map +1 -0
  165. package/dist/commands/which.d.ts +4 -0
  166. package/dist/commands/which.d.ts.map +1 -0
  167. package/dist/commands/which.js +78 -0
  168. package/dist/commands/which.js.map +1 -0
  169. package/dist/commands/whoami.d.ts +4 -0
  170. package/dist/commands/whoami.d.ts.map +1 -0
  171. package/dist/commands/whoami.js +28 -0
  172. package/dist/commands/whoami.js.map +1 -0
  173. package/dist/index.d.ts +14 -0
  174. package/dist/index.d.ts.map +1 -1
  175. package/dist/index.js +67 -1
  176. package/dist/index.js.map +1 -1
  177. package/dist/shared/terminal-command.d.ts +8 -2
  178. package/dist/shared/terminal-command.d.ts.map +1 -1
  179. package/dist/shared/terminal-command.js +42 -17
  180. package/dist/shared/terminal-command.js.map +1 -1
  181. package/package.json +4 -2
  182. package/src/commands/basename.ts +84 -0
  183. package/src/commands/cal.ts +111 -0
  184. package/src/commands/cat.ts +37 -27
  185. package/src/commands/cd.ts +40 -12
  186. package/src/commands/chmod.ts +37 -11
  187. package/src/commands/comm.ts +169 -0
  188. package/src/commands/cp.ts +73 -15
  189. package/src/commands/cut.ts +214 -0
  190. package/src/commands/date.ts +97 -0
  191. package/src/commands/diff.ts +204 -0
  192. package/src/commands/dirname.ts +57 -0
  193. package/src/commands/echo.ts +51 -11
  194. package/src/commands/false.ts +31 -0
  195. package/src/commands/find.ts +184 -0
  196. package/src/commands/grep.ts +44 -11
  197. package/src/commands/head.ts +46 -10
  198. package/src/commands/hex.ts +19 -7
  199. package/src/commands/id.ts +94 -0
  200. package/src/commands/join.ts +162 -0
  201. package/src/commands/less.ts +20 -8
  202. package/src/commands/ln.ts +50 -13
  203. package/src/commands/ls.ts +110 -87
  204. package/src/commands/mkdir.ts +41 -12
  205. package/src/commands/mv.ts +31 -9
  206. package/src/commands/nc.ts +499 -0
  207. package/src/commands/nl.ts +201 -0
  208. package/src/commands/passkey.ts +46 -19
  209. package/src/commands/paste.ts +172 -0
  210. package/src/commands/pwd.ts +16 -4
  211. package/src/commands/rm.ts +118 -13
  212. package/src/commands/rmdir.ts +41 -12
  213. package/src/commands/sed.ts +64 -30
  214. package/src/commands/seq.ts +147 -0
  215. package/src/commands/sockets.ts +283 -0
  216. package/src/commands/sort.ts +175 -0
  217. package/src/commands/split.ts +154 -0
  218. package/src/commands/stat.ts +17 -8
  219. package/src/commands/tail.ts +46 -10
  220. package/src/commands/tee.ts +43 -11
  221. package/src/commands/test.ts +116 -0
  222. package/src/commands/touch.ts +41 -12
  223. package/src/commands/tr.ts +170 -0
  224. package/src/commands/true.ts +31 -0
  225. package/src/commands/uniq.ts +189 -0
  226. package/src/commands/user.ts +436 -0
  227. package/src/commands/wc.ts +181 -0
  228. package/src/commands/which.ts +89 -0
  229. package/src/commands/whoami.ts +32 -0
  230. package/src/index.ts +67 -1
  231. 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: parseUsage.OptionDefinition[]
28
- run: (argv: CommandLineOptions, process?: Process) => Promise<number | void>
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
- this.run = async (pid: number, argv: string[]) => {
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
- return await run(parsed, process)
56
- } catch (error) {
57
- await writelnStderr(process, this.terminal, chalk.red(String(error)))
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}]`