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.
Files changed (46) hide show
  1. package/.github/workflows/pages.yml +12 -1
  2. package/CHANGELOG.md +14 -0
  3. package/Readme.md +191 -3
  4. package/dist/shell.es.js +809 -606
  5. package/dist/shell.es.js.map +1 -1
  6. package/dist/shell.min.js +13 -9
  7. package/dist/shell.min.js.map +1 -1
  8. package/dist/terminal.es.js +818 -615
  9. package/dist/terminal.es.js.map +1 -1
  10. package/dist/terminal.min.js +16 -12
  11. package/dist/terminal.min.js.map +1 -1
  12. package/index.js +81 -8
  13. package/lib/BaseFileSystem.js +82 -0
  14. package/lib/Context.js +2 -2
  15. package/lib/FileSystem.js +64 -27
  16. package/lib/NodeFileSystem.js +26 -16
  17. package/lib/Terminal.js +5 -3
  18. package/lib/builtins/cat.js +4 -1
  19. package/lib/builtins/echo.js +2 -0
  20. package/lib/builtins/false.js +1 -1
  21. package/lib/builtins/grep.js +10 -2
  22. package/lib/builtins/ls.js +6 -2
  23. package/lib/builtins/test.js +2 -4
  24. package/lib/builtins/true.js +1 -1
  25. package/lib/local/alias.js +2 -0
  26. package/lib/local/cd.js +5 -3
  27. package/lib/local/chmod.js +2 -3
  28. package/lib/local/clear.js +2 -0
  29. package/lib/local/cp.js +13 -8
  30. package/lib/local/curl.js +7 -2
  31. package/lib/local/exit.js +2 -0
  32. package/lib/local/export.js +2 -0
  33. package/lib/local/jsfunction.js +1 -1
  34. package/lib/local/mkdir.js +3 -3
  35. package/lib/local/mv.js +12 -6
  36. package/lib/local/pwd.js +2 -0
  37. package/lib/local/read.js +2 -0
  38. package/lib/local/rm.js +1 -1
  39. package/lib/local/source.js +4 -1
  40. package/lib/local/which.js +2 -0
  41. package/package.json +6 -4
  42. package/test/builtins.test.js +11 -0
  43. package/test/local-commands.test.js +11 -0
  44. package/test/node-filesystem.test.js +18 -0
  45. package/test/shell.test.js +42 -0
  46. package/vite.config.demo.js +35 -0
@@ -3,7 +3,7 @@
3
3
  const LocalCommand = require('./LocalCommand');
4
4
 
