brsh 2.1.2 → 2.1.4
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.
- package/.github/workflows/pages.yml +12 -1
- package/CHANGELOG.md +14 -0
- package/Readme.md +191 -3
- package/dist/shell.es.js +809 -606
- package/dist/shell.es.js.map +1 -1
- package/dist/shell.min.js +13 -9
- package/dist/shell.min.js.map +1 -1
- package/dist/terminal.es.js +818 -615
- package/dist/terminal.es.js.map +1 -1
- package/dist/terminal.min.js +16 -12
- package/dist/terminal.min.js.map +1 -1
- package/index.js +81 -8
- package/lib/BaseFileSystem.js +82 -0
- package/lib/Context.js +2 -2
- package/lib/FileSystem.js +64 -27
- package/lib/NodeFileSystem.js +26 -16
- package/lib/Terminal.js +5 -3
- package/lib/builtins/cat.js +4 -1
- package/lib/builtins/echo.js +2 -0
- package/lib/builtins/false.js +1 -1
- package/lib/builtins/grep.js +10 -2
- package/lib/builtins/ls.js +6 -2
- package/lib/builtins/test.js +2 -4
- package/lib/builtins/true.js +1 -1
- package/lib/local/alias.js +2 -0
- package/lib/local/cd.js +5 -3
- package/lib/local/chmod.js +2 -3
- package/lib/local/clear.js +2 -0
- package/lib/local/cp.js +13 -8
- package/lib/local/curl.js +7 -2
- package/lib/local/exit.js +2 -0
- package/lib/local/export.js +2 -0
- package/lib/local/jsfunction.js +1 -1
- package/lib/local/mkdir.js +3 -3
- package/lib/local/mv.js +12 -6
- package/lib/local/pwd.js +2 -0
- package/lib/local/read.js +2 -0
- package/lib/local/rm.js +1 -1
- package/lib/local/source.js +4 -1
- package/lib/local/which.js +2 -0
- package/package.json +6 -4
- package/test/builtins.test.js +11 -0
- package/test/local-commands.test.js +11 -0
- package/test/node-filesystem.test.js +18 -0
- package/test/shell.test.js +42 -0
- package/vite.config.demo.js +35 -0
|
@@ -26,11 +26,22 @@ jobs:
|
|
|
26
26
|
steps:
|
|
27
27
|
- uses: actions/checkout@v4
|
|
28
28
|
|
|
29
|
+
- uses: actions/setup-node@v4
|
|
30
|
+
with:
|
|
31
|
+
node-version: '22'
|
|
32
|
+
cache: npm
|
|
33
|
+
|
|
34
|
+
- name: Install dependencies
|
|
35
|
+
run: npm ci --legacy-peer-deps
|
|
36
|
+
|
|
37
|
+
- name: Build demo
|
|
38
|
+
run: npm run build:demo
|
|
39
|
+
|
|
29
40
|
- uses: actions/configure-pages@v5
|
|
30
41
|
|
|
31
42
|
- uses: actions/upload-pages-artifact@v3
|
|
32
43
|
with:
|
|
33
|
-
path: demo/
|
|
44
|
+
path: dist-demo/
|
|
34
45
|
|
|
35
46
|
- id: deployment
|
|
36
47
|
uses: actions/deploy-pages@v4
|
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,17 @@
|
|
|
1
|
+
## [2.1.4](https://github.com/eliotstocker/Brsh-JS/compare/v2.1.3...v2.1.4) (2026-05-22)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* running js when using nodeFileSystem ([4573c51](https://github.com/eliotstocker/Brsh-JS/commit/4573c51ca5c9522eeb88978909a6cafef6fe6b30))
|
|
7
|
+
|
|
8
|
+
## [2.1.3](https://github.com/eliotstocker/Brsh-JS/compare/v2.1.2...v2.1.3) (2026-05-22)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Bug Fixes
|
|
12
|
+
|
|
13
|
+
* lots of small issues and a build time mangling problem. ([93cdcc5](https://github.com/eliotstocker/Brsh-JS/commit/93cdcc5791b92fe5c2d346e0eef4b4bf722d0058))
|
|
14
|
+
|
|
1
15
|
## [2.1.2](https://github.com/eliotstocker/Brsh-JS/compare/v2.1.1...v2.1.2) (2026-05-22)
|
|
2
16
|
|
|
3
17
|
|
package/Readme.md
CHANGED
|
@@ -6,15 +6,203 @@ An extensible shell written in Javascript with an accompanying terminal emulator
|
|
|
6
6
|
[Demo](http://eliotstocker.github.io/Brsh-JS/)
|
|
7
7
|
|
|
8
8
|
## What is it?
|
|
9
|
-
A Unix
|
|
9
|
+
A Unix-like Shell backend and Terminal Emulator that lets you embed a working terminal in a webapp, or run an interactive shell against the real filesystem in Node.
|
|
10
10
|
|
|
11
11
|
## Why?
|
|
12
12
|
No idea, you tell me
|
|
13
13
|
|
|
14
14
|
## Known Issues
|
|
15
15
|
* Input on android is not working
|
|
16
|
-
|
|
17
|
-
|
|
16
|
+
|
|
17
|
+
# How?
|
|
18
|
+
To create a new terminal first add both Terminal and Shell to your page:
|
|
19
|
+
```html
|
|
20
|
+
<script src="//unpkg.com/brsh@2/dist/shell.min.js"></script>
|
|
21
|
+
<script src="//unpkg.com/brsh@2/dist/terminal.min.js"></script>
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
You may also use it as a node module. Install via npm:
|
|
25
|
+
```shell
|
|
26
|
+
npm i brsh
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
Import the terminal in your code:
|
|
30
|
+
```js
|
|
31
|
+
// ESM
|
|
32
|
+
import Terminal from 'brsh/terminal';
|
|
33
|
+
// CJS
|
|
34
|
+
const Terminal = require('brsh/terminal');
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
You may then implement the terminal like so:
|
|
38
|
+
```js
|
|
39
|
+
const term = new Terminal({
|
|
40
|
+
el: document.body,
|
|
41
|
+
profile: '/test/.profile',
|
|
42
|
+
cwd: '/test',
|
|
43
|
+
filesystem: { test: { '.profile': 'echo "hi there!"' } },
|
|
44
|
+
cursor: 'blink',
|
|
45
|
+
outputAnimation: 'type',
|
|
46
|
+
animateSpeed: 2
|
|
47
|
+
});
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
The Terminal constructor options extend those of the Shell:
|
|
51
|
+
|
|
52
|
+
### Terminal Specific options
|
|
53
|
+
| option | required | default | description |
|
|
54
|
+
| ------ | -------- | ------- | ----------- |
|
|
55
|
+
| el | Yes | | A HTMLElement to fill with the terminal emulator |
|
|
56
|
+
| font | No | `Roboto Mono` | Name of a Google Webfont to use for the console font; a monospace font is recommended |
|
|
57
|
+
| cursor | No | `none` | One of: `none`, `block`, `blink`. If `block` or `blink`, a cursor block character is shown at the end of the current line |
|
|
58
|
+
| outputAnimation | No | `none` | One of: `none`, `type`. If `type`, each character is appended in order for a retro typing effect |
|
|
59
|
+
| animateSpeed | No | `1` | Characters per browser frame for the `type` animation. Higher is faster |
|
|
60
|
+
| onExit | No | `null` | Function called when the shell session is destroyed. Without this, typing `exit` will leave the terminal in an ended state |
|
|
61
|
+
| mobileInput | No | `click` | One of `none` or `click`. Set to `click` to enable tap-to-focus (and show the on-screen keyboard on mobile) |
|
|
62
|
+
|
|
63
|
+
### Shell options
|
|
64
|
+
| option | required | default | description |
|
|
65
|
+
| ------ | -------- | ------- | ----------- |
|
|
66
|
+
| path | No | `'/bin'` | Path in which to look for (and register) built-in binaries |
|
|
67
|
+
| profile | No | | A script to run before user interactivity (see [scripts](#writing-scripts)) |
|
|
68
|
+
| hostname | No | `'browser'` | Hostname shown in the prompt |
|
|
69
|
+
| filesystem | No | `{}` | Object representing the virtual file system. Classes extending `Command` are treated as executables. Use [exportFileSystem](bin/Readme.md) to generate this from a real directory |
|
|
70
|
+
| permissions | No | `{}` | Map of virtual absolute paths to Unix mode bits (e.g. `{ '/bin/myscript.sh': 0o755 }`). Files without an entry default to `0o644`; function/class entries default to `0o755` |
|
|
71
|
+
| cwd | No | `'/'` | Starting working directory |
|
|
72
|
+
| useRealFilesystem | No | `false` | When `true`, uses the real Node.js filesystem instead of the virtual one. Intended for CLI use |
|
|
73
|
+
|
|
74
|
+
## Running an interactive shell in Node
|
|
75
|
+
|
|
76
|
+
`npm run shell` starts an interactive brsh session against the real filesystem, with tab completion:
|
|
77
|
+
|
|
78
|
+
```shell
|
|
79
|
+
npm run shell
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
To use it programmatically:
|
|
83
|
+
|
|
84
|
+
```js
|
|
85
|
+
const Shell = require('brsh');
|
|
86
|
+
|
|
87
|
+
const shell = new Shell({
|
|
88
|
+
useRealFilesystem: true,
|
|
89
|
+
cwd: process.cwd(),
|
|
90
|
+
hostname: 'myapp'
|
|
91
|
+
});
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
## Shell Public Methods
|
|
95
|
+
| Method | Arguments | Description |
|
|
96
|
+
| ------ | --------- | ----------- |
|
|
97
|
+
| onCommand | `String` command | Run a command string in the shell, e.g. `ls -al ./root`. Returns a Promise |
|
|
98
|
+
| tabCompletion | `String` partial | Returns `{ path, options }` with completion candidates, or `null` if there are none |
|
|
99
|
+
| getPrompt | | Returns the current hostname and cwd formatted as a prompt string |
|
|
100
|
+
| destroy | `Number` code (optional) | Destroys the shell instance and emits the `exit` event |
|
|
101
|
+
| clear | | Emits the `clear` event |
|
|
102
|
+
|
|
103
|
+
## Shell Events
|
|
104
|
+
| Event | Arguments | Description |
|
|
105
|
+
| ------ | --------- | ----------- |
|
|
106
|
+
| stdOut | `String` line | A line of standard output to display to the user |
|
|
107
|
+
| stdErr | `String` line | A line of standard error to display to the user |
|
|
108
|
+
| exitCode | `Number` code | Exit code of the most recently completed command |
|
|
109
|
+
| status | `String` status | Shell status. `Shell.STATUS_READY` means a new command can be accepted; `Shell.STATUS_WORKING` means a command is running |
|
|
110
|
+
| clear | | Fired when the terminal should clear the screen |
|
|
111
|
+
| exit | `Number` code | Fired when the shell is destroyed. `code` is the final exit code |
|
|
112
|
+
|
|
113
|
+
An example CLI implementation can be found in [bin/brsh-shell.js](bin/brsh-shell.js).
|
|
114
|
+
|
|
115
|
+
# Built-in and Local commands
|
|
116
|
+
|
|
117
|
+
These are simplified versions of standard Unix commands.
|
|
118
|
+
|
|
119
|
+
**Built-ins** (always available, no filesystem lookup):
|
|
120
|
+
* `cat`
|
|
121
|
+
* `echo`
|
|
122
|
+
* `false`
|
|
123
|
+
* `grep`
|
|
124
|
+
* `ls`
|
|
125
|
+
* `test` / `[`
|
|
126
|
+
* `true`
|
|
127
|
+
|
|
128
|
+
**Local commands**:
|
|
129
|
+
* `alias`
|
|
130
|
+
* `cd`
|
|
131
|
+
* `chmod`
|
|
132
|
+
* `clear`
|
|
133
|
+
* `cp`
|
|
134
|
+
* `curl`
|
|
135
|
+
* `exit`
|
|
136
|
+
* `export`
|
|
137
|
+
* `jsfunction`
|
|
138
|
+
* `mkdir`
|
|
139
|
+
* `mv`
|
|
140
|
+
* `pwd`
|
|
141
|
+
* `read`
|
|
142
|
+
* `rm`
|
|
143
|
+
* `source`
|
|
144
|
+
* `which`
|
|
145
|
+
|
|
146
|
+
## Adding new commands
|
|
147
|
+
|
|
148
|
+
Create a class that extends `Command`. You can access it via `Shell.Command`:
|
|
149
|
+
```js
|
|
150
|
+
class MyCommand extends Shell.Command {
|
|
151
|
+
get name() { return 'mycommand'; }
|
|
152
|
+
|
|
153
|
+
run() {
|
|
154
|
+
this.stdOut = 'Hello!';
|
|
155
|
+
return Promise.resolve();
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
Or import `Command` directly:
|
|
161
|
+
```js
|
|
162
|
+
// ESM
|
|
163
|
+
import { createRequire } from 'module';
|
|
164
|
+
const require = createRequire(import.meta.url);
|
|
165
|
+
const Command = require('brsh/lib/Command');
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
For more on writing commands see the [lib docs](lib/Readme.md).
|
|
169
|
+
|
|
170
|
+
### Streaming output
|
|
171
|
+
|
|
172
|
+
Commands can emit output in real time before they finish by calling `this.flush()`:
|
|
173
|
+
|
|
174
|
+
```js
|
|
175
|
+
class ProgressCommand extends Shell.Command {
|
|
176
|
+
get name() { return 'progress'; }
|
|
177
|
+
|
|
178
|
+
run() {
|
|
179
|
+
this.stdOut = 'Starting...';
|
|
180
|
+
this.flush(); // emits stdOut immediately
|
|
181
|
+
return doWork().then(result => result);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
## Writing Scripts
|
|
187
|
+
|
|
188
|
+
Scripts must start with the shebang `#!/sh.js`:
|
|
189
|
+
|
|
190
|
+
```sh
|
|
191
|
+
#!/sh.js
|
|
192
|
+
echo "Hello from a script"
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
Scripts run in a sub-shell and do not share context with the parent. To persist context changes use `source`.
|
|
196
|
+
|
|
197
|
+
**Note:** when using [exportFileSystem](bin/Readme.md), do not give scripts a `.js` extension — that causes them to be loaded as JavaScript modules rather than script strings.
|
|
198
|
+
|
|
199
|
+
## More Examples
|
|
200
|
+
On my website I have extended the functionality adding some simple extra utilities such as an ANSI image viewer and a markdown parser based on open-source node CLI utilities.
|
|
201
|
+
|
|
202
|
+
[See the site in action here](https://piratemedia.tv/)
|
|
203
|
+
|
|
204
|
+
[GitHub source code here](https://github.com/eliotstocker/piratemedia.tv)
|
|
205
|
+
|
|
18
206
|
|
|
19
207
|
# How?
|
|
20
208
|
To create a new terminal first add both Terminal and Shell to your page like so:
|