brsh 3.1.3 → 3.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.
package/index.js CHANGED
@@ -18,7 +18,7 @@ const BaseFileSystem = require('./lib/BaseFileSystem');
18
18
  const BinaryContainer = require('./lib/BinaryContainer');
19
19
  const { isAsyncFilesystemSource, resolveFilesystemSource } = BinaryContainer;
20
20
 
21
- const variableRegex = /\$(?:\{([\w\d?#-]+)\}|([\w\d]+|[?#-]))/g;
21
+ const variableRegex = /\$(?:\{([\w\d?#@*-]+)\}|([\w\d]+|[?#@*-]))/g;
22
22
 
23
23
 
24
24
  const EventEmitter = events.EventEmitter || events.default || events;
@@ -1016,8 +1016,7 @@ class Shell extends EventEmitter {
1016
1016
  }
1017
1017
 
1018
1018
  _splitInput(input) {
1019
- const commentStripped = input.split('#')[0];
1020
- return this._parseOperators(commentStripped);
1019
+ return this._parseOperators(input);
1021
1020
  }
1022
1021
 
1023
1022
  _parseOperators(input) {
@@ -1031,6 +1030,14 @@ class Shell extends EventEmitter {
1031
1030
  if (ch === "'" && !dq) { sq = !sq; current += ch; i++; continue; }
1032
1031
  if (ch === '"' && !sq) { dq = !dq; current += ch; i++; continue; }
1033
1032
  if (!sq && !dq) {
1033
+ if (ch === '#' && (current.trim() === '' || /\s/.test(input[i - 1]))) {
1034
+ const nextNewline = input.indexOf('\n', i);
1035
+ if (nextNewline === -1) {
1036
+ break;
1037
+ }
1038
+ i = nextNewline;
1039
+ continue;
1040
+ }
1034
1041
  if (ch === '&' && input[i + 1] === '&') {
1035
1042
  segments.push({ command: current.trim(), op: '&&' });
1036
1043
  current = ''; i += 2; continue;
@@ -44,6 +44,14 @@ class ScriptCommand extends LocalCommand {
44
44
  });
45
45
  this.sandbox.context.source(this.context);
46
46
 
47
+ if (Array.isArray(this.arguments)) {
48
+ this.arguments.forEach((arg, i) =>
49
+ this.sandbox.context.setVar(String(i + 1), arg));
50
+ this.sandbox.context.setVar('#', String(this.arguments.length));
51
+ this.sandbox.context.setVar('@', this.arguments.join(' '));
52
+ this.sandbox.context.setVar('*', this.arguments.join(' '));
53
+ }
54
+
47
55
  this.sandbox.on('stdOut', data => { this.stdOut = data; this.flush(); });
48
56
  this.sandbox.on('stdErr', data => { this.stdErr = data; this.flush(); });
49
57
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "brsh",
3
- "version": "3.1.3",
3
+ "version": "3.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",
@@ -130,6 +130,35 @@ describe('shell script execution', () => {
130
130
  expect(errors.length).toBeGreaterThan(0);
131
131
  expect(exitCode).toBeGreaterThan(0);
132
132
  });
133
+
134
+ it('passes positional parameters to a script', async () => {
135
+ const shell = await createShell({
136
+ filesystem: {
137
+ scripts: {
138
+ 'args.sh': '#!/bin/sh\necho 1: $1\necho 2: $2\necho count: $#\necho all: $@\n'
139
+ }
140
+ },
141
+ permissions: { '/scripts/args.sh': 0o755 }
142
+ });
143
+ const { output } = await run(shell, '/scripts/args.sh "hello world" test');
144
+ expect(output).toContain('1: hello world');
145
+ expect(output).toContain('2: test');
146
+ expect(output).toContain('count: 2');
147
+ expect(output).toContain('all: hello world test');
148
+ });
149
+
150
+ it('forwards "$@" to subcommands within a script', async () => {
151
+ const shell = await createShell({
152
+ filesystem: {
153
+ scripts: {
154
+ 'forward.sh': '#!/bin/sh\necho "$@"\n'
155
+ }
156
+ },
157
+ permissions: { '/scripts/forward.sh': 0o755 }
158
+ });
159
+ const { output } = await run(shell, '/scripts/forward.sh "quoted string"');
160
+ expect(output).toContain('quoted string');
161
+ });
133
162
  });
134
163
 
135
164
  // ---------------------------------------------------------------------------