brsh 1.3.0 → 2.0.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 (59) hide show
  1. package/.github/workflows/busybox-compat.yml +166 -0
  2. package/.github/workflows/release.yml +40 -0
  3. package/.releaserc.json +18 -0
  4. package/CHANGELOG.md +355 -0
  5. package/bin/brsh-cmd.js +78 -0
  6. package/bin/exportFileSystem.js +81 -156
  7. package/busybox-testsuite/cat.tests +49 -0
  8. package/busybox-testsuite/cp.tests +206 -0
  9. package/busybox-testsuite/grep.tests +237 -0
  10. package/busybox-testsuite/testing.sh +180 -0
  11. package/dist/brsh.css +2 -0
  12. package/dist/shell.es.js +5667 -0
  13. package/dist/shell.es.js.map +1 -0
  14. package/dist/shell.min.js +14 -219
  15. package/dist/shell.min.js.map +1 -147
  16. package/dist/terminal.es.js +6228 -0
  17. package/dist/terminal.es.js.map +1 -0
  18. package/dist/terminal.min.js +113 -18
  19. package/dist/terminal.min.js.map +1 -21
  20. package/index.js +231 -56
  21. package/lib/Command.js +12 -1
  22. package/lib/Context.js +12 -4
  23. package/lib/FileSystem.js +99 -6
  24. package/lib/NodeFileSystem.js +238 -0
  25. package/lib/Result.js +6 -0
  26. package/lib/ScriptCommand.js +193 -15
  27. package/lib/Terminal.js +192 -11
  28. package/lib/WasmCommand.js +252 -0
  29. package/lib/buildFilesystemStructure.js +58 -0
  30. package/lib/builtins/cat.js +67 -9
  31. package/lib/builtins/echo.js +36 -5
  32. package/lib/builtins/false.js +13 -0
  33. package/lib/builtins/grep.js +267 -0
  34. package/lib/builtins/index.js +9 -2
  35. package/lib/builtins/ls.js +86 -18
  36. package/lib/builtins/test.js +105 -0
  37. package/lib/builtins/true.js +10 -0
  38. package/lib/local/chmod.js +72 -0
  39. package/lib/local/cp.js +156 -0
  40. package/lib/local/index.js +9 -1
  41. package/lib/local/jsfunction.js +58 -0
  42. package/lib/local/mkdir.js +44 -0
  43. package/lib/local/mv.js +96 -0
  44. package/lib/local/pwd.js +13 -0
  45. package/lib/local/read.js +107 -0
  46. package/lib/local/rm.js +71 -0
  47. package/lib/local/which.js +2 -2
  48. package/package.json +31 -34
  49. package/test/builtins.test.js +505 -0
  50. package/test/exportFileSystem.test.js +226 -0
  51. package/test/helpers.js +35 -0
  52. package/test/local-commands.test.js +593 -0
  53. package/test/node-filesystem.test.js +133 -0
  54. package/test/scripts.test.js +277 -0
  55. package/test/shell.test.js +245 -0
  56. package/test/streaming.test.js +113 -0
  57. package/vite.config.shell.js +21 -0
  58. package/vite.config.terminal.js +27 -0
  59. package/vitest.config.js +15 -0