5
5
  class Chmod extends LocalCommand {
6
- get name() { return 'chmod'; }
6
+ static get name() { return 'chmod'; }
7
7
  get requiresFilesystem() { return true; }
8
8
 
9
9
  run() {
@@ -18,8 +18,7 @@ class Chmod extends LocalCommand {
18
18
  const paths = args.slice(1);
19
19
 
20
20
  for (const p of paths) {
21
- const file = this.fs.getFileByPath(p);
22
- if (file === null || file === undefined) {
21
+ if (!this.fs.exists(p)) {
23
22
  this.stdErr = `chmod: cannot access '${p}': No such file or directory`;
24
23
  this.exitCode = 1;
25
24
  continue;
@@ -3,6 +3,8 @@
3
3
  const LocalCommand = require('./LocalCommand');
4
4
 
5
5
  class Clear extends LocalCommand {
6
+ static get name() { return 'clear'; }
7
+
6
8
  run() {
7
9
  if(this.arguments.length > 0) {
8
10
  return Promise.reject('clear does not require arguments');
package/lib/local/cp.js CHANGED
@@ -4,7 +4,7 @@ const LocalCommand = require('./LocalCommand');
4
4
  const nodeFs = require('fs');
5
5
 
6
6
  class Cp extends LocalCommand {
7
- get name() { return 'cp'; }
7
+ static get name() { return 'cp'; }
8
8
  get requiresFilesystem() { return true; }
9
9
 
10
10
  run() {
@@ -55,8 +55,8 @@ class Cp extends LocalCommand {
55
55
 
56
56
  const dest = paths[paths.length - 1];
57
57
  const sources = paths.slice(0, -1);
58
- const destEntry = this.fs.getFileByPath(dest);
59
- const destIsDir = destEntry !== null && destEntry !== undefined && destEntry.constructor === Object;
58
+ const destStat = this.fs.stat(dest);
59
+ const destIsDir = destStat !== null && destStat.isDirectory;
60
60
 
61
61
  // Determine per-item copy mode and the mode for recursive children.
62
62
  // -H always takes effect for CL-level symlinks (follow), regardless of which mode wins.
@@ -71,8 +71,8 @@ class Cp extends LocalCommand {
71
71
  const isLink = this.fs.isSymlink ? this.fs.isSymlink(src) : false;
72
72
 
73
73
  // Stat (follows symlinks) to get effective type
74
- const stat = this.fs.getFileByPath(src);
75
- if (stat === null || stat === undefined) {
74
+ const srcStat = this.fs.stat(src);
75
+ if (srcStat === null) {
76
76
  this.stdErr = `cp: cannot stat '${src}': No such file or directory`;
77
77
  this.exitCode = 1;
78
78
  continue;
@@ -81,7 +81,7 @@ class Cp extends LocalCommand {
81
81
  // If we're preserving this symlink, it won't become a directory
82
82
  const effectiveIsDir = (isLink && clItemMode === 'preserve')
83
83
  ? false
84
- : stat.constructor === Object;
84
+ : srcStat.isDirectory;
85
85
 
86
86
  if (effectiveIsDir && !recursive) {
87
87
  this.stdErr = `cp: omitting directory '${src}'`;
@@ -118,7 +118,8 @@ class Cp extends LocalCommand {
118
118
 
119
119
  const content = this.fs.getFileByPath(src);
120
120
  if (content === null || content === undefined) {
121
- return `cp: cannot stat '${src}': No such file or directory`;
121
+ const msg = this.fs.exists(src) ? 'Permission denied' : 'No such file or directory';
122
+ return `cp: cannot stat '${src}': ${msg}`;
122
123
  }
123
124
 
124
125
  if (content.constructor === Object) {
@@ -147,7 +148,11 @@ class Cp extends LocalCommand {
147
148
  }
148
149
  }
149
150
  } else {
150
- this.fs.setFileByPath(dest, content, true);
151
+ try {
152
+ this.fs.setFileByPath(dest, content, true);
153
+ } catch(e) {
154
+ return `cp: cannot create '${dest}': ${e.message}`;
155
+ }
151
156
  }
152
157
  return null;
153
158
  }
package/lib/local/curl.js CHANGED
@@ -3,7 +3,7 @@
3
3
  const LocalCommand = require('./LocalCommand');
4
4
 
5
5
  class Curl extends LocalCommand {
6
- get name() { return 'curl'; }
6
+ static get name() { return 'curl'; }
7
7
  get requiresFilesystem() { return true; }
8
8
 
9
9
  run() {
@@ -94,7 +94,12 @@ class Curl extends LocalCommand {
94
94
  const text = await res.text();
95
95
 
96
96
  if (outputFile) {
97
- this.fs.setFileByPath(outputFile, text, false);
97
+ try {
98
+ this.fs.setFileByPath(outputFile, text, false);
99
+ } catch(e) {
100
+ this.stdErr = `curl: cannot write to '${outputFile}': ${e.message}`;
101
+ this.exitCode = 1;
102
+ }
98
103
  } else {
99
104
  text.split(/\r?\n/).forEach(line => { this.stdOut = line; });
100
105
  }
package/lib/local/exit.js CHANGED
@@ -3,6 +3,8 @@
3
3
  const LocalCommand = require('./LocalCommand');
4
4
 
5
5
  class Exit extends LocalCommand {
6
+ static get name() { return 'exit'; }
7
+
6
8
  run() {
7
9
  const code = parseInt(this.arguments[0]) || 0;
8
10
  this.exitCode = code;
@@ -3,6 +3,8 @@
3
3
  const LocalCommand = require('./LocalCommand');
4
4
 
5
5
  class Export extends LocalCommand {
6
+ static get name() { return 'export'; }
7
+
6
8
  run() {
7
9
  if(this.arguments.length !== 1) {
8
10
  return Promise.reject('export requires a single argument');
@@ -4,7 +4,7 @@ const LocalCommand = require('./LocalCommand');
4
4
  const Command = require('../Command');
5
5
 
6
6
  class JsFunction extends LocalCommand {
7
- get name() { return 'jsfunction'; }
7
+ static get name() { return 'jsfunction'; }
8
8
 
9
9
  run() {
10
10
  const [name, ...bodyParts] = this.arguments;
@@ -3,7 +3,7 @@
3
3
  const LocalCommand = require('./LocalCommand');
4
4
 
5
5
  class Mkdir extends LocalCommand {
6
- get name() { return 'mkdir'; }
6
+ static get name() { return 'mkdir'; }
7
7
  get requiresFilesystem() { return true; }
8
8
 
9
9
  run() {
@@ -22,8 +22,8 @@ class Mkdir extends LocalCommand {
22
22
  }
23
23
 
24
24
  for (const dir of paths) {
25
- const existing = this.fs.getFileByPath(dir);
26
- if (existing !== null && existing !== undefined) {
25
+ const existing = this.fs.exists(dir);
26
+ if (existing) {
27
27
  if (!createParents) {
28
28
  this.stdErr = `mkdir: cannot create directory '${dir}': File exists`;
29
29
  this.exitCode = 1;
package/lib/local/mv.js CHANGED
@@ -4,7 +4,7 @@ const LocalCommand = require('./LocalCommand');
4
4
  const nodeFs = require('fs');
5
5
 
6
6
  class Mv extends LocalCommand {
7
- get name() { return 'mv'; }
7
+ static get name() { return 'mv'; }
8
8
  get requiresFilesystem() { return true; }
9
9
 
10
10
  run() {
@@ -44,8 +44,8 @@ class Mv extends LocalCommand {
44
44
  return Promise.resolve();
45
45
  }
46
46
 
47
- const destEntry = this.fs.getFileByPath(dest);
48
- const destIsDir = destEntry !== null && destEntry !== undefined && destEntry.constructor === Object;
47
+ const destStat = this.fs.stat(dest);
48
+ const destIsDir = destStat !== null && destStat.isDirectory;
49
49
 
50
50
  for (const src of sources) {
51
51
  const targetPath = destIsDir
@@ -73,12 +73,18 @@ class Mv extends LocalCommand {
73
73
  } else {
74
74
  const srcEntry = this.fs.getFileByPath(src);
75
75
  if (srcEntry === null || srcEntry === undefined) {
76
- this.stdErr = `mv: cannot stat '${src}': No such file or directory`;
76
+ const msg = this.fs.exists(src) ? 'Permission denied' : 'No such file or directory';
77
+ this.stdErr = `mv: cannot stat '${src}': ${msg}`;
77
78
  this.exitCode = 1;
78
79
  continue;
79
80
  }
80
- this.fs.setFileByPath(targetPath, this._deepCopy(srcEntry), true);
81
- this.fs.deleteFileByPath(src);
81
+ try {
82
+ this.fs.setFileByPath(targetPath, this._deepCopy(srcEntry), true);
83
+ this.fs.deleteFileByPath(src);
84
+ } catch(e) {
85
+ this.stdErr = `mv: cannot move '${src}' to '${targetPath}': ${e.message}`;
86
+ this.exitCode = 1;
87
+ }
82
88
  }
83
89
  }
84
90
  return Promise.resolve();
package/lib/local/pwd.js CHANGED
@@ -3,6 +3,8 @@
3
3
  const LocalCommand = require('./LocalCommand');
4
4
 
5
5
  class Pwd extends LocalCommand {
6
+ static get name() { return 'pwd'; }
7
+
6
8
  run() {
7
9
  const cwd = this.context.fs.cwd;
8
10
  this.stdOut = cwd.endsWith('/') && cwd !== '/' ? cwd.slice(0, -1) : cwd;
package/lib/local/read.js CHANGED
@@ -4,6 +4,8 @@ const LocalCommand = require('./LocalCommand');
4
4
  const commandLineArgs = require('command-line-args');
5
5
 
6
6
  class Read extends LocalCommand {
7
+ static get name() { return 'read'; }
8
+
7
9
  get requiresFilesystem() {
8
10
  return false;
9
11
  }
package/lib/local/rm.js CHANGED
@@ -4,7 +4,7 @@ const LocalCommand = require('./LocalCommand');
4
4
  const nodeFs = require('fs');
5
5
 
6
6
  class Rm extends LocalCommand {
7
- get name() { return 'rm'; }
7
+ static get name() { return 'rm'; }
8
8
  get requiresFilesystem() { return true; }
9
9
 
10
10
  run() {
@@ -4,6 +4,8 @@ const ScriptCommand = require('../ScriptCommand');
4
4
  const LocalCommand = require('./LocalCommand');
5
5
 
6
6
  class Source extends LocalCommand {
7
+ static get name() { return 'source'; }
8
+
7
9
  constructor(args, context) {
8
10
  super(args, context);
9
11
  if(this.arguments.length !== 1) {
@@ -28,7 +30,8 @@ class Source extends LocalCommand {
28
30
  const handle = this.context.fs.getFileByPath(script);
29
31
 
30
32
  if (!handle) {
31
- return Promise.reject(`${script}: No such file or directory`);
33
+ const msg = this.context.fs.exists(script) ? 'Permission denied' : 'No such file or directory';
34
+ return Promise.reject(`${script}: ${msg}`);
32
35
  }
33
36
 
34
37
  if (handle.constructor === Object) {
@@ -3,6 +3,8 @@
3
3
  const LocalCommand = require('./LocalCommand');
4
4
 
5
5
  class Which extends LocalCommand {
6
+ static get name() { return 'which'; }
7
+
6
8
  run() {
7
9
  if(this.arguments.length !== 1) {
8
10
  return Promise.reject('which requires a single argument');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "brsh",
3
- "version": "2.1.2",
3
+ "version": "2.1.4",
4
4
  "description": "A unix like shell written in javascript",
5
5
  "main": "./dist/shell.min.js",
6
6
  "module": "./dist/shell.es.js",
@@ -17,11 +17,13 @@
17
17
  }
18
18
  },
19
19
  "bin": {
20
- "exportFileSystem": "bin/exportFileSystem.js"
20
+ "exportFileSystem": "bin/exportFileSystem.js",
21
+ "brsh": "bin/brsh-shell.js"
21
22
  },
22
23
  "scripts": {
23
- "start": "vite --config vite.config.js demo/",
24
- "build:demofs": "node bin/exportFileSystem.js ./demo/filesystem -o ./demo/fs.js -v fs",
24
+ "start": "node bin/exportFileSystem.js ./demo/filesystem -o ./demo/fs.js -v fs && vite --config vite.config.js demo/",
25
+ "build:demofs": "node bin/exportFileSystem.js ./demo/filesystem -o ./dist-demo/fs.js -v fs",
26
+ "build:demo": "vite build --config vite.config.demo.js && npm run build:demofs",
25
27
  "build": "npm run build:shell && npm run build:terminal",
26
28
  "build:shell": "vite build --config vite.config.shell.js",
27
29
  "build:terminal": "vite build --config vite.config.terminal.js",
@@ -136,6 +136,17 @@ describe('cat', () => {
136
136
  expect(errors.length).toBeGreaterThan(0);
137
137
  expect(exitCode).toBeGreaterThan(0);
138
138
  });
139
+
140
+ it('errors when reading a file without read permission', async () => {
141
+ const shell = await createShell({
142
+ filesystem: { home: { 'secret.txt': 'private' } },
143
+ permissions: { '/home/secret.txt': 0o000 }
144
+ });
145
+ const { errors, exitCode } = await run(shell, 'cat /home/secret.txt');
146
+ expect(errors.length).toBeGreaterThan(0);
147
+ expect(errors.join(' ')).toMatch(/permission denied/i);
148
+ expect(exitCode).toBeGreaterThan(0);
149
+ });
139
150
  });
140
151
 
141
152
  describe('true / false', () => {
@@ -360,6 +360,17 @@ describe('mkdir', () => {
360
360
  const { exitCode } = await run(shell, 'mkdir');
361
361
  expect(exitCode).toBeGreaterThan(0);
362
362
  });
363
+
364
+ it('errors when parent directory is not writable', async () => {
365
+ const shell = await createShell({
366
+ filesystem: { protected: {} },
367
+ permissions: { '/protected': 0o555 }
368
+ });
369
+ const { exitCode, errors } = await run(shell, 'mkdir /protected/newdir');
370
+ expect(exitCode).toBeGreaterThan(0);
371
+ expect(errors.join(' ')).toMatch(/permission denied/i);
372
+ expect(shell.context.fs.getFileByPath('/protected/newdir')).toBeNull();
373
+ });
363
374
  });
364
375
 
365
376
  describe('rm', () => {
@@ -130,4 +130,22 @@ describe('useRealFilesystem', () => {
130
130
  const { output } = await run(shell, scriptPath);
131
131
  expect(output).toContain('Hello World');
132
132
  });
133
+
134
+ it('executable .js files are loaded as command classes', async () => {
135
+ const commandPath = path.resolve(__dirname, '../lib/Command.js');
136
+ const jsContent = `'use strict';
137
+ const Command = require(${JSON.stringify(commandPath)});
138
+ class Hello extends Command {
139
+ static get name() { return 'hello'; }
140
+ run() { return Promise.resolve('hello from js bin'); }
141
+ }
142
+ module.exports = Hello;
143
+ `;
144
+ const binPath = path.join(tmpDir, 'hello.js');
145
+ fs.writeFileSync(binPath, jsContent);
146
+ fs.chmodSync(binPath, 0o755);
147
+ const { output, exitCode } = await run(shell, binPath);
148
+ expect(exitCode).toBe(0);
149
+ expect(output).toContain('hello from js bin');
150
+ });
133
151
  });
@@ -215,6 +215,48 @@ describe('unclosed quote error', () => {
215
215
  });
216
216
  });
217
217
 
218
+ describe('output redirection', () => {
219
+ it('writes command stdout to a file with >', async () => {
220
+ const shell = await createShell({ filesystem: { home: {} } });
221
+ await run(shell, 'echo hello > /home/out.txt');
222
+ expect(shell.context.fs.getFileByPath('/home/out.txt')).toBe('hello');
223
+ });
224
+
225
+ it('overwrites an existing file with >', async () => {
226
+ const shell = await createShell({ filesystem: { home: { 'out.txt': 'old' } } });
227
+ await run(shell, 'echo new > /home/out.txt');
228
+ expect(shell.context.fs.getFileByPath('/home/out.txt')).toBe('new');
229
+ });
230
+
231
+ it('appends to an existing file with >>', async () => {
232
+ const shell = await createShell({ filesystem: { home: { 'out.txt': 'first' } } });
233
+ await run(shell, 'echo second >> /home/out.txt');
234
+ expect(shell.context.fs.getFileByPath('/home/out.txt')).toBe('first\nsecond');
235
+ });
236
+
237
+ it('errors when target file is not writable', async () => {
238
+ const shell = await createShell({
239
+ filesystem: { home: { 'readonly.txt': 'original' } },
240
+ permissions: { '/home/readonly.txt': 0o444 }
241
+ });
242
+ const { errors, exitCode } = await run(shell, 'echo foo > /home/readonly.txt');
243
+ expect(exitCode).toBeGreaterThan(0);
244
+ expect(errors.join(' ')).toMatch(/permission denied/i);
245
+ expect(shell.context.fs.getFileByPath('/home/readonly.txt')).toBe('original');
246
+ });
247
+
248
+ it('errors when target directory is not writable', async () => {
249
+ const shell = await createShell({
250
+ filesystem: { protected: {} },
251
+ permissions: { '/protected': 0o555 }
252
+ });
253
+ const { errors, exitCode } = await run(shell, 'echo foo > /protected/newfile.txt');
254
+ expect(exitCode).toBeGreaterThan(0);
255
+ expect(errors.join(' ')).toMatch(/permission denied/i);
256
+ expect(shell.context.fs.getFileByPath('/protected/newfile.txt')).toBeNull();
257
+ });
258
+ });
259
+
218
260
  describe('tabCompletion', () => {
219
261
  it('returns matching built-in commands from PATH', async () => {
220
262
  const shell = await createShell();
@@ -0,0 +1,35 @@
1
+ import { defineConfig } from 'vite';
2
+ import { nodePolyfills } from 'vite-plugin-node-polyfills';
3
+ import { viteCommonjs } from '@originjs/vite-plugin-commonjs';
4
+ import path from 'path';
5
+
6
+ const projectRoot = import.meta.dirname;
7
+
8
+ export default defineConfig({
9
+ root: path.resolve(projectRoot, 'demo'),
10
+ plugins: [
11
+ nodePolyfills(),
12
+ viteCommonjs()
13
+ ],
14
+ resolve: {
15
+ alias: [
16
+ {
17
+ find: /^brsh\/terminal$/,
18
+ replacement: path.resolve(projectRoot, 'lib/Terminal.js')
19
+ },
20
+ {
21
+ find: /^brsh$/,
22
+ replacement: path.resolve(projectRoot, 'index.js')
23
+ },
24
+ {
25
+ find: /^Shell$/,
26
+ replacement: path.resolve(projectRoot, 'index.js')
27
+ }
28
+ ]
29
+ },
30
+ base: './',
31
+ build: {
32
+ outDir: path.resolve(projectRoot, 'dist-demo'),
33
+ emptyOutDir: true,
34
+ }
35
+ });