brsh 1.4.0 → 2.1.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 (58) hide show
  1. package/.github/workflows/busybox-compat.yml +166 -0
  2. package/.github/workflows/pages.yml +33 -0
  3. package/.github/workflows/release.yml +40 -0
  4. package/.releaserc.json +18 -0
  5. package/CHANGELOG.md +362 -0
  6. package/bin/brsh-cmd.js +78 -0
  7. package/bin/exportFileSystem.js +81 -156
  8. package/busybox-testsuite/cat.tests +49 -0
  9. package/busybox-testsuite/cp.tests +206 -0
  10. package/busybox-testsuite/grep.tests +237 -0
  11. package/busybox-testsuite/testing.sh +180 -0
  12. package/dist/brsh.css +2 -0
  13. package/dist/shell.es.js +5667 -0
  14. package/dist/shell.es.js.map +1 -0
  15. package/dist/shell.min.js +14 -222
  16. package/dist/shell.min.js.map +1 -149
  17. package/dist/terminal.es.js +6228 -0
  18. package/dist/terminal.es.js.map +1 -0
  19. package/dist/terminal.min.js +113 -18
  20. package/dist/terminal.min.js.map +1 -21
  21. package/index.js +206 -49
  22. package/lib/Context.js +12 -4
  23. package/lib/FileSystem.js +99 -6
  24. package/lib/NodeFileSystem.js +238 -0
  25. package/lib/ScriptCommand.js +193 -15
  26. package/lib/Terminal.js +188 -12
  27. package/lib/WasmCommand.js +252 -0
  28. package/lib/buildFilesystemStructure.js +58 -0
  29. package/lib/builtins/cat.js +67 -9
  30. package/lib/builtins/echo.js +36 -5
  31. package/lib/builtins/false.js +13 -0
  32. package/lib/builtins/grep.js +267 -0
  33. package/lib/builtins/index.js +9 -2
  34. package/lib/builtins/ls.js +86 -18
  35. package/lib/builtins/test.js +105 -0
  36. package/lib/builtins/true.js +10 -0
  37. package/lib/local/chmod.js +72 -0
  38. package/lib/local/cp.js +156 -0
  39. package/lib/local/index.js +8 -1
  40. package/lib/local/jsfunction.js +58 -0
  41. package/lib/local/mkdir.js +44 -0
  42. package/lib/local/mv.js +96 -0
  43. package/lib/local/pwd.js +13 -0
  44. package/lib/local/read.js +6 -4
  45. package/lib/local/rm.js +71 -0
  46. package/lib/local/which.js +2 -2
  47. package/package.json +31 -34
  48. package/test/builtins.test.js +505 -0
  49. package/test/exportFileSystem.test.js +226 -0
  50. package/test/helpers.js +35 -0
  51. package/test/local-commands.test.js +593 -0
  52. package/test/node-filesystem.test.js +133 -0
  53. package/test/scripts.test.js +277 -0
  54. package/test/shell.test.js +245 -0
  55. package/test/streaming.test.js +113 -0
  56. package/vite.config.shell.js +21 -0
  57. package/vite.config.terminal.js +27 -0
  58. package/vitest.config.js +15 -0