@@ -0,0 +1,113 @@
1
+ import { createRequire } from 'module';
2
+ import { describe, it, expect, beforeEach } from 'vitest';
3
+ import { createShell } from './helpers.js';
4
+
5
+ // Use createRequire so Command shares the same CJS module instance as
6
+ // index.js's internal require() — required for instanceof checks.
7
+ const require = createRequire(import.meta.url);
8
+ const Command = require('../lib/Command.js');
9
+
10
+ // A command that flushes output in chunks before completing.
11
+ class ProgressCommand extends Command {
12
+ get name() { return 'progress'; }
13
+
14
+ run() {
15
+ this.stdOut = 'step:1';
16
+ this.flush();
17
+ this.stdOut = 'step:2';
18
+ this.flush();
19
+ // step:3 arrives via the resolved value (not flushed early)
20
+ return Promise.resolve('step:3');
21
+ }
22
+ }
23
+
24
+ // A command with an async pause between flushes.
25
+ class AsyncProgressCommand extends Command {
26
+ get name() { return 'async-progress'; }
27
+
28
+ run() {
29
+ return new Promise((resolve) => {
30
+ this.stdOut = 'async:1';
31
+ this.flush();
32
+
33
+ setTimeout(() => {
34
+ this.stdOut = 'async:2';
35
+ this.flush();
36
+ resolve('async:3');
37
+ }, 20);
38
+ });
39
+ }
40
+ }
41
+
42
+ describe('real-time streaming output', () => {
43
+ let shell;
44
+
45
+ beforeEach(async () => {
46
+ shell = await createShell({
47
+ filesystem: {
48
+ bin: {
49
+ progress: ProgressCommand,
50
+ 'async-progress': AsyncProgressCommand
51
+ }
52
+ }
53
+ });
54
+ });
55
+
56
+ it('receives flushed output before the command finishes (sync)', async () => {
57
+ const received = [];
58
+ shell.on('stdOut', (line) => received.push(line));
59
+ await shell.onCommand('./bin/progress');
60
+ shell.removeAllListeners('stdOut');
61
+
62
+ expect(received).toContain('step:1');
63
+ expect(received).toContain('step:2');
64
+ expect(received).toContain('step:3');
65
+ expect(received.indexOf('step:1')).toBeLessThan(received.indexOf('step:3'));
66
+ expect(received.indexOf('step:2')).toBeLessThan(received.indexOf('step:3'));
67
+ });
68
+
69
+ it('receives flushed output in order (sync)', async () => {
70
+ const received = [];
71
+ shell.on('stdOut', (line) => received.push(line));
72
+ await shell.onCommand('./bin/progress');
73
+ shell.removeAllListeners('stdOut');
74
+
75
+ expect(received).toEqual(['step:1', 'step:2', 'step:3']);
76
+ });
77
+
78
+ it('receives flushed output before the command finishes (async)', async () => {
79
+ const received = [];
80
+ shell.on('stdOut', (line) => received.push(line));
81
+ await shell.onCommand('./bin/async-progress');
82
+ shell.removeAllListeners('stdOut');
83
+
84
+ expect(received).toContain('async:1');
85
+ expect(received).toContain('async:2');
86
+ expect(received).toContain('async:3');
87
+ expect(received.indexOf('async:1')).toBeLessThan(received.indexOf('async:3'));
88
+ });
89
+
90
+ it('does not double-emit flushed lines in the final output', async () => {
91
+ const received = [];
92
+ shell.on('stdOut', (line) => received.push(line));
93
+ await shell.onCommand('./bin/progress');
94
+ shell.removeAllListeners('stdOut');
95
+
96
+ expect(received.filter((l) => l === 'step:1').length).toBe(1);
97
+ expect(received.filter((l) => l === 'step:2').length).toBe(1);
98
+ expect(received.filter((l) => l === 'step:3').length).toBe(1);
99
+ });
100
+
101
+ it('read command streams its prompt before blocking', () => {
102
+ return new Promise((resolve, reject) => {
103
+ const tid = setTimeout(() => reject(new Error('prompt not streamed')), 500);
104
+ shell.once('stdErr', (line) => {
105
+ clearTimeout(tid);
106
+ expect(line).toBe('> ');
107
+ shell.onInput('x');
108
+ resolve();
109
+ });
110
+ shell.onCommand('read -p "> " -n 1 V');
111
+ });
112
+ });
113
+ });
@@ -0,0 +1,21 @@
1
+ import { defineConfig } from 'vite';
2
+ import { nodePolyfills } from 'vite-plugin-node-polyfills';
3
+ import path from 'path';
4
+
5
+ export default defineConfig({
6
+ plugins: [
7
+ nodePolyfills()
8
+ ],
9
+ build: {
10
+ lib: {
11
+ entry: path.resolve(import.meta.dirname, 'index.js'),
12
+ name: 'Shell',
13
+ formats: ['es', 'iife'],
14
+ fileName: (format) => format === 'iife' ? 'shell.min.js' : 'shell.es.js'
15
+ },
16
+ minify: true,
17
+ sourcemap: true,
18
+ outDir: 'dist',
19
+ emptyOutDir: false
20
+ }
21
+ });
@@ -0,0 +1,27 @@
1
+ import { defineConfig } from 'vite';
2
+ import { nodePolyfills } from 'vite-plugin-node-polyfills';
3
+ import path from 'path';
4
+
5
+ export default defineConfig({
6
+ plugins: [
7
+ nodePolyfills()
8
+ ],
9
+ resolve: {
10
+ // Terminal.js does require('Shell') — point that at the real entry
11
+ alias: {
12
+ Shell: path.resolve(import.meta.dirname, 'index.js')
13
+ }
14
+ },
15
+ build: {
16
+ lib: {
17
+ entry: path.resolve(import.meta.dirname, 'lib/Terminal.js'),
18
+ name: 'Terminal',
19
+ formats: ['es', 'iife'],
20
+ fileName: (format) => format === 'iife' ? 'terminal.min.js' : 'terminal.es.js'
21
+ },
22
+ minify: true,
23
+ sourcemap: true,
24
+ outDir: 'dist',
25
+ emptyOutDir: false
26
+ }
27
+ });
@@ -0,0 +1,15 @@
1
+ import { defineConfig } from 'vitest/config';
2
+
3
+ export default defineConfig({
4
+ test: {
5
+ environment: 'node',
6
+ globals: false,
7
+ include: ['test/**/*.test.js'],
8
+ testTimeout: 10000,
9
+ coverage: {
10
+ provider: 'v8',
11
+ include: ['index.js', 'lib/**/*.js', 'bin/exportFileSystem.js'],
12
+ exclude: ['lib/Terminal.js', 'lib/terminal.css']
13
+ }
14
+ }
15
+ });