typescript-virtual-container 1.2.5 → 1.2.6

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 (245) hide show
  1. package/README.md +387 -193
  2. package/benchmark-results.txt +21 -21
  3. package/dist/SSHMimic/exec.js +2 -2
  4. package/dist/SSHMimic/executor.d.ts +6 -7
  5. package/dist/SSHMimic/executor.d.ts.map +1 -1
  6. package/dist/SSHMimic/executor.js +77 -60
  7. package/dist/SSHMimic/index.d.ts.map +1 -1
  8. package/dist/SSHMimic/index.js +6 -20
  9. package/dist/SSHMimic/sftp.d.ts.map +1 -1
  10. package/dist/SSHMimic/sftp.js +14 -0
  11. package/dist/VirtualFileSystem/index.d.ts.map +1 -1
  12. package/dist/VirtualFileSystem/index.js +13 -36
  13. package/dist/VirtualShell/shell.d.ts.map +1 -1
  14. package/dist/VirtualShell/shell.js +19 -2
  15. package/dist/VirtualShell/shellParser.d.ts +20 -2
  16. package/dist/VirtualShell/shellParser.d.ts.map +1 -1
  17. package/dist/VirtualShell/shellParser.js +229 -120
  18. package/dist/VirtualUserManager/index.d.ts.map +1 -1
  19. package/dist/commands/adduser.d.ts.map +1 -1
  20. package/dist/commands/adduser.js +2 -0
  21. package/dist/commands/awk.d.ts +3 -0
  22. package/dist/commands/awk.d.ts.map +1 -0
  23. package/dist/commands/awk.js +29 -0
  24. package/dist/commands/base64.d.ts +3 -0
  25. package/dist/commands/base64.d.ts.map +1 -0
  26. package/dist/commands/base64.js +20 -0
  27. package/dist/commands/cat.d.ts.map +1 -1
  28. package/dist/commands/cat.js +2 -0
  29. package/dist/commands/cd.d.ts.map +1 -1
  30. package/dist/commands/cd.js +2 -0
  31. package/dist/commands/chmod.d.ts.map +1 -1
  32. package/dist/commands/chmod.js +2 -0
  33. package/dist/commands/clear.d.ts.map +1 -1
  34. package/dist/commands/clear.js +4 -1
  35. package/dist/commands/cp.d.ts.map +1 -1
  36. package/dist/commands/cp.js +2 -0
  37. package/dist/commands/curl.d.ts.map +1 -1
  38. package/dist/commands/curl.js +2 -0
  39. package/dist/commands/cut.d.ts +3 -0
  40. package/dist/commands/cut.d.ts.map +1 -0
  41. package/dist/commands/cut.js +27 -0
  42. package/dist/commands/date.d.ts +3 -0
  43. package/dist/commands/date.d.ts.map +1 -0
  44. package/dist/commands/date.js +22 -0
  45. package/dist/commands/deluser.d.ts.map +1 -1
  46. package/dist/commands/deluser.js +2 -0
  47. package/dist/commands/df.d.ts +3 -0
  48. package/dist/commands/df.d.ts.map +1 -0
  49. package/dist/commands/df.js +16 -0
  50. package/dist/commands/diff.d.ts +3 -0
  51. package/dist/commands/diff.d.ts.map +1 -0
  52. package/dist/commands/diff.js +40 -0
  53. package/dist/commands/du.d.ts +3 -0
  54. package/dist/commands/du.d.ts.map +1 -0
  55. package/dist/commands/du.js +39 -0
  56. package/dist/commands/echo.d.ts.map +1 -1
  57. package/dist/commands/echo.js +2 -0
  58. package/dist/commands/env.d.ts.map +1 -1
  59. package/dist/commands/env.js +6 -14
  60. package/dist/commands/export.d.ts.map +1 -1
  61. package/dist/commands/export.js +11 -21
  62. package/dist/commands/find.d.ts.map +1 -1
  63. package/dist/commands/find.js +2 -0
  64. package/dist/commands/grep.d.ts.map +1 -1
  65. package/dist/commands/grep.js +4 -7
  66. package/dist/commands/groups.d.ts +3 -0
  67. package/dist/commands/groups.d.ts.map +1 -0
  68. package/dist/commands/groups.js +12 -0
  69. package/dist/commands/gzip.d.ts +4 -0
  70. package/dist/commands/gzip.d.ts.map +1 -0
  71. package/dist/commands/gzip.js +40 -0
  72. package/dist/commands/head.d.ts.map +1 -1
  73. package/dist/commands/head.js +2 -0
  74. package/dist/commands/help.d.ts +1 -1
  75. package/dist/commands/help.d.ts.map +1 -1
  76. package/dist/commands/help.js +75 -3
  77. package/dist/commands/hostname.d.ts.map +1 -1
  78. package/dist/commands/hostname.js +2 -0
  79. package/dist/commands/htop.d.ts.map +1 -1
  80. package/dist/commands/htop.js +2 -0
  81. package/dist/commands/id.d.ts +3 -0
  82. package/dist/commands/id.d.ts.map +1 -0
  83. package/dist/commands/id.js +14 -0
  84. package/dist/commands/index.d.ts +5 -2
  85. package/dist/commands/index.d.ts.map +1 -1
  86. package/dist/commands/index.js +89 -62
  87. package/dist/commands/kill.d.ts +3 -0
  88. package/dist/commands/kill.d.ts.map +1 -0
  89. package/dist/commands/kill.js +13 -0
  90. package/dist/commands/ln.d.ts.map +1 -1
  91. package/dist/commands/ln.js +2 -0
  92. package/dist/commands/ls.d.ts.map +1 -1
  93. package/dist/commands/ls.js +2 -0
  94. package/dist/commands/mkdir.d.ts.map +1 -1
  95. package/dist/commands/mkdir.js +2 -0
  96. package/dist/commands/mv.d.ts.map +1 -1
  97. package/dist/commands/mv.js +2 -0
  98. package/dist/commands/nano.d.ts.map +1 -1
  99. package/dist/commands/nano.js +2 -0
  100. package/dist/commands/neofetch.d.ts.map +1 -1
  101. package/dist/commands/neofetch.js +2 -0
  102. package/dist/commands/passwd.d.ts.map +1 -1
  103. package/dist/commands/passwd.js +2 -0
  104. package/dist/commands/ping.d.ts +3 -0
  105. package/dist/commands/ping.d.ts.map +1 -0
  106. package/dist/commands/ping.js +18 -0
  107. package/dist/commands/ps.d.ts +3 -0
  108. package/dist/commands/ps.d.ts.map +1 -0
  109. package/dist/commands/ps.js +17 -0
  110. package/dist/commands/pwd.d.ts.map +1 -1
  111. package/dist/commands/pwd.js +2 -0
  112. package/dist/commands/rm.d.ts.map +1 -1
  113. package/dist/commands/rm.js +2 -0
  114. package/dist/commands/sed.d.ts +3 -0
  115. package/dist/commands/sed.d.ts.map +1 -0
  116. package/dist/commands/sed.js +47 -0
  117. package/dist/commands/set.d.ts +3 -0
  118. package/dist/commands/set.d.ts.map +1 -1
  119. package/dist/commands/set.js +19 -46
  120. package/dist/commands/sh.d.ts +0 -1
  121. package/dist/commands/sh.d.ts.map +1 -1
  122. package/dist/commands/sh.js +228 -35
  123. package/dist/commands/sleep.d.ts +3 -0
  124. package/dist/commands/sleep.d.ts.map +1 -0
  125. package/dist/commands/sleep.js +13 -0
  126. package/dist/commands/sort.d.ts +3 -0
  127. package/dist/commands/sort.d.ts.map +1 -0
  128. package/dist/commands/sort.js +37 -0
  129. package/dist/commands/su.d.ts.map +1 -1
  130. package/dist/commands/su.js +2 -0
  131. package/dist/commands/sudo.d.ts.map +1 -1
  132. package/dist/commands/sudo.js +2 -0
  133. package/dist/commands/tail.d.ts.map +1 -1
  134. package/dist/commands/tail.js +2 -0
  135. package/dist/commands/tar.d.ts +3 -0
  136. package/dist/commands/tar.d.ts.map +1 -0
  137. package/dist/commands/tar.js +64 -0
  138. package/dist/commands/tee.d.ts +3 -0
  139. package/dist/commands/tee.d.ts.map +1 -0
  140. package/dist/commands/tee.js +29 -0
  141. package/dist/commands/touch.d.ts.map +1 -1
  142. package/dist/commands/touch.js +2 -0
  143. package/dist/commands/tr.d.ts +3 -0
  144. package/dist/commands/tr.d.ts.map +1 -0
  145. package/dist/commands/tr.js +24 -0
  146. package/dist/commands/tree.d.ts.map +1 -1
  147. package/dist/commands/tree.js +2 -0
  148. package/dist/commands/uname.d.ts +3 -0
  149. package/dist/commands/uname.d.ts.map +1 -0
  150. package/dist/commands/uname.js +21 -0
  151. package/dist/commands/uniq.d.ts +3 -0
  152. package/dist/commands/uniq.d.ts.map +1 -0
  153. package/dist/commands/uniq.js +33 -0
  154. package/dist/commands/unset.d.ts.map +1 -1
  155. package/dist/commands/unset.js +6 -10
  156. package/dist/commands/wc.d.ts.map +1 -1
  157. package/dist/commands/wc.js +2 -0
  158. package/dist/commands/wget.d.ts.map +1 -1
  159. package/dist/commands/wget.js +2 -0
  160. package/dist/commands/who.d.ts.map +1 -1
  161. package/dist/commands/who.js +2 -0
  162. package/dist/commands/whoami.d.ts.map +1 -1
  163. package/dist/commands/whoami.js +2 -0
  164. package/dist/commands/xargs.d.ts +3 -0
  165. package/dist/commands/xargs.d.ts.map +1 -0
  166. package/dist/commands/xargs.js +16 -0
  167. package/dist/types/commands.d.ts +13 -0
  168. package/dist/types/commands.d.ts.map +1 -1
  169. package/dist/types/pipeline.d.ts +20 -0
  170. package/dist/types/pipeline.d.ts.map +1 -1
  171. package/package.json +1 -1
  172. package/src/SSHMimic/exec.ts +2 -2
  173. package/src/SSHMimic/executor.ts +95 -98
  174. package/src/SSHMimic/index.ts +15 -49
  175. package/src/SSHMimic/sftp.ts +15 -0
  176. package/src/VirtualFileSystem/index.ts +27 -75
  177. package/src/VirtualShell/shell.ts +19 -2
  178. package/src/VirtualShell/shellParser.ts +202 -168
  179. package/src/VirtualUserManager/index.ts +2 -7
  180. package/src/commands/adduser.ts +2 -0
  181. package/src/commands/awk.ts +30 -0
  182. package/src/commands/base64.ts +18 -0
  183. package/src/commands/cat.ts +2 -0
  184. package/src/commands/cd.ts +2 -0
  185. package/src/commands/chmod.ts +2 -0
  186. package/src/commands/clear.ts +4 -1
  187. package/src/commands/cp.ts +2 -0
  188. package/src/commands/curl.ts +2 -0
  189. package/src/commands/cut.ts +29 -0
  190. package/src/commands/date.ts +24 -0
  191. package/src/commands/deluser.ts +2 -0
  192. package/src/commands/df.ts +18 -0
  193. package/src/commands/diff.ts +29 -0
  194. package/src/commands/du.ts +39 -0
  195. package/src/commands/echo.ts +2 -0
  196. package/src/commands/env.ts +6 -16
  197. package/src/commands/export.ts +11 -24
  198. package/src/commands/find.ts +2 -0
  199. package/src/commands/grep.ts +4 -7
  200. package/src/commands/groups.ts +14 -0
  201. package/src/commands/gzip.ts +31 -0
  202. package/src/commands/head.ts +2 -0
  203. package/src/commands/help.ts +81 -3
  204. package/src/commands/hostname.ts +2 -0
  205. package/src/commands/htop.ts +2 -0
  206. package/src/commands/id.ts +16 -0
  207. package/src/commands/index.ts +98 -99
  208. package/src/commands/kill.ts +14 -0
  209. package/src/commands/ln.ts +2 -0
  210. package/src/commands/ls.ts +2 -0
  211. package/src/commands/mkdir.ts +2 -0
  212. package/src/commands/mv.ts +2 -0
  213. package/src/commands/nano.ts +2 -0
  214. package/src/commands/neofetch.ts +2 -0
  215. package/src/commands/passwd.ts +2 -0
  216. package/src/commands/ping.ts +20 -0
  217. package/src/commands/ps.ts +19 -0
  218. package/src/commands/pwd.ts +2 -0
  219. package/src/commands/rm.ts +2 -0
  220. package/src/commands/sed.ts +45 -0
  221. package/src/commands/set.ts +19 -50
  222. package/src/commands/sh.ts +192 -43
  223. package/src/commands/sleep.ts +14 -0
  224. package/src/commands/sort.ts +37 -0
  225. package/src/commands/su.ts +2 -0
  226. package/src/commands/sudo.ts +2 -0
  227. package/src/commands/tail.ts +2 -0
  228. package/src/commands/tar.ts +58 -0
  229. package/src/commands/tee.ts +25 -0
  230. package/src/commands/touch.ts +2 -0
  231. package/src/commands/tr.ts +24 -0
  232. package/src/commands/tree.ts +2 -0
  233. package/src/commands/uname.ts +20 -0
  234. package/src/commands/uniq.ts +28 -0
  235. package/src/commands/unset.ts +5 -12
  236. package/src/commands/wc.ts +2 -0
  237. package/src/commands/wget.ts +2 -0
  238. package/src/commands/who.ts +2 -0
  239. package/src/commands/whoami.ts +2 -0
  240. package/src/commands/xargs.ts +17 -0
  241. package/src/types/commands.ts +14 -0
  242. package/src/types/pipeline.ts +23 -0
  243. package/standalone.js +92 -64
  244. package/standalone.js.map +4 -4
  245. package/tests/users.test.ts +5 -34
