brsh 2.2.1 → 2.3.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/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
+ # [2.3.0](https://github.com/eliotstocker/Brsh-JS/compare/v2.2.1...v2.3.0) (2026-05-23)
2
+
3
+
4
+ ### Features
5
+
6
+ * add set cmd and allow -c -e etc to brsh-shell ([02be2a6](https://github.com/eliotstocker/Brsh-JS/commit/02be2a68594526d8c6685092afcf4b115b0c033c))
7
+
1
8
  ## [2.2.1](https://github.com/eliotstocker/Brsh-JS/compare/v2.2.0...v2.2.1) (2026-05-23)
2
9
 
3
10
 
package/bin/brsh-shell.js CHANGED
@@ -1,11 +1,56 @@
1
1
  #!/usr/bin/env node
2
2
  'use strict';
3
3
 
4
- // Interactive brsh shell against the real filesystem.
5
- // Run via: npm run shell
4
+ // brsh shell — interactive REPL or non-interactive command runner.
5
+ //
6
+ // Usage:
7
+ // brsh-shell interactive REPL
8
+ // brsh-shell -c "cmd" run a single command and exit
9
+ // brsh-shell script.sh [arg1 ...] run a script file and exit
10
+ // brsh-shell -e -x script.sh run with shell flags active from the start
11
+ // brsh-shell -i -c "cmd" force interactive after running -c
6
12
 
7
13
  const Shell = require('../index.js');
8
14
  const readline = require('readline');
15
+ const nodePath = require('path');
16
+
17
+ // ── CLI argument parsing ──────────────────────────────────────────────────────
18
+
19
+ const argv = process.argv.slice(2);
20
+ let commandString = null; // -c "string"
21
+ let scriptFile = null; // positional script path
22
+ let scriptArgs = []; // args after the script filename
23
+ const shellFlags = []; // single-char flags to activate via `set`
24
+ let forceInteractive = false;
25
+
26
+ for (let i = 0; i < argv.length; i++) {
27
+ const arg = argv[i];
28
+ if (arg === '--') {
29
+ if (scriptFile) scriptArgs = argv.slice(i + 1);
30
+ break;
31
+ }
32
+ if (arg === '-c') {
33
+ commandString = argv[++i] !== undefined ? argv[i] : '';
34
+ continue;
35
+ }
36
+ if (arg === '-i') {
37
+ forceInteractive = true;
38
+ continue;
39
+ }
40
+ if (arg.startsWith('-') && arg.length > 1) {
41
+ // Flags like -e, -x, -u, -n, or combined -ex
42
+ shellFlags.push(...arg.slice(1));
43
+ continue;
44
+ }
45
+ // First non-flag argument is the script file; the rest are its positional args
46
+ scriptFile = arg;
47
+ scriptArgs = argv.slice(i + 1);
48
+ break;
49
+ }
50
+
51
+ const isInteractive = forceInteractive || (commandString === null && scriptFile === null);
52
+
53
+ // ── Shell setup ───────────────────────────────────────────────────────────────
9
54
 
10
55
  const shell = new Shell({
11
56
  useRealFilesystem: true,
@@ -15,16 +60,57 @@ const shell = new Shell({
15
60
 
16
61
  shell.on('stdOut', line => process.stdout.write(line + '\n'));
17
62
  shell.on('stdErr', line => process.stderr.write(line + '\n'));
63
+
18
64
  let destroyed = false;
65
+ let rl;
66
+
19
67
  shell.on('exit', code => {
20
68
  destroyed = true;
21
- rl.close();
69
+ if (rl) rl.close();
22
70
  process.exit(code);
23
71
  });
24
- shell.on('status', status => {
25
- if (status === Shell.STATUS_READY) {
26
- ask();
72
+
73
+ // ── Non-interactive execution ─────────────────────────────────────────────────
74
+
75
+ async function runNonInteractive() {
76
+ // Apply any shell flags (-e, -x, etc.) first
77
+ if (shellFlags.length > 0) {
78
+ await shell.onCommand(`set -${shellFlags.join('')}`, false);
79
+ }
80
+
81
+ if (commandString !== null) {
82
+ // -c mode: run the command string directly
83
+ await shell.onCommand(commandString, false);
84
+ } else {
85
+ // Script file mode: set positional params then run the script
86
+ const absScript = nodePath.resolve(process.cwd(), scriptFile);
87
+ if (scriptArgs.length > 0) {
88
+ const quoted = scriptArgs.map(a => `"${a.replace(/"/g, '\\"')}"`).join(' ');
89
+ await shell.onCommand(`set -- ${quoted}`, false);
90
+ }
91
+ // Use the absolute path; _parseCommand will find it on the real filesystem.
92
+ // If the file lacks execute permission, fall back to source.
93
+ const runCmd = `"${absScript.replace(/"/g, '\\"')}"`;
94
+ await shell.onCommand(runCmd, false).catch(async () => {
95
+ await shell.onCommand(`source "${absScript.replace(/"/g, '\\"')}"`, false);
96
+ });
27
97
  }
98
+
99
+ const exitCode = parseInt(shell.context.getVar('?') || '0', 10);
100
+ process.exit(exitCode);
101
+ }
102
+
103
+ if (!isInteractive) {
104
+ shell.once('status', status => {
105
+ if (status === Shell.STATUS_READY) runNonInteractive();
106
+ });
107
+ return;
108
+ }
109
+
110
+ // ── Interactive mode ──────────────────────────────────────────────────────────
111
+
112
+ shell.on('status', status => {
113
+ if (status === Shell.STATUS_READY) ask();
28
114
  });
29
115
 
30
116
  function prompt() {
@@ -43,7 +129,7 @@ function completer(line) {
43
129
  return [completions, partial];
44
130
  }
45
131
 
46
- const rl = readline.createInterface({
132
+ rl = readline.createInterface({
47
133
  input: process.stdin,
48
134
  output: process.stdout,
49
135
  terminal: true,
@@ -71,3 +157,4 @@ rl.on('line', function(line) {
71
157
  shell.onInput('Enter');
72
158
  }
73
159
  });
160
+