@@ -0,0 +1,226 @@
1
+ import { createRequire } from 'module';
2
+ import { describe, it, expect, beforeEach, afterEach } from 'vitest';
3
+ import { spawnSync } from 'child_process';
4
+ import { mkdtempSync, writeFileSync, mkdirSync, chmodSync, readFileSync, rmSync } from 'fs';
5
+ import { tmpdir } from 'os';
6
+ import { join, resolve } from 'path';
7
+ import { fileURLToPath } from 'url';
8
+
9
+ const require = createRequire(import.meta.url);
10
+ const { buildStructure } = require('../lib/buildFilesystemStructure.js');
11
+
12
+ const CLI = fileURLToPath(new URL('../bin/exportFileSystem.js', import.meta.url));
13
+
14
+ // ---------------------------------------------------------------------------
15
+ // Helpers
16
+ // ---------------------------------------------------------------------------
17
+
18
+ function tmpDir() {
19
+ return mkdtempSync(join(tmpdir(), 'brsh-test-'));
20
+ }
21
+
22
+ function runCLI(args, cwd) {
23
+ return spawnSync(process.execPath, [CLI, ...args], {
24
+ cwd: cwd || process.cwd(),
25
+ encoding: 'utf8',
26
+ timeout: 30000
27
+ });
28
+ }
29
+
30
+ // ---------------------------------------------------------------------------
31
+ // Unit tests: buildStructure
32
+ // ---------------------------------------------------------------------------
33
+
34
+ describe('buildStructure', () => {
35
+ let root;
36
+
37
+ beforeEach(() => {
38
+ root = tmpDir();
39
+ });
40
+
41
+ afterEach(() => {
42
+ rmSync(root, { recursive: true, force: true });
43
+ });
44
+
45
+ it('returns { filesystem, permissions } with empty objects for an empty directory', () => {
46
+ const result = buildStructure(root);
47
+ expect(result).toHaveProperty('filesystem');
48
+ expect(result).toHaveProperty('permissions');
49
+ expect(result.filesystem).toEqual({});
50
+ expect(result.permissions).toEqual({});
51
+ });
52
+
53
+ it('includes a plain text file with its full name as the key', () => {
54
+ writeFileSync(join(root, 'readme.txt'), 'hello');
55
+ const { filesystem } = buildStructure(root);
56
+ expect(filesystem).toHaveProperty('readme.txt');
57
+ expect(filesystem['readme.txt']).toMatch(/^require\('/);
58
+ expect(filesystem['readme.txt']).toContain('readme.txt');
59
+ });
60
+
61
+ it('strips the .js extension from JavaScript file keys', () => {
62
+ writeFileSync(join(root, 'app.js'), 'module.exports = {}');
63
+ const { filesystem } = buildStructure(root);
64
+ expect(filesystem).toHaveProperty('app');
65
+ expect(filesystem).not.toHaveProperty('app.js');
66
+ expect(filesystem['app']).toMatch(/^require\('/);
67
+ });
68
+
69
+ it('uses an absolute path in the require() string', () => {
70
+ writeFileSync(join(root, 'data.txt'), 'content');
71
+ const { filesystem } = buildStructure(root);
72
+ const reqPath = filesystem['data.txt'].match(/require\('(.+?)'\)/)[1];
73
+ expect(reqPath).toBe(resolve(root, 'data.txt'));
74
+ });
75
+
76
+ it('recursively includes nested directories', () => {
77
+ const sub = join(root, 'subdir');
78
+ mkdirSync(sub);
79
+ writeFileSync(join(sub, 'nested.txt'), 'nested');
80
+ const { filesystem } = buildStructure(root);
81
+ expect(filesystem).toHaveProperty('subdir');
82
+ expect(filesystem.subdir).toHaveProperty('nested.txt');
83
+ });
84
+
85
+ it('handles multiple levels of nesting', () => {
86
+ const a = join(root, 'a');
87
+ const b = join(a, 'b');
88
+ mkdirSync(a);
89
+ mkdirSync(b);
90
+ writeFileSync(join(b, 'deep.txt'), 'deep');
91
+ const { filesystem } = buildStructure(root);
92
+ expect(filesystem.a.b['deep.txt']).toMatch(/^require\('/);
93
+ });
94
+
95
+ it('ignores .DS_Store files', () => {
96
+ writeFileSync(join(root, '.DS_Store'), '');
97
+ writeFileSync(join(root, 'keep.txt'), 'keep');
98
+ const { filesystem } = buildStructure(root);
99
+ expect(filesystem).not.toHaveProperty('.DS_Store');
100
+ expect(filesystem).toHaveProperty('keep.txt');
101
+ });
102
+
103
+ it('includes multiple files in the same directory', () => {
104
+ writeFileSync(join(root, 'a.txt'), 'a');
105
+ writeFileSync(join(root, 'b.txt'), 'b');
106
+ writeFileSync(join(root, 'c.js'), 'module.exports = {}');
107
+ const { filesystem } = buildStructure(root);
108
+ expect(Object.keys(filesystem)).toHaveLength(3);
109
+ expect(filesystem).toHaveProperty('a.txt');
110
+ expect(filesystem).toHaveProperty('b.txt');
111
+ expect(filesystem).toHaveProperty('c');
112
+ });
113
+
114
+ it('throws when the root directory does not exist', () => {
115
+ expect(() => buildStructure('/does/not/exist')).toThrow();
116
+ });
117
+
118
+ it('records permissions for each file', () => {
119
+ writeFileSync(join(root, 'script.sh'), '#!/bin/sh\necho hi');
120
+ chmodSync(join(root, 'script.sh'), 0o755);
121
+ const { permissions } = buildStructure(root);
122
+ expect(permissions['/script.sh']).toBe(0o755);
123
+ });
124
+
125
+ it('records read-only file permission', () => {
126
+ writeFileSync(join(root, 'readme.txt'), 'hello');
127
+ chmodSync(join(root, 'readme.txt'), 0o444);
128
+ const { permissions } = buildStructure(root);
129
+ expect(permissions['/readme.txt']).toBe(0o444);
130
+ });
131
+
132
+ it('records permissions for nested files using virtual paths', () => {
133
+ const sub = join(root, 'bin');
134
+ mkdirSync(sub);
135
+ writeFileSync(join(sub, 'tool.sh'), '#!/bin/sh');
136
+ chmodSync(join(sub, 'tool.sh'), 0o755);
137
+ const { permissions } = buildStructure(root);
138
+ expect(permissions['/bin/tool.sh']).toBe(0o755);
139
+ });
140
+ });
141
+
142
+ // ---------------------------------------------------------------------------
143
+ // CLI integration tests
144
+ // ---------------------------------------------------------------------------
145
+
146
+ describe('exportFileSystem CLI', () => {
147
+ let root;
148
+ let outDir;
149
+
150
+ beforeEach(() => {
151
+ root = tmpDir();
152
+ outDir = tmpDir();
153
+ writeFileSync(join(root, 'hello.txt'), 'Hello World');
154
+ writeFileSync(join(root, 'app.js'), 'module.exports = function() {}');
155
+ });
156
+
157
+ afterEach(() => {
158
+ rmSync(root, { recursive: true, force: true });
159
+ rmSync(outDir, { recursive: true, force: true });
160
+ });
161
+
162
+ it('exits non-zero when --variable is missing', () => {
163
+ const r = runCLI([root]);
164
+ expect(r.status).toBeGreaterThan(0);
165
+ });
166
+
167
+ it('exits non-zero when the root directory is missing', () => {
168
+ const r = runCLI(['-v', 'FS']);
169
+ expect(r.status).toBeGreaterThan(0);
170
+ });
171
+
172
+ it('exits non-zero when the root directory does not exist', () => {
173
+ const r = runCLI(['/definitely/not/a/real/path', '-v', 'FS']);
174
+ expect(r.status).toBeGreaterThan(0);
175
+ });
176
+
177
+ it('builds successfully and writes output file', () => {
178
+ const outFile = join(outDir, 'fs.js');
179
+ const r = runCLI([root, '-v', 'MyFS', '-o', outFile]);
180
+ expect(r.status).toBe(0);
181
+ expect(r.stdout).toContain('Build succeeded');
182
+ const content = readFileSync(outFile, 'utf8');
183
+ expect(content.length).toBeGreaterThan(0);
184
+ });
185
+
186
+ it('output bundle assigns the requested global variable name', () => {
187
+ const outFile = join(outDir, 'fs.js');
188
+ runCLI([root, '-v', 'MyFilesystem', '-o', outFile]);
189
+ const content = readFileSync(outFile, 'utf8');
190
+ expect(content).toContain('MyFilesystem');
191
+ });
192
+
193
+ it('output bundle is valid JavaScript (parseable)', () => {
194
+ const outFile = join(outDir, 'fs.js');
195
+ runCLI([root, '-v', 'FS', '-o', outFile]);
196
+ const content = readFileSync(outFile, 'utf8');
197
+ expect(() => new Function(content)).not.toThrow();
198
+ });
199
+
200
+ it('bundle contains text file contents', () => {
201
+ const outFile = join(outDir, 'fs.js');
202
+ runCLI([root, '-v', 'FS', '-o', outFile]);
203
+ const content = readFileSync(outFile, 'utf8');
204
+ expect(content).toContain('Hello World');
205
+ });
206
+
207
+ it('handles a nested directory structure', () => {
208
+ const sub = join(root, 'sub');
209
+ mkdirSync(sub);
210
+ writeFileSync(join(sub, 'nested.txt'), 'Nested content');
211
+ const outFile = join(outDir, 'fs.js');
212
+ const r = runCLI([root, '-v', 'FS', '-o', outFile]);
213
+ expect(r.status).toBe(0);
214
+ const content = readFileSync(outFile, 'utf8');
215
+ expect(content).toContain('Nested content');
216
+ });
217
+
218
+ it('bundle exports an object with filesystem and permissions keys', () => {
219
+ const outFile = join(outDir, 'fs.js');
220
+ runCLI([root, '-v', 'FS', '-o', outFile]);
221
+ const content = readFileSync(outFile, 'utf8');
222
+ // The generated source should contain both keys before bundling minifies it
223
+ // Just check it builds and is valid JS
224
+ expect(() => new Function(content)).not.toThrow();
225
+ });
226
+ });
@@ -0,0 +1,35 @@
1
+ import { createRequire } from 'module';
2
+
3
+ const require = createRequire(import.meta.url);
4
+ const Shell = require('../index.js');
5
+
6
+ /**
7
+ * Creates a shell instance and resolves once it emits STATUS_READY.
8
+ */
9
+ export function createShell(options = {}) {
10
+ return new Promise((resolve) => {
11
+ const shell = new Shell({ filesystem: {}, ...options });
12
+ shell.once('status', () => resolve(shell));
13
+ });
14
+ }
15
+
16
+ /**
17
+ * Runs a command and returns collected stdout/stderr and final exit code.
18
+ */
19
+ export async function run(shell, command) {
20
+ const output = [];
21
+ const errors = [];
22
+
23
+ const onOut = (line) => output.push(line);
24
+ const onErr = (line) => errors.push(line);
25
+
26
+ shell.on('stdOut', onOut);
27
+ shell.on('stdErr', onErr);
28
+
29
+ await shell.onCommand(command);
30
+
31
+ shell.removeListener('stdOut', onOut);
32
+ shell.removeListener('stdErr', onErr);
33
+
34
+ return { output, errors, exitCode: shell.lastCode };
35
+ }