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
package/index.js CHANGED
@@ -9,6 +9,7 @@ const Command = require('./lib/Command');
9
9
  const builtins = require('./lib/builtins');
10
10
  const localCommands = require('./lib/local');
11
11
  const events = require('events');
12
+ const BaseFileSystem = require('./lib/BaseFileSystem');
12
13
 
13
14
  const variableRegex = /\${?([\w\d]+)}?/g;
14
15
 
@@ -41,8 +42,9 @@ class Shell extends EventEmitter {
41
42
  }
42
43
  this.context.setCwd(cwd);
43
44
 
45
+ const pathDirExisted = useRealFilesystem || this.context.fs.exists(this.context.getVar('PATH'));
44
46
  this._loadDefaultCommands();
45
- this._loadBuiltinCommands();
47
+ this._loadBuiltinCommands(pathDirExisted);
46
48
 
47
49
  this._lastCode = 0;
48
50
 
@@ -83,10 +85,15 @@ class Shell extends EventEmitter {
83
85
  });
84
86
  }
85
87
 
86
- _loadBuiltinCommands() {
88
+ _loadBuiltinCommands(pathDirExisted = false) {
87
89
  builtins.forEach(command => {
88
90
  this.context.setCommand(command);
89
91
  });
92
+ // Lock the bin directory only if we implicitly created it.
93
+ // If the caller supplied their own bin dir, respect their permissions.
94
+ if (!pathDirExisted) {
95
+ this.context.fs.setMode(this.context.getVar('PATH'), 0o555);
96
+ }
90
97
  }
91
98
 
92
99
  _loadProfile(profile) {
@@ -147,7 +154,29 @@ class Shell extends EventEmitter {
147
154
  }
148
155
 
149
156
  if (!args.length) return;
150
- let bin = args.shift();
157
+ let bin = args.shift().value;
158
+
159
+ // Parse output redirection (> and >>)
160
+ // Quoted tokens (hasQuote: true) are never treated as redirects.
161
+ let redirectPath = null;
162
+ let appendMode = false;
163
+ const filteredArgs = [];
164
+ for (let i = 0; i < args.length; i++) {
165
+ const { value: arg, hasQuote } = args[i];
166
+ if (!hasQuote && (arg === '>>' || arg === '>') && i + 1 < args.length) {
167
+ appendMode = arg === '>>';
168
+ redirectPath = args[++i].value;
169
+ } else if (!hasQuote && arg.startsWith('>>') && arg.length > 2) {
170
+ appendMode = true;
171
+ redirectPath = arg.slice(2);
172
+ } else if (!hasQuote && arg.startsWith('>') && arg.length > 1) {
173
+ appendMode = false;
174
+ redirectPath = arg.slice(1);
175
+ } else {
176
+ filteredArgs.push(arg);
177
+ }
178
+ }
179
+ args = filteredArgs;
151
180
 
152
181
  //short circuit for aliases
153
182
  const alias = this.context.getAlias(bin);
@@ -181,7 +210,7 @@ class Shell extends EventEmitter {
181
210
  }
182
211
 
183
212
  if(instance.requiresFilesystem) {
184
- instance.fs = this.context.fs;
213
+ instance.fs = this.context.fs.sandbox();
185
214
  }
186
215
 
187
216
  if (this._stdin !== undefined) {
@@ -196,7 +225,33 @@ class Shell extends EventEmitter {
196
225
  }
197
226
 
198
227
  return instance.runCommand().then(result => {
199
- this._emitOutput(result.getStdOutput());
228
+ const stdOutput = result.getStdOutput();
229
+
230
+ if (redirectPath) {
231
+ const absTarget = this.context.fs.absolutePath(redirectPath);
232
+ const outLines = stdOutput.filter(l => l.type === 'out').map(l => l.string);
233
+ const content = appendMode
234
+ ? (() => {
235
+ const existing = this.context.fs._getFileRaw(absTarget);
236
+ return typeof existing === 'string'
237
+ ? existing + '\n' + outLines.join('\n')
238
+ : outLines.join('\n');
239
+ })()
240
+ : outLines.join('\n');
241
+ try {
242
+ this.context.fs.setFileByPath(absTarget, content, true);
243
+ } catch (e) {
244
+ this.emit('stdErr', `${redirectPath}: Permission denied`);
245
+ this.lastCode = 1;
246
+ this.emit('exitCode', this.lastCode);
247
+ delete this.runningCommand;
248
+ return;
249
+ }
250
+ this._emitOutput(stdOutput.filter(l => l.type === 'err'));
251
+ } else {
252
+ this._emitOutput(stdOutput);
253
+ }
254
+
200
255
  this.lastCode = result.getExitCode();
201
256
  this.emit('exitCode', this.lastCode);
202
257
  delete this.runningCommand;
@@ -267,7 +322,7 @@ class Shell extends EventEmitter {
267
322
 
268
323
  _parseCommand(command) {
269
324
  if(command.startsWith('./') || command.startsWith('/')) {
270
- const handle = this.context.fs.getFileByPath(command);
325
+ const handle = this.context.fs._getFileRaw(command);
271
326
  if(!handle) {
272
327
  throw new Error(`${command}: No such file or directory`);
273
328
  }
@@ -297,6 +352,23 @@ class Shell extends EventEmitter {
297
352
  return WasmCommand.bind(null, command);
298
353
  }
299
354
 
355
+ // Executable .js files on the real filesystem: load via require() as a Command class.
356
+ if (this.context.fs.isRealFilesystem && typeof handle === 'string' && command.endsWith('.js')) {
357
+ if (!this.context.fs.isExecutable(command)) {
358
+ throw new Error(`${command}: permission denied`);
359
+ }
360
+ const abs = this.context.fs.absolutePath(command);
361
+ try {
362
+ const mod = require(abs);
363
+ const Cls = mod.default || mod;
364
+ if (typeof Cls === 'function' && typeof Cls.prototype.run === 'function' && typeof Cls.prototype.runCommand === 'function') {
365
+ return Cls;
366
+ }
367
+ } catch(e) {
368
+ // Not a loadable command module — fall through
369
+ }
370
+ }
371
+
300
372
  throw new Error(`${command}: permission denied`);
301
373
  }
302
374
  const cmd = this.context.getCommand(command);
@@ -316,9 +388,9 @@ class Shell extends EventEmitter {
316
388
  )
317
389
  );
318
390
  // Keep tokens that are non-empty OR were explicitly quoted (preserves "" → '')
391
+ // Preserve hasQuote so redirect detection can skip quoted tokens.
319
392
  return replaced
320
- .filter(({ value, hasQuote }) => value.length > 0 || hasQuote)
321
- .map(({ value }) => value);
393
+ .filter(({ value, hasQuote }) => value.length > 0 || hasQuote);
322
394
  }
323
395
 
324
396
  static _sanitiseCommandPart(string) {
@@ -471,6 +543,7 @@ Shell.STATUS_WORKING = 'WORKING';
471
543
  Shell.STATUS_DESTROYED = 'DESTROYED';
472
544
 
473
545
  Shell.Command = Command;
546
+ Shell.FS = BaseFileSystem;
474
547
 
475
548
  global.shell = Shell;
476
549
  module.exports = Shell;
@@ -0,0 +1,82 @@
1
+ 'use strict';
2
+
3
+ // The set of methods exposed to command instances via sandbox().
4
+ // Anything not in this list is inaccessible to user-supplied commands.
5
+ const SANDBOX_ALLOWED = new Set([
6
+ 'getFileByPath', 'setFileByPath', 'deleteFileByPath',
7
+ 'exists', 'stat', 'absolutePath',
8
+ 'getMode', 'setMode',
9
+ 'isReadable', 'isWritable', 'isExecutable',
10
+ 'getSize', 'autoComplete',
11
+ 'isRealFilesystem', 'isSymlink',
12
+ ]);
13
+
14
+ /**
15
+ * Abstract base class for filesystem implementations.
16
+ *
17
+ * Subclasses must implement:
18
+ * getFileByPath(path)
19
+ * _getFileRaw(path)
20
+ * setFileByPath(path, content, createDir)
21
+ * deleteFileByPath(path)
22
+ * exists(path)
23
+ * stat(path)
24
+ * absolutePath(path)
25
+ * getMode(path)
26
+ * setMode(path, mode)
27
+ * getSize(path)
28
+ * autoComplete(initial, commandPath)
29
+ * setCwd(cwd)
30
+ * getRaw()
31
+ * getRawPermissions()
32
+ * addFiles(commands)
33
+ */
34
+ class BaseFileSystem {
35
+ isReadable(path) {
36
+ const mode = this.getMode(path);
37
+ if (mode === null) return false;
38
+ return (mode & 0o400) !== 0;
39
+ }
40
+
41
+ isWritable(path) {
42
+ const mode = this.getMode(path);
43
+ if (mode === null) return false;
44
+ return (mode & 0o200) !== 0;
45
+ }
46
+
47
+ isExecutable(path) {
48
+ const mode = this.getMode(path);
49
+ if (mode === null) return false;
50
+ return (mode & 0o100) !== 0;
51
+ }
52
+
53
+ // Returns a sandboxed proxy of this filesystem for use by commands.
54
+ // Only the public API is accessible; raw/internal methods are blocked.
55
+ sandbox() {
56
+ const target = this;
57
+ return new Proxy(Object.create(null), {
58
+ get(_, prop) {
59
+ if (!SANDBOX_ALLOWED.has(prop)) return undefined;
60
+ const val = target[prop];
61
+ return typeof val === 'function' ? val.bind(target) : val;
62
+ },
63
+ set() { return false; },
64
+ has(_, prop) { return SANDBOX_ALLOWED.has(prop); },
65
+ });
66
+ }
67
+
68
+ // Schedules a single onChange notification per microtask tick,
69
+ // so multiple mutations in one command emit only one callback.
70
+ _triggerChange() {
71
+ if (!this._onChange || this._changeScheduled) return;
72
+ this._changeScheduled = true;
73
+ Promise.resolve().then(() => {
74
+ this._changeScheduled = false;
75
+ if (typeof this._onChange === 'function') {
76
+ this._onChange(this.getRaw(), this.getRawPermissions());
77
+ }
78
+ });
79
+ }
80
+ }
81
+
82
+ module.exports = BaseFileSystem;
package/lib/Context.js CHANGED
@@ -56,7 +56,7 @@ class Context {
56
56
  getCommand(command) {
57
57
  const path = this.getVar('PATH');
58
58
  const location = `${path}/${command}`;
59
- return this.fs.getFileByPath(location) || null;
59
+ return this.fs._getFileRaw(location) || null;
60
60
  }
61
61
 
62
62
  setNodeFilesystem(cwd) {
@@ -66,7 +66,7 @@ class Context {
66
66
  }
67
67
 
68
68
  setFilesystem(filesystem, permissions = {}) {
69
- const commands = this.fs.getFileByPath(this.getVar('PATH')) || {};
69
+ const commands = this.fs._getFileRaw(this.getVar('PATH')) || {};
70
70
  const oldPermissions = this.fs.getRawPermissions();
71
71
  const onChange = this.fs._onChange;
72
72
  this.fs = new FileSystem(this.fs.cwd, filesystem, Object.assign({}, oldPermissions, permissions), onChange);
package/lib/FileSystem.js CHANGED
@@ -1,7 +1,10 @@
1
1
  'use strict';
2
2
 
3
- class FileSystem {
3
+ const BaseFileSystem = require('./BaseFileSystem');
4
+
5
+ class FileSystem extends BaseFileSystem {
4
6
  constructor(cwd = '/', filesystem = {}, permissions = {}, onChange = null) {
7
+ super();
5
8
  this.fileSystem = filesystem;
6
9
  this.permissions = Object.assign({}, permissions);
7
10
  this.cwd = cwd;
@@ -9,10 +12,38 @@ class FileSystem {
9
12
  this._changeScheduled = false;
10
13
  }
11
14
 
15
+ // Returns file content if the path exists AND is readable; null otherwise.
16
+ // Use exists() or stat() for permission-free existence/type checks.
12
17
  getFileByPath(path) {
18
+ const file = this._getFileByAbsolutePath(this._pathToAbsolute(path));
19
+ if (file === null || file === undefined) return null;
20
+ if (!this.isReadable(path)) return null;
21
+ return file;
22
+ }
23
+
24
+ // Raw read bypassing permission checks — for internal/system use only.
25
+ _getFileRaw(path) {
13
26
  return this._getFileByAbsolutePath(this._pathToAbsolute(path));
14
27
  }
15
28
 
29
+ // Returns true if the path exists regardless of permissions.
30
+ exists(path) {
31
+ const file = this._getFileByAbsolutePath(this._pathToAbsolute(path));
32
+ return file !== null && file !== undefined;
33
+ }
34
+
35
+ // Returns type metadata without content access or permission checks.
36
+ // Returns null if the path does not exist.
37
+ stat(path) {
38
+ const file = this._getFileByAbsolutePath(this._pathToAbsolute(path));
39
+ if (file === null || file === undefined) return null;
40
+ return {
41
+ isDirectory: file.constructor === Object,
42
+ isFile: typeof file === 'string',
43
+ isCommand: typeof file === 'function'
44
+ };
45
+ }
46
+
16
47
  _pathToAbsolute(path) {
17
48
  if(path.startsWith('./')) {
18
49
  path = path.substring(2);
@@ -38,7 +69,26 @@ class FileSystem {
38
69
  return this._pathToAbsolute(path);
39
70
  }
40
71
 
72
+ // Writes with permission enforcement — throws Error('Permission denied') if not writable.
73
+ // Use _setFileRaw() for internal/system writes that must bypass permission checks.
41
74
  setFileByPath(path, content, createDir = false) {
75
+ const absolute = this._pathToAbsolute(path);
76
+ const existing = this._getFileByAbsolutePath(absolute);
77
+ if (existing !== null && existing !== undefined) {
78
+ if (!this.isWritable(path)) throw new Error('Permission denied');
79
+ } else {
80
+ const parentParts = absolute.split('/');
81
+ parentParts.pop();
82
+ const parentAbs = parentParts.join('/') || '/';
83
+ const parentMode = this.getMode(parentAbs);
84
+ if (parentMode !== null && !(parentMode & 0o200)) throw new Error('Permission denied');
85
+ }
86
+ this._setFileByAbsolutePath(absolute, content, createDir);
87
+ this._triggerChange();
88
+ }
89
+
90
+ // Raw write bypassing permission checks — for internal/system use only.
91
+ _setFileRaw(path, content, createDir = false) {
42
92
  const absolute = this._pathToAbsolute(path);
43
93
  this._setFileByAbsolutePath(absolute, content, createDir);
44
94
  this._triggerChange();
@@ -81,6 +131,14 @@ class FileSystem {
81
131
 
82
132
  deleteFileByPath(path) {
83
133
  const absolute = this._pathToAbsolute(path);
134
+ // Deleting requires write permission on the parent directory.
135
+ const parentParts = absolute.split('/');
136
+ parentParts.pop();
137
+ const parentAbs = parentParts.join('/') || '/';
138
+ const parentMode = this.getMode(parentAbs);
139
+ if (parentMode !== null && !(parentMode & 0o200)) {
140
+ throw new Error(`${path}: Permission denied`);
141
+ }
84
142
  this._deleteByAbsolutePath(absolute);
85
143
  // Remove any explicit permissions for this path and children
86
144
  const prefix = absolute + '/';
@@ -118,7 +176,7 @@ class FileSystem {
118
176
  const file = this._getFileByAbsolutePath(absolute);
119
177
  if (file === null || file === undefined) return null;
120
178
  if (file.constructor === Object) return 0o755;
121
- if (typeof file === 'function') return 0o755;
179
+ if (typeof file === 'function') return 0o555;
122
180
  return 0o644;
123
181
  }
124
182
 
@@ -128,12 +186,6 @@ class FileSystem {
128
186
  this._triggerChange();
129
187
  }
130
188
 
131
- isExecutable(path) {
132
- const mode = this.getMode(path);
133
- if (mode === null) return false;
134
- return (mode & 0o100) !== 0;
135
- }
136
-
137
189
  getSize(path) {
138
190
  const absolute = this._pathToAbsolute(path);
139
191
  const file = this._getFileByAbsolutePath(absolute);
@@ -154,13 +206,9 @@ class FileSystem {
154
206
  return this.fileSystem;
155
207
  }
156
208
 
157
- getRawPermissions() {
158
- return Object.assign({}, this.permissions);
159
- }
160
-
161
209
  addFiles(commands) {
162
210
  Object.entries(commands).forEach(([path, command]) => {
163
- this.setFileByPath(path, command, true);
211
+ this._setFileRaw(path, command, true);
164
212
  });
165
213
  }
166
214
 
@@ -170,8 +218,8 @@ class FileSystem {
170
218
  const parts = absolute.split('/');
171
219
  const end = parts.pop();
172
220
 
173
- const dir = this.getFileByPath(parts.length > 1 ? parts.join('/') : '/');
174
- const pathDir = this.getFileByPath(path);
221
+ const dir = this._getFileRaw(parts.length > 1 ? parts.join('/') : '/');
222
+ const pathDir = this._getFileRaw(path);
175
223
 
176
224
  if(dir && dir.constructor === Object) {
177
225
  let options = Object.keys(dir).filter(item => item.startsWith(end));
@@ -198,18 +246,7 @@ class FileSystem {
198
246
  return null;
199
247
  }
200
248
 
201
- // Schedules a single onChange notification per microtask tick,
202
- // so multiple mutations in one command emit only one callback.
203
- _triggerChange() {
204
- if (!this._onChange || this._changeScheduled) return;
205
- this._changeScheduled = true;
206
- Promise.resolve().then(() => {
207
- this._changeScheduled = false;
208
- if (typeof this._onChange === 'function') {
209
- this._onChange(this.getRaw(), this.getRawPermissions());
210
- }
211
- });
212
- }
213
249
  }
214
250
 
251
+
215
252
  module.exports = FileSystem;
@@ -2,9 +2,11 @@
2
2
 
3
3
  const nodeFs = require('fs');
4
4
  const nodePath = require('path');
5
+ const BaseFileSystem = require('./BaseFileSystem');
5
6
 
6
- class NodeFileSystem {
7
+ class NodeFileSystem extends BaseFileSystem {
7
8
  constructor(cwd, onChange = null) {
9
+ super();
8
10
  this._onChange = onChange;
9
11
  this._changeScheduled = false;
10
12
  this._virtualFiles = {};
@@ -134,12 +136,6 @@ class NodeFileSystem {
134
136
  this._triggerChange();
135
137
  }
136
138
 
137
- isExecutable(path) {
138
- const mode = this.getMode(path);
139
- if (mode === null) return false;
140
- return (mode & 0o100) !== 0;
141
- }
142
-
143
139
  getSize(path) {
144
140
  const abs = this.absolutePath(path);
145
141
  if (this._virtualFiles[abs] !== undefined) return 0;
@@ -227,16 +223,30 @@ class NodeFileSystem {
227
223
  };
228
224
  }
229
225
 
230
- _triggerChange() {
231
- if (!this._onChange || this._changeScheduled) return;
232
- this._changeScheduled = true;
233
- Promise.resolve().then(() => {
234
- this._changeScheduled = false;
235
- if (typeof this._onChange === 'function') {
236
- this._onChange(null, null);
237
- }
238
- });
226
+ // System-internal bypass (NodeFileSystem has no permission layer over real FS)
227
+ _getFileRaw(path) {
228
+ return this.getFileByPath(path);
229
+ }
230
+
231
+ exists(path) {
232
+ const abs = this.absolutePath(path);
233
+ if (this._virtualFiles[abs] !== undefined) return true;
234
+ try { nodeFs.statSync(abs); return true; } catch(e) { return false; }
235
+ }
236
+
237
+ stat(path) {
238
+ const abs = this.absolutePath(path);
239
+ if (this._virtualFiles[abs] !== undefined) {
240
+ const v = this._virtualFiles[abs];
241
+ return { isDirectory: v !== null && v !== undefined && v.constructor === Object, isFile: typeof v === 'string', isCommand: typeof v === 'function' };
242
+ }
243
+ try {
244
+ const s = nodeFs.statSync(abs);
245
+ return { isDirectory: s.isDirectory(), isFile: s.isFile(), isCommand: false };
246
+ } catch(e) { return null; }
239
247
  }
240
248
  }
241
249
 
242
250
  module.exports = NodeFileSystem;
251
+
252
+ module.exports = NodeFileSystem;
package/lib/Terminal.js CHANGED
@@ -1,7 +1,9 @@
1
1
  'use strict';
2
2
 
3
3
  const Shell = require('Shell');
4
- const AnsiUp = require('ansi_up');
4
+ const _AnsiUp = require('ansi_up');
5
+ // ansi_up is a pre-compiled ESM→CJS shim: module.exports = { default: AnsiUp }
6
+ const AnsiUp = _AnsiUp?.default ?? _AnsiUp;
5
7
 
6
8
  const HISTORY_FILE = '/.brsh_history';
7
9
 
@@ -287,7 +289,7 @@ class Terminal {
287
289
 
288
290
  _loadHistory() {
289
291
  try {
290
- const file = this.cli.context.fs.getFileByPath(HISTORY_FILE);
292
+ const file = this.cli.context.fs._getFileRaw(HISTORY_FILE);
291
293
  if (file && typeof file === 'string' && file.length > 0) {
292
294
  this._history = file.split('\n').filter(l => l.length > 0);
293
295
  }
@@ -298,7 +300,7 @@ class Terminal {
298
300
 
299
301
  _saveHistory() {
300
302
  try {
301
- this.cli.context.fs.setFileByPath(HISTORY_FILE, this._history.join('\n'), true);
303
+ this.cli.context.fs._setFileRaw(HISTORY_FILE, this._history.join('\n'), true);
302
304
  } catch(e) {
303
305
  // Ignore persistence errors
304
306
  }
@@ -3,6 +3,8 @@
3
3
  const Command = require('../Command');
4
4
 
5
5
  class Cat extends Command {
6
+ static get name() { return 'cat'; }
7
+
6
8
  get requiresFilesystem() {
7
9
  return true;
8
10
  }
@@ -43,7 +45,8 @@ class Cat extends Command {
43
45
  }
44
46
  const file = this.fs.getFileByPath(path);
45
47
  if (file === null || file === undefined) {
46
- this.stdErr = `${path}: No such file or directory`;
48
+ const msg = this.fs.exists(path) ? 'Permission denied' : 'No such file or directory';
49
+ this.stdErr = `${path}: ${msg}`;
47
50
  this.exitCode = 1;
48
51
  return Promise.resolve();
49
52
  }
@@ -3,6 +3,8 @@
3
3
  const Command = require('../Command');
4
4
 
5
5
  class Echo extends Command {
6
+ static get name() { return 'echo'; }
7
+
6
8
  run() {
7
9
  const args = [...this.arguments];
8
10
  let noNewline = false;
@@ -3,7 +3,7 @@
3
3
  const Command = require('../Command');
4
4
 
5
5
  class False extends Command {
6
- get name() { return 'false'; }
6
+ static get name() { return 'false'; }
7
7
  run() {
8
8
  this.exitCode = 1;
9
9
  return Promise.resolve();
@@ -3,6 +3,8 @@
3
3
  const Command = require('../Command');
4
4
 
5
5
  class Grep extends Command {
6
+ static get name() { return 'grep'; }
7
+
6
8
  get requiresFilesystem() { return true; }
7
9
 
8
10
  run() {
@@ -89,7 +91,10 @@ class Grep extends Command {
89
91
  } else {
90
92
  const pf = this.fs.getFileByPath(patternFile);
91
93
  if (pf === null || pf === undefined) {
92
- if (!suppressErrors) this.stdErr = `grep: ${patternFile}: No such file or directory`;
94
+ if (!suppressErrors) {
95
+ const msg = this.fs.exists(patternFile) ? 'Permission denied' : 'No such file or directory';
96
+ this.stdErr = `grep: ${patternFile}: ${msg}`;
97
+ }
93
98
  this.exitCode = 2;
94
99
  return Promise.resolve();
95
100
  }
@@ -136,7 +141,10 @@ class Grep extends Command {
136
141
  const collectFiles = (p, topLevel = false) => {
137
142
  const entry = this.fs.getFileByPath(p);
138
143
  if (entry === null || entry === undefined) {
139
- if (!suppressErrors) this.stdErr = `grep: ${p}: No such file or directory`;
144
+ if (!suppressErrors) {
145
+ const msg = this.fs.exists(p) ? 'Permission denied' : 'No such file or directory';
146
+ this.stdErr = `grep: ${p}: ${msg}`;
147
+ }
140
148
  hasError = true;
141
149
  return;
142
150
  }
@@ -4,6 +4,8 @@ const Command = require('../Command');
4
4
  const commandLineArgs = require('command-line-args');
5
5
 
6
6
  class Ls extends Command {
7
+ static get name() { return 'ls'; }
8
+
7
9
  get requiresFilesystem() {
8
10
  return true;
9
11
  }
@@ -30,7 +32,8 @@ class Ls extends Command {
30
32
  const file = this.fs.getFileByPath(options.path);
31
33
 
32
34
  if(!file) {
33
- return Promise.reject(`${options.path}: No such file or directory`);
35
+ const msg = this.fs.exists(options.path) ? 'Permission denied' : 'No such file or directory';
36
+ return Promise.reject(`${options.path}: ${msg}`);
34
37
  }
35
38
 
36
39
  if(file.constructor === Object) {
@@ -94,8 +97,9 @@ class Ls extends Command {
94
97
  }
95
98
 
96
99
  printLong(items, basePath) {
100
+ const base = basePath.endsWith('/') ? basePath.slice(0, -1) : basePath;
97
101
  Object.entries(items).forEach(([name, content]) => {
98
- const itemPath = basePath + '/' + name;
102
+ const itemPath = base + '/' + name;
99
103
  const isDir = content !== null && content !== undefined && content.constructor === Object;
100
104
  const rawMode = this.fs.getMode(itemPath);
101
105
  const mode = rawMode !== null ? rawMode : (isDir ? 0o755 : 0o644);
@@ -3,7 +3,7 @@
3
3
  const Command = require('../Command');
4
4
 
5
5
  class Test extends Command {
6
- get name() { return 'test'; }
6
+ static get name() { return 'test'; }
7
7
  get requiresFilesystem() { return true; }
8
8
 
9
9
  run() {
@@ -97,9 +97,7 @@ class Test extends Command {
97
97
  }
98
98
 
99
99
  class TestBracket extends Test {
100
- get name() { return '['; }
100
+ static get name() { return '['; }
101
101
  }
102
- // Override the class name so Context.setCommand stores it under '[' not 'testbracket'
103
- Object.defineProperty(TestBracket, 'name', { value: '[' });
104
102
 
105
103
  module.exports = { Test, TestBracket };
@@ -3,7 +3,7 @@
3
3
  const Command = require('../Command');
4
4
 
5
5
  class True extends Command {
6
- get name() { return 'true'; }
6
+ static get name() { return 'true'; }
7
7
  run() { return Promise.resolve(); }
8
8
  }
9
9
 
@@ -3,6 +3,8 @@
3
3
  const LocalCommand = require('./LocalCommand');
4
4
 
5
5
  class Alias extends LocalCommand {
6
+ static get name() { return 'alias'; }
7
+
6
8
  run() {
7
9
  if(this.arguments.length !== 1) {
8
10
  return Promise.reject('alias requires a single argument');
package/lib/local/cd.js CHANGED
@@ -3,18 +3,20 @@
3
3
  const LocalCommand = require('./LocalCommand');
4
4
 
5
5
  class CD extends LocalCommand {
6
+ static get name() { return 'cd'; }
7
+
6
8
  run() {
7
9
  if(this.arguments.length !== 1) {
8
10
  return Promise.reject('cd requires a single argument');
9
11
  }
10
12
 
11
- const handle = this.context.fs.getFileByPath(this.arguments[0]);
13
+ const s = this.context.fs.stat(this.arguments[0]);
12
14
 
13
- if(!handle) {
15
+ if(!s) {
14
16
  return Promise.reject('file or directory not found');
15
17
  }
16
18
 
17
- if(handle.constructor === Object) {
19
+ if(s.isDirectory) {
18
20
  this.context.setCwd(this.arguments[0]);
19
21
  return Promise.resolve();
20
22
  }