@@ -1,45 +1,238 @@
1
- import { getArg, getFlag } from "./command-helpers";
1
+ import { getArg, ifFlag } from "./command-helpers";
2
+ import { resolvePath } from "./helpers";
2
3
  import { runCommand } from "./index";
3
- /** Simple shell script executor with basic variable support */
4
+ /** Expand $VAR and ${VAR:-default} in a line using the current env */
5
+ function expandVars(line, env, lastExit) {
6
+ return line
7
+ .replace(/\$\?/g, String(lastExit))
8
+ .replace(/\$\{([^}:]+):-([^}]*)\}/g, (_, n, d) => env[n] ?? d)
9
+ .replace(/\$\{([^}]+)\}/g, (_, n) => env[n] ?? "")
10
+ .replace(/\$([A-Za-z_][A-Za-z0-9_]*)/g, (_, n) => env[n] ?? "")
11
+ .replace(/^~(\/|$)/, `${env.HOME ?? "/home/user"}$1`);
12
+ }
13
+ /** Very small shell interpreter: supports if/elif/else/fi, for/do/done, while/do/done */
14
+ function parseBlocks(lines) {
15
+ const blocks = [];
16
+ let i = 0;
17
+ while (i < lines.length) {
18
+ const line = lines[i].trim();
19
+ if (!line || line.startsWith("#")) {
20
+ i++;
21
+ continue;
22
+ }
23
+ if (line.startsWith("if ") || line === "if") {
24
+ const cond = line.replace(/^if\s+/, "").replace(/;\s*then\s*$/, "").trim();
25
+ const thenLines = [];
26
+ const elifBlocks = [];
27
+ const elseLines = [];
28
+ let section = "then";
29
+ let elifCond = "";
30
+ i++;
31
+ while (i < lines.length && lines[i]?.trim() !== "fi") {
32
+ const l = lines[i].trim();
33
+ if (l.startsWith("elif ")) {
34
+ section = "elif";
35
+ elifCond = l.replace(/^elif\s+/, "").replace(/;\s*then\s*$/, "").trim();
36
+ elifBlocks.push({ cond: elifCond, body: [] });
37
+ }
38
+ else if (l === "else") {
39
+ section = "else";
40
+ }
41
+ else if (l !== "then") {
42
+ if (section === "then")
43
+ thenLines.push(l);
44
+ else if (section === "elif" && elifBlocks.length > 0)
45
+ elifBlocks[elifBlocks.length - 1].body.push(l);
46
+ else
47
+ elseLines.push(l);
48
+ }
49
+ i++;
50
+ }
51
+ blocks.push({ type: "if", cond, then: thenLines, elif: elifBlocks, else_: elseLines });
52
+ }
53
+ else if (line.startsWith("for ")) {
54
+ const m = line.match(/^for\s+(\w+)\s+in\s+(.+?)(?:\s*;\s*do)?$/);
55
+ if (m) {
56
+ const body = [];
57
+ i++;
58
+ while (i < lines.length && lines[i]?.trim() !== "done") {
59
+ const l = lines[i].trim();
60
+ if (l !== "do")
61
+ body.push(l);
62
+ i++;
63
+ }
64
+ blocks.push({ type: "for", var: m[1], list: m[2], body });
65
+ }
66
+ else {
67
+ blocks.push({ type: "cmd", line });
68
+ }
69
+ }
70
+ else if (line.startsWith("while ")) {
71
+ const cond = line.replace(/^while\s+/, "").replace(/;\s*do\s*$/, "").trim();
72
+ const body = [];
73
+ i++;
74
+ while (i < lines.length && lines[i]?.trim() !== "done") {
75
+ const l = lines[i].trim();
76
+ if (l !== "do")
77
+ body.push(l);
78
+ i++;
79
+ }
80
+ blocks.push({ type: "while", cond, body });
81
+ }
82
+ else {
83
+ blocks.push({ type: "cmd", line });
84
+ }
85
+ i++;
86
+ }
87
+ return blocks;
88
+ }
89
+ async function evalCondition(cond, ctx) {
90
+ const expanded = expandVars(cond, ctx.env.vars, ctx.env.lastExitCode);
91
+ // test -f / test -d / [ ... ]
92
+ const testMatch = expanded.match(/^\[?\s*(.+?)\s*\]?$/);
93
+ if (testMatch) {
94
+ const expr = testMatch[1];
95
+ // -f file
96
+ const fTest = expr.match(/^-([fdeznr])\s+(.+)$/);
97
+ if (fTest) {
98
+ const [, flag, arg] = fTest;
99
+ const p = resolvePath(ctx.cwd, arg);
100
+ if (flag === "f")
101
+ return ctx.shell.vfs.exists(p) && ctx.shell.vfs.stat(p).type === "file";
102
+ if (flag === "d")
103
+ return ctx.shell.vfs.exists(p) && ctx.shell.vfs.stat(p).type === "directory";
104
+ if (flag === "e")
105
+ return ctx.shell.vfs.exists(p);
106
+ if (flag === "z")
107
+ return (arg ?? "").length === 0;
108
+ if (flag === "n")
109
+ return (arg ?? "").length > 0;
110
+ }
111
+ // string comparison
112
+ const cmpMatch = expr.match(/^"?([^"]*)"?\s*(==|!=|=|<|>)\s*"?([^"]*)"?$/);
113
+ if (cmpMatch) {
114
+ const [, a, op, b] = cmpMatch;
115
+ if (op === "==" || op === "=")
116
+ return a === b;
117
+ if (op === "!=")
118
+ return a !== b;
119
+ }
120
+ // numeric
121
+ const numMatch = expr.match(/^(\S+)\s+(-eq|-ne|-lt|-le|-gt|-ge)\s+(\S+)$/);
122
+ if (numMatch) {
123
+ const [, a, op, b] = numMatch;
124
+ const na = Number(a), nb = Number(b);
125
+ if (op === "-eq")
126
+ return na === nb;
127
+ if (op === "-ne")
128
+ return na !== nb;
129
+ if (op === "-lt")
130
+ return na < nb;
131
+ if (op === "-le")
132
+ return na <= nb;
133
+ if (op === "-gt")
134
+ return na > nb;
135
+ if (op === "-ge")
136
+ return na >= nb;
137
+ }
138
+ }
139
+ // fallback: run command and check exit code
140
+ const r = await runCommand(expanded, ctx.authUser, ctx.hostname, ctx.mode, ctx.cwd, ctx.shell, undefined, ctx.env);
141
+ return (r.exitCode ?? 0) === 0;
142
+ }
143
+ async function runBlocks(blocks, ctx) {
144
+ let lastResult = { exitCode: 0 };
145
+ let output = "";
146
+ for (const block of blocks) {
147
+ if (block.type === "cmd") {
148
+ const expanded = expandVars(block.line, ctx.env.vars, ctx.env.lastExitCode);
149
+ const r = await runCommand(expanded, ctx.authUser, ctx.hostname, ctx.mode, ctx.cwd, ctx.shell, undefined, ctx.env);
150
+ ctx.env.lastExitCode = r.exitCode ?? 0;
151
+ if (r.stdout)
152
+ output += `${r.stdout}\n`;
153
+ if (r.stderr)
154
+ return { ...r, stdout: output.trim() };
155
+ lastResult = r;
156
+ }
157
+ else if (block.type === "if") {
158
+ let ran = false;
159
+ if (await evalCondition(block.cond, ctx)) {
160
+ const sub = await runBlocks(parseBlocks(block.then), ctx);
161
+ if (sub.stdout)
162
+ output += `${sub.stdout}\n`;
163
+ ran = true;
164
+ }
165
+ else {
166
+ for (const elif of block.elif) {
167
+ if (await evalCondition(elif.cond, ctx)) {
168
+ const sub = await runBlocks(parseBlocks(elif.body), ctx);
169
+ if (sub.stdout)
170
+ output += `${sub.stdout}\n`;
171
+ ran = true;
172
+ break;
173
+ }
174
+ }
175
+ if (!ran && block.else_.length > 0) {
176
+ const sub = await runBlocks(parseBlocks(block.else_), ctx);
177
+ if (sub.stdout)
178
+ output += `${sub.stdout}\n`;
179
+ }
180
+ }
181
+ }
182
+ else if (block.type === "for") {
183
+ const listExpanded = expandVars(block.list, ctx.env.vars, ctx.env.lastExitCode);
184
+ const items = listExpanded.trim().split(/\s+/);
185
+ for (const item of items) {
186
+ ctx.env.vars[block.var] = item;
187
+ const sub = await runBlocks(parseBlocks(block.body), ctx);
188
+ if (sub.stdout)
189
+ output += `${sub.stdout}\n`;
190
+ if (sub.closeSession)
191
+ return sub;
192
+ }
193
+ }
194
+ else if (block.type === "while") {
195
+ let iterations = 0;
196
+ while (iterations < 1000 && await evalCondition(block.cond, ctx)) {
197
+ const sub = await runBlocks(parseBlocks(block.body), ctx);
198
+ if (sub.stdout)
199
+ output += `${sub.stdout}\n`;
200
+ if (sub.closeSession)
201
+ return sub;
202
+ iterations++;
203
+ }
204
+ }
205
+ }
206
+ return { ...lastResult, stdout: output.trim() || lastResult.stdout };
207
+ }
4
208
  export const shCommand = {
5
209
  name: "sh",
6
- params: ["-c <script>", "[<file>]"],
7
210
  aliases: ["bash"],
211
+ description: "Execute shell script or command",
212
+ category: "shell",
213
+ params: ["-c <script>", "[<file>]"],
8
214
  run: async (ctx) => {
9
- const { args, authUser, hostname, mode, cwd } = ctx;
10
- // Handle -c option: sh -c "command"
11
- if (getFlag(args, "-c") && args.length >= 2) {
215
+ const { args, authUser, shell, cwd } = ctx;
216
+ // sh -c "inline script"
217
+ if (ifFlag(args, "-c")) {
12
218
  const script = getArg(args, 1) ?? "";
13
- if (!script) {
219
+ if (!script)
14
220
  return { stderr: "sh: -c requires a script", exitCode: 1 };
15
- }
16
- const scriptArgs = args.slice(2);
17
- // Split by semicolon and newline
18
- const lines = script
19
- .split(/[;\n]/)
20
- .map((line) => line.trim())
21
- .filter((line) => line && !line.startsWith("#"));
22
- let output = "";
23
- const exitCode = 0;
24
- for (const line of lines) {
25
- // Simple variable substitution
26
- let command = line;
27
- for (let i = 0; i < scriptArgs.length; i++) {
28
- const arg = scriptArgs[i] ?? "";
29
- command = command.replaceAll(`$${i}`, arg);
30
- }
31
- command = command.replaceAll("$@", scriptArgs.join(" "));
32
- // Execute the command
33
- const result = await Promise.resolve(runCommand(command, authUser, hostname, mode, cwd, ctx.shell));
34
- if (result.stdout) {
35
- output += `${result.stdout}\n`;
36
- }
37
- if (result.stderr) {
38
- return { stderr: result.stderr, exitCode: result.exitCode ?? 1 };
39
- }
40
- }
41
- return { stdout: output.trim(), exitCode };
221
+ const lines = script.split(/[;\n]/).map((l) => l.trim()).filter((l) => l && !l.startsWith("#"));
222
+ const blocks = parseBlocks(lines);
223
+ return runBlocks(blocks, ctx);
224
+ }
225
+ // sh <file>
226
+ const fileArg = args[0];
227
+ if (fileArg) {
228
+ const p = resolvePath(cwd, fileArg);
229
+ if (!shell.vfs.exists(p))
230
+ return { stderr: `sh: ${fileArg}: No such file or directory`, exitCode: 1 };
231
+ const content = shell.vfs.readFile(p);
232
+ const lines = content.split("\n").map((l) => l.trim()).filter((l) => l && !l.startsWith("#"));
233
+ const blocks = parseBlocks(lines);
234
+ return runBlocks(blocks, ctx);
42
235
  }
43
- return { stderr: "sh: invalid usage", exitCode: 1 };
236
+ return { stderr: "sh: invalid usage. Use: sh -c 'cmd' or sh <file>", exitCode: 1 };
44
237
  },
45
238
  };
@@ -0,0 +1,3 @@
1
+ import type { ShellModule } from "../types/commands";
2
+ export declare const sleepCommand: ShellModule;
3
+ //# sourceMappingURL=sleep.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sleep.d.ts","sourceRoot":"","sources":["../../src/commands/sleep.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAErD,eAAO,MAAM,YAAY,EAAE,WAW1B,CAAC"}
@@ -0,0 +1,13 @@
1
+ export const sleepCommand = {
2
+ name: "sleep",
3
+ description: "Delay execution",
4
+ category: "system",
5
+ params: ["<seconds>"],
6
+ run: async ({ args }) => {
7
+ const secs = parseFloat(args[0] ?? "1");
8
+ if (Number.isNaN(secs) || secs < 0)
9
+ return { stderr: "sleep: invalid time", exitCode: 1 };
10
+ await new Promise((r) => setTimeout(r, secs * 1000));
11
+ return { exitCode: 0 };
12
+ },
13
+ };
@@ -0,0 +1,3 @@
1
+ import type { ShellModule } from "../types/commands";
2
+ export declare const sortCommand: ShellModule;
3
+ //# sourceMappingURL=sort.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sort.d.ts","sourceRoot":"","sources":["../../src/commands/sort.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAIrD,eAAO,MAAM,WAAW,EAAE,WAgCzB,CAAC"}
@@ -0,0 +1,37 @@
1
+ import { ifFlag } from "./command-helpers";
2
+ import { assertPathAccess, resolvePath } from "./helpers";
3
+ export const sortCommand = {
4
+ name: "sort",
5
+ description: "Sort lines of text",
6
+ category: "text",
7
+ params: ["[-r] [-n] [-u] [-k <col>] [file...]"],
8
+ run: ({ authUser, shell, cwd, args, stdin }) => {
9
+ const reverse = ifFlag(args, ["-r"]);
10
+ const numeric = ifFlag(args, ["-n"]);
11
+ const unique = ifFlag(args, ["-u"]);
12
+ const files = args.filter((a) => !a.startsWith("-"));
13
+ const getContent = () => {
14
+ if (files.length > 0) {
15
+ return files.map((f) => {
16
+ try {
17
+ assertPathAccess(authUser, resolvePath(cwd, f), "sort");
18
+ return shell.vfs.readFile(resolvePath(cwd, f));
19
+ }
20
+ catch {
21
+ return "";
22
+ }
23
+ }).join("\n");
24
+ }
25
+ return stdin ?? "";
26
+ };
27
+ const lines = getContent().split("\n").filter(Boolean);
28
+ const sorted = [...lines].sort((a, b) => {
29
+ if (numeric)
30
+ return Number(a) - Number(b);
31
+ return a.localeCompare(b);
32
+ });
33
+ const result = reverse ? sorted.reverse() : sorted;
34
+ const out = unique ? [...new Set(result)] : result;
35
+ return { stdout: out.join("\n"), exitCode: 0 };
36
+ },
37
+ };
@@ -1 +1 @@
1
- {"version":3,"file":"su.d.ts","sourceRoot":"","sources":["../../src/commands/su.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAGrD,eAAO,MAAM,SAAS,EAAE,WA4BvB,CAAC"}
1
+ {"version":3,"file":"su.d.ts","sourceRoot":"","sources":["../../src/commands/su.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAGrD,eAAO,MAAM,SAAS,EAAE,WA8BvB,CAAC"}
@@ -1,6 +1,8 @@
1
1
  import { getArg } from "./command-helpers";
2
2
  export const suCommand = {
3
3
  name: "su",
4
+ description: "Switch user",
5
+ category: "users",
4
6
  params: ["- <username>"],
5
7
  run: ({ authUser, shell, args }) => {
6
8
  const users = shell.users;
@@ -1 +1 @@
1
- {"version":3,"file":"sudo.d.ts","sourceRoot":"","sources":["../../src/commands/sudo.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAqBrD,eAAO,MAAM,WAAW,EAAE,WA+CzB,CAAC"}
1
+ {"version":3,"file":"sudo.d.ts","sourceRoot":"","sources":["../../src/commands/sudo.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAqBrD,eAAO,MAAM,WAAW,EAAE,WAiDzB,CAAC"}
@@ -12,6 +12,8 @@ function parseSudoArgs(args) {
12
12
  }
13
13
  export const sudoCommand = {
14
14
  name: "sudo",
15
+ description: "Execute as superuser",
16
+ category: "users",
15
17
  params: ["<command...>"],
16
18
  run: async ({ authUser, hostname, mode, cwd, shell, args }) => {
17
19
  const { targetUser, loginShell, commandLine } = parseSudoArgs(args);
@@ -1 +1 @@
1
- {"version":3,"file":"tail.d.ts","sourceRoot":"","sources":["../../src/commands/tail.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAIrD,eAAO,MAAM,WAAW,EAAE,WAgCzB,CAAC"}
1
+ {"version":3,"file":"tail.d.ts","sourceRoot":"","sources":["../../src/commands/tail.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAIrD,eAAO,MAAM,WAAW,EAAE,WAkCzB,CAAC"}
@@ -2,6 +2,8 @@ import { getFlag } from "./command-helpers";
2
2
  import { assertPathAccess, resolvePath } from "./helpers";
3
3
  export const tailCommand = {
4
4
  name: "tail",
5
+ description: "Output last lines",
6
+ category: "text",
5
7
  params: ["[-n <lines>] [file...]"],
6
8
  run: ({ authUser, shell, cwd, args, stdin }) => {
7
9
  const nArg = getFlag(args, ["-n"]);
@@ -0,0 +1,3 @@
1
+ import type { ShellModule } from "../types/commands";
2
+ export declare const tarCommand: ShellModule;
3
+ //# sourceMappingURL=tar.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tar.d.ts","sourceRoot":"","sources":["../../src/commands/tar.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAIrD,eAAO,MAAM,UAAU,EAAE,WAqDxB,CAAC"}
@@ -0,0 +1,64 @@
1
+ import { ifFlag } from "./command-helpers";
2
+ import { resolvePath } from "./helpers";
3
+ export const tarCommand = {
4
+ name: "tar",
5
+ description: "Archive utility",
6
+ category: "archive",
7
+ params: ["[-czf|-xzf|-tf] <archive> [files...]"],
8
+ run: ({ authUser, shell, cwd, args }) => {
9
+ const create = ifFlag(args, ["-c"]);
10
+ const extract = ifFlag(args, ["-x"]);
11
+ const list = ifFlag(args, ["-t"]);
12
+ const fFlag = args.findIndex((a) => a.includes("f"));
13
+ const archiveName = fFlag !== -1 ? args[fFlag + 1] : args.find((a) => a.endsWith(".tar") || a.endsWith(".tar.gz") || a.endsWith(".tgz"));
14
+ if (!archiveName)
15
+ return { stderr: "tar: no archive specified", exitCode: 1 };
16
+ const archivePath = resolvePath(cwd, archiveName);
17
+ if (create) {
18
+ const fileArgs = args.filter((a) => !a.startsWith("-") && a !== archiveName);
19
+ const entries = {};
20
+ for (const f of fileArgs) {
21
+ const p = resolvePath(cwd, f);
22
+ try {
23
+ const stat = shell.vfs.stat(p);
24
+ if (stat.type === "file")
25
+ entries[f] = shell.vfs.readFile(p);
26
+ else {
27
+ const walk = (dir, prefix) => {
28
+ for (const e of shell.vfs.list(dir)) {
29
+ const full = `${dir}/${e}`, rel = `${prefix}/${e}`;
30
+ const s = shell.vfs.stat(full);
31
+ if (s.type === "file")
32
+ entries[rel] = shell.vfs.readFile(full);
33
+ else
34
+ walk(full, rel);
35
+ }
36
+ };
37
+ walk(p, f);
38
+ }
39
+ }
40
+ catch {
41
+ return { stderr: `tar: ${f}: No such file or directory`, exitCode: 1 };
42
+ }
43
+ }
44
+ shell.writeFileAsUser(authUser, archivePath, JSON.stringify(entries));
45
+ return { exitCode: 0 };
46
+ }
47
+ if (list || extract) {
48
+ let entries;
49
+ try {
50
+ entries = JSON.parse(shell.vfs.readFile(archivePath));
51
+ }
52
+ catch {
53
+ return { stderr: `tar: ${archiveName}: cannot open archive`, exitCode: 1 };
54
+ }
55
+ if (list)
56
+ return { stdout: Object.keys(entries).join("\n"), exitCode: 0 };
57
+ for (const [name, content] of Object.entries(entries)) {
58
+ shell.writeFileAsUser(authUser, resolvePath(cwd, name), content);
59
+ }
60
+ return { exitCode: 0 };
61
+ }
62
+ return { stderr: "tar: must specify -c, -x, or -t", exitCode: 1 };
63
+ },
64
+ };
@@ -0,0 +1,3 @@
1
+ import type { ShellModule } from "../types/commands";
2
+ export declare const teeCommand: ShellModule;
3
+ //# sourceMappingURL=tee.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tee.d.ts","sourceRoot":"","sources":["../../src/commands/tee.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAIrD,eAAO,MAAM,UAAU,EAAE,WAoBxB,CAAC"}
@@ -0,0 +1,29 @@
1
+ import { ifFlag } from "./command-helpers";
2
+ import { resolvePath } from "./helpers";
3
+ export const teeCommand = {
4
+ name: "tee",
5
+ description: "Read stdin, write to stdout and files",
6
+ category: "text",
7
+ params: ["[-a] <file...>"],
8
+ run: ({ authUser, shell, cwd, args, stdin }) => {
9
+ const append = ifFlag(args, ["-a"]);
10
+ const files = args.filter((a) => !a.startsWith("-"));
11
+ const input = stdin ?? "";
12
+ for (const f of files) {
13
+ const p = resolvePath(cwd, f);
14
+ if (append) {
15
+ const existing = (() => { try {
16
+ return shell.vfs.readFile(p);
17
+ }
18
+ catch {
19
+ return "";
20
+ } })();
21
+ shell.writeFileAsUser(authUser, p, existing + input);
22
+ }
23
+ else {
24
+ shell.writeFileAsUser(authUser, p, input);
25
+ }
26
+ }
27
+ return { stdout: input, exitCode: 0 };
28
+ },
29
+ };
@@ -1 +1 @@
1
- {"version":3,"file":"touch.d.ts","sourceRoot":"","sources":["../../src/commands/touch.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAGrD,eAAO,MAAM,YAAY,EAAE,WAiB1B,CAAC"}
1
+ {"version":3,"file":"touch.d.ts","sourceRoot":"","sources":["../../src/commands/touch.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAGrD,eAAO,MAAM,YAAY,EAAE,WAmB1B,CAAC"}
@@ -1,6 +1,8 @@
1
1
  import { assertPathAccess, resolvePath } from "./helpers";
2
2
  export const touchCommand = {
3
3
  name: "touch",
4
+ description: "Create or update files",
5
+ category: "files",
4
6
  params: ["<file>"],
5
7
  run: ({ authUser, shell, cwd, args }) => {
6
8
  if (args.length === 0) {
@@ -0,0 +1,3 @@
1
+ import type { ShellModule } from "../types/commands";
2
+ export declare const trCommand: ShellModule;
3
+ //# sourceMappingURL=tr.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tr.d.ts","sourceRoot":"","sources":["../../src/commands/tr.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAGrD,eAAO,MAAM,SAAS,EAAE,WAoBvB,CAAC"}
@@ -0,0 +1,24 @@
1
+ import { ifFlag } from "./command-helpers";
2
+ export const trCommand = {
3
+ name: "tr",
4
+ description: "Translate or delete characters",
5
+ category: "text",
6
+ params: ["[-d] <set1> [set2]"],
7
+ run: ({ args, stdin }) => {
8
+ const del = ifFlag(args, ["-d"]);
9
+ const positionals = args.filter((a) => !a.startsWith("-"));
10
+ const set1 = positionals[0] ?? "";
11
+ const set2 = positionals[1] ?? "";
12
+ let input = stdin ?? "";
13
+ if (del) {
14
+ for (const c of set1)
15
+ input = input.split(c).join("");
16
+ }
17
+ else if (set2) {
18
+ for (let i = 0; i < set1.length; i++) {
19
+ input = input.split(set1[i]).join(set2[i] ?? set2[set2.length - 1] ?? "");
20
+ }
21
+ }
22
+ return { stdout: input, exitCode: 0 };
23
+ },
24
+ };
@@ -1 +1 @@
1
- {"version":3,"file":"tree.d.ts","sourceRoot":"","sources":["../../src/commands/tree.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAIrD,eAAO,MAAM,WAAW,EAAE,WAQzB,CAAC"}
1
+ {"version":3,"file":"tree.d.ts","sourceRoot":"","sources":["../../src/commands/tree.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAIrD,eAAO,MAAM,WAAW,EAAE,WAUzB,CAAC"}
@@ -2,6 +2,8 @@ import { getArg } from "./command-helpers";
2
2
  import { assertPathAccess, resolvePath } from "./helpers";
3
3
  export const treeCommand = {
4
4
  name: "tree",
5
+ description: "Display directory tree",
6
+ category: "navigation",
5
7
  params: ["[path]"],
6
8
  run: ({ authUser, shell, cwd, args }) => {
7
9
  const target = resolvePath(cwd, getArg(args, 0) ?? cwd);
@@ -0,0 +1,3 @@
1
+ import type { ShellModule } from "../types/commands";
2
+ export declare const unameCommand: ShellModule;
3
+ //# sourceMappingURL=uname.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"uname.d.ts","sourceRoot":"","sources":["../../src/commands/uname.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAGrD,eAAO,MAAM,YAAY,EAAE,WAgB1B,CAAC"}
@@ -0,0 +1,21 @@
1
+ import { ifFlag } from "./command-helpers";
2
+ export const unameCommand = {
3
+ name: "uname",
4
+ description: "Print system information",
5
+ category: "system",
6
+ params: ["[-a] [-s] [-r] [-m]"],
7
+ run: ({ shell, args }) => {
8
+ const all = ifFlag(args, ["-a"]);
9
+ const sysname = "Linux";
10
+ const release = shell.properties?.kernel ?? "5.15.0";
11
+ const machine = shell.properties?.arch ?? "x86_64";
12
+ const hostname = shell.hostname;
13
+ if (all)
14
+ return { stdout: `${sysname} ${hostname} ${release} #1 SMP ${machine} GNU/Linux`, exitCode: 0 };
15
+ if (ifFlag(args, ["-r"]))
16
+ return { stdout: release, exitCode: 0 };
17
+ if (ifFlag(args, ["-m"]))
18
+ return { stdout: machine, exitCode: 0 };
19
+ return { stdout: sysname, exitCode: 0 };
20
+ },
21
+ };
@@ -0,0 +1,3 @@
1
+ import type { ShellModule } from "../types/commands";
2
+ export declare const uniqCommand: ShellModule;
3
+ //# sourceMappingURL=uniq.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"uniq.d.ts","sourceRoot":"","sources":["../../src/commands/uniq.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAGrD,eAAO,MAAM,WAAW,EAAE,WAwBzB,CAAC"}
@@ -0,0 +1,33 @@
1
+ import { ifFlag } from "./command-helpers";
2
+ export const uniqCommand = {
3
+ name: "uniq",
4
+ description: "Report or filter out repeated lines",
5
+ category: "text",
6
+ params: ["[-c] [-d] [-u] [file]"],
7
+ run: ({ args, stdin }) => {
8
+ const count = ifFlag(args, ["-c"]);
9
+ const dupOnly = ifFlag(args, ["-d"]);
10
+ const uniqOnly = ifFlag(args, ["-u"]);
11
+ const lines = (stdin ?? "").split("\n");
12
+ const out = [];
13
+ let i = 0;
14
+ while (i < lines.length) {
15
+ let j = i;
16
+ while (j < lines.length && lines[j] === lines[i])
17
+ j++;
18
+ const n = j - i;
19
+ const line = lines[i];
20
+ if (dupOnly && n === 1) {
21
+ i = j;
22
+ continue;
23
+ }
24
+ if (uniqOnly && n > 1) {
25
+ i = j;
26
+ continue;
27
+ }
28
+ out.push(count ? `${String(n).padStart(4)} ${line}` : line);
29
+ i = j;
30
+ }
31
+ return { stdout: out.join("\n"), exitCode: 0 };
32
+ },
33
+ };
@@ -1 +1 @@
1
- {"version":3,"file":"unset.d.ts","sourceRoot":"","sources":["../../src/commands/unset.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAGrD,eAAO,MAAM,YAAY,EAAE,WAe1B,CAAC"}
1
+ {"version":3,"file":"unset.d.ts","sourceRoot":"","sources":["../../src/commands/unset.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAErD,eAAO,MAAM,YAAY,EAAE,WAS1B,CAAC"}