brsh 2.1.2 → 2.1.3
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 +7 -0
- package/Readme.md +191 -3
- package/dist/shell.es.js +750 -563
- 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 +759 -572
- 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 +56 -6
- package/lib/BaseFileSystem.js +82 -0
- package/lib/Context.js +2 -2
- package/lib/FileSystem.js +55 -26
- 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/shell.test.js +42 -0
- 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
|
|
|
@@ -147,7 +148,29 @@ class Shell extends EventEmitter {
|
|
|
147
148
|
}
|
|
148
149
|
|
|
149
150
|
if (!args.length) return;
|
|
150
|
-
let bin = args.shift();
|
|
151
|
+
let bin = args.shift().value;
|
|
152
|
+
|
|
153
|
+
// Parse output redirection (> and >>)
|
|
154
|
+
// Quoted tokens (hasQuote: true) are never treated as redirects.
|
|
155
|
+
let redirectPath = null;
|
|
156
|
+
let appendMode = false;
|
|
157
|
+
const filteredArgs = [];
|
|
158
|
+
for (let i = 0; i < args.length; i++) {
|
|
159
|
+
const { value: arg, hasQuote } = args[i];
|
|
160
|
+
if (!hasQuote && (arg === '>>' || arg === '>') && i + 1 < args.length) {
|
|
161
|
+
appendMode = arg === '>>';
|
|
162
|
+
redirectPath = args[++i].value;
|
|
163
|
+
} else if (!hasQuote && arg.startsWith('>>') && arg.length > 2) {
|
|
164
|
+
appendMode = true;
|
|
165
|
+
redirectPath = arg.slice(2);
|
|
166
|
+
} else if (!hasQuote && arg.startsWith('>') && arg.length > 1) {
|
|
167
|
+
appendMode = false;
|
|
168
|
+
redirectPath = arg.slice(1);
|
|
169
|
+
} else {
|
|
170
|
+
filteredArgs.push(arg);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
args = filteredArgs;
|
|
151
174
|
|
|
152
175
|
//short circuit for aliases
|
|
153
176
|
const alias = this.context.getAlias(bin);
|
|
@@ -181,7 +204,7 @@ class Shell extends EventEmitter {
|
|
|
181
204
|
}
|
|
182
205
|
|
|
183
206
|
if(instance.requiresFilesystem) {
|
|
184
|
-
instance.fs = this.context.fs;
|
|
207
|
+
instance.fs = this.context.fs.sandbox();
|
|
185
208
|
}
|
|
186
209
|
|
|
187
210
|
if (this._stdin !== undefined) {
|
|
@@ -196,7 +219,33 @@ class Shell extends EventEmitter {
|
|
|
196
219
|
}
|
|
197
220
|
|
|
198
221
|
return instance.runCommand().then(result => {
|
|
199
|
-
|
|
222
|
+
const stdOutput = result.getStdOutput();
|
|
223
|
+
|
|
224
|
+
if (redirectPath) {
|
|
225
|
+
const absTarget = this.context.fs.absolutePath(redirectPath);
|
|
226
|
+
const outLines = stdOutput.filter(l => l.type === 'out').map(l => l.string);
|
|
227
|
+
const content = appendMode
|
|
228
|
+
? (() => {
|
|
229
|
+
const existing = this.context.fs._getFileRaw(absTarget);
|
|
230
|
+
return typeof existing === 'string'
|
|
231
|
+
? existing + '\n' + outLines.join('\n')
|
|
232
|
+
: outLines.join('\n');
|
|
233
|
+
})()
|
|
234
|
+
: outLines.join('\n');
|
|
235
|
+
try {
|
|
236
|
+
this.context.fs.setFileByPath(absTarget, content, true);
|
|
237
|
+
} catch (e) {
|
|
238
|
+
this.emit('stdErr', `${redirectPath}: Permission denied`);
|
|
239
|
+
this.lastCode = 1;
|
|
240
|
+
this.emit('exitCode', this.lastCode);
|
|
241
|
+
delete this.runningCommand;
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
this._emitOutput(stdOutput.filter(l => l.type === 'err'));
|
|
245
|
+
} else {
|
|
246
|
+
this._emitOutput(stdOutput);
|
|
247
|
+
}
|
|
248
|
+
|
|
200
249
|
this.lastCode = result.getExitCode();
|
|
201
250
|
this.emit('exitCode', this.lastCode);
|
|
202
251
|
delete this.runningCommand;
|
|
@@ -267,7 +316,7 @@ class Shell extends EventEmitter {
|
|
|
267
316
|
|
|
268
317
|
_parseCommand(command) {
|
|
269
318
|
if(command.startsWith('./') || command.startsWith('/')) {
|
|
270
|
-
const handle = this.context.fs.
|
|
319
|
+
const handle = this.context.fs._getFileRaw(command);
|
|
271
320
|
if(!handle) {
|
|
272
321
|
throw new Error(`${command}: No such file or directory`);
|
|
273
322
|
}
|
|
@@ -316,9 +365,9 @@ class Shell extends EventEmitter {
|
|
|
316
365
|
)
|
|
317
366
|
);
|
|
318
367
|
// Keep tokens that are non-empty OR were explicitly quoted (preserves "" → '')
|
|
368
|
+
// Preserve hasQuote so redirect detection can skip quoted tokens.
|
|
319
369
|
return replaced
|
|
320
|
-
.filter(({ value, hasQuote }) => value.length > 0 || hasQuote)
|
|
321
|
-
.map(({ value }) => value);
|
|
370
|
+
.filter(({ value, hasQuote }) => value.length > 0 || hasQuote);
|
|
322
371
|
}
|
|
323
372
|
|
|
324
373
|
static _sanitiseCommandPart(string) {
|
|
@@ -471,6 +520,7 @@ Shell.STATUS_WORKING = 'WORKING';
|
|
|
471
520
|
Shell.STATUS_DESTROYED = 'DESTROYED';
|
|
472
521
|
|
|
473
522
|
Shell.Command = Command;
|
|
523
|
+
Shell.FS = BaseFileSystem;
|
|
474
524
|
|
|
475
525
|
global.shell = Shell;
|
|
476
526
|
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.
|
|
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.
|
|
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
|
-
|
|
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();
|
|
@@ -128,12 +178,6 @@ class FileSystem {
|
|
|
128
178
|
this._triggerChange();
|
|
129
179
|
}
|
|
130
180
|
|
|
131
|
-
isExecutable(path) {
|
|
132
|
-
const mode = this.getMode(path);
|
|
133
|
-
if (mode === null) return false;
|
|
134
|
-
return (mode & 0o100) !== 0;
|
|
135
|
-
}
|
|
136
|
-
|
|
137
181
|
getSize(path) {
|
|
138
182
|
const absolute = this._pathToAbsolute(path);
|
|
139
183
|
const file = this._getFileByAbsolutePath(absolute);
|
|
@@ -154,13 +198,9 @@ class FileSystem {
|
|
|
154
198
|
return this.fileSystem;
|
|
155
199
|
}
|
|
156
200
|
|
|
157
|
-
getRawPermissions() {
|
|
158
|
-
return Object.assign({}, this.permissions);
|
|
159
|
-
}
|
|
160
|
-
|
|
161
201
|
addFiles(commands) {
|
|
162
202
|
Object.entries(commands).forEach(([path, command]) => {
|
|
163
|
-
this.
|
|
203
|
+
this._setFileRaw(path, command, true);
|
|
164
204
|
});
|
|
165
205
|
}
|
|
166
206
|
|
|
@@ -170,8 +210,8 @@ class FileSystem {
|
|
|
170
210
|
const parts = absolute.split('/');
|
|
171
211
|
const end = parts.pop();
|
|
172
212
|
|
|
173
|
-
const dir = this.
|
|
174
|
-
const pathDir = this.
|
|
213
|
+
const dir = this._getFileRaw(parts.length > 1 ? parts.join('/') : '/');
|
|
214
|
+
const pathDir = this._getFileRaw(path);
|
|
175
215
|
|
|
176
216
|
if(dir && dir.constructor === Object) {
|
|
177
217
|
let options = Object.keys(dir).filter(item => item.startsWith(end));
|
|
@@ -198,18 +238,7 @@ class FileSystem {
|
|
|
198
238
|
return null;
|
|
199
239
|
}
|
|
200
240
|
|
|
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
241
|
}
|
|
214
242
|
|
|
243
|
+
|
|
215
244
|
module.exports = FileSystem;
|
package/lib/NodeFileSystem.js
CHANGED
|
@@ -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
|
-
|
|
231
|
-
|
|
232
|
-
this.
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
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
|
|
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.
|
|
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.
|
|
303
|
+
this.cli.context.fs._setFileRaw(HISTORY_FILE, this._history.join('\n'), true);
|
|
302
304
|
} catch(e) {
|
|
303
305
|
// Ignore persistence errors
|
|
304
306
|
}
|
package/lib/builtins/cat.js
CHANGED
|
@@ -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.
|
|
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
|
}
|
package/lib/builtins/echo.js
CHANGED
package/lib/builtins/false.js
CHANGED
package/lib/builtins/grep.js
CHANGED
|
@@ -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)
|
|
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)
|
|
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
|
}
|
package/lib/builtins/ls.js
CHANGED
|
@@ -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
|
-
|
|
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 =
|
|
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);
|
package/lib/builtins/test.js
CHANGED
|
@@ -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 };
|
package/lib/builtins/true.js
CHANGED
package/lib/local/alias.js
CHANGED
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
|
|
13
|
+
const s = this.context.fs.stat(this.arguments[0]);
|
|
12
14
|
|
|
13
|
-
if(!
|
|
15
|
+
if(!s) {
|
|
14
16
|
return Promise.reject('file or directory not found');
|
|
15
17
|
}
|
|
16
18
|
|
|
17
|
-
if(
|
|
19
|
+
if(s.isDirectory) {
|
|
18
20
|
this.context.setCwd(this.arguments[0]);
|
|
19
21
|
return Promise.resolve();
|
|
20
22
|
}
|
package/lib/local/chmod.js
CHANGED
|
@@ -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
|
-
|
|
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;
|
package/lib/local/clear.js
CHANGED