brsh 2.3.0 → 2.4.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/index.js CHANGED
@@ -102,9 +102,10 @@ class Shell extends EventEmitter {
102
102
 
103
103
  onCommand(raw, events = true) {
104
104
  const segments = this._splitInput(raw);
105
+ const groups = this._groupPipes(segments);
105
106
  let shouldRun = true;
106
107
 
107
- const promise = segments.reduce((chain, { command, op }) => {
108
+ const promise = groups.reduce((chain, { commands, op }) => {
108
109
  return chain.then(() => {
109
110
  if (!shouldRun) {
110
111
  if (op === '&&') shouldRun = this._lastCode === 0;
@@ -112,7 +113,7 @@ class Shell extends EventEmitter {
112
113
  else if (op === ';') shouldRun = true;
113
114
  return;
114
115
  }
115
- return this._runCommand(command).then(() => {
116
+ return this._runPipeChain(commands).then(() => {
116
117
  if (op === '&&') shouldRun = this._lastCode === 0;
117
118
  else if (op === '||') shouldRun = this._lastCode !== 0;
118
119
  else if (op === ';') shouldRun = true;
@@ -318,6 +319,10 @@ class Shell extends EventEmitter {
318
319
  segments.push({ command: current.trim(), op: '||' });
319
320
  current = ''; i += 2; continue;
320
321
  }
322
+ if (ch === '|') {
323
+ segments.push({ command: current.trim(), op: '|' });
324
+ current = ''; i++; continue;
325
+ }
321
326
  if (ch === ';') {
322
327
  segments.push({ command: current.trim(), op: ';' });
323
328
  current = ''; i++; continue;
@@ -464,6 +469,56 @@ class Shell extends EventEmitter {
464
469
  return lines.join('\n').trimEnd();
465
470
  }
466
471
 
472
+ _groupPipes(segments) {
473
+ const groups = [];
474
+ let commands = [];
475
+ for (const { command, op } of segments) {
476
+ commands.push(command);
477
+ if (op !== '|') {
478
+ groups.push({ commands, op });
479
+ commands = [];
480
+ }
481
+ }
482
+ if (commands.length) groups.push({ commands, op: null });
483
+ return groups;
484
+ }
485
+
486
+ async _runPipeChain(commands) {
487
+ if (commands.length === 1) {
488
+ return this._runCommand(commands[0]);
489
+ }
490
+ let stdinData = undefined;
491
+ for (let i = 0; i < commands.length; i++) {
492
+ const isLast = i === commands.length - 1;
493
+ if (isLast) {
494
+ if (stdinData !== undefined) this._stdin = stdinData;
495
+ try {
496
+ await this._runCommand(commands[i]);
497
+ } finally {
498
+ delete this._stdin;
499
+ }
500
+ } else {
501
+ stdinData = await this._captureForPipe(commands[i], stdinData);
502
+ }
503
+ }
504
+ }
505
+
506
+ async _captureForPipe(cmd, stdin) {
507
+ const subshell = new global.shell({
508
+ cwd: this.context.fs.cwd,
509
+ filesystem: this.context.fs.getRaw(),
510
+ permissions: this.context.fs.getRawPermissions()
511
+ });
512
+ subshell.context.source(this.context);
513
+ if (stdin !== undefined) subshell.stdin = stdin;
514
+ const lines = [];
515
+ subshell.on('stdOut', line => lines.push(line));
516
+ subshell.on('stdErr', line => this.emit('stdErr', line));
517
+ await subshell.onCommand(cmd, false);
518
+ this.lastCode = subshell.lastCode;
519
+ return lines.join('\n');
520
+ }
521
+
467
522
  // Returns Array<{value: string, hasQuote: boolean}>.
468
523
  // hasQuote=true means the token contained an explicit quoted segment (even empty ""),
469
524
  // which must be preserved even if the final value is an empty string.
@@ -0,0 +1,9 @@
1
+ 'use strict';
2
+
3
+ const Source = require('./source');
4
+
5
+ class Dot extends Source {
6
+ static get name() { return '.'; }
7
+ }
8
+
9
+ module.exports = Dot;
@@ -17,6 +17,7 @@ module.exports = [
17
17
  require('./read'),
18
18
  require('./jsfunction'),
19
19
  require('./curl'),
20
- require('./set')
20
+ require('./set'),
21
+ require('./dot')
21
22
  ];
22
23
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "brsh",
3
- "version": "2.3.0",
3
+ "version": "2.4.0",
4
4
  "description": "A unix like shell written in javascript",
5
5
  "main": "./dist/shell.min.js",
6
6
  "module": "./dist/shell.es.js",
@@ -285,3 +285,53 @@ describe('tabCompletion', () => {
285
285
  expect(result.options).toContain('mydir/');
286
286
  });
287
287
  });
288
+
289
+ describe('Pipes', () => {
290
+ let shell;
291
+ beforeEach(async () => { shell = await createShell(); });
292
+
293
+ it('pipes stdout of first command to stdin of second', async () => {
294
+ const { output } = await run(shell, 'echo hello world | cat');
295
+ expect(output.join('\n')).toContain('hello world');
296
+ });
297
+
298
+ it('filters output through grep via pipe', async () => {
299
+ const { output } = await run(shell, 'echo -e "foo\nbar\nbaz" | grep bar');
300
+ expect(output.join('\n')).toContain('bar');
301
+ expect(output.join('\n')).not.toContain('foo');
302
+ });
303
+
304
+ it('supports chaining three commands', async () => {
305
+ const { output } = await run(shell, 'echo -e "alpha\nbeta\nalpha" | grep alpha | cat');
306
+ const joined = output.join('\n');
307
+ expect(joined).toContain('alpha');
308
+ expect(joined).not.toContain('beta');
309
+ });
310
+
311
+ it('sets exit code from the last command in the pipe', async () => {
312
+ const { exitCode } = await run(shell, 'echo hello | grep nomatch');
313
+ expect(exitCode).toBe(1);
314
+ });
315
+
316
+ it('does not confuse || with |', async () => {
317
+ await shell.onCommand('export X=0');
318
+ const { output } = await run(shell, 'false || echo fallback');
319
+ expect(output.join('\n')).toContain('fallback');
320
+ });
321
+ });
322
+
323
+ describe('. (dot) command', () => {
324
+ it('is an alias for source and executes a script file', async () => {
325
+ const shell = await createShell({
326
+ filesystem: {
327
+ bin: {},
328
+ home: {
329
+ 'greet.sh': '#!/bin/sh\nexport GREETED=yes\n'
330
+ }
331
+ }
332
+ });
333
+ await shell.onCommand('cd /home');
334
+ await shell.onCommand('. /home/greet.sh');
335
+ expect(shell.context.getVar('GREETED')).toBe('yes');
336
+ });
337
+ });