typescript-virtual-container 1.2.8 → 1.3.0

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 (307) hide show
  1. package/.vscode/settings.json +0 -1
  2. package/README.md +462 -44
  3. package/biome.json +7 -0
  4. package/dist/SSHMimic/exec.d.ts.map +1 -1
  5. package/dist/SSHMimic/executor.d.ts.map +1 -1
  6. package/dist/SSHMimic/executor.js +35 -21
  7. package/dist/SSHMimic/index.d.ts.map +1 -1
  8. package/dist/SSHMimic/index.js +20 -6
  9. package/dist/VirtualFileSystem/binaryPack.d.ts.map +1 -1
  10. package/dist/VirtualFileSystem/binaryPack.js +29 -6
  11. package/dist/VirtualFileSystem/index.d.ts.map +1 -1
  12. package/dist/VirtualFileSystem/index.js +36 -13
  13. package/dist/VirtualPackageManager/index.d.ts +202 -0
  14. package/dist/VirtualPackageManager/index.d.ts.map +1 -0
  15. package/dist/VirtualPackageManager/index.js +825 -0
  16. package/dist/VirtualShell/index.d.ts +93 -12
  17. package/dist/VirtualShell/index.d.ts.map +1 -1
  18. package/dist/VirtualShell/index.js +95 -13
  19. package/dist/VirtualShell/shell.d.ts.map +1 -1
  20. package/dist/VirtualShell/shell.js +3 -1
  21. package/dist/VirtualShell/shellParser.d.ts.map +1 -1
  22. package/dist/VirtualUserManager/index.d.ts +52 -20
  23. package/dist/VirtualUserManager/index.d.ts.map +1 -1
  24. package/dist/VirtualUserManager/index.js +54 -20
  25. package/dist/commands/adduser.d.ts +6 -0
  26. package/dist/commands/adduser.d.ts.map +1 -1
  27. package/dist/commands/adduser.js +6 -0
  28. package/dist/commands/alias.d.ts +9 -0
  29. package/dist/commands/alias.d.ts.map +1 -0
  30. package/dist/commands/alias.js +63 -0
  31. package/dist/commands/apt.d.ts +9 -0
  32. package/dist/commands/apt.d.ts.map +1 -0
  33. package/dist/commands/apt.js +205 -0
  34. package/dist/commands/awk.d.ts +11 -0
  35. package/dist/commands/awk.d.ts.map +1 -1
  36. package/dist/commands/awk.js +15 -2
  37. package/dist/commands/base64.d.ts +5 -0
  38. package/dist/commands/base64.d.ts.map +1 -1
  39. package/dist/commands/base64.js +9 -1
  40. package/dist/commands/cat.d.ts +5 -0
  41. package/dist/commands/cat.d.ts.map +1 -1
  42. package/dist/commands/cat.js +35 -8
  43. package/dist/commands/cd.d.ts +5 -0
  44. package/dist/commands/cd.d.ts.map +1 -1
  45. package/dist/commands/cd.js +5 -0
  46. package/dist/commands/chmod.d.ts +5 -0
  47. package/dist/commands/chmod.d.ts.map +1 -1
  48. package/dist/commands/chmod.js +57 -3
  49. package/dist/commands/command-helpers.d.ts +78 -4
  50. package/dist/commands/command-helpers.d.ts.map +1 -1
  51. package/dist/commands/command-helpers.js +78 -4
  52. package/dist/commands/cp.d.ts +5 -0
  53. package/dist/commands/cp.d.ts.map +1 -1
  54. package/dist/commands/cp.js +5 -0
  55. package/dist/commands/curl.d.ts +5 -0
  56. package/dist/commands/curl.d.ts.map +1 -1
  57. package/dist/commands/curl.js +106 -26
  58. package/dist/commands/cut.d.ts +5 -0
  59. package/dist/commands/cut.d.ts.map +1 -1
  60. package/dist/commands/cut.js +8 -1
  61. package/dist/commands/date.d.ts +5 -0
  62. package/dist/commands/date.d.ts.map +1 -1
  63. package/dist/commands/date.js +7 -1
  64. package/dist/commands/declare.d.ts +3 -0
  65. package/dist/commands/declare.d.ts.map +1 -0
  66. package/dist/commands/declare.js +39 -0
  67. package/dist/commands/diff.d.ts +5 -0
  68. package/dist/commands/diff.d.ts.map +1 -1
  69. package/dist/commands/diff.js +5 -0
  70. package/dist/commands/dpkg.d.ts +9 -0
  71. package/dist/commands/dpkg.d.ts.map +1 -0
  72. package/dist/commands/dpkg.js +161 -0
  73. package/dist/commands/du.d.ts.map +1 -1
  74. package/dist/commands/du.js +8 -2
  75. package/dist/commands/echo.d.ts +5 -0
  76. package/dist/commands/echo.d.ts.map +1 -1
  77. package/dist/commands/echo.js +33 -12
  78. package/dist/commands/env.d.ts +5 -0
  79. package/dist/commands/env.d.ts.map +1 -1
  80. package/dist/commands/env.js +11 -1
  81. package/dist/commands/exit.d.ts +5 -0
  82. package/dist/commands/exit.d.ts.map +1 -1
  83. package/dist/commands/exit.js +12 -2
  84. package/dist/commands/export.d.ts.map +1 -1
  85. package/dist/commands/export.js +3 -1
  86. package/dist/commands/find.d.ts +5 -0
  87. package/dist/commands/find.d.ts.map +1 -1
  88. package/dist/commands/find.js +5 -0
  89. package/dist/commands/free.d.ts +8 -0
  90. package/dist/commands/free.d.ts.map +1 -0
  91. package/dist/commands/free.js +43 -0
  92. package/dist/commands/grep.d.ts +5 -0
  93. package/dist/commands/grep.d.ts.map +1 -1
  94. package/dist/commands/grep.js +12 -2
  95. package/dist/commands/gzip.d.ts +5 -0
  96. package/dist/commands/gzip.d.ts.map +1 -1
  97. package/dist/commands/gzip.js +18 -2
  98. package/dist/commands/head.d.ts +5 -0
  99. package/dist/commands/head.d.ts.map +1 -1
  100. package/dist/commands/head.js +5 -0
  101. package/dist/commands/help.d.ts.map +1 -1
  102. package/dist/commands/help.js +98 -45
  103. package/dist/commands/helpers.d.ts +3 -0
  104. package/dist/commands/helpers.d.ts.map +1 -1
  105. package/dist/commands/helpers.js +3 -0
  106. package/dist/commands/history.d.ts +8 -0
  107. package/dist/commands/history.d.ts.map +1 -0
  108. package/dist/commands/history.js +26 -0
  109. package/dist/commands/hostname.d.ts +5 -0
  110. package/dist/commands/hostname.d.ts.map +1 -1
  111. package/dist/commands/hostname.js +5 -0
  112. package/dist/commands/id.d.ts.map +1 -1
  113. package/dist/commands/id.js +4 -1
  114. package/dist/commands/index.d.ts +2 -10
  115. package/dist/commands/index.d.ts.map +1 -1
  116. package/dist/commands/index.js +2 -231
  117. package/dist/commands/ls.d.ts.map +1 -1
  118. package/dist/commands/ls.js +6 -3
  119. package/dist/commands/lsb-release.d.ts +3 -0
  120. package/dist/commands/lsb-release.d.ts.map +1 -0
  121. package/dist/commands/lsb-release.js +56 -0
  122. package/dist/commands/man.d.ts +3 -0
  123. package/dist/commands/man.d.ts.map +1 -0
  124. package/dist/commands/man.js +155 -0
  125. package/dist/commands/nano.js +1 -1
  126. package/dist/commands/neofetch.d.ts.map +1 -1
  127. package/dist/commands/neofetch.js +6 -1
  128. package/dist/commands/node.d.ts +9 -0
  129. package/dist/commands/node.d.ts.map +1 -0
  130. package/dist/commands/node.js +316 -0
  131. package/dist/commands/npm.d.ts +19 -0
  132. package/dist/commands/npm.d.ts.map +1 -0
  133. package/dist/commands/npm.js +109 -0
  134. package/dist/commands/ping.d.ts.map +1 -1
  135. package/dist/commands/ping.js +7 -2
  136. package/dist/commands/printf.d.ts +3 -0
  137. package/dist/commands/printf.d.ts.map +1 -0
  138. package/dist/commands/printf.js +113 -0
  139. package/dist/commands/ps.d.ts.map +1 -1
  140. package/dist/commands/ps.js +30 -6
  141. package/dist/commands/python.d.ts +30 -0
  142. package/dist/commands/python.d.ts.map +1 -0
  143. package/dist/commands/python.js +2058 -0
  144. package/dist/commands/read.d.ts +3 -0
  145. package/dist/commands/read.d.ts.map +1 -0
  146. package/dist/commands/read.js +34 -0
  147. package/dist/commands/registry.d.ts +8 -0
  148. package/dist/commands/registry.d.ts.map +1 -0
  149. package/dist/commands/registry.js +229 -0
  150. package/dist/commands/runtime.d.ts +6 -0
  151. package/dist/commands/runtime.d.ts.map +1 -0
  152. package/dist/commands/runtime.js +280 -0
  153. package/dist/commands/sed.d.ts.map +1 -1
  154. package/dist/commands/sed.js +11 -3
  155. package/dist/commands/set.d.ts.map +1 -1
  156. package/dist/commands/set.js +9 -3
  157. package/dist/commands/sh.d.ts.map +1 -1
  158. package/dist/commands/sh.js +69 -30
  159. package/dist/commands/shift.d.ts +5 -0
  160. package/dist/commands/shift.d.ts.map +1 -0
  161. package/dist/commands/shift.js +52 -0
  162. package/dist/commands/sleep.d.ts.map +1 -1
  163. package/dist/commands/sort.d.ts.map +1 -1
  164. package/dist/commands/sort.js +4 -2
  165. package/dist/commands/source.d.ts +3 -0
  166. package/dist/commands/source.d.ts.map +1 -0
  167. package/dist/commands/source.js +34 -0
  168. package/dist/commands/sudo.js +1 -1
  169. package/dist/commands/tar.d.ts.map +1 -1
  170. package/dist/commands/tar.js +11 -3
  171. package/dist/commands/tee.d.ts.map +1 -1
  172. package/dist/commands/tee.js +8 -6
  173. package/dist/commands/test.d.ts +3 -0
  174. package/dist/commands/test.d.ts.map +1 -0
  175. package/dist/commands/test.js +114 -0
  176. package/dist/commands/tr.d.ts.map +1 -1
  177. package/dist/commands/tr.js +3 -1
  178. package/dist/commands/true.d.ts +4 -0
  179. package/dist/commands/true.d.ts.map +1 -0
  180. package/dist/commands/true.js +14 -0
  181. package/dist/commands/type.d.ts +3 -0
  182. package/dist/commands/type.d.ts.map +1 -0
  183. package/dist/commands/type.js +34 -0
  184. package/dist/commands/uname.d.ts.map +1 -1
  185. package/dist/commands/uname.js +4 -1
  186. package/dist/commands/uniq.d.ts.map +1 -1
  187. package/dist/commands/uptime.d.ts +3 -0
  188. package/dist/commands/uptime.d.ts.map +1 -0
  189. package/dist/commands/uptime.js +43 -0
  190. package/dist/commands/wget.d.ts.map +1 -1
  191. package/dist/commands/wget.js +92 -96
  192. package/dist/commands/which.d.ts +3 -0
  193. package/dist/commands/which.d.ts.map +1 -0
  194. package/dist/commands/which.js +32 -0
  195. package/dist/commands/xargs.d.ts.map +1 -1
  196. package/dist/commands/xargs.js +1 -1
  197. package/dist/index.d.ts +15 -11
  198. package/dist/index.d.ts.map +1 -1
  199. package/dist/index.js +9 -8
  200. package/dist/modules/linuxRootfs.d.ts +41 -0
  201. package/dist/modules/linuxRootfs.d.ts.map +1 -0
  202. package/dist/modules/linuxRootfs.js +440 -0
  203. package/dist/modules/neofetch.d.ts.map +1 -1
  204. package/dist/modules/neofetch.js +1 -0
  205. package/dist/standalone-wo-sftp.d.ts +2 -0
  206. package/dist/standalone-wo-sftp.d.ts.map +1 -0
  207. package/dist/standalone-wo-sftp.js +30 -0
  208. package/dist/utils/expand.d.ts +50 -0
  209. package/dist/utils/expand.d.ts.map +1 -0
  210. package/dist/utils/expand.js +183 -0
  211. package/dist/utils/vfsDiff.d.ts +90 -0
  212. package/dist/utils/vfsDiff.d.ts.map +1 -0
  213. package/dist/utils/vfsDiff.js +177 -0
  214. package/package.json +3 -1
  215. package/src/SSHMimic/exec.ts +10 -1
  216. package/src/SSHMimic/executor.ts +105 -21
  217. package/src/SSHMimic/index.ts +49 -15
  218. package/src/VirtualFileSystem/binaryPack.ts +35 -8
  219. package/src/VirtualFileSystem/index.ts +78 -28
  220. package/src/VirtualPackageManager/index.ts +979 -0
  221. package/src/VirtualShell/index.ts +133 -14
  222. package/src/VirtualShell/shell.ts +23 -3
  223. package/src/VirtualShell/shellParser.ts +134 -36
  224. package/src/VirtualUserManager/index.ts +62 -22
  225. package/src/commands/adduser.ts +6 -0
  226. package/src/commands/alias.ts +64 -0
  227. package/src/commands/apt.ts +228 -0
  228. package/src/commands/awk.ts +20 -6
  229. package/src/commands/base64.ts +13 -2
  230. package/src/commands/cat.ts +40 -8
  231. package/src/commands/cd.ts +5 -0
  232. package/src/commands/chmod.ts +53 -3
  233. package/src/commands/command-helpers.ts +78 -4
  234. package/src/commands/cp.ts +5 -0
  235. package/src/commands/curl.ts +118 -33
  236. package/src/commands/cut.ts +8 -1
  237. package/src/commands/date.ts +7 -1
  238. package/src/commands/declare.ts +44 -0
  239. package/src/commands/diff.ts +17 -3
  240. package/src/commands/dpkg.ts +180 -0
  241. package/src/commands/du.ts +17 -5
  242. package/src/commands/echo.ts +41 -12
  243. package/src/commands/env.ts +11 -1
  244. package/src/commands/exit.ts +12 -2
  245. package/src/commands/export.ts +3 -1
  246. package/src/commands/find.ts +5 -0
  247. package/src/commands/free.ts +47 -0
  248. package/src/commands/grep.ts +12 -2
  249. package/src/commands/gzip.ts +28 -4
  250. package/src/commands/head.ts +5 -0
  251. package/src/commands/help.ts +121 -47
  252. package/src/commands/helpers.ts +8 -0
  253. package/src/commands/history.ts +34 -0
  254. package/src/commands/hostname.ts +5 -0
  255. package/src/commands/id.ts +4 -1
  256. package/src/commands/index.ts +9 -255
  257. package/src/commands/ls.ts +6 -3
  258. package/src/commands/lsb-release.ts +58 -0
  259. package/src/commands/man.ts +166 -0
  260. package/src/commands/nano.ts +1 -1
  261. package/src/commands/neofetch.ts +6 -1
  262. package/src/commands/node.ts +341 -0
  263. package/src/commands/npm.ts +132 -0
  264. package/src/commands/ping.ts +10 -3
  265. package/src/commands/printf.ts +112 -0
  266. package/src/commands/ps.ts +40 -6
  267. package/src/commands/python.ts +2229 -0
  268. package/src/commands/read.ts +41 -0
  269. package/src/commands/registry.ts +244 -0
  270. package/src/commands/runtime.ts +353 -0
  271. package/src/commands/sed.ts +27 -9
  272. package/src/commands/set.ts +9 -3
  273. package/src/commands/sh.ts +170 -44
  274. package/src/commands/shift.ts +53 -0
  275. package/src/commands/sleep.ts +2 -1
  276. package/src/commands/sort.ts +10 -6
  277. package/src/commands/source.ts +47 -0
  278. package/src/commands/sudo.ts +1 -1
  279. package/src/commands/tar.ts +28 -7
  280. package/src/commands/tee.ts +7 -1
  281. package/src/commands/test.ts +135 -0
  282. package/src/commands/tr.ts +3 -1
  283. package/src/commands/true.ts +17 -0
  284. package/src/commands/type.ts +43 -0
  285. package/src/commands/uname.ts +5 -1
  286. package/src/commands/uniq.ts +8 -2
  287. package/src/commands/uptime.ts +49 -0
  288. package/src/commands/wget.ts +105 -119
  289. package/src/commands/which.ts +37 -0
  290. package/src/commands/xargs.ts +11 -2
  291. package/src/index.ts +27 -18
  292. package/src/modules/linuxRootfs.ts +642 -0
  293. package/src/modules/neofetch.ts +1 -0
  294. package/src/standalone-wo-sftp.ts +38 -0
  295. package/src/utils/expand.ts +238 -0
  296. package/src/utils/vfsDiff.ts +275 -0
  297. package/standalone-wo-sftp.js +507 -0
  298. package/standalone-wo-sftp.js.map +7 -0
  299. package/standalone.js +486 -109
  300. package/standalone.js.map +4 -4
  301. package/tests/bun-test-shim.ts +9 -1
  302. package/tests/command-helpers.test.ts +1 -5
  303. package/tests/new-features.test.ts +1036 -0
  304. package/tests/parser-executor.test.ts +27 -27
  305. package/tests/sftp.test.ts +122 -42
  306. package/tests/users.test.ts +23 -5
  307. package/CHANGELOG.md +0 -150
@@ -1,61 +1,146 @@
1
1
  import type { ShellModule } from "../types/commands";
2
- import { parseArgs } from "./command-helpers";
3
- import {
4
- assertPathAccess,
5
- normalizeTerminalOutput,
6
- resolvePath,
7
- runHostCommand,
8
- } from "./helpers";
2
+ import { ifFlag, parseArgs } from "./command-helpers";
3
+ import { assertPathAccess, resolvePath } from "./helpers";
9
4
 
5
+ /**
6
+ * HTTP client wrapper using `fetch()` semantics (virtual curl).
7
+ * @category network
8
+ * @params ["[options] <url>"]
9
+ */
10
10
  export const curlCommand: ShellModule = {
11
11
  name: "curl",
12
- description: "HTTP client",
12
+ description: "Transfer data from or to a server (pure fetch)",
13
13
  category: "network",
14
- params: ["[-o file] <url>"],
14
+ params: ["[options] <url>"],
15
15
  run: async ({ authUser, cwd, args, shell }) => {
16
16
  const { flagsWithValues, positionals } = parseArgs(args, {
17
- flagsWithValue: ["-o", "--output"],
17
+ flagsWithValue: [
18
+ "-o",
19
+ "--output",
20
+ "-X",
21
+ "--request",
22
+ "-d",
23
+ "--data",
24
+ "-H",
25
+ "--header",
26
+ "-u",
27
+ "--user",
28
+ ],
18
29
  });
19
- const outputPath =
20
- flagsWithValues.get("-o") || flagsWithValues.get("--output") || null;
30
+
31
+ if (ifFlag(args, ["--help", "-h"])) {
32
+ return {
33
+ stdout: [
34
+ "Usage: curl [options] <url>",
35
+ " -o, --output <file> Write to file",
36
+ " -X, --request <method> HTTP method",
37
+ " -d, --data <data> POST data",
38
+ " -H, --header <hdr> Extra header",
39
+ " -s, --silent Silent mode",
40
+ " -I, --head Fetch headers only",
41
+ " -L, --location Follow redirects",
42
+ " -v, --verbose Verbose",
43
+ ].join("\n"),
44
+ exitCode: 0,
45
+ };
46
+ }
47
+
21
48
  const url = positionals[0];
49
+ if (!url) return { stderr: "curl: no URL specified", exitCode: 1 };
50
+
51
+ const outputPath =
52
+ flagsWithValues.get("-o") ?? flagsWithValues.get("--output") ?? null;
53
+ const method = (
54
+ flagsWithValues.get("-X") ??
55
+ flagsWithValues.get("--request") ??
56
+ "GET"
57
+ ).toUpperCase();
58
+ const postData =
59
+ flagsWithValues.get("-d") ?? flagsWithValues.get("--data") ?? null;
60
+ const headerRaw =
61
+ flagsWithValues.get("-H") ?? flagsWithValues.get("--header") ?? null;
62
+ const silent = ifFlag(args, ["-s", "--silent"]);
63
+ const headOnly = ifFlag(args, ["-I", "--head"]);
64
+ const followRedirects = ifFlag(args, ["-L", "--location"]);
65
+ const verbose = ifFlag(args, ["-v", "--verbose"]);
22
66
 
23
- if (!url) {
24
- return { stderr: "curl: missing URL", exitCode: 1 };
67
+ const extraHeaders: Record<string, string> = {
68
+ "User-Agent": "curl/7.88.1",
69
+ };
70
+ if (headerRaw) {
71
+ const idx = headerRaw.indexOf(":");
72
+ if (idx !== -1)
73
+ extraHeaders[headerRaw.slice(0, idx).trim()] = headerRaw
74
+ .slice(idx + 1)
75
+ .trim();
25
76
  }
26
77
 
27
- const passthroughArgs = outputPath
28
- ? [...positionals, "-o", "-"]
29
- : positionals;
30
- const result = await runHostCommand("curl", passthroughArgs);
78
+ const finalMethod = postData && method === "GET" ? "POST" : method;
79
+ const fetchOpts: RequestInit = {
80
+ method: finalMethod,
81
+ headers: extraHeaders,
82
+ redirect: followRedirects ? "follow" : "manual",
83
+ };
84
+ if (postData) {
85
+ extraHeaders["Content-Type"] ??= "application/x-www-form-urlencoded";
86
+ fetchOpts.body = postData;
87
+ }
88
+
89
+ const stderrLines: string[] = [];
90
+ if (verbose) {
91
+ stderrLines.push(`* Trying ${url}...`, `* Connected`);
92
+ stderrLines.push(
93
+ `> ${finalMethod} / HTTP/1.1`,
94
+ `> Host: ${new URL(url).host}`,
95
+ );
96
+ }
31
97
 
32
- if (result.exitCode !== 0) {
98
+ let response: Response;
99
+ try {
100
+ response = await fetch(url, fetchOpts);
101
+ } catch (err) {
102
+ const msg = err instanceof Error ? err.message : String(err);
33
103
  return {
34
- stderr: normalizeTerminalOutput(
35
- result.stderr || `curl: exited with code ${result.exitCode}`,
36
- ),
37
- exitCode: result.exitCode,
104
+ stderr: `curl: (6) Could not resolve host: ${msg}`,
105
+ exitCode: 6,
38
106
  };
39
107
  }
40
108
 
109
+ if (verbose) {
110
+ stderrLines.push(`< HTTP/1.1 ${response.status} ${response.statusText}`);
111
+ }
112
+
113
+ if (headOnly) {
114
+ const lines = [`HTTP/1.1 ${response.status} ${response.statusText}`];
115
+ for (const [k, v] of response.headers.entries()) lines.push(`${k}: ${v}`);
116
+ return { stdout: `${lines.join("\r\n")}\r\n`, exitCode: 0 };
117
+ }
118
+
119
+ let body: string;
120
+ try {
121
+ body = await response.text();
122
+ } catch {
123
+ return { stderr: "curl: failed to read response body", exitCode: 1 };
124
+ }
125
+
41
126
  if (outputPath) {
42
127
  const target = resolvePath(cwd, outputPath);
43
128
  assertPathAccess(authUser, target, "curl");
44
- shell.writeFileAsUser(authUser, target, result.stdout);
129
+ shell.writeFileAsUser(authUser, target, body);
130
+ if (!silent)
131
+ stderrLines.push(
132
+ ` % Total % Received\n100 ${body.length} 100 ${body.length}`,
133
+ );
45
134
  return {
46
- stderr: result.stderr
47
- ? normalizeTerminalOutput(result.stderr)
48
- : undefined,
49
- exitCode: 0,
135
+ stderr: stderrLines.join("\n") || undefined,
136
+ exitCode: response.ok ? 0 : 22,
50
137
  };
51
138
  }
52
139
 
53
140
  return {
54
- stdout: result.stdout,
55
- stderr: result.stderr
56
- ? normalizeTerminalOutput(result.stderr)
57
- : undefined,
58
- exitCode: 0,
141
+ stdout: body,
142
+ stderr: stderrLines.length > 0 ? stderrLines.join("\n") : undefined,
143
+ exitCode: response.ok ? 0 : 22,
59
144
  };
60
145
  },
61
146
  };
@@ -1,6 +1,11 @@
1
1
  import type { ShellModule } from "../types/commands";
2
2
  import { getFlag } from "./command-helpers";
3
3
 
4
+ /**
5
+ * Extract selected fields from each line of input.
6
+ * @category text
7
+ * @params ["-d <delim> -f <fields> [file]"]
8
+ */
4
9
  export const cutCommand: ShellModule = {
5
10
  name: "cut",
6
11
  description: "Remove sections from lines",
@@ -11,7 +16,9 @@ export const cutCommand: ShellModule = {
11
16
  const fields = (getFlag(args, ["-f"]) as string | undefined) ?? "1";
12
17
  const cols = fields.split(",").map((f) => {
13
18
  const [a, b] = f.split("-").map(Number);
14
- return b !== undefined ? { from: (a ?? 1) - 1, to: b - 1 } : { from: (a ?? 1) - 1, to: (a ?? 1) - 1 };
19
+ return b !== undefined
20
+ ? { from: (a ?? 1) - 1, to: b - 1 }
21
+ : { from: (a ?? 1) - 1, to: (a ?? 1) - 1 };
15
22
  });
16
23
  const lines = (stdin ?? "").split("\n");
17
24
  const out = lines.map((line) => {
@@ -1,5 +1,10 @@
1
1
  import type { ShellModule } from "../types/commands";
2
2
 
3
+ /**
4
+ * Print the current date/time or a formatted representation.
5
+ * @category system
6
+ * @params ["[+format]"]
7
+ */
3
8
  export const dateCommand: ShellModule = {
4
9
  name: "date",
5
10
  description: "Print current date and time",
@@ -9,7 +14,8 @@ export const dateCommand: ShellModule = {
9
14
  const now = new Date();
10
15
  const fmt = args[0];
11
16
  if (fmt?.startsWith("+")) {
12
- const f = fmt.slice(1)
17
+ const f = fmt
18
+ .slice(1)
13
19
  .replace("%Y", String(now.getFullYear()))
14
20
  .replace("%m", String(now.getMonth() + 1).padStart(2, "0"))
15
21
  .replace("%d", String(now.getDate()).padStart(2, "0"))
@@ -0,0 +1,44 @@
1
+ import type { ShellModule } from "../types/commands";
2
+ import { ifFlag } from "./command-helpers";
3
+
4
+ export const declareCommand: ShellModule = {
5
+ name: "declare",
6
+ aliases: ["local", "typeset"],
7
+ description: "Declare variables and give them attributes",
8
+ category: "shell",
9
+ params: ["[-i] [-r] [-x] [-a] [name[=value]...]"],
10
+ run: ({ args, env }) => {
11
+ if (!env) return { exitCode: 0 };
12
+
13
+ const integer = ifFlag(args, ["-i"]);
14
+ const _readonly = ifFlag(args, ["-r"]);
15
+ const _export_ = ifFlag(args, ["-x"]);
16
+ const printAll = args.filter((a) => !a.startsWith("-")).length === 0;
17
+
18
+ if (printAll) {
19
+ const lines = Object.entries(env.vars).map(
20
+ ([k, v]) => `declare -- ${k}="${v}"`,
21
+ );
22
+ return { stdout: lines.join("\n"), exitCode: 0 };
23
+ }
24
+
25
+ const assignments = args.filter((a) => !a.startsWith("-"));
26
+ for (const token of assignments) {
27
+ const eq = token.indexOf("=");
28
+ if (eq === -1) {
29
+ // Just declare (no value)
30
+ if (!(token in env.vars)) env.vars[token] = "";
31
+ } else {
32
+ const name = token.slice(0, eq);
33
+ let val = token.slice(eq + 1);
34
+ if (integer) {
35
+ const n = parseInt(val, 10);
36
+ val = Number.isNaN(n) ? "0" : String(n);
37
+ }
38
+ env.vars[name] = val;
39
+ }
40
+ }
41
+
42
+ return { exitCode: 0 };
43
+ },
44
+ };
@@ -1,6 +1,11 @@
1
1
  import type { ShellModule } from "../types/commands";
2
2
  import { resolvePath } from "./helpers";
3
3
 
4
+ /**
5
+ * Compare files line-by-line and print differing lines.
6
+ * @category text
7
+ * @params ["<file1> <file2>"]
8
+ */
4
9
  export const diffCommand: ShellModule = {
5
10
  name: "diff",
6
11
  description: "Compare files line by line",
@@ -12,13 +17,22 @@ export const diffCommand: ShellModule = {
12
17
  const p1 = resolvePath(cwd, f1);
13
18
  const p2 = resolvePath(cwd, f2);
14
19
  let a: string[], b: string[];
15
- try { a = shell.vfs.readFile(p1).split("\n"); } catch { return { stderr: `diff: ${f1}: No such file or directory`, exitCode: 2 }; }
16
- try { b = shell.vfs.readFile(p2).split("\n"); } catch { return { stderr: `diff: ${f2}: No such file or directory`, exitCode: 2 }; }
20
+ try {
21
+ a = shell.vfs.readFile(p1).split("\n");
22
+ } catch {
23
+ return { stderr: `diff: ${f1}: No such file or directory`, exitCode: 2 };
24
+ }
25
+ try {
26
+ b = shell.vfs.readFile(p2).split("\n");
27
+ } catch {
28
+ return { stderr: `diff: ${f2}: No such file or directory`, exitCode: 2 };
29
+ }
17
30
 
18
31
  const out: string[] = [];
19
32
  const max = Math.max(a.length, b.length);
20
33
  for (let i = 0; i < max; i++) {
21
- const la = a[i]; const lb = b[i];
34
+ const la = a[i];
35
+ const lb = b[i];
22
36
  if (la !== lb) {
23
37
  if (la !== undefined) out.push(`< ${la}`);
24
38
  if (lb !== undefined) out.push(`> ${lb}`);
@@ -0,0 +1,180 @@
1
+ import type { ShellModule } from "../types/commands";
2
+ import { ifFlag, parseArgs } from "./command-helpers";
3
+ import { getPackageManager } from "./helpers";
4
+
5
+ /**
6
+ * dpkg compatibility command (query/remove/list) backed by the virtual package manager.
7
+ * @category package
8
+ * @params ["[-l] [-s pkg] [-L pkg] [-i pkg] [--remove pkg]"]
9
+ */
10
+ export const dpkgCommand: ShellModule = {
11
+ name: "dpkg",
12
+ description: "Debian package manager low-level tool",
13
+ category: "package",
14
+ params: ["[-l] [-s pkg] [-L pkg] [-i pkg] [--remove pkg]"],
15
+ run: ({ args, authUser, shell }) => {
16
+ const pm = getPackageManager(shell);
17
+ if (!pm)
18
+ return { stderr: "dpkg: package manager not initialised", exitCode: 1 };
19
+
20
+ const listFlag = ifFlag(args, ["-l", "--list"]);
21
+ const statusFlag = ifFlag(args, ["-s", "--status"]);
22
+ const listFilesFlag = ifFlag(args, ["-L", "--listfiles"]);
23
+ const removeFlag = ifFlag(args, ["-r", "--remove"]);
24
+ const purgeFlag = ifFlag(args, ["-P", "--purge"]);
25
+
26
+ const { positionals } = parseArgs(args, {
27
+ flags: [
28
+ "-l",
29
+ "--list",
30
+ "-s",
31
+ "--status",
32
+ "-L",
33
+ "--listfiles",
34
+ "-r",
35
+ "--remove",
36
+ "-P",
37
+ "--purge",
38
+ ],
39
+ });
40
+
41
+ if (listFlag) {
42
+ const pkgList = pm.listInstalled();
43
+ if (pkgList.length === 0) {
44
+ return {
45
+ stdout: [
46
+ "Desired=Unknown/Install/Remove/Purge/Hold",
47
+ "|Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend",
48
+ "|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)",
49
+ "||/ Name Version Architecture Description",
50
+ "+++-==============-===============-============-========================================",
51
+ "(no packages installed)",
52
+ ].join("\n"),
53
+ exitCode: 0,
54
+ };
55
+ }
56
+
57
+ const header = [
58
+ "Desired=Unknown/Install/Remove/Purge/Hold",
59
+ "|Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend",
60
+ "|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)",
61
+ "||/ Name Version Architecture Description",
62
+ "+++-==============-===============-============-========================================",
63
+ ];
64
+
65
+ const rows = pkgList.map((p) => {
66
+ const name = p.name.padEnd(14).slice(0, 14);
67
+ const ver = p.version.padEnd(15).slice(0, 15);
68
+ const arch = p.architecture.padEnd(12).slice(0, 12);
69
+ const desc = (p.description || "").slice(0, 40);
70
+ return `ii ${name} ${ver} ${arch} ${desc}`;
71
+ });
72
+
73
+ return { stdout: [...header, ...rows].join("\n"), exitCode: 0 };
74
+ }
75
+
76
+ if (statusFlag) {
77
+ const pkgName = positionals[0];
78
+ if (!pkgName)
79
+ return { stderr: "dpkg: -s needs a package name", exitCode: 1 };
80
+ const info = pm.show(pkgName);
81
+ if (!info)
82
+ return {
83
+ stderr: `dpkg-query: package '${pkgName}' is not installed and no information is available`,
84
+ exitCode: 1,
85
+ };
86
+ return { stdout: info, exitCode: 0 };
87
+ }
88
+
89
+ if (listFilesFlag) {
90
+ const pkgName = positionals[0];
91
+ if (!pkgName)
92
+ return { stderr: "dpkg: -L needs a package name", exitCode: 1 };
93
+ const installed = pm.listInstalled().find((p) => p.name === pkgName);
94
+ if (!installed)
95
+ return {
96
+ stderr: `dpkg-query: package '${pkgName}' is not installed`,
97
+ exitCode: 1,
98
+ };
99
+ if (installed.files.length === 0)
100
+ return { stdout: "/.keep", exitCode: 0 };
101
+ return { stdout: installed.files.join("\n"), exitCode: 0 };
102
+ }
103
+
104
+ if (removeFlag || purgeFlag) {
105
+ if (authUser !== "root")
106
+ return {
107
+ stderr:
108
+ "dpkg: error: requested operation requires superuser privilege",
109
+ exitCode: 2,
110
+ };
111
+ if (positionals.length === 0)
112
+ return { stderr: "dpkg: error: need an action option", exitCode: 2 };
113
+ const { output, exitCode } = pm.remove(positionals, { purge: purgeFlag });
114
+ return { stdout: output || undefined, exitCode };
115
+ }
116
+
117
+ // Default: show help
118
+ return {
119
+ stdout: [
120
+ "Usage: dpkg [<option>...] <command>",
121
+ "",
122
+ "Commands:",
123
+ " -l, --list List packages matching given pattern",
124
+ " -s, --status <pkg>... Report status of specified package",
125
+ " -L, --listfiles <pkg>... List files owned by package",
126
+ " -r, --remove <pkg>... Remove <pkg> but leave its configuration",
127
+ " -P, --purge <pkg>... Remove <pkg> and its configuration",
128
+ ].join("\n"),
129
+ exitCode: 0,
130
+ };
131
+ },
132
+ };
133
+
134
+ export const dpkgQueryCommand: ShellModule = {
135
+ name: "dpkg-query",
136
+ description: "Show information about installed packages",
137
+ category: "package",
138
+ params: ["-W [pkg] | -l [pattern]"],
139
+ run: ({ args, shell }) => {
140
+ const pm = getPackageManager(shell);
141
+ if (!pm)
142
+ return {
143
+ stderr: "dpkg-query: package manager not initialised",
144
+ exitCode: 1,
145
+ };
146
+
147
+ const listFlag = ifFlag(args, ["-l"]);
148
+ const showFlag = ifFlag(args, ["-W", "--show"]);
149
+ const { positionals } = parseArgs(args, {
150
+ flags: ["-l", "-W", "--show"],
151
+ });
152
+
153
+ if (listFlag || showFlag) {
154
+ const pkgList = pm.listInstalled();
155
+ const pattern = positionals[0];
156
+ const filtered = pattern
157
+ ? pkgList.filter((p) => p.name.includes(pattern))
158
+ : pkgList;
159
+
160
+ if (showFlag) {
161
+ return {
162
+ stdout: filtered.map((p) => `${p.name}\t${p.version}`).join("\n"),
163
+ exitCode: 0,
164
+ };
165
+ }
166
+
167
+ const rows = filtered.map((p) => {
168
+ const name = p.name.padEnd(14).slice(0, 14);
169
+ const ver = p.version.padEnd(15).slice(0, 15);
170
+ return `ii ${name} ${ver} amd64 ${(p.description || "").slice(0, 40)}`;
171
+ });
172
+ return {
173
+ stdout: rows.join("\n") || "(no packages match)",
174
+ exitCode: 0,
175
+ };
176
+ }
177
+
178
+ return { stderr: "dpkg-query: need a flag (-l, -W)", exitCode: 1 };
179
+ },
180
+ };
@@ -13,22 +13,34 @@ export const duCommand: ShellModule = {
13
13
  const target = args.find((a) => !a.startsWith("-")) ?? ".";
14
14
  const p = resolvePath(cwd, target);
15
15
 
16
- const fmt = (b: number) => human ? `${(b / 1024).toFixed(1)}K` : String(Math.ceil(b / 1024));
16
+ const fmt = (b: number) =>
17
+ human ? `${(b / 1024).toFixed(1)}K` : String(Math.ceil(b / 1024));
17
18
 
18
- if (!shell.vfs.exists(p)) return { stderr: `du: ${target}: No such file or directory`, exitCode: 1 };
19
+ if (!shell.vfs.exists(p))
20
+ return {
21
+ stderr: `du: ${target}: No such file or directory`,
22
+ exitCode: 1,
23
+ };
19
24
 
20
25
  if (summary || shell.vfs.stat(p).type === "file") {
21
- return { stdout: `${fmt(shell.vfs.getUsageBytes(p))}\t${target}`, exitCode: 0 };
26
+ return {
27
+ stdout: `${fmt(shell.vfs.getUsageBytes(p))}\t${target}`,
28
+ exitCode: 0,
29
+ };
22
30
  }
23
31
 
24
32
  const lines: string[] = [];
25
33
  const walk = (dir: string, rel: string) => {
26
34
  let total = 0;
27
35
  for (const e of shell.vfs.list(dir)) {
28
- const full = `${dir}/${e}`, r = `${rel}/${e}`;
36
+ const full = `${dir}/${e}`,
37
+ r = `${rel}/${e}`;
29
38
  const st = shell.vfs.stat(full);
30
39
  if (st.type === "directory") total += walk(full, r);
31
- else { total += st.size; if (!summary) lines.push(`${fmt(st.size)}\t${r}`); }
40
+ else {
41
+ total += st.size;
42
+ if (!summary) lines.push(`${fmt(st.size)}\t${r}`);
43
+ }
32
44
  }
33
45
  lines.push(`${fmt(total)}\t${rel}`);
34
46
  return total;
@@ -1,28 +1,57 @@
1
1
  import type { ShellModule } from "../types/commands";
2
2
  import { parseArgs } from "./command-helpers";
3
- import { getAllEnvVars } from "./set";
3
+ import { expandSync } from "../utils/expand";
4
4
 
5
- function expandEnvVars(input: string, env: Record<string, string>): string {
6
- return input.replace(/\$([A-Za-z_][A-Za-z0-9_]*)/g, (_, name: string) => {
7
- return env[name] ?? "";
8
- });
5
+ /**
6
+ * Expand escape sequences for `echo -e`.
7
+ * Handles \n \t \r \\ \a \b \f \v and \0NNN (octal).
8
+ */
9
+ function expandEscapes(text: string): string {
10
+ return text
11
+ .replace(/\\n/g, "\n")
12
+ .replace(/\\t/g, "\t")
13
+ .replace(/\\r/g, "\r")
14
+ .replace(/\\\\/g, "\\")
15
+ .replace(/\\a/g, "\x07")
16
+ .replace(/\\b/g, "\x08")
17
+ .replace(/\\f/g, "\x0C")
18
+ .replace(/\\v/g, "\x0B")
19
+ .replace(/\\0(\d{1,3})/g, (_, oct) =>
20
+ String.fromCharCode(parseInt(oct, 8)),
21
+ );
9
22
  }
10
23
 
24
+ /**
25
+ * Echo text to stdout with shell-style expansion and escape support.
26
+ * @category shell
27
+ * @params ["[-n] [-e] [text...]"]
28
+ */
11
29
  export const echoCommand: ShellModule = {
12
30
  name: "echo",
13
31
  description: "Display text",
14
32
  category: "shell",
15
- params: ["[options] [text...]"],
16
- run: ({ args, authUser, stdin }) => {
17
- const { flags, positionals } = parseArgs(args, { flags: ["-n"] });
18
- const newline = !flags.has("-n");
33
+ params: ["[-n] [-e] [text...]"],
34
+ run: ({ args, stdin, env }) => {
35
+ const { flags, positionals } = parseArgs(args, {
36
+ flags: ["-n", "-e", "-E"],
37
+ });
38
+ const noNewline = flags.has("-n");
39
+ const escapes = flags.has("-e");
40
+
19
41
  const rawText =
20
42
  positionals.length > 0 ? positionals.join(" ") : (stdin ?? "");
21
- const env = getAllEnvVars(authUser);
22
- const text = expandEnvVars(rawText, env);
43
+
44
+ // Full expansion: $? ${#VAR} $((expr)) ~ ${VAR:-def} $VAR etc.
45
+ // $(cmd) is already resolved upstream by runCommand before echo.run is called.
46
+ const expanded = expandSync(
47
+ rawText,
48
+ env?.vars ?? {},
49
+ env?.lastExitCode ?? 0,
50
+ );
51
+ const text = escapes ? expandEscapes(expanded) : expanded;
23
52
 
24
53
  return {
25
- stdout: newline ? text : text.trimEnd(),
54
+ stdout: noNewline ? text : `${text}\n`,
26
55
  exitCode: 0,
27
56
  };
28
57
  },
@@ -1,6 +1,11 @@
1
1
  /** biome-ignore-all lint/style/useNamingConvention: ENV VARS */
2
2
  import type { ShellModule } from "../types/commands";
3
3
 
4
+ /**
5
+ * Print environment variables for the current session.
6
+ * @category shell
7
+ * @params []
8
+ */
4
9
  export const envCommand: ShellModule = {
5
10
  name: "env",
6
11
  description: "Print environment variables",
@@ -8,6 +13,11 @@ export const envCommand: ShellModule = {
8
13
  params: [],
9
14
  run: ({ env, authUser }) => {
10
15
  const vars = { ...env.vars, USER: authUser, HOME: `/home/${authUser}` };
11
- return { stdout: Object.entries(vars).map(([k, v]) => `${k}=${v}`).join("\n"), exitCode: 0 };
16
+ return {
17
+ stdout: Object.entries(vars)
18
+ .map(([k, v]) => `${k}=${v}`)
19
+ .join("\n"),
20
+ exitCode: 0,
21
+ };
12
22
  },
13
23
  };
@@ -1,8 +1,18 @@
1
1
  import type { ShellModule } from "../types/commands";
2
2
 
3
+ /**
4
+ * Exit the current shell session (closeSession flag).
5
+ * @category shell
6
+ * @params ["[code]"]
7
+ */
3
8
  export const exitCommand: ShellModule = {
4
9
  name: "exit",
5
10
  aliases: ["bye"],
6
- params: [],
7
- run: () => ({ closeSession: true, exitCode: 0 }),
11
+ description: "Exit the shell session",
12
+ category: "shell",
13
+ params: ["[code]"],
14
+ run: ({ args }) => ({
15
+ closeSession: true,
16
+ exitCode: parseInt(args[0] ?? "0", 10) || 0,
17
+ }),
8
18
  };
@@ -7,7 +7,9 @@ export const exportCommand: ShellModule = {
7
7
  params: ["[VAR=value]"],
8
8
  run: ({ args, env }) => {
9
9
  if (args.length === 0) {
10
- const out = Object.entries(env.vars).map(([k, v]) => `declare -x ${k}="${v}"`).join("\n");
10
+ const out = Object.entries(env.vars)
11
+ .map(([k, v]) => `declare -x ${k}="${v}"`)
12
+ .join("\n");
11
13
  return { stdout: out, exitCode: 0 };
12
14
  }
13
15
  for (const arg of args) {
@@ -2,6 +2,11 @@ import type { ShellModule } from "../types/commands";
2
2
  import { getFlag } from "./command-helpers";
3
3
  import { assertPathAccess, resolvePath } from "./helpers";
4
4
 
5
+ /**
6
+ * Find files and directories by name and type with minimal pattern support.
7
+ * @category files
8
+ * @params ["[path] [-name <pattern>] [-type f|d]"]
9
+ */
5
10
  export const findCommand: ShellModule = {
6
11
  name: "find",
7
12
  description: "Search for files",