brsh 1.4.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.
- package/.github/workflows/busybox-compat.yml +166 -0
- package/.github/workflows/release.yml +40 -0
- package/.releaserc.json +18 -0
- package/CHANGELOG.md +355 -0
- package/bin/brsh-cmd.js +78 -0
- package/bin/exportFileSystem.js +81 -156
- package/busybox-testsuite/cat.tests +49 -0
- package/busybox-testsuite/cp.tests +206 -0
- package/busybox-testsuite/grep.tests +237 -0
- package/busybox-testsuite/testing.sh +180 -0
- package/dist/brsh.css +2 -0
- package/dist/shell.es.js +5667 -0
- package/dist/shell.es.js.map +1 -0
- package/dist/shell.min.js +14 -222
- package/dist/shell.min.js.map +1 -149
- package/dist/terminal.es.js +6228 -0
- package/dist/terminal.es.js.map +1 -0
- package/dist/terminal.min.js +113 -18
- package/dist/terminal.min.js.map +1 -21
- package/index.js +206 -49
- package/lib/Context.js +12 -4
- package/lib/FileSystem.js +99 -6
- package/lib/NodeFileSystem.js +238 -0
- package/lib/ScriptCommand.js +193 -15
- package/lib/Terminal.js +188 -12
- package/lib/WasmCommand.js +252 -0
- package/lib/buildFilesystemStructure.js +58 -0
- package/lib/builtins/cat.js +67 -9
- package/lib/builtins/echo.js +36 -5
- package/lib/builtins/false.js +13 -0
- package/lib/builtins/grep.js +267 -0
- package/lib/builtins/index.js +9 -2
- package/lib/builtins/ls.js +86 -18
- package/lib/builtins/test.js +105 -0
- package/lib/builtins/true.js +10 -0
- package/lib/local/chmod.js +72 -0
- package/lib/local/cp.js +156 -0
- package/lib/local/index.js +8 -1
- package/lib/local/jsfunction.js +58 -0
- package/lib/local/mkdir.js +44 -0
- package/lib/local/mv.js +96 -0
- package/lib/local/pwd.js +13 -0
- package/lib/local/read.js +6 -4
- package/lib/local/rm.js +71 -0
- package/lib/local/which.js +2 -2
- package/package.json +31 -34
- package/test/builtins.test.js +505 -0
- package/test/exportFileSystem.test.js +226 -0
- package/test/helpers.js +35 -0
- package/test/local-commands.test.js +593 -0
- package/test/node-filesystem.test.js +133 -0
- package/test/scripts.test.js +277 -0
- package/test/shell.test.js +245 -0
- package/test/streaming.test.js +113 -0
- package/vite.config.shell.js +21 -0
- package/vite.config.terminal.js +27 -0
- package/vitest.config.js +15 -0
package/index.js
CHANGED
|
@@ -5,6 +5,7 @@ const EventEmitter = require('events');
|
|
|
5
5
|
const Context = require('./lib/Context');
|
|
6
6
|
const LocalCommand = require('./lib/local/LocalCommand');
|
|
7
7
|
const ScriptCommand = require('./lib/ScriptCommand');
|
|
8
|
+
const WasmCommand = require('./lib/WasmCommand');
|
|
8
9
|
const Command = require('./lib/Command');
|
|
9
10
|
|
|
10
11
|
const builtins = require('./lib/builtins');
|
|
@@ -22,16 +23,21 @@ require('string.prototype.matchall').shim();
|
|
|
22
23
|
class Shell extends EventEmitter {
|
|
23
24
|
constructor(options) {
|
|
24
25
|
super();
|
|
25
|
-
const {path = '/bin', profile, hostname = 'browser', filesystem = {}, cwd = '/'} = options;
|
|
26
|
+
const {path = '/bin', profile, hostname = 'browser', filesystem = {}, permissions = {}, cwd = '/', onFsChange, useRealFilesystem = false} = options;
|
|
26
27
|
|
|
27
28
|
this.context = new Context(Object.assign({}, options, {
|
|
28
29
|
clearFn: this.clear.bind(this),
|
|
29
|
-
destroyFn: this.destroy.bind(this)
|
|
30
|
+
destroyFn: this.destroy.bind(this),
|
|
31
|
+
onFsChange: typeof onFsChange === 'function' ? onFsChange : null
|
|
30
32
|
}));
|
|
31
33
|
|
|
32
34
|
this.context.setVar('PATH', path);
|
|
33
35
|
this.context.setVar('HOST', hostname);
|
|
34
|
-
|
|
36
|
+
if (useRealFilesystem) {
|
|
37
|
+
this.context.setNodeFilesystem(cwd);
|
|
38
|
+
} else {
|
|
39
|
+
this.context.setFilesystem(filesystem, permissions);
|
|
40
|
+
}
|
|
35
41
|
this.context.setCwd(cwd);
|
|
36
42
|
|
|
37
43
|
this._loadDefaultCommands();
|
|
@@ -59,6 +65,10 @@ class Shell extends EventEmitter {
|
|
|
59
65
|
return this._lastCode;
|
|
60
66
|
}
|
|
61
67
|
|
|
68
|
+
set stdin(data) {
|
|
69
|
+
this._stdin = data;
|
|
70
|
+
}
|
|
71
|
+
|
|
62
72
|
onInput(char) {
|
|
63
73
|
if(this.runningCommand && this.runningCommand.onInput && this.runningCommand.captureInput) {
|
|
64
74
|
this.runningCommand.onInput(char);
|
|
@@ -83,21 +93,29 @@ class Shell extends EventEmitter {
|
|
|
83
93
|
}
|
|
84
94
|
|
|
85
95
|
onCommand(raw, events = true) {
|
|
86
|
-
const
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
96
|
+
const segments = this._splitInput(raw);
|
|
97
|
+
let shouldRun = true;
|
|
98
|
+
|
|
99
|
+
const promise = segments.reduce((chain, { command, op }) => {
|
|
100
|
+
return chain.then(() => {
|
|
101
|
+
if (!shouldRun) {
|
|
102
|
+
if (op === '&&') shouldRun = this._lastCode === 0;
|
|
103
|
+
else if (op === '||') shouldRun = this._lastCode !== 0;
|
|
104
|
+
else if (op === ';') shouldRun = true;
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
return this._runCommand(command).then(() => {
|
|
108
|
+
if (op === '&&') shouldRun = this._lastCode === 0;
|
|
109
|
+
else if (op === '||') shouldRun = this._lastCode !== 0;
|
|
110
|
+
else if (op === ';') shouldRun = true;
|
|
111
|
+
});
|
|
112
|
+
});
|
|
95
113
|
}, Promise.resolve());
|
|
96
114
|
|
|
97
115
|
if(events) {
|
|
98
116
|
return promise.then(() => {
|
|
99
117
|
if(!this.destroyed) {
|
|
100
|
-
this.emit('status', Shell.STATUS_READY)
|
|
118
|
+
this.emit('status', Shell.STATUS_READY);
|
|
101
119
|
}
|
|
102
120
|
})
|
|
103
121
|
.catch(() => this.emit('status', Shell.STATUS_READY));
|
|
@@ -110,14 +128,24 @@ class Shell extends EventEmitter {
|
|
|
110
128
|
return this.context.fs.autoComplete(path, this.path);
|
|
111
129
|
}
|
|
112
130
|
|
|
113
|
-
_runCommand(command) {
|
|
114
|
-
if(command.length < 1) {
|
|
115
|
-
return
|
|
131
|
+
async _runCommand(command) {
|
|
132
|
+
if(!command || command.length < 1) {
|
|
133
|
+
return;
|
|
116
134
|
}
|
|
117
135
|
|
|
118
136
|
this.emit('status', Shell.STATUS_WORKING);
|
|
119
137
|
|
|
120
|
-
let args
|
|
138
|
+
let args;
|
|
139
|
+
try {
|
|
140
|
+
args = await this._parseCommandLine(command);
|
|
141
|
+
} catch(e) {
|
|
142
|
+
this.emit('stdErr', e.message || String(e));
|
|
143
|
+
this.lastCode = 1;
|
|
144
|
+
this.emit('exitCode', this.lastCode);
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
if (!args.length) return;
|
|
121
149
|
let bin = args.shift();
|
|
122
150
|
|
|
123
151
|
//short circuit for aliases
|
|
@@ -128,12 +156,14 @@ class Shell extends EventEmitter {
|
|
|
128
156
|
|
|
129
157
|
let Cmd;
|
|
130
158
|
try {
|
|
131
|
-
Cmd = this._parseCommand(
|
|
159
|
+
Cmd = this._parseCommand(
|
|
160
|
+
(bin.startsWith('/') || bin.startsWith('./')) ? bin : bin.toLowerCase()
|
|
161
|
+
);
|
|
132
162
|
} catch(e) {
|
|
133
163
|
this.emit('stdErr', e.message);
|
|
134
164
|
this.lastCode = 1;
|
|
135
165
|
this.emit('exitCode', this.lastCode);
|
|
136
|
-
return
|
|
166
|
+
return;
|
|
137
167
|
}
|
|
138
168
|
|
|
139
169
|
let instance;
|
|
@@ -145,18 +175,22 @@ class Shell extends EventEmitter {
|
|
|
145
175
|
this.emit('stdErr', `${bin}: Command not found`);
|
|
146
176
|
this.lastCode = 1;
|
|
147
177
|
this.emit('exitCode', this.lastCode);
|
|
148
|
-
return
|
|
178
|
+
return;
|
|
149
179
|
}
|
|
150
180
|
|
|
151
181
|
if(instance.requiresFilesystem) {
|
|
152
182
|
instance.fs = this.context.fs;
|
|
153
183
|
}
|
|
154
184
|
|
|
185
|
+
if (this._stdin !== undefined) {
|
|
186
|
+
instance.stdin = this._stdin;
|
|
187
|
+
}
|
|
188
|
+
|
|
155
189
|
this.runningCommand = instance;
|
|
156
190
|
if(this.runningCommand.on) {
|
|
157
191
|
this.runningCommand.on('flush', lines => {
|
|
158
192
|
this._emitOutput(lines);
|
|
159
|
-
})
|
|
193
|
+
});
|
|
160
194
|
}
|
|
161
195
|
|
|
162
196
|
return instance.runCommand().then(result => {
|
|
@@ -195,12 +229,38 @@ class Shell extends EventEmitter {
|
|
|
195
229
|
}
|
|
196
230
|
|
|
197
231
|
_splitInput(input) {
|
|
198
|
-
|
|
199
|
-
|
|
232
|
+
const commentStripped = input.split('#')[0];
|
|
233
|
+
return this._parseOperators(commentStripped);
|
|
234
|
+
}
|
|
200
235
|
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
236
|
+
_parseOperators(input) {
|
|
237
|
+
const segments = [];
|
|
238
|
+
let current = '';
|
|
239
|
+
let i = 0;
|
|
240
|
+
let sq = false, dq = false;
|
|
241
|
+
|
|
242
|
+
while (i < input.length) {
|
|
243
|
+
const ch = input[i];
|
|
244
|
+
if (ch === "'" && !dq) { sq = !sq; current += ch; i++; continue; }
|
|
245
|
+
if (ch === '"' && !sq) { dq = !dq; current += ch; i++; continue; }
|
|
246
|
+
if (!sq && !dq) {
|
|
247
|
+
if (ch === '&' && input[i + 1] === '&') {
|
|
248
|
+
segments.push({ command: current.trim(), op: '&&' });
|
|
249
|
+
current = ''; i += 2; continue;
|
|
250
|
+
}
|
|
251
|
+
if (ch === '|' && input[i + 1] === '|') {
|
|
252
|
+
segments.push({ command: current.trim(), op: '||' });
|
|
253
|
+
current = ''; i += 2; continue;
|
|
254
|
+
}
|
|
255
|
+
if (ch === ';') {
|
|
256
|
+
segments.push({ command: current.trim(), op: ';' });
|
|
257
|
+
current = ''; i++; continue;
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
current += ch; i++;
|
|
261
|
+
}
|
|
262
|
+
if (current.trim()) segments.push({ command: current.trim(), op: null });
|
|
263
|
+
return segments;
|
|
204
264
|
}
|
|
205
265
|
|
|
206
266
|
_parseCommand(command) {
|
|
@@ -219,9 +279,19 @@ class Shell extends EventEmitter {
|
|
|
219
279
|
}
|
|
220
280
|
|
|
221
281
|
if(handle.constructor === String && handle.startsWith('#!')) {
|
|
282
|
+
if (!this.context.fs.isExecutable(command)) {
|
|
283
|
+
throw new Error(`${command}: permission denied`);
|
|
284
|
+
}
|
|
222
285
|
return ScriptCommand.bind(this, handle);
|
|
223
286
|
}
|
|
224
287
|
|
|
288
|
+
if(this._isWasmFile(command, handle)) {
|
|
289
|
+
if (!this.context.fs.isExecutable(command)) {
|
|
290
|
+
throw new Error(`${command}: permission denied`);
|
|
291
|
+
}
|
|
292
|
+
return WasmCommand.bind(null, command);
|
|
293
|
+
}
|
|
294
|
+
|
|
225
295
|
throw new Error(`${command}: permission denied`);
|
|
226
296
|
}
|
|
227
297
|
const cmd = this.context.getCommand(command);
|
|
@@ -233,16 +303,22 @@ class Shell extends EventEmitter {
|
|
|
233
303
|
return cmd;
|
|
234
304
|
}
|
|
235
305
|
|
|
236
|
-
_parseCommandLine(string) {
|
|
237
|
-
const parts = Shell.
|
|
238
|
-
const replaced =
|
|
239
|
-
|
|
240
|
-
|
|
306
|
+
async _parseCommandLine(string) {
|
|
307
|
+
const parts = Shell._splitCommand(string);
|
|
308
|
+
const replaced = await Promise.all(
|
|
309
|
+
parts.map(({ value, hasQuote }) =>
|
|
310
|
+
this._replaceVariables(value).then(v => ({ value: v, hasQuote }))
|
|
311
|
+
)
|
|
312
|
+
);
|
|
313
|
+
// Keep tokens that are non-empty OR were explicitly quoted (preserves "" → '')
|
|
314
|
+
return replaced
|
|
315
|
+
.filter(({ value, hasQuote }) => value.length > 0 || hasQuote)
|
|
316
|
+
.map(({ value }) => value);
|
|
241
317
|
}
|
|
242
318
|
|
|
243
319
|
static _sanitiseCommandPart(string) {
|
|
244
320
|
return (string.startsWith('"') && string.endsWith('"'))
|
|
245
|
-
|| (string.startsWith('
|
|
321
|
+
|| (string.startsWith("'") && string.endsWith("'"))
|
|
246
322
|
? string.substring(1, string.length - 1) : string;
|
|
247
323
|
}
|
|
248
324
|
|
|
@@ -250,55 +326,136 @@ class Shell extends EventEmitter {
|
|
|
250
326
|
return array.filter(item => item.length > 0);
|
|
251
327
|
}
|
|
252
328
|
|
|
253
|
-
_replaceVariables(part) {
|
|
254
|
-
|
|
329
|
+
async _replaceVariables(part) {
|
|
330
|
+
// 1. Arithmetic substitution $(( expr ))
|
|
331
|
+
const arithMatches = [...part.matchAll(/\$\(\(([^)]+)\)\)/g)];
|
|
332
|
+
for (const m of arithMatches) {
|
|
333
|
+
part = part.replace(m[0], String(this._evalArithmetic(m[1])));
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
// 2. Command substitution $( cmd ) — negative lookahead excludes $((
|
|
337
|
+
const cmdMatches = [...part.matchAll(/\$\((?!\()([^)]+)\)/g)];
|
|
338
|
+
for (const m of cmdMatches) {
|
|
339
|
+
const output = await this._runAndCapture(m[1]);
|
|
340
|
+
part = part.replace(m[0], output);
|
|
341
|
+
}
|
|
255
342
|
|
|
343
|
+
// 3. Variable substitution $VAR and ${VAR}
|
|
344
|
+
const matches = part.matchAll(variableRegex);
|
|
256
345
|
let match = matches.next();
|
|
257
346
|
while (!match.done) {
|
|
258
347
|
const value = this.context.getVar(match.value[1]);
|
|
259
|
-
part = part.replace(new RegExp(Shell._escapeRegExp(match.value[0]), 'g'), value);
|
|
348
|
+
part = part.replace(new RegExp(Shell._escapeRegExp(match.value[0]), 'g'), String(value));
|
|
260
349
|
match = matches.next();
|
|
261
350
|
}
|
|
262
351
|
|
|
263
352
|
return part;
|
|
264
353
|
}
|
|
265
354
|
|
|
355
|
+
_evalArithmetic(expr) {
|
|
356
|
+
const expanded = expr.replace(/\${?([\w\d]+)}?/g, (m, name) =>
|
|
357
|
+
String(parseInt(this.context.getVar(name)) || 0));
|
|
358
|
+
if (!/^[\d\s+\-*/%()]+$/.test(expanded.trim())) return 0;
|
|
359
|
+
try {
|
|
360
|
+
return new Function('return (' + expanded + ')')();
|
|
361
|
+
} catch {
|
|
362
|
+
return 0;
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
async _runAndCapture(cmd) {
|
|
367
|
+
const subshell = new global.shell({
|
|
368
|
+
cwd: this.context.fs.cwd,
|
|
369
|
+
filesystem: this.context.fs.getRaw(),
|
|
370
|
+
permissions: this.context.fs.getRawPermissions()
|
|
371
|
+
});
|
|
372
|
+
subshell.context.source(this.context);
|
|
373
|
+
|
|
374
|
+
const lines = [];
|
|
375
|
+
subshell.on('stdOut', line => lines.push(line));
|
|
376
|
+
await subshell.onCommand(cmd, false);
|
|
377
|
+
|
|
378
|
+
return lines.join('\n').trimEnd();
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
// Returns Array<{value: string, hasQuote: boolean}>.
|
|
382
|
+
// hasQuote=true means the token contained an explicit quoted segment (even empty ""),
|
|
383
|
+
// which must be preserved even if the final value is an empty string.
|
|
266
384
|
static _splitCommand(string) {
|
|
267
385
|
const separator = /\s/g;
|
|
268
386
|
let singleQuoteOpen = false;
|
|
269
387
|
let doubleQuoteOpen = false;
|
|
388
|
+
let dollarDepth = 0;
|
|
270
389
|
let tokenBuffer = [];
|
|
390
|
+
let tokenHasQuote = false;
|
|
271
391
|
const parts = [];
|
|
272
392
|
|
|
273
|
-
for (let
|
|
274
|
-
const
|
|
275
|
-
|
|
393
|
+
for (let i = 0; i < string.length; i++) {
|
|
394
|
+
const ch = string[i];
|
|
395
|
+
|
|
396
|
+
// Inside $(...) or $((...)) — keep everything verbatim, track nesting
|
|
397
|
+
if (dollarDepth > 0) {
|
|
398
|
+
tokenBuffer.push(ch);
|
|
399
|
+
if (ch === '(') dollarDepth++;
|
|
400
|
+
else if (ch === ')') dollarDepth--;
|
|
401
|
+
continue;
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
// Quote handling
|
|
405
|
+
if (ch === "'" && !doubleQuoteOpen) {
|
|
276
406
|
singleQuoteOpen = !singleQuoteOpen;
|
|
407
|
+
tokenHasQuote = true;
|
|
277
408
|
continue;
|
|
278
|
-
}
|
|
409
|
+
}
|
|
410
|
+
if (ch === '"' && !singleQuoteOpen) {
|
|
279
411
|
doubleQuoteOpen = !doubleQuoteOpen;
|
|
412
|
+
tokenHasQuote = true;
|
|
280
413
|
continue;
|
|
281
414
|
}
|
|
282
415
|
|
|
283
|
-
if (!singleQuoteOpen && !doubleQuoteOpen
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
416
|
+
if (!singleQuoteOpen && !doubleQuoteOpen) {
|
|
417
|
+
// Start of $( or $(( substitution — consume the opening '(' too
|
|
418
|
+
if (ch === '$' && i + 1 < string.length && string[i + 1] === '(') {
|
|
419
|
+
dollarDepth = 1;
|
|
420
|
+
tokenBuffer.push('$');
|
|
421
|
+
tokenBuffer.push('(');
|
|
422
|
+
i++;
|
|
423
|
+
continue;
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
// Whitespace splits tokens
|
|
427
|
+
if (ch.match(separator)) {
|
|
428
|
+
if (tokenBuffer.length > 0 || tokenHasQuote) {
|
|
429
|
+
parts.push({ value: tokenBuffer.join(''), hasQuote: tokenHasQuote });
|
|
430
|
+
tokenBuffer = [];
|
|
431
|
+
tokenHasQuote = false;
|
|
432
|
+
}
|
|
433
|
+
continue;
|
|
287
434
|
}
|
|
288
|
-
} else {
|
|
289
|
-
tokenBuffer.push(character);
|
|
290
435
|
}
|
|
436
|
+
|
|
437
|
+
tokenBuffer.push(ch);
|
|
291
438
|
}
|
|
292
|
-
|
|
293
|
-
|
|
439
|
+
|
|
440
|
+
if (tokenBuffer.length > 0 || tokenHasQuote) {
|
|
441
|
+
parts.push({ value: tokenBuffer.join(''), hasQuote: tokenHasQuote });
|
|
294
442
|
}
|
|
295
443
|
|
|
296
|
-
if(singleQuoteOpen || doubleQuoteOpen) {
|
|
444
|
+
if (singleQuoteOpen || doubleQuoteOpen) {
|
|
297
445
|
throw new Error('quote expression not closed');
|
|
298
446
|
}
|
|
299
447
|
return parts;
|
|
300
448
|
}
|
|
301
449
|
|
|
450
|
+
_isWasmFile(path, content) {
|
|
451
|
+
if (path.endsWith('.wasm') || path.endsWith('.wat')) return true;
|
|
452
|
+
if (typeof content !== 'string') return false;
|
|
453
|
+
// Binary WASM: magic bytes \0asm
|
|
454
|
+
if (content.charCodeAt(0) === 0 && content.slice(1, 4) === 'asm') return true;
|
|
455
|
+
// WAT text format
|
|
456
|
+
return content.trimStart().startsWith('(module');
|
|
457
|
+
}
|
|
458
|
+
|
|
302
459
|
static _escapeRegExp(string) {
|
|
303
460
|
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
304
461
|
}
|
|
@@ -311,4 +468,4 @@ Shell.STATUS_DESTROYED = 'DESTROYED';
|
|
|
311
468
|
Shell.Command = Command;
|
|
312
469
|
|
|
313
470
|
global.shell = Shell;
|
|
314
|
-
module.exports = Shell;
|
|
471
|
+
module.exports = Shell;
|
package/lib/Context.js
CHANGED
|
@@ -5,11 +5,11 @@ const FileSystem = require('./FileSystem');
|
|
|
5
5
|
class Context {
|
|
6
6
|
constructor(options) {
|
|
7
7
|
this.options = options;
|
|
8
|
-
const {cwd = '/', clearFn, destroyFn} = this.options;
|
|
8
|
+
const {cwd = '/', clearFn, destroyFn, onFsChange} = this.options;
|
|
9
9
|
|
|
10
10
|
this.vars = {};
|
|
11
11
|
this.aliases = {};
|
|
12
|
-
this.fs = new FileSystem(cwd);
|
|
12
|
+
this.fs = new FileSystem(cwd, {}, {}, onFsChange || null);
|
|
13
13
|
this._clearFn = clearFn;
|
|
14
14
|
this._destroyFn = destroyFn;
|
|
15
15
|
}
|
|
@@ -58,9 +58,17 @@ class Context {
|
|
|
58
58
|
return this.fs.getFileByPath(location) || null;
|
|
59
59
|
}
|
|
60
60
|
|
|
61
|
-
|
|
61
|
+
setNodeFilesystem(cwd) {
|
|
62
|
+
const NodeFileSystem = require('./NodeFileSystem');
|
|
63
|
+
const onChange = this.fs._onChange;
|
|
64
|
+
this.fs = new NodeFileSystem(cwd, onChange);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
setFilesystem(filesystem, permissions = {}) {
|
|
62
68
|
const commands = this.fs.getFileByPath(this.getVar('PATH')) || {};
|
|
63
|
-
|
|
69
|
+
const oldPermissions = this.fs.getRawPermissions();
|
|
70
|
+
const onChange = this.fs._onChange;
|
|
71
|
+
this.fs = new FileSystem(this.fs.cwd, filesystem, Object.assign({}, oldPermissions, permissions), onChange);
|
|
64
72
|
this.fs.addFiles(commands);
|
|
65
73
|
}
|
|
66
74
|
|
package/lib/FileSystem.js
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
class FileSystem {
|
|
4
|
-
constructor(cwd = '/', filesystem = {}) {
|
|
4
|
+
constructor(cwd = '/', filesystem = {}, permissions = {}, onChange = null) {
|
|
5
5
|
this.fileSystem = filesystem;
|
|
6
|
+
this.permissions = Object.assign({}, permissions);
|
|
6
7
|
this.cwd = cwd;
|
|
8
|
+
this._onChange = onChange;
|
|
9
|
+
this._changeScheduled = false;
|
|
7
10
|
}
|
|
8
11
|
|
|
9
12
|
getFileByPath(path) {
|
|
@@ -31,10 +34,14 @@ class FileSystem {
|
|
|
31
34
|
return pathParts.join('/');
|
|
32
35
|
}
|
|
33
36
|
|
|
37
|
+
absolutePath(path) {
|
|
38
|
+
return this._pathToAbsolute(path);
|
|
39
|
+
}
|
|
40
|
+
|
|
34
41
|
setFileByPath(path, content, createDir = false) {
|
|
35
42
|
const absolute = this._pathToAbsolute(path);
|
|
36
|
-
|
|
37
43
|
this._setFileByAbsolutePath(absolute, content, createDir);
|
|
44
|
+
this._triggerChange();
|
|
38
45
|
}
|
|
39
46
|
|
|
40
47
|
_setFileByAbsolutePath(absolute, content, createDir = true) {
|
|
@@ -72,6 +79,73 @@ class FileSystem {
|
|
|
72
79
|
}, this.fileSystem);
|
|
73
80
|
}
|
|
74
81
|
|
|
82
|
+
deleteFileByPath(path) {
|
|
83
|
+
const absolute = this._pathToAbsolute(path);
|
|
84
|
+
this._deleteByAbsolutePath(absolute);
|
|
85
|
+
// Remove any explicit permissions for this path and children
|
|
86
|
+
const prefix = absolute + '/';
|
|
87
|
+
Object.keys(this.permissions).forEach(k => {
|
|
88
|
+
if (k === absolute || k.startsWith(prefix)) {
|
|
89
|
+
delete this.permissions[k];
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
this._triggerChange();
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
_deleteByAbsolutePath(absolute) {
|
|
96
|
+
const parts = absolute.split('/');
|
|
97
|
+
parts.shift();
|
|
98
|
+
if (parts[parts.length - 1] === '') parts.pop();
|
|
99
|
+
|
|
100
|
+
const filename = parts.pop();
|
|
101
|
+
if (!filename) return;
|
|
102
|
+
|
|
103
|
+
const parent = parts.reduce((pointer, handle) => {
|
|
104
|
+
if (pointer && pointer[handle]) return pointer[handle];
|
|
105
|
+
return null;
|
|
106
|
+
}, this.fileSystem);
|
|
107
|
+
|
|
108
|
+
if (parent && filename in parent) {
|
|
109
|
+
delete parent[filename];
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
getMode(path) {
|
|
114
|
+
const absolute = this._pathToAbsolute(path);
|
|
115
|
+
if (this.permissions[absolute] !== undefined) {
|
|
116
|
+
return this.permissions[absolute];
|
|
117
|
+
}
|
|
118
|
+
const file = this._getFileByAbsolutePath(absolute);
|
|
119
|
+
if (file === null || file === undefined) return null;
|
|
120
|
+
if (file.constructor === Object) return 0o755;
|
|
121
|
+
if (typeof file === 'function') return 0o755;
|
|
122
|
+
return 0o644;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
setMode(path, mode) {
|
|
126
|
+
const absolute = this._pathToAbsolute(path);
|
|
127
|
+
this.permissions[absolute] = mode;
|
|
128
|
+
this._triggerChange();
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
isExecutable(path) {
|
|
132
|
+
const mode = this.getMode(path);
|
|
133
|
+
if (mode === null) return false;
|
|
134
|
+
return (mode & 0o100) !== 0;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
getSize(path) {
|
|
138
|
+
const absolute = this._pathToAbsolute(path);
|
|
139
|
+
const file = this._getFileByAbsolutePath(absolute);
|
|
140
|
+
if (file === null || file === undefined) return 0;
|
|
141
|
+
if (file.constructor === Object) return JSON.stringify(file).length;
|
|
142
|
+
return file.toString().length;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
getRawPermissions() {
|
|
146
|
+
return Object.assign({}, this.permissions);
|
|
147
|
+
}
|
|
148
|
+
|
|
75
149
|
setCwd(cwd) {
|
|
76
150
|
this.cwd = this._pathToAbsolute(cwd.endsWith('/') ? cwd : (cwd + '/'));
|
|
77
151
|
}
|
|
@@ -80,6 +154,10 @@ class FileSystem {
|
|
|
80
154
|
return this.fileSystem;
|
|
81
155
|
}
|
|
82
156
|
|
|
157
|
+
getRawPermissions() {
|
|
158
|
+
return Object.assign({}, this.permissions);
|
|
159
|
+
}
|
|
160
|
+
|
|
83
161
|
addFiles(commands) {
|
|
84
162
|
Object.entries(commands).forEach(([path, command]) => {
|
|
85
163
|
this.setFileByPath(path, command, true);
|
|
@@ -95,7 +173,7 @@ class FileSystem {
|
|
|
95
173
|
const dir = this.getFileByPath(parts.length > 1 ? parts.join('/') : '/');
|
|
96
174
|
const pathDir = this.getFileByPath(path);
|
|
97
175
|
|
|
98
|
-
if(dir.constructor === Object) {
|
|
176
|
+
if(dir && dir.constructor === Object) {
|
|
99
177
|
let options = Object.keys(dir).filter(item => item.startsWith(end));
|
|
100
178
|
if(end.length < 1) {
|
|
101
179
|
options = options.filter(item => !item.startsWith('.'));
|
|
@@ -104,19 +182,34 @@ class FileSystem {
|
|
|
104
182
|
const initialParts = initial.split('/');
|
|
105
183
|
initialParts.pop();
|
|
106
184
|
|
|
107
|
-
|
|
185
|
+
let sourceDir = dir;
|
|
186
|
+
if(options.length < 1 && initialParts.length < 1 && pathDir) {
|
|
108
187
|
options = Object.keys(pathDir).filter(item => item.startsWith(end));
|
|
188
|
+
sourceDir = pathDir;
|
|
109
189
|
}
|
|
110
190
|
|
|
111
191
|
if(options.length) {
|
|
112
192
|
return {
|
|
113
193
|
path: initialParts.length ? initialParts.join('/') : null,
|
|
114
|
-
options: options.map(
|
|
194
|
+
options: options.map(item => sourceDir[item] && sourceDir[item].constructor === Object ? item.concat('/') : item)
|
|
115
195
|
};
|
|
116
196
|
}
|
|
117
197
|
}
|
|
118
198
|
return null;
|
|
119
199
|
}
|
|
200
|
+
|
|
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
|
+
}
|
|
120
213
|
}
|
|
121
214
|
|
|
122
|
-
module.exports = FileSystem;
|
|
215
|
+
module.exports = FileSystem;
|