brsh 1.4.0 → 2.1.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/pages.yml +33 -0
- package/.github/workflows/release.yml +40 -0
- package/.releaserc.json +18 -0
- package/CHANGELOG.md +362 -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/lib/builtins/echo.js
CHANGED
|
@@ -1,16 +1,47 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
const Command = require('../Command');
|
|
4
|
-
const Result = require('../Result');
|
|
5
4
|
|
|
6
5
|
class Echo extends Command {
|
|
7
6
|
run() {
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
const args = [...this.arguments];
|
|
8
|
+
let noNewline = false;
|
|
9
|
+
let interpretEscapes = false;
|
|
10
|
+
|
|
11
|
+
// Parse leading flags: -n, -e, -E, -ne, -en, etc.
|
|
12
|
+
while (args.length > 0 && /^-[neE]+$/.test(args[0])) {
|
|
13
|
+
const flag = args.shift();
|
|
14
|
+
if (flag.includes('n')) noNewline = true;
|
|
15
|
+
if (flag.includes('e')) interpretEscapes = true;
|
|
16
|
+
if (flag === '-E') interpretEscapes = false;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
let output = args.join(' ');
|
|
20
|
+
|
|
21
|
+
if (interpretEscapes) {
|
|
22
|
+
output = output
|
|
23
|
+
.replace(/\\c.*$/s, () => { noNewline = true; return ''; })
|
|
24
|
+
.replace(/\\0([0-7]{1,3})/g, (_, n) => String.fromCharCode(parseInt(n, 8)))
|
|
25
|
+
.replace(/\\x([0-9a-fA-F]{1,2})/g, (_, n) => String.fromCharCode(parseInt(n, 16)))
|
|
26
|
+
.replace(/\\n/g, '\n')
|
|
27
|
+
.replace(/\\t/g, '\t')
|
|
28
|
+
.replace(/\\r/g, '\r')
|
|
29
|
+
.replace(/\\a/g, '\x07')
|
|
30
|
+
.replace(/\\b/g, '\x08')
|
|
31
|
+
.replace(/\\e/g, '\x1b')
|
|
32
|
+
.replace(/\\f/g, '\x0c')
|
|
33
|
+
.replace(/\\v/g, '\x0b')
|
|
34
|
+
.replace(/\\\\/g, '\\');
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
if (noNewline) {
|
|
38
|
+
// Emit directly so runCommand doesn't add an extra blank line
|
|
39
|
+
output.split(/\r?\n/).forEach(line => this.stdOut = line);
|
|
40
|
+
return Promise.resolve();
|
|
10
41
|
}
|
|
11
42
|
|
|
12
|
-
return Promise.resolve(
|
|
43
|
+
return Promise.resolve(output);
|
|
13
44
|
}
|
|
14
45
|
}
|
|
15
46
|
|
|
16
|
-
module.exports = Echo;
|
|
47
|
+
module.exports = Echo;
|
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const Command = require('../Command');
|
|
4
|
+
|
|
5
|
+
class Grep extends Command {
|
|
6
|
+
get requiresFilesystem() { return true; }
|
|
7
|
+
|
|
8
|
+
run() {
|
|
9
|
+
let ignoreCase = false;
|
|
10
|
+
let invertMatch = false;
|
|
11
|
+
let lineNumbers = false;
|
|
12
|
+
let countOnly = false;
|
|
13
|
+
let listFiles = false;
|
|
14
|
+
let listNonMatching = false;
|
|
15
|
+
let quiet = false;
|
|
16
|
+
let recursive = false;
|
|
17
|
+
let fixedString = false;
|
|
18
|
+
let onlyMatching = false;
|
|
19
|
+
let wordMatch = false;
|
|
20
|
+
let lineMatch = false;
|
|
21
|
+
let suppressErrors = false;
|
|
22
|
+
let patternFile = null;
|
|
23
|
+
let patternSourceGiven = false;
|
|
24
|
+
const patterns = [];
|
|
25
|
+
const paths = [];
|
|
26
|
+
|
|
27
|
+
const args = [...this.arguments];
|
|
28
|
+
let i = 0;
|
|
29
|
+
|
|
30
|
+
while (i < args.length) {
|
|
31
|
+
const arg = args[i];
|
|
32
|
+
if (arg === '--') { i++; break; }
|
|
33
|
+
|
|
34
|
+
if (arg.startsWith('-') && arg.length > 1) {
|
|
35
|
+
const flags = arg.slice(1);
|
|
36
|
+
let skipRest = false;
|
|
37
|
+
for (let j = 0; j < flags.length && !skipRest; j++) {
|
|
38
|
+
switch (flags[j]) {
|
|
39
|
+
case 'i': ignoreCase = true; break;
|
|
40
|
+
case 'v': invertMatch = true; break;
|
|
41
|
+
case 'n': lineNumbers = true; break;
|
|
42
|
+
case 'c': countOnly = true; break;
|
|
43
|
+
case 'l': listFiles = true; break;
|
|
44
|
+
case 'L': listNonMatching = true; break;
|
|
45
|
+
case 'q': quiet = true; break;
|
|
46
|
+
case 'r': case 'R': recursive = true; break;
|
|
47
|
+
case 'F': fixedString = true; break;
|
|
48
|
+
case 'E': break;
|
|
49
|
+
case 'o': onlyMatching = true; break;
|
|
50
|
+
case 'w': wordMatch = true; break;
|
|
51
|
+
case 'x': lineMatch = true; break;
|
|
52
|
+
case 's': suppressErrors = true; break;
|
|
53
|
+
case 'a': break;
|
|
54
|
+
case 'e': {
|
|
55
|
+
const rest = flags.slice(j + 1);
|
|
56
|
+
const eVal = rest.length > 0 ? rest : args[++i];
|
|
57
|
+
for (const p of eVal.split('\n')) { if (p.length > 0) patterns.push(p); }
|
|
58
|
+
patternSourceGiven = true;
|
|
59
|
+
skipRest = true;
|
|
60
|
+
break;
|
|
61
|
+
}
|
|
62
|
+
case 'f': {
|
|
63
|
+
const rest = flags.slice(j + 1);
|
|
64
|
+
patternFile = rest.length > 0 ? rest : args[++i];
|
|
65
|
+
patternSourceGiven = true;
|
|
66
|
+
skipRest = true;
|
|
67
|
+
break;
|
|
68
|
+
}
|
|
69
|
+
default:
|
|
70
|
+
if (!suppressErrors) {
|
|
71
|
+
this.stdErr = `grep: invalid option -- '${flags[j]}'`;
|
|
72
|
+
}
|
|
73
|
+
this.exitCode = 2;
|
|
74
|
+
return Promise.resolve();
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
} else {
|
|
78
|
+
paths.push(arg);
|
|
79
|
+
}
|
|
80
|
+
i++;
|
|
81
|
+
}
|
|
82
|
+
while (i < args.length) paths.push(args[i++]);
|
|
83
|
+
|
|
84
|
+
// -f: read patterns from file or stdin
|
|
85
|
+
if (patternFile !== null) {
|
|
86
|
+
let pfContent = '';
|
|
87
|
+
if (patternFile === '-') {
|
|
88
|
+
pfContent = this.stdin || '';
|
|
89
|
+
} else {
|
|
90
|
+
const pf = this.fs.getFileByPath(patternFile);
|
|
91
|
+
if (pf === null || pf === undefined) {
|
|
92
|
+
if (!suppressErrors) this.stdErr = `grep: ${patternFile}: No such file or directory`;
|
|
93
|
+
this.exitCode = 2;
|
|
94
|
+
return Promise.resolve();
|
|
95
|
+
}
|
|
96
|
+
pfContent = pf.toString();
|
|
97
|
+
}
|
|
98
|
+
for (const p of pfContent.split('\n')) {
|
|
99
|
+
if (p.length > 0) patterns.push(p);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// First non-flag arg becomes the pattern if no -e/-f patterns
|
|
104
|
+
if (patterns.length === 0 && !patternSourceGiven) {
|
|
105
|
+
if (paths.length === 0) {
|
|
106
|
+
this.stdErr = 'grep: no pattern given';
|
|
107
|
+
this.exitCode = 2;
|
|
108
|
+
return Promise.resolve();
|
|
109
|
+
}
|
|
110
|
+
const posArg = paths.shift();
|
|
111
|
+
for (const p of posArg.split('\n')) { if (p.length > 0) patterns.push(p); }
|
|
112
|
+
if (patterns.length === 0) patterns.push(posArg); // preserve empty-string pattern
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// Build one regex per pattern
|
|
116
|
+
const rxFlags = ignoreCase ? 'gi' : 'g';
|
|
117
|
+
const regexes = [];
|
|
118
|
+
for (const pat of patterns) {
|
|
119
|
+
let src = fixedString ? pat.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') : pat;
|
|
120
|
+
if (wordMatch) src = `(?:^|\\W)(?:${src})(?:\\W|$)`;
|
|
121
|
+
if (lineMatch) src = `^(?:${src})$`;
|
|
122
|
+
try {
|
|
123
|
+
regexes.push(new RegExp(src, rxFlags));
|
|
124
|
+
} catch(e) {
|
|
125
|
+
this.stdErr = `grep: invalid regex: ${e.message}`;
|
|
126
|
+
this.exitCode = 2;
|
|
127
|
+
return Promise.resolve();
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// Collect input sources: {label, content}
|
|
132
|
+
const sources = [];
|
|
133
|
+
let hasError = false;
|
|
134
|
+
const stdinContent = this.stdin || '';
|
|
135
|
+
|
|
136
|
+
const collectFiles = (p, topLevel = false) => {
|
|
137
|
+
const entry = this.fs.getFileByPath(p);
|
|
138
|
+
if (entry === null || entry === undefined) {
|
|
139
|
+
if (!suppressErrors) this.stdErr = `grep: ${p}: No such file or directory`;
|
|
140
|
+
hasError = true;
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
if (entry.constructor === Object) {
|
|
144
|
+
if (!recursive) {
|
|
145
|
+
if (!suppressErrors) this.stdErr = `grep: ${p}: Is a directory`;
|
|
146
|
+
hasError = true;
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
// During recursion, don't follow symlinks to directories
|
|
150
|
+
if (!topLevel && this.fs.isSymlink && this.fs.isSymlink(p)) return;
|
|
151
|
+
for (const name of Object.keys(entry)) {
|
|
152
|
+
collectFiles(p.replace(/\/$/, '') + '/' + name, false);
|
|
153
|
+
}
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
sources.push({ label: p, content: entry.toString() });
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
if (paths.length === 0) {
|
|
160
|
+
sources.push({ label: null, content: stdinContent });
|
|
161
|
+
} else {
|
|
162
|
+
for (const p of paths) {
|
|
163
|
+
if (p === '-') {
|
|
164
|
+
sources.push({ label: '(standard input)', content: stdinContent });
|
|
165
|
+
} else {
|
|
166
|
+
collectFiles(p, true);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
const multiSource = recursive || sources.length + (hasError ? 1 : 0) > 1;
|
|
172
|
+
let anyMatch = false;
|
|
173
|
+
|
|
174
|
+
const testLine = (line) => {
|
|
175
|
+
if (regexes.length === 0) return false;
|
|
176
|
+
for (const rx of regexes) {
|
|
177
|
+
rx.lastIndex = 0;
|
|
178
|
+
if (rx.test(line)) return true;
|
|
179
|
+
}
|
|
180
|
+
return false;
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
const collectMatches = (line) => {
|
|
184
|
+
const segments = [];
|
|
185
|
+
for (const rx of regexes) {
|
|
186
|
+
rx.lastIndex = 0;
|
|
187
|
+
let m;
|
|
188
|
+
while ((m = rx.exec(line)) !== null) {
|
|
189
|
+
if (m[0].length === 0) { rx.lastIndex++; continue; }
|
|
190
|
+
segments.push(m[0]);
|
|
191
|
+
if (!rx.global) break;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
return segments;
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
for (const { label, content } of sources) {
|
|
198
|
+
let lines = content.split('\n');
|
|
199
|
+
if (lines.length > 0 && lines[lines.length - 1] === '') lines.pop();
|
|
200
|
+
|
|
201
|
+
let matchCount = 0;
|
|
202
|
+
const hits = [];
|
|
203
|
+
|
|
204
|
+
for (let ln = 0; ln < lines.length; ln++) {
|
|
205
|
+
const line = lines[ln];
|
|
206
|
+
let isMatch;
|
|
207
|
+
let segments = [];
|
|
208
|
+
|
|
209
|
+
if (onlyMatching && !invertMatch) {
|
|
210
|
+
segments = collectMatches(line);
|
|
211
|
+
isMatch = segments.length > 0;
|
|
212
|
+
} else {
|
|
213
|
+
isMatch = testLine(line);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
if (isMatch !== invertMatch) {
|
|
217
|
+
matchCount++;
|
|
218
|
+
hits.push({ text: line, num: ln + 1, segments });
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
if (listNonMatching) {
|
|
223
|
+
// -L: print files with NO match; success = printed something
|
|
224
|
+
if (matchCount === 0) {
|
|
225
|
+
this.stdOut = label || '(standard input)';
|
|
226
|
+
anyMatch = true;
|
|
227
|
+
}
|
|
228
|
+
continue;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
if (matchCount > 0) anyMatch = true;
|
|
232
|
+
|
|
233
|
+
if (quiet) continue;
|
|
234
|
+
|
|
235
|
+
if (listFiles) {
|
|
236
|
+
if (matchCount > 0) this.stdOut = label || '(standard input)';
|
|
237
|
+
continue;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
if (countOnly) {
|
|
241
|
+
const pfx = multiSource && label ? `${label}:` : '';
|
|
242
|
+
this.stdOut = `${pfx}${matchCount}`;
|
|
243
|
+
continue;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
for (const { text, num, segments } of hits) {
|
|
247
|
+
const filePfx = multiSource && label ? `${label}:` : '';
|
|
248
|
+
const linePfx = lineNumbers ? `${num}:` : '';
|
|
249
|
+
if (onlyMatching) {
|
|
250
|
+
for (const seg of segments) {
|
|
251
|
+
this.stdOut = `${filePfx}${linePfx}${seg}`;
|
|
252
|
+
}
|
|
253
|
+
} else {
|
|
254
|
+
this.stdOut = `${filePfx}${linePfx}${text}`;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
// SUSv3: -q with a match exits 0 even if there were also file errors
|
|
260
|
+
if (hasError && !(quiet && anyMatch)) this.exitCode = 2;
|
|
261
|
+
else if (!anyMatch) this.exitCode = 1;
|
|
262
|
+
|
|
263
|
+
return Promise.resolve();
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
module.exports = Grep;
|
package/lib/builtins/index.js
CHANGED
|
@@ -1,7 +1,14 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
const { Test, TestBracket } = require('./test');
|
|
4
|
+
|
|
3
5
|
module.exports = [
|
|
4
6
|
require('./cat'),
|
|
5
7
|
require('./ls'),
|
|
6
|
-
require('./echo')
|
|
7
|
-
|
|
8
|
+
require('./echo'),
|
|
9
|
+
require('./grep'),
|
|
10
|
+
require('./true'),
|
|
11
|
+
require('./false'),
|
|
12
|
+
Test,
|
|
13
|
+
TestBracket,
|
|
14
|
+
];
|
package/lib/builtins/ls.js
CHANGED
|
@@ -9,13 +9,18 @@ class Ls extends Command {
|
|
|
9
9
|
}
|
|
10
10
|
|
|
11
11
|
run() {
|
|
12
|
+
// Pre-process numeric flags that command-line-args can't handle as aliases
|
|
13
|
+
const rawArgs = this.arguments.filter(a => a !== '-1');
|
|
14
|
+
const onePerLine = rawArgs.length !== this.arguments.length;
|
|
15
|
+
|
|
12
16
|
let options;
|
|
13
17
|
try {
|
|
14
|
-
options = this.parseArgs();
|
|
18
|
+
options = this.parseArgs(rawArgs);
|
|
15
19
|
} catch(e) {
|
|
16
20
|
this.printHelp(e);
|
|
17
21
|
return Promise.reject();
|
|
18
22
|
}
|
|
23
|
+
options.one = options.one || onePerLine;
|
|
19
24
|
|
|
20
25
|
if(options.help) {
|
|
21
26
|
this.printHelp();
|
|
@@ -38,11 +43,37 @@ class Ls extends Command {
|
|
|
38
43
|
return acc;
|
|
39
44
|
}, {});
|
|
40
45
|
}
|
|
46
|
+
|
|
47
|
+
const absBase = this.fs.absolutePath(options.path);
|
|
48
|
+
|
|
41
49
|
if(options.long) {
|
|
42
|
-
this.printLong(items);
|
|
50
|
+
this.printLong(items, absBase);
|
|
51
|
+
} else if (options.one) {
|
|
52
|
+
Object.entries(items).forEach(([name, content]) => {
|
|
53
|
+
this.stdOut = this._applyIndicator(name, content, options.classify);
|
|
54
|
+
});
|
|
43
55
|
} else {
|
|
44
|
-
this.printSimple(
|
|
56
|
+
this.printSimple(items, options.classify);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (options.recursive) {
|
|
60
|
+
Object.entries(items).forEach(([name, content]) => {
|
|
61
|
+
if (content && content.constructor === Object) {
|
|
62
|
+
const subPath = absBase.endsWith('/') ? absBase + name : absBase + '/' + name;
|
|
63
|
+
this.stdOut = '';
|
|
64
|
+
this.stdOut = subPath + ':';
|
|
65
|
+
this.run.call(
|
|
66
|
+
Object.create(this, {
|
|
67
|
+
arguments: { value: [
|
|
68
|
+
...this.arguments.filter(a => a !== options.path && a !== './'),
|
|
69
|
+
subPath
|
|
70
|
+
]}
|
|
71
|
+
})
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
});
|
|
45
75
|
}
|
|
76
|
+
|
|
46
77
|
return Promise.resolve();
|
|
47
78
|
}
|
|
48
79
|
|
|
@@ -53,25 +84,48 @@ class Ls extends Command {
|
|
|
53
84
|
if(e) {
|
|
54
85
|
this.stdOut = e.message;
|
|
55
86
|
}
|
|
56
|
-
this.stdOut = 'usage: ls [-
|
|
87
|
+
this.stdOut = 'usage: ls [-alRF1h] [file ...]'
|
|
57
88
|
}
|
|
58
89
|
|
|
59
|
-
printSimple(items) {
|
|
60
|
-
this.stdOut = items.reduce((out,
|
|
90
|
+
printSimple(items, classify) {
|
|
91
|
+
this.stdOut = Object.entries(items).reduce((out, [name, content]) => {
|
|
92
|
+
return out + this.padString(this._applyIndicator(name, content, classify));
|
|
93
|
+
}, '');
|
|
61
94
|
}
|
|
62
95
|
|
|
63
|
-
printLong(items) {
|
|
64
|
-
Object.entries(items).forEach(([
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
return this.stdOut =
|
|
96
|
+
printLong(items, basePath) {
|
|
97
|
+
Object.entries(items).forEach(([name, content]) => {
|
|
98
|
+
const itemPath = basePath + '/' + name;
|
|
99
|
+
const isDir = content !== null && content !== undefined && content.constructor === Object;
|
|
100
|
+
const rawMode = this.fs.getMode(itemPath);
|
|
101
|
+
const mode = rawMode !== null ? rawMode : (isDir ? 0o755 : 0o644);
|
|
102
|
+
const permStr = this._modeToString(mode, isDir);
|
|
103
|
+
const size = this.fs.getSize(itemPath);
|
|
104
|
+
return this.stdOut = `${permStr} root users ${this.padString(size, 10)} ${name}`;
|
|
72
105
|
});
|
|
73
106
|
}
|
|
74
107
|
|
|
108
|
+
_applyIndicator(name, content, classify) {
|
|
109
|
+
if (!classify) return name;
|
|
110
|
+
if (content && content.constructor === Object) return name + '/';
|
|
111
|
+
if (typeof content === 'function' || this.fs.isExecutable(name)) return name + '*';
|
|
112
|
+
return name;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
_modeToString(mode, isDir) {
|
|
116
|
+
const d = isDir ? 'd' : '-';
|
|
117
|
+
return d +
|
|
118
|
+
((mode & 0o400) ? 'r' : '-') +
|
|
119
|
+
((mode & 0o200) ? 'w' : '-') +
|
|
120
|
+
((mode & 0o100) ? 'x' : '-') +
|
|
121
|
+
((mode & 0o040) ? 'r' : '-') +
|
|
122
|
+
((mode & 0o020) ? 'w' : '-') +
|
|
123
|
+
((mode & 0o010) ? 'x' : '-') +
|
|
124
|
+
((mode & 0o004) ? 'r' : '-') +
|
|
125
|
+
((mode & 0o002) ? 'w' : '-') +
|
|
126
|
+
((mode & 0o001) ? 'x' : '-');
|
|
127
|
+
}
|
|
128
|
+
|
|
75
129
|
padString(content, size = 16) {
|
|
76
130
|
const pad = size - content.toString().length;
|
|
77
131
|
let padding = '';
|
|
@@ -82,7 +136,7 @@ class Ls extends Command {
|
|
|
82
136
|
return content.toString() + padding;
|
|
83
137
|
}
|
|
84
138
|
|
|
85
|
-
parseArgs() {
|
|
139
|
+
parseArgs(argv = this.arguments) {
|
|
86
140
|
return commandLineArgs([
|
|
87
141
|
{
|
|
88
142
|
name: 'path',
|
|
@@ -100,13 +154,27 @@ class Ls extends Command {
|
|
|
100
154
|
alias: 'l',
|
|
101
155
|
type: Boolean
|
|
102
156
|
},
|
|
157
|
+
{
|
|
158
|
+
name: 'recursive',
|
|
159
|
+
alias: 'R',
|
|
160
|
+
type: Boolean
|
|
161
|
+
},
|
|
162
|
+
{
|
|
163
|
+
name: 'one',
|
|
164
|
+
type: Boolean
|
|
165
|
+
},
|
|
166
|
+
{
|
|
167
|
+
name: 'classify',
|
|
168
|
+
alias: 'F',
|
|
169
|
+
type: Boolean
|
|
170
|
+
},
|
|
103
171
|
{
|
|
104
172
|
name: 'help',
|
|
105
173
|
alias: 'h',
|
|
106
174
|
type: Boolean
|
|
107
175
|
}
|
|
108
|
-
], {argv
|
|
176
|
+
], {argv});
|
|
109
177
|
}
|
|
110
178
|
}
|
|
111
179
|
|
|
112
|
-
module.exports = Ls;
|
|
180
|
+
module.exports = Ls;
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const Command = require('../Command');
|
|
4
|
+
|
|
5
|
+
class Test extends Command {
|
|
6
|
+
get name() { return 'test'; }
|
|
7
|
+
get requiresFilesystem() { return true; }
|
|
8
|
+
|
|
9
|
+
run() {
|
|
10
|
+
const args = [...this.arguments];
|
|
11
|
+
if (args[args.length - 1] === ']') args.pop();
|
|
12
|
+
|
|
13
|
+
if (!this._evaluate(args)) this.exitCode = 1;
|
|
14
|
+
return Promise.resolve();
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
_evaluate(args) {
|
|
18
|
+
if (args.length === 0) return false;
|
|
19
|
+
|
|
20
|
+
// -o (OR) has lower precedence than -a (AND)
|
|
21
|
+
const orIdx = args.indexOf('-o');
|
|
22
|
+
if (orIdx !== -1) {
|
|
23
|
+
return this._evaluate(args.slice(0, orIdx)) || this._evaluate(args.slice(orIdx + 1));
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const andIdx = args.indexOf('-a');
|
|
27
|
+
if (andIdx !== -1) {
|
|
28
|
+
return this._evaluate(args.slice(0, andIdx)) && this._evaluate(args.slice(andIdx + 1));
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
if (args[0] === '!') return !this._evaluate(args.slice(1));
|
|
32
|
+
|
|
33
|
+
if (args.length === 1) return args[0].length > 0;
|
|
34
|
+
|
|
35
|
+
if (args.length === 2) {
|
|
36
|
+
const [op, val] = args;
|
|
37
|
+
switch (op) {
|
|
38
|
+
case '-n': return val.length > 0;
|
|
39
|
+
case '-z': return val.length === 0;
|
|
40
|
+
case '-e': {
|
|
41
|
+
if (!this.fs) return false;
|
|
42
|
+
const e = this.fs.getFileByPath(val);
|
|
43
|
+
return e !== null && e !== undefined;
|
|
44
|
+
}
|
|
45
|
+
case '-f': {
|
|
46
|
+
if (!this.fs) return false;
|
|
47
|
+
const e = this.fs.getFileByPath(val);
|
|
48
|
+
return e !== null && e !== undefined && e.constructor !== Object;
|
|
49
|
+
}
|
|
50
|
+
case '-d': {
|
|
51
|
+
if (!this.fs) return false;
|
|
52
|
+
const e = this.fs.getFileByPath(val);
|
|
53
|
+
return e !== null && e !== undefined && e.constructor === Object;
|
|
54
|
+
}
|
|
55
|
+
case '-r': {
|
|
56
|
+
if (!this.fs) return false;
|
|
57
|
+
const m = this.fs.getMode(val);
|
|
58
|
+
return m !== null && (m & 0o444) !== 0;
|
|
59
|
+
}
|
|
60
|
+
case '-w': {
|
|
61
|
+
if (!this.fs) return false;
|
|
62
|
+
const m = this.fs.getMode(val);
|
|
63
|
+
return m !== null && (m & 0o222) !== 0;
|
|
64
|
+
}
|
|
65
|
+
case '-x': {
|
|
66
|
+
if (!this.fs) return false;
|
|
67
|
+
const m = this.fs.getMode(val);
|
|
68
|
+
return m !== null && (m & 0o111) !== 0;
|
|
69
|
+
}
|
|
70
|
+
case '-s': {
|
|
71
|
+
if (!this.fs) return false;
|
|
72
|
+
const e = this.fs.getFileByPath(val);
|
|
73
|
+
if (e === null || e === undefined || e.constructor === Object) return false;
|
|
74
|
+
return e.toString().length > 0;
|
|
75
|
+
}
|
|
76
|
+
default: return false;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
if (args.length === 3) {
|
|
81
|
+
const [a, op, b] = args;
|
|
82
|
+
switch (op) {
|
|
83
|
+
case '=': case '==': return a === b;
|
|
84
|
+
case '!=': return a !== b;
|
|
85
|
+
case '-eq': return parseInt(a) === parseInt(b);
|
|
86
|
+
case '-ne': return parseInt(a) !== parseInt(b);
|
|
87
|
+
case '-lt': return parseInt(a) < parseInt(b);
|
|
88
|
+
case '-le': return parseInt(a) <= parseInt(b);
|
|
89
|
+
case '-gt': return parseInt(a) > parseInt(b);
|
|
90
|
+
case '-ge': return parseInt(a) >= parseInt(b);
|
|
91
|
+
default: return false;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return false;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
class TestBracket extends Test {
|
|
100
|
+
get name() { return '['; }
|
|
101
|
+
}
|
|
102
|
+
// Override the class name so Context.setCommand stores it under '[' not 'testbracket'
|
|
103
|
+
Object.defineProperty(TestBracket, 'name', { value: '[' });
|
|
104
|
+
|
|
105
|
+
module.exports = { Test, TestBracket };
|