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,5 +1,11 @@
1
1
  import type { ShellModule } from "../types/commands";
2
2
 
3
+ /**
4
+ * Add a new user to the virtual user database.
5
+ * @category users
6
+ * @params ["<username> <password>"]
7
+ * @returns ShellModule
8
+ */
3
9
  export const adduserCommand: ShellModule = {
4
10
  name: "adduser",
5
11
  description: "Add a new user",
@@ -0,0 +1,64 @@
1
+ import type { ShellModule } from "../types/commands";
2
+ import { ifFlag } from "./command-helpers";
3
+ /**
4
+ * Manage shell aliases (list / set / remove).
5
+ * @category shell
6
+ * @params ["[name[=value] ...]"]
7
+ */
8
+ export const aliasCommand: ShellModule = {
9
+ name: "alias",
10
+ description: "Define or display aliases",
11
+ category: "shell",
12
+ params: ["[name[=value] ...]"],
13
+ run: ({ args, env }) => {
14
+ if (!env) return { exitCode: 0 };
15
+
16
+ // Aliases stored in env.vars under prefix __alias_
17
+ if (args.length === 0) {
18
+ const aliases = Object.entries(env.vars)
19
+ .filter(([k]) => k.startsWith("__alias_"))
20
+ .map(([k, v]) => `alias ${k.slice("__alias_".length)}='${v}'`);
21
+ return { stdout: aliases.join("\n") || "", exitCode: 0 };
22
+ }
23
+
24
+ const lines: string[] = [];
25
+ for (const arg of args) {
26
+ const eq = arg.indexOf("=");
27
+ if (eq === -1) {
28
+ // Display single alias
29
+ const val = env.vars[`__alias_${arg}`];
30
+ if (val) lines.push(`alias ${arg}='${val}'`);
31
+ else return { stderr: `alias: ${arg}: not found`, exitCode: 1 };
32
+ } else {
33
+ // Set alias
34
+ const name = arg.slice(0, eq);
35
+ const val = arg.slice(eq + 1).replace(/^['"]|['"]$/g, "");
36
+ env.vars[`__alias_${name}`] = val;
37
+ }
38
+ }
39
+
40
+ return { stdout: lines.join("\n") || undefined, exitCode: 0 };
41
+ },
42
+ };
43
+
44
+ export const unaliasCommand: ShellModule = {
45
+ name: "unalias",
46
+ description: "Remove alias definitions",
47
+ category: "shell",
48
+ params: ["<name...> | -a"],
49
+ run: ({ args, env }) => {
50
+ if (!env) return { exitCode: 0 };
51
+
52
+ if (ifFlag(args, ["-a"])) {
53
+ for (const k of Object.keys(env.vars)) {
54
+ if (k.startsWith("__alias_")) delete env.vars[k];
55
+ }
56
+ return { exitCode: 0 };
57
+ }
58
+
59
+ for (const name of args) {
60
+ delete env.vars[`__alias_${name}`];
61
+ }
62
+ return { exitCode: 0 };
63
+ },
64
+ };
@@ -0,0 +1,228 @@
1
+ import type { ShellModule } from "../types/commands";
2
+ import { ifFlag } from "./command-helpers";
3
+ import { getPackageManager } from "./helpers";
4
+
5
+ /**
6
+ * APT package manager front-end (simulated).
7
+ * @category package
8
+ * @params ["<install|remove|update|upgrade|search|show|list> [pkg...]"]
9
+ */
10
+ export const aptCommand: ShellModule = {
11
+ name: "apt",
12
+ aliases: ["apt-get"],
13
+ description: "Package manager",
14
+ category: "package",
15
+ params: ["<install|remove|update|upgrade|search|show|list> [pkg...]"],
16
+ run: ({ args, shell, authUser }) => {
17
+ const pm = getPackageManager(shell);
18
+ if (!pm)
19
+ return { stderr: "apt: package manager not initialised", exitCode: 1 };
20
+
21
+ const sub = args[0]?.toLowerCase();
22
+ const rest = args.slice(1);
23
+
24
+ const quiet = ifFlag(rest, ["-q", "--quiet", "-qq"]);
25
+ const purge = ifFlag(rest, ["--purge"]);
26
+ const pkgs = rest.filter((a) => !a.startsWith("-"));
27
+
28
+ // Non-root check
29
+ const restricted = ["install", "remove", "purge", "upgrade", "update"];
30
+ if (restricted.includes(sub ?? "") && authUser !== "root") {
31
+ return {
32
+ stderr:
33
+ "E: Could not open lock file /var/lib/dpkg/lock-frontend - open (13: Permission denied)\nE: Unable to acquire the dpkg frontend lock, are you root?",
34
+ exitCode: 100,
35
+ };
36
+ }
37
+
38
+ switch (sub) {
39
+ case "install": {
40
+ if (pkgs.length === 0)
41
+ return { stderr: "apt: no packages specified", exitCode: 1 };
42
+ const { output, exitCode } = pm.install(pkgs, { quiet });
43
+ return { stdout: output || undefined, exitCode };
44
+ }
45
+
46
+ case "remove":
47
+ case "purge": {
48
+ if (pkgs.length === 0)
49
+ return { stderr: "apt: no packages specified", exitCode: 1 };
50
+ const { output, exitCode } = pm.remove(pkgs, {
51
+ purge: sub === "purge" || purge,
52
+ quiet,
53
+ });
54
+ return { stdout: output || undefined, exitCode };
55
+ }
56
+
57
+ case "update": {
58
+ return {
59
+ stdout: [
60
+ "Hit:1 fortune://packages.fortune.local aurora InRelease",
61
+ "Hit:2 fortune://security.fortune.local aurora-security InRelease",
62
+ "Reading package lists... Done",
63
+ "Building dependency tree... Done",
64
+ "Reading state information... Done",
65
+ `All packages are up to date.`,
66
+ ].join("\n"),
67
+ exitCode: 0,
68
+ };
69
+ }
70
+
71
+ case "upgrade": {
72
+ return {
73
+ stdout: [
74
+ "Reading package lists... Done",
75
+ "Building dependency tree... Done",
76
+ "Reading state information... Done",
77
+ "Calculating upgrade... Done",
78
+ "0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.",
79
+ ].join("\n"),
80
+ exitCode: 0,
81
+ };
82
+ }
83
+
84
+ case "search": {
85
+ const term = pkgs[0];
86
+ if (!term)
87
+ return { stderr: "apt: search requires a term", exitCode: 1 };
88
+ const results = pm.search(term);
89
+ if (results.length === 0)
90
+ return {
91
+ stdout: `Sorting... Done\nFull Text Search... Done\n(no results)`,
92
+ exitCode: 0,
93
+ };
94
+ const lines = results.map(
95
+ (p) =>
96
+ `${p.name}/${p.section ?? "misc"} ${p.version} amd64\n ${p.shortDesc ?? p.description}`,
97
+ );
98
+ return {
99
+ stdout: `Sorting... Done\nFull Text Search... Done\n${lines.join("\n")}`,
100
+ exitCode: 0,
101
+ };
102
+ }
103
+
104
+ case "show": {
105
+ const name = pkgs[0];
106
+ if (!name)
107
+ return { stderr: "apt: show requires a package name", exitCode: 1 };
108
+ const info = pm.show(name);
109
+ if (!info)
110
+ return {
111
+ stderr: `N: Unable to locate package ${name}`,
112
+ exitCode: 100,
113
+ };
114
+ return { stdout: info, exitCode: 0 };
115
+ }
116
+
117
+ case "list": {
118
+ const installedFlag = ifFlag(rest, ["--installed"]);
119
+ if (installedFlag) {
120
+ const pkgList = pm.listInstalled();
121
+ if (pkgList.length === 0)
122
+ return {
123
+ stdout: "Listing... Done\n(no packages installed)",
124
+ exitCode: 0,
125
+ };
126
+ const lines = pkgList.map(
127
+ (p) =>
128
+ `${p.name}/${p.section} ${p.version} ${p.architecture} [installed]`,
129
+ );
130
+ return {
131
+ stdout: `Listing... Done\n${lines.join("\n")}`,
132
+ exitCode: 0,
133
+ };
134
+ }
135
+ // all available
136
+ const all = pm.listAvailable();
137
+ const lines = all.map(
138
+ (p) => `${p.name}/${p.section ?? "misc"} ${p.version} amd64`,
139
+ );
140
+ return { stdout: `Listing... Done\n${lines.join("\n")}`, exitCode: 0 };
141
+ }
142
+
143
+ default: {
144
+ return {
145
+ stdout: [
146
+ "Usage: apt [options] command",
147
+ "",
148
+ "Commands:",
149
+ " install <pkg...> Install packages",
150
+ " remove <pkg...> Remove packages",
151
+ " purge <pkg...> Remove packages and config files",
152
+ " update Refresh package index",
153
+ " upgrade Upgrade all packages",
154
+ " search <term> Search in package descriptions",
155
+ " show <pkg> Show package details",
156
+ " list [--installed] List packages",
157
+ ].join("\n"),
158
+ exitCode: 0,
159
+ };
160
+ }
161
+ }
162
+ },
163
+ };
164
+
165
+ export const aptCacheCommand: ShellModule = {
166
+ name: "apt-cache",
167
+ description: "Query the package cache",
168
+ category: "package",
169
+ params: ["<search|show|policy> [pkg]"],
170
+ run: ({ args, shell }) => {
171
+ const pm = getPackageManager(shell);
172
+ if (!pm)
173
+ return {
174
+ stderr: "apt-cache: package manager not initialised",
175
+ exitCode: 1,
176
+ };
177
+
178
+ const sub = args[0]?.toLowerCase();
179
+ const pkgName = args[1];
180
+
181
+ switch (sub) {
182
+ case "search": {
183
+ if (!pkgName) return { stderr: "Need a search term", exitCode: 1 };
184
+ const results = pm.search(pkgName);
185
+ return {
186
+ stdout:
187
+ results
188
+ .map((p) => `${p.name} - ${p.shortDesc ?? p.description}`)
189
+ .join("\n") || "(no results)",
190
+ exitCode: 0,
191
+ };
192
+ }
193
+ case "show": {
194
+ if (!pkgName) return { stderr: "Need a package name", exitCode: 1 };
195
+ const info = pm.show(pkgName);
196
+ return info
197
+ ? { stdout: info, exitCode: 0 }
198
+ : { stderr: `N: Unable to locate package ${pkgName}`, exitCode: 100 };
199
+ }
200
+ case "policy": {
201
+ if (!pkgName) return { stderr: "Need a package name", exitCode: 1 };
202
+ const def = pm.findInRegistry(pkgName);
203
+ if (!def)
204
+ return {
205
+ stderr: `N: Unable to locate package ${pkgName}`,
206
+ exitCode: 100,
207
+ };
208
+ const inst = pm.isInstalled(pkgName);
209
+ return {
210
+ stdout: [
211
+ `${pkgName}:`,
212
+ ` Installed: ${inst ? def.version : "(none)"}`,
213
+ ` Candidate: ${def.version}`,
214
+ ` Version table:`,
215
+ ` ${def.version} 500`,
216
+ ` 500 fortune://packages.fortune.local aurora/main amd64 Packages`,
217
+ ].join("\n"),
218
+ exitCode: 0,
219
+ };
220
+ }
221
+ default:
222
+ return {
223
+ stderr: `apt-cache: unknown command '${sub ?? ""}'`,
224
+ exitCode: 1,
225
+ };
226
+ }
227
+ },
228
+ };
@@ -1,6 +1,17 @@
1
1
  import type { ShellModule } from "../types/commands";
2
2
  import { getFlag } from "./command-helpers";
3
3
 
4
+ /**
5
+ * Minimal `awk`-like pattern scanner (supports simple print patterns).
6
+ * @category text
7
+ * @params ["[-F <sep>] '<program>' [file]"]
8
+ *
9
+ * Supported program patterns:
10
+ * - `print $N` (e.g. `print $1`, `print $2, $3`, `print $0`)
11
+ * - `{print $N}` (e.g. `{print $1}`, `{print $2, $3}`, `{print $0}`)
12
+ *
13
+ * The field separator can be set with `-F` (default is space, which splits on any whitespace).
14
+ */
4
15
  export const awkCommand: ShellModule = {
5
16
  name: "awk",
6
17
  description: "Pattern scanning and processing language (minimal)",
@@ -13,17 +24,20 @@ export const awkCommand: ShellModule = {
13
24
 
14
25
  // Only support print $N and {print $N} patterns
15
26
  const printMatch = prog.match(/^\{?\s*print\s+([^}]+)\s*\}?$/);
16
- if (!printMatch) return { stderr: `awk: unsupported program: ${prog}`, exitCode: 1 };
27
+ if (!printMatch)
28
+ return { stderr: `awk: unsupported program: ${prog}`, exitCode: 1 };
17
29
 
18
30
  const fields = printMatch[1]!.split(/\s*,\s*/).map((f) => f.trim());
19
31
  const lines = (stdin ?? "").split("\n").filter(Boolean);
20
32
  const out = lines.map((line) => {
21
33
  const parts = line.split(sep === " " ? /\s+/ : sep);
22
- return fields.map((f) => {
23
- if (f === "$0") return line;
24
- const n = parseInt(f.replace("$", ""), 10);
25
- return Number.isNaN(n) ? f.replace(/"/g, "") : (parts[n - 1] ?? "");
26
- }).join(sep === " " ? "\t" : sep);
34
+ return fields
35
+ .map((f) => {
36
+ if (f === "$0") return line;
37
+ const n = parseInt(f.replace("$", ""), 10);
38
+ return Number.isNaN(n) ? f.replace(/"/g, "") : (parts[n - 1] ?? "");
39
+ })
40
+ .join(sep === " " ? "\t" : sep);
27
41
  });
28
42
  return { stdout: out.join("\n"), exitCode: 0 };
29
43
  },
@@ -1,6 +1,11 @@
1
1
  import type { ShellModule } from "../types/commands";
2
2
  import { ifFlag } from "./command-helpers";
3
3
 
4
+ /**
5
+ * Encode or decode base64 data.
6
+ * @category text
7
+ * @params ["[-d] [file]"]
8
+ */
4
9
  export const base64Command: ShellModule = {
5
10
  name: "base64",
6
11
  description: "Encode/decode base64",
@@ -10,8 +15,14 @@ export const base64Command: ShellModule = {
10
15
  const decode = ifFlag(args, ["-d", "--decode"]);
11
16
  const input = stdin ?? "";
12
17
  if (decode) {
13
- try { return { stdout: Buffer.from(input.trim(), "base64").toString("utf8"), exitCode: 0 }; }
14
- catch { return { stderr: "base64: invalid input", exitCode: 1 }; }
18
+ try {
19
+ return {
20
+ stdout: Buffer.from(input.trim(), "base64").toString("utf8"),
21
+ exitCode: 0,
22
+ };
23
+ } catch {
24
+ return { stderr: "base64: invalid input", exitCode: 1 };
25
+ }
15
26
  }
16
27
  return { stdout: Buffer.from(input).toString("base64"), exitCode: 0 };
17
28
  },
@@ -1,20 +1,52 @@
1
1
  import type { ShellModule } from "../types/commands";
2
- import { getArg } from "./command-helpers";
2
+ import { ifFlag } from "./command-helpers";
3
3
  import { assertPathAccess, resolveReadablePath } from "./helpers";
4
4
 
5
+ /**
6
+ * Concatenate and print files to stdout.
7
+ * @category files
8
+ * @params ["[-n] [-b] <file...>"]
9
+ */
5
10
  export const catCommand: ShellModule = {
6
11
  name: "cat",
7
12
  description: "Concatenate and print files",
8
13
  category: "files",
9
- params: ["<file>"],
10
- run: ({ authUser, shell, cwd, args }) => {
11
- const fileArg = getArg(args, 0);
12
- if (!fileArg) {
14
+ params: ["[-n] [-b] <file...>"],
15
+ run: ({ authUser, shell, cwd, args, stdin }) => {
16
+ const numberAll = ifFlag(args, ["-n", "--number"]);
17
+ const numberNonBlank = ifFlag(args, ["-b", "--number-nonblank"]);
18
+ const fileArgs = args.filter((a) => !a.startsWith("-"));
19
+
20
+ if (fileArgs.length === 0 && stdin !== undefined) {
21
+ return { stdout: stdin, exitCode: 0 };
22
+ }
23
+
24
+ if (fileArgs.length === 0) {
13
25
  return { stderr: "cat: missing file operand", exitCode: 1 };
14
26
  }
15
27
 
16
- const target = resolveReadablePath(shell.vfs, cwd, fileArg);
17
- assertPathAccess(authUser, target, "cat");
18
- return { stdout: shell.vfs.readFile(target), exitCode: 0 };
28
+ const parts: string[] = [];
29
+ for (const fileArg of fileArgs) {
30
+ const target = resolveReadablePath(shell.vfs, cwd, fileArg);
31
+ assertPathAccess(authUser, target, "cat");
32
+ parts.push(shell.vfs.readFile(target));
33
+ }
34
+
35
+ const combined = parts.join("");
36
+
37
+ if (!numberAll && !numberNonBlank) {
38
+ return { stdout: combined, exitCode: 0 };
39
+ }
40
+
41
+ let lineNum = 1;
42
+ const numbered = combined
43
+ .split("\n")
44
+ .map((line) => {
45
+ if (numberNonBlank && line.trim() === "") return line;
46
+ return `${String(lineNum++).padStart(6)}\t${line}`;
47
+ })
48
+ .join("\n");
49
+
50
+ return { stdout: numbered, exitCode: 0 };
19
51
  },
20
52
  };
@@ -1,6 +1,11 @@
1
1
  import type { ShellModule } from "../types/commands";
2
2
  import { assertPathAccess, resolvePath } from "./helpers";
3
3
 
4
+ /**
5
+ * Change current working directory.
6
+ * @category navigation
7
+ * @params ["[path]"]
8
+ */
4
9
  export const cdCommand: ShellModule = {
5
10
  name: "cd",
6
11
  description: "Change directory",
@@ -1,6 +1,47 @@
1
1
  import type { ShellModule } from "../types/commands";
2
2
  import { assertPathAccess, resolvePath } from "./helpers";
3
3
 
4
+ /**
5
+ * Parse a symbolic chmod mode string (e.g. "+x", "u+x", "go-w", "a+rx")
6
+ * and apply it to the existing mode bits.
7
+ * Returns null if the string is not a valid symbolic mode.
8
+ */
9
+ function applySymbolicMode(existing: number, modeStr: string): number | null {
10
+ const pattern = /^([ugoa]*)([+\-=])([rwx]*)$/;
11
+ const parts = modeStr.split(",");
12
+ let mode = existing;
13
+ for (const part of parts) {
14
+ const m = part.trim().match(pattern);
15
+ if (!m) return null;
16
+ const [, who = "a", op, perms = ""] = m;
17
+ const targets = who === "" || who === "a" ? ["u", "g", "o"] : who.split("");
18
+ const bits: Record<string, Record<string, number>> = {
19
+ u: { r: 0o400, w: 0o200, x: 0o100 },
20
+ g: { r: 0o040, w: 0o020, x: 0o010 },
21
+ o: { r: 0o004, w: 0o002, x: 0o001 },
22
+ };
23
+ for (const t of targets) {
24
+ for (const p of perms.split("")) {
25
+ const bit = bits[t]?.[p];
26
+ if (bit === undefined) continue;
27
+ if (op === "+") mode |= bit;
28
+ else if (op === "-") mode &= ~bit;
29
+ else if (op === "=") {
30
+ // clear all bits for this target, then set requested
31
+ const mask = Object.values(bits[t] ?? {}).reduce((a, b) => a | b, 0);
32
+ mode = (mode & ~mask) | bit;
33
+ }
34
+ }
35
+ }
36
+ }
37
+ return mode;
38
+ }
39
+
40
+ /**
41
+ * Change file permissions (octal or symbolic).
42
+ * @category files
43
+ * @params ["<mode> <file>"]
44
+ */
4
45
  export const chmodCommand: ShellModule = {
5
46
  name: "chmod",
6
47
  description: "Change file permissions",
@@ -21,9 +62,18 @@ export const chmodCommand: ShellModule = {
21
62
  exitCode: 1,
22
63
  };
23
64
  }
24
- const mode = parseInt(modeArg, 8);
25
- if (Number.isNaN(mode)) {
26
- return { stderr: `chmod: invalid mode: ${modeArg}`, exitCode: 1 };
65
+ let mode: number;
66
+ const octal = parseInt(modeArg, 8);
67
+ if (!Number.isNaN(octal) && /^[0-7]+$/.test(modeArg)) {
68
+ mode = octal;
69
+ } else {
70
+ // symbolic mode
71
+ const existing = shell.vfs.stat(filePath).mode;
72
+ const result = applySymbolicMode(existing, modeArg);
73
+ if (result === null) {
74
+ return { stderr: `chmod: invalid mode: ${modeArg}`, exitCode: 1 };
75
+ }
76
+ mode = result;
27
77
  }
28
78
  shell.vfs.chmod(filePath, mode);
29
79
  return { exitCode: 0 };
@@ -80,6 +80,23 @@ function collectPositionals(
80
80
  return positionals;
81
81
  }
82
82
 
83
+ /**
84
+ * Returns `true` when any of the given flags appear in `args`.
85
+ *
86
+ * Matches both standalone tokens (`-s`, `--silent`) and inline forms
87
+ * (`--output=file`). Useful for simple boolean flag checks inside command
88
+ * `run` handlers.
89
+ *
90
+ * @param args Tokenized argument array from `CommandContext.args`.
91
+ * @param flags Single flag string or array of equivalent flag strings.
92
+ * @returns `true` if at least one flag is present, otherwise `false`.
93
+ *
94
+ * @example
95
+ * ```ts
96
+ * ifFlag(args, "-r") // single flag
97
+ * ifFlag(args, ["-r", "--recursive"]) // aliases
98
+ * ```
99
+ */
83
100
  export function ifFlag(args: string[], flags: string | string[]): boolean {
84
101
  const allFlags = toFlagList(flags);
85
102
 
@@ -94,6 +111,25 @@ export function ifFlag(args: string[], flags: string | string[]): boolean {
94
111
  return false;
95
112
  }
96
113
 
114
+ /**
115
+ * Returns the value associated with a flag, or `true` if the flag is present
116
+ * but has no associated value, or `undefined` if the flag is absent.
117
+ *
118
+ * Handles three forms:
119
+ * - `--output file` → returns `"file"` (next token)
120
+ * - `--output=file` → returns `"file"` (inline `=` form)
121
+ * - `--verbose` → returns `true` (flag with no value)
122
+ *
123
+ * @param args Tokenized argument array from `CommandContext.args`.
124
+ * @param flags Single flag string or array of equivalent flag strings.
125
+ * @returns The flag value string, `true` when valueless, or `undefined`.
126
+ *
127
+ * @example
128
+ * ```ts
129
+ * const output = getFlag(args, ["-o", "--output"]);
130
+ * if (typeof output === "string") { /* use path *\/ }
131
+ * ```
132
+ */
97
133
  export function getFlag(
98
134
  args: string[],
99
135
  flags: string | string[],
@@ -125,6 +161,26 @@ export function getFlag(
125
161
  return undefined;
126
162
  }
127
163
 
164
+ /**
165
+ * Returns the positional argument at the given zero-based index, skipping
166
+ * known flags and their values.
167
+ *
168
+ * Flags declared in `options.flags` are treated as boolean and skipped.
169
+ * Flags declared in `options.flagsWithValue` consume the next token too.
170
+ * Tokens after `--` are always treated as positionals.
171
+ *
172
+ * @param args Tokenized argument array from `CommandContext.args`.
173
+ * @param index Zero-based positional index to retrieve.
174
+ * @param options Optional flag declarations to skip during positional collection.
175
+ * @returns The positional value, or `undefined` if the index is out of range.
176
+ *
177
+ * @example
178
+ * ```ts
179
+ * // args = ["-r", "src", "dest"]
180
+ * getArg(args, 0, { flags: ["-r"] }) // "src"
181
+ * getArg(args, 1, { flags: ["-r"] }) // "dest"
182
+ * ```
183
+ */
128
184
  export function getArg(
129
185
  args: string[],
130
186
  index: number,
@@ -135,10 +191,28 @@ export function getArg(
135
191
  }
136
192
 
137
193
  /**
138
- * Parse arguments into flags, flags with values, and positionals.
139
- * @param args - Array of arguments to parse.
140
- * @param options - Parsing options for flags and flags with values.
141
- * @returns Parsed arguments as { flags, flagsWithValues, positionals }.
194
+ * Parses an argument array into structured flags, flag values, and positionals.
195
+ *
196
+ * - `options.flags` boolean flags (e.g. `["-r", "--recursive"]`); collected
197
+ * into a `Set<string>` and not treated as positionals.
198
+ * - `options.flagsWithValue` — flags that consume the next token or an inline
199
+ * `=value`; collected into a `Map<string, string>`.
200
+ * - All remaining tokens are positionals.
201
+ * - Tokens after `--` are always positionals, regardless of `-` prefix.
202
+ *
203
+ * @param args Tokenized argument array from `CommandContext.args`.
204
+ * @param options Flag declaration lists.
205
+ * @returns `{ flags, flagsWithValues, positionals }`.
206
+ *
207
+ * @example
208
+ * ```ts
209
+ * const { flags, flagsWithValues, positionals } = parseArgs(args, {
210
+ * flags: ["-r", "--recursive"],
211
+ * flagsWithValue: ["-o", "--output"],
212
+ * });
213
+ * const recursive = flags.has("-r");
214
+ * const output = flagsWithValues.get("-o");
215
+ * ```
142
216
  */
143
217
  export function parseArgs(
144
218
  args: string[],
@@ -2,6 +2,11 @@ import type { ShellModule } from "../types/commands";
2
2
  import { ifFlag } from "./command-helpers";
3
3
  import { assertPathAccess, resolvePath } from "./helpers";
4
4
 
5
+ /**
6
+ * Copy files or directories inside the virtual filesystem.
7
+ * @category files
8
+ * @params ["[-r] <source> <dest>"]
9
+ */
5
10
  export const cpCommand: ShellModule = {
6
11
  name: "cp",
7
12
  description: "Copy files or directories",