brsh 1.3.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 -219
- package/dist/shell.min.js.map +1 -147
- 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 +231 -56
- package/lib/Command.js +12 -1
- package/lib/Context.js +12 -4
- package/lib/FileSystem.js +99 -6
- package/lib/NodeFileSystem.js +238 -0
- package/lib/Result.js +6 -0
- package/lib/ScriptCommand.js +193 -15
- package/lib/Terminal.js +192 -11
- 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 +9 -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 +107 -0
- 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
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const LocalCommand = require('./LocalCommand');
|
|
4
|
+
|
|
5
|
+
class Chmod extends LocalCommand {
|
|
6
|
+
get name() { return 'chmod'; }
|
|
7
|
+
get requiresFilesystem() { return true; }
|
|
8
|
+
|
|
9
|
+
run() {
|
|
10
|
+
const args = this.arguments;
|
|
11
|
+
if (args.length < 2) {
|
|
12
|
+
this.stdErr = 'chmod: usage: chmod MODE FILE...';
|
|
13
|
+
this.exitCode = 1;
|
|
14
|
+
return Promise.resolve();
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const modeStr = args[0];
|
|
18
|
+
const paths = args.slice(1);
|
|
19
|
+
|
|
20
|
+
for (const p of paths) {
|
|
21
|
+
const file = this.fs.getFileByPath(p);
|
|
22
|
+
if (file === null || file === undefined) {
|
|
23
|
+
this.stdErr = `chmod: cannot access '${p}': No such file or directory`;
|
|
24
|
+
this.exitCode = 1;
|
|
25
|
+
continue;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const currentMode = this.fs.getMode(p);
|
|
29
|
+
const mode = this._parseMode(modeStr, currentMode !== null ? currentMode : 0o644);
|
|
30
|
+
if (mode === null) {
|
|
31
|
+
this.stdErr = `chmod: invalid mode: '${modeStr}'`;
|
|
32
|
+
this.exitCode = 1;
|
|
33
|
+
return Promise.resolve();
|
|
34
|
+
}
|
|
35
|
+
this.fs.setMode(p, mode);
|
|
36
|
+
}
|
|
37
|
+
return Promise.resolve();
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
_parseMode(str, current) {
|
|
41
|
+
if (/^0?[0-7]{3,4}$/.test(str)) {
|
|
42
|
+
return parseInt(str, 8);
|
|
43
|
+
}
|
|
44
|
+
const match = str.match(/^([ugoa]*)([+\-=])([rwx]+)$/);
|
|
45
|
+
if (!match) return null;
|
|
46
|
+
|
|
47
|
+
const [, who, op, perms] = match;
|
|
48
|
+
const targets = (!who || who === 'a') ? ['u', 'g', 'o'] : who.split('');
|
|
49
|
+
|
|
50
|
+
const R = { u: 0o400, g: 0o040, o: 0o004 };
|
|
51
|
+
const W = { u: 0o200, g: 0o020, o: 0o002 };
|
|
52
|
+
const X = { u: 0o100, g: 0o010, o: 0o001 };
|
|
53
|
+
|
|
54
|
+
let mask = 0;
|
|
55
|
+
for (const t of targets) {
|
|
56
|
+
if (perms.includes('r')) mask |= R[t];
|
|
57
|
+
if (perms.includes('w')) mask |= W[t];
|
|
58
|
+
if (perms.includes('x')) mask |= X[t];
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (op === '+') return current | mask;
|
|
62
|
+
if (op === '-') return current & ~mask;
|
|
63
|
+
if (op === '=') {
|
|
64
|
+
let clear = 0;
|
|
65
|
+
for (const t of targets) { clear |= R[t] | W[t] | X[t]; }
|
|
66
|
+
return (current & ~clear) | mask;
|
|
67
|
+
}
|
|
68
|
+
return null;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
module.exports = Chmod;
|
package/lib/local/cp.js
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const LocalCommand = require('./LocalCommand');
|
|
4
|
+
const nodeFs = require('fs');
|
|
5
|
+
|
|
6
|
+
class Cp extends LocalCommand {
|
|
7
|
+
get name() { return 'cp'; }
|
|
8
|
+
get requiresFilesystem() { return true; }
|
|
9
|
+
|
|
10
|
+
run() {
|
|
11
|
+
let recursive = false;
|
|
12
|
+
// symlinkMode: 'follow' | 'preserve' | 'follow_cl'
|
|
13
|
+
// 'follow' - dereference all symlinks
|
|
14
|
+
// 'preserve' - keep symlinks as symlinks (default with -R)
|
|
15
|
+
// 'follow_cl' - follow command-line symlinks, preserve during recursion (-H)
|
|
16
|
+
let symlinkMode = null; // null = unset; resolved after parsing
|
|
17
|
+
let rSeen = false;
|
|
18
|
+
let hSeen = false;
|
|
19
|
+
let childSymlinkModeOverride = null; // tracks last -L/-P/-d for child mode when -H is last
|
|
20
|
+
const paths = [];
|
|
21
|
+
|
|
22
|
+
const args = [...this.arguments];
|
|
23
|
+
let i = 0;
|
|
24
|
+
while (i < args.length) {
|
|
25
|
+
const arg = args[i];
|
|
26
|
+
if (arg === '--') { i++; break; }
|
|
27
|
+
if (arg.startsWith('-') && arg.length > 1) {
|
|
28
|
+
for (const ch of arg.slice(1)) {
|
|
29
|
+
switch (ch) {
|
|
30
|
+
case 'r': case 'R': recursive = true; rSeen = true; break;
|
|
31
|
+
case 'a': recursive = true; rSeen = true; symlinkMode = 'preserve'; childSymlinkModeOverride = 'preserve'; break;
|
|
32
|
+
case 'd': case 'P': symlinkMode = 'preserve'; childSymlinkModeOverride = 'preserve'; break;
|
|
33
|
+
case 'L': symlinkMode = 'follow'; childSymlinkModeOverride = 'follow'; break;
|
|
34
|
+
case 'H': symlinkMode = 'follow_cl'; hSeen = true; break;
|
|
35
|
+
case 'f': break;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
} else {
|
|
39
|
+
paths.push(arg);
|
|
40
|
+
}
|
|
41
|
+
i++;
|
|
42
|
+
}
|
|
43
|
+
while (i < args.length) paths.push(args[i++]);
|
|
44
|
+
|
|
45
|
+
// Resolve default symlink mode: -R without explicit flag → preserve
|
|
46
|
+
const resolvedMode = symlinkMode !== null ? symlinkMode
|
|
47
|
+
: rSeen ? 'preserve'
|
|
48
|
+
: 'follow';
|
|
49
|
+
|
|
50
|
+
if (paths.length < 2) {
|
|
51
|
+
this.stdErr = `cp: missing destination file operand${paths.length ? ` after '${paths[0]}'` : ''}`;
|
|
52
|
+
this.exitCode = 1;
|
|
53
|
+
return Promise.resolve();
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const dest = paths[paths.length - 1];
|
|
57
|
+
const sources = paths.slice(0, -1);
|
|
58
|
+
const destEntry = this.fs.getFileByPath(dest);
|
|
59
|
+
const destIsDir = destEntry !== null && destEntry !== undefined && destEntry.constructor === Object;
|
|
60
|
+
|
|
61
|
+
// Determine per-item copy mode and the mode for recursive children.
|
|
62
|
+
// -H always takes effect for CL-level symlinks (follow), regardless of which mode wins.
|
|
63
|
+
// Child mode comes from the overriding -L/-P flag, or 'preserve' if H alone.
|
|
64
|
+
// When H is not present, both levels use resolvedMode.
|
|
65
|
+
const clItemMode = hSeen ? 'follow' : (resolvedMode === 'follow_cl' ? 'follow' : resolvedMode);
|
|
66
|
+
const childMode = (hSeen || resolvedMode === 'follow_cl')
|
|
67
|
+
? (childSymlinkModeOverride !== null ? childSymlinkModeOverride : 'preserve')
|
|
68
|
+
: resolvedMode;
|
|
69
|
+
|
|
70
|
+
for (const src of sources) {
|
|
71
|
+
const isLink = this.fs.isSymlink ? this.fs.isSymlink(src) : false;
|
|
72
|
+
|
|
73
|
+
// Stat (follows symlinks) to get effective type
|
|
74
|
+
const stat = this.fs.getFileByPath(src);
|
|
75
|
+
if (stat === null || stat === undefined) {
|
|
76
|
+
this.stdErr = `cp: cannot stat '${src}': No such file or directory`;
|
|
77
|
+
this.exitCode = 1;
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// If we're preserving this symlink, it won't become a directory
|
|
82
|
+
const effectiveIsDir = (isLink && clItemMode === 'preserve')
|
|
83
|
+
? false
|
|
84
|
+
: stat.constructor === Object;
|
|
85
|
+
|
|
86
|
+
if (effectiveIsDir && !recursive) {
|
|
87
|
+
this.stdErr = `cp: omitting directory '${src}'`;
|
|
88
|
+
this.exitCode = 1;
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const targetPath = destIsDir
|
|
93
|
+
? dest.replace(/\/$/, '') + '/' + src.split('/').pop()
|
|
94
|
+
: dest;
|
|
95
|
+
|
|
96
|
+
const err = this._copy(src, targetPath, clItemMode, isLink, childMode);
|
|
97
|
+
if (err) {
|
|
98
|
+
this.stdErr = err;
|
|
99
|
+
this.exitCode = 1;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return Promise.resolve();
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// mode: how to handle symlinks for this item ('follow' | 'preserve')
|
|
106
|
+
// childMode: how to handle symlinks for recursive children
|
|
107
|
+
_copy(src, dest, mode, isLink, childMode) {
|
|
108
|
+
if (isLink && mode === 'preserve' && this.fs.readSymlink) {
|
|
109
|
+
const target = this.fs.readSymlink(src);
|
|
110
|
+
if (target === null) return `cp: cannot read symlink '${src}'`;
|
|
111
|
+
try {
|
|
112
|
+
this.fs.createSymlink(target, dest);
|
|
113
|
+
} catch(e) {
|
|
114
|
+
return `cp: cannot create symlink '${dest}': ${e.message}`;
|
|
115
|
+
}
|
|
116
|
+
return null;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const content = this.fs.getFileByPath(src);
|
|
120
|
+
if (content === null || content === undefined) {
|
|
121
|
+
return `cp: cannot stat '${src}': No such file or directory`;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
if (content.constructor === Object) {
|
|
125
|
+
try {
|
|
126
|
+
this.fs.setFileByPath(dest, {}, true);
|
|
127
|
+
} catch(e) {
|
|
128
|
+
return `cp: cannot create directory '${dest}': ${e.message}`;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const absDir = this.fs.absolutePath(src);
|
|
132
|
+
let entries;
|
|
133
|
+
try {
|
|
134
|
+
entries = nodeFs.readdirSync(absDir);
|
|
135
|
+
} catch(e) {
|
|
136
|
+
entries = Object.keys(content);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
for (const name of entries) {
|
|
140
|
+
const childSrc = src.replace(/\/$/, '') + '/' + name;
|
|
141
|
+
const childDest = dest.replace(/\/$/, '') + '/' + name;
|
|
142
|
+
const childIsLink = this.fs.isSymlink ? this.fs.isSymlink(childSrc) : false;
|
|
143
|
+
const err = this._copy(childSrc, childDest, childMode, childIsLink, childMode);
|
|
144
|
+
if (err) {
|
|
145
|
+
this.stdErr = err;
|
|
146
|
+
this.exitCode = 1;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
} else {
|
|
150
|
+
this.fs.setFileByPath(dest, content, true);
|
|
151
|
+
}
|
|
152
|
+
return null;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
module.exports = Cp;
|
package/lib/local/index.js
CHANGED
|
@@ -4,9 +4,17 @@ module.exports = [
|
|
|
4
4
|
require('./alias'),
|
|
5
5
|
require('./export'),
|
|
6
6
|
require('./cd'),
|
|
7
|
+
require('./chmod'),
|
|
8
|
+
require('./cp'),
|
|
9
|
+
require('./mkdir'),
|
|
10
|
+
require('./mv'),
|
|
11
|
+
require('./pwd'),
|
|
12
|
+
require('./rm'),
|
|
7
13
|
require('./which'),
|
|
8
14
|
require('./source'),
|
|
9
15
|
require('./clear'),
|
|
10
|
-
require('./exit')
|
|
16
|
+
require('./exit'),
|
|
17
|
+
require('./read'),
|
|
18
|
+
require('./jsfunction')
|
|
11
19
|
];
|
|
12
20
|
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const LocalCommand = require('./LocalCommand');
|
|
4
|
+
const Command = require('../Command');
|
|
5
|
+
|
|
6
|
+
class JsFunction extends LocalCommand {
|
|
7
|
+
get name() { return 'jsfunction'; }
|
|
8
|
+
|
|
9
|
+
run() {
|
|
10
|
+
const [name, ...bodyParts] = this.arguments;
|
|
11
|
+
if (!name || bodyParts.length === 0) {
|
|
12
|
+
this.stdErr = 'usage: jsfunction <name> <function-body>';
|
|
13
|
+
this.exitCode = 1;
|
|
14
|
+
return Promise.resolve();
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const body = bodyParts.join(' ');
|
|
18
|
+
let fn;
|
|
19
|
+
try {
|
|
20
|
+
// eslint-disable-next-line no-new-func
|
|
21
|
+
fn = new Function('return (' + body + ')')();
|
|
22
|
+
if (typeof fn !== 'function') throw new Error('expression is not a function');
|
|
23
|
+
} catch(e) {
|
|
24
|
+
this.stdErr = `jsfunction: ${e.message}`;
|
|
25
|
+
this.exitCode = 1;
|
|
26
|
+
return Promise.resolve();
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
class DynamicCommand extends Command {
|
|
30
|
+
run() {
|
|
31
|
+
let result;
|
|
32
|
+
try {
|
|
33
|
+
result = fn(this.arguments);
|
|
34
|
+
} catch(e) {
|
|
35
|
+
this.stdErr = e.message || String(e);
|
|
36
|
+
this.exitCode = 1;
|
|
37
|
+
return Promise.resolve();
|
|
38
|
+
}
|
|
39
|
+
if (result && typeof result.then === 'function') {
|
|
40
|
+
return result.then(v => {
|
|
41
|
+
if (v != null) this.stdOut = String(v);
|
|
42
|
+
}).catch(e => {
|
|
43
|
+
this.stdErr = e.message || String(e);
|
|
44
|
+
this.exitCode = 1;
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
if (result != null) this.stdOut = String(result);
|
|
48
|
+
return Promise.resolve();
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
Object.defineProperty(DynamicCommand, 'name', { value: name, configurable: true });
|
|
52
|
+
|
|
53
|
+
this.context.setCommand(DynamicCommand);
|
|
54
|
+
return Promise.resolve();
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
module.exports = JsFunction;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const LocalCommand = require('./LocalCommand');
|
|
4
|
+
|
|
5
|
+
class Mkdir extends LocalCommand {
|
|
6
|
+
get name() { return 'mkdir'; }
|
|
7
|
+
get requiresFilesystem() { return true; }
|
|
8
|
+
|
|
9
|
+
run() {
|
|
10
|
+
let createParents = false;
|
|
11
|
+
const paths = [];
|
|
12
|
+
|
|
13
|
+
for (const arg of this.arguments) {
|
|
14
|
+
if (arg === '-p' || arg === '--parents') createParents = true;
|
|
15
|
+
else paths.push(arg);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
if (paths.length === 0) {
|
|
19
|
+
this.stdErr = 'mkdir: missing operand';
|
|
20
|
+
this.exitCode = 1;
|
|
21
|
+
return Promise.resolve();
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
for (const dir of paths) {
|
|
25
|
+
const existing = this.fs.getFileByPath(dir);
|
|
26
|
+
if (existing !== null && existing !== undefined) {
|
|
27
|
+
if (!createParents) {
|
|
28
|
+
this.stdErr = `mkdir: cannot create directory '${dir}': File exists`;
|
|
29
|
+
this.exitCode = 1;
|
|
30
|
+
}
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
33
|
+
try {
|
|
34
|
+
this.fs.setFileByPath(dir, {}, createParents);
|
|
35
|
+
} catch(e) {
|
|
36
|
+
this.stdErr = `mkdir: cannot create directory '${dir}': ${e.message}`;
|
|
37
|
+
this.exitCode = 1;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return Promise.resolve();
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
module.exports = Mkdir;
|
package/lib/local/mv.js
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const LocalCommand = require('./LocalCommand');
|
|
4
|
+
const nodeFs = require('fs');
|
|
5
|
+
|
|
6
|
+
class Mv extends LocalCommand {
|
|
7
|
+
get name() { return 'mv'; }
|
|
8
|
+
get requiresFilesystem() { return true; }
|
|
9
|
+
|
|
10
|
+
run() {
|
|
11
|
+
let destFirst = null;
|
|
12
|
+
const rawPaths = [];
|
|
13
|
+
|
|
14
|
+
const args = [...this.arguments];
|
|
15
|
+
let i = 0;
|
|
16
|
+
while (i < args.length) {
|
|
17
|
+
const arg = args[i];
|
|
18
|
+
if (arg === '--') { i++; break; }
|
|
19
|
+
if (arg.startsWith('-') && arg.length > 1) {
|
|
20
|
+
for (let j = 1; j < arg.length; j++) {
|
|
21
|
+
switch (arg[j]) {
|
|
22
|
+
case 'f': case 'i': case 'n': break;
|
|
23
|
+
case 't': {
|
|
24
|
+
const rest = arg.slice(j + 1);
|
|
25
|
+
destFirst = rest.length > 0 ? rest : args[++i];
|
|
26
|
+
j = arg.length;
|
|
27
|
+
break;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
} else {
|
|
32
|
+
rawPaths.push(arg);
|
|
33
|
+
}
|
|
34
|
+
i++;
|
|
35
|
+
}
|
|
36
|
+
while (i < args.length) rawPaths.push(args[i++]);
|
|
37
|
+
|
|
38
|
+
const dest = destFirst !== null ? destFirst : rawPaths[rawPaths.length - 1];
|
|
39
|
+
const sources = destFirst !== null ? rawPaths : rawPaths.slice(0, -1);
|
|
40
|
+
|
|
41
|
+
if (!dest || sources.length === 0) {
|
|
42
|
+
this.stdErr = `mv: missing destination file operand${sources.length ? ` after '${sources[0]}'` : ''}`;
|
|
43
|
+
this.exitCode = 1;
|
|
44
|
+
return Promise.resolve();
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const destEntry = this.fs.getFileByPath(dest);
|
|
48
|
+
const destIsDir = destEntry !== null && destEntry !== undefined && destEntry.constructor === Object;
|
|
49
|
+
|
|
50
|
+
for (const src of sources) {
|
|
51
|
+
const targetPath = destIsDir
|
|
52
|
+
? dest.replace(/\/$/, '') + '/' + src.split('/').pop()
|
|
53
|
+
: dest;
|
|
54
|
+
|
|
55
|
+
if (this.fs.isRealFilesystem) {
|
|
56
|
+
const srcAbs = this.fs.absolutePath(src);
|
|
57
|
+
const destAbs = this.fs.absolutePath(targetPath);
|
|
58
|
+
// lstatSync works for unreadable files and dangling symlinks;
|
|
59
|
+
// getFileByPath would return null for both, masking existence.
|
|
60
|
+
try { nodeFs.lstatSync(srcAbs); } catch {
|
|
61
|
+
this.stdErr = `mv: cannot stat '${src}': No such file or directory`;
|
|
62
|
+
this.exitCode = 1;
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
try {
|
|
66
|
+
const nodePath = require('path');
|
|
67
|
+
nodeFs.mkdirSync(nodePath.dirname(destAbs), { recursive: true });
|
|
68
|
+
nodeFs.renameSync(srcAbs, destAbs);
|
|
69
|
+
} catch(e) {
|
|
70
|
+
this.stdErr = `mv: cannot move '${src}' to '${targetPath}': ${e.message}`;
|
|
71
|
+
this.exitCode = 1;
|
|
72
|
+
}
|
|
73
|
+
} else {
|
|
74
|
+
const srcEntry = this.fs.getFileByPath(src);
|
|
75
|
+
if (srcEntry === null || srcEntry === undefined) {
|
|
76
|
+
this.stdErr = `mv: cannot stat '${src}': No such file or directory`;
|
|
77
|
+
this.exitCode = 1;
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
this.fs.setFileByPath(targetPath, this._deepCopy(srcEntry), true);
|
|
81
|
+
this.fs.deleteFileByPath(src);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return Promise.resolve();
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
_deepCopy(content) {
|
|
88
|
+
if (content === null || content === undefined) return content;
|
|
89
|
+
if (typeof content === 'function' || typeof content !== 'object') return content;
|
|
90
|
+
const copy = {};
|
|
91
|
+
for (const [k, v] of Object.entries(content)) copy[k] = this._deepCopy(v);
|
|
92
|
+
return copy;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
module.exports = Mv;
|
package/lib/local/pwd.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const LocalCommand = require('./LocalCommand');
|
|
4
|
+
|
|
5
|
+
class Pwd extends LocalCommand {
|
|
6
|
+
run() {
|
|
7
|
+
const cwd = this.context.fs.cwd;
|
|
8
|
+
this.stdOut = cwd.endsWith('/') && cwd !== '/' ? cwd.slice(0, -1) : cwd;
|
|
9
|
+
return Promise.resolve();
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
module.exports = Pwd;
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const LocalCommand = require('./LocalCommand');
|
|
4
|
+
const commandLineArgs = require('command-line-args');
|
|
5
|
+
|
|
6
|
+
class Read extends LocalCommand {
|
|
7
|
+
get requiresFilesystem() {
|
|
8
|
+
return false;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
get captureInput() {
|
|
12
|
+
return true;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
run() {
|
|
16
|
+
let options;
|
|
17
|
+
try {
|
|
18
|
+
options = this.parseArgs();
|
|
19
|
+
} catch(e) {
|
|
20
|
+
this.printHelp(e);
|
|
21
|
+
return Promise.reject();
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
if(options.help) {
|
|
25
|
+
this.printHelp();
|
|
26
|
+
return Promise.resolve();
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
this.nchars = options.nchars;
|
|
30
|
+
this.var = options.name;
|
|
31
|
+
|
|
32
|
+
return new Promise((resolve) => {
|
|
33
|
+
const tid = setTimeout(() => {
|
|
34
|
+
resolve();
|
|
35
|
+
this.exitCode = 1;
|
|
36
|
+
}, options.timeout ? options.timeout * 1000 : 86400000);
|
|
37
|
+
|
|
38
|
+
this.completionCallback = (data) => {
|
|
39
|
+
clearTimeout(tid);
|
|
40
|
+
resolve(data);
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
// Flush prompt after completionCallback is set so that any
|
|
44
|
+
// synchronous onInput triggered by the prompt can complete it.
|
|
45
|
+
this.stdErr = options.prompt || "";
|
|
46
|
+
this.flush();
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
onInput(key) {
|
|
51
|
+
if(!this.inputBuffer) {
|
|
52
|
+
this.inputBuffer = ""
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (key.length === 1) {
|
|
56
|
+
this.inputBuffer += key;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if(this.inputBuffer.length === this.nchars) {
|
|
60
|
+
if(this.var) {
|
|
61
|
+
this.context.setVar(this.var, this.inputBuffer)
|
|
62
|
+
this.completionCallback();
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
this.completionCallback(this.inputBuffer);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
printHelp(e) {
|
|
70
|
+
if(e) {
|
|
71
|
+
this.stdOut = e.message;
|
|
72
|
+
}
|
|
73
|
+
this.stdOut = 'usage: read [-n nchars] [-p prompt] [-t timeout] [name ...]'
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
parseArgs() {
|
|
77
|
+
return commandLineArgs([
|
|
78
|
+
{
|
|
79
|
+
name: 'prompt',
|
|
80
|
+
alias: 'p',
|
|
81
|
+
type: String
|
|
82
|
+
},
|
|
83
|
+
{
|
|
84
|
+
name: 'nchars',
|
|
85
|
+
alias: 'n',
|
|
86
|
+
type: Number
|
|
87
|
+
},
|
|
88
|
+
{
|
|
89
|
+
name: 'timeout',
|
|
90
|
+
alias: 't',
|
|
91
|
+
type: Number
|
|
92
|
+
},
|
|
93
|
+
{
|
|
94
|
+
name: 'help',
|
|
95
|
+
alias: 'h',
|
|
96
|
+
type: Boolean
|
|
97
|
+
},
|
|
98
|
+
{
|
|
99
|
+
defaultOption: true,
|
|
100
|
+
name: 'name',
|
|
101
|
+
type: String
|
|
102
|
+
},
|
|
103
|
+
], {argv: this.arguments});
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
module.exports = Read;
|
package/lib/local/rm.js
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const LocalCommand = require('./LocalCommand');
|
|
4
|
+
const nodeFs = require('fs');
|
|
5
|
+
|
|
6
|
+
class Rm extends LocalCommand {
|
|
7
|
+
get name() { return 'rm'; }
|
|
8
|
+
get requiresFilesystem() { return true; }
|
|
9
|
+
|
|
10
|
+
run() {
|
|
11
|
+
let recursive = false;
|
|
12
|
+
let force = false;
|
|
13
|
+
const paths = [];
|
|
14
|
+
|
|
15
|
+
for (const arg of this.arguments) {
|
|
16
|
+
if (/^-[rRf]+$/.test(arg)) {
|
|
17
|
+
if (arg.includes('r') || arg.includes('R')) recursive = true;
|
|
18
|
+
if (arg.includes('f')) force = true;
|
|
19
|
+
} else {
|
|
20
|
+
paths.push(arg);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
if (paths.length === 0) {
|
|
25
|
+
if (!force) {
|
|
26
|
+
this.stdErr = 'rm: missing operand';
|
|
27
|
+
this.exitCode = 1;
|
|
28
|
+
}
|
|
29
|
+
return Promise.resolve();
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
for (const path of paths) {
|
|
33
|
+
try {
|
|
34
|
+
this._remove(path, recursive, force);
|
|
35
|
+
} catch(e) {
|
|
36
|
+
this.stdErr = e.message;
|
|
37
|
+
this.exitCode = 1;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return Promise.resolve();
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
_remove(path, recursive, force) {
|
|
44
|
+
const isLink = this.fs.isSymlink ? this.fs.isSymlink(path) : false;
|
|
45
|
+
if (!isLink) {
|
|
46
|
+
const file = this.fs.getFileByPath(path);
|
|
47
|
+
if (file === null || file === undefined) {
|
|
48
|
+
if (!force) throw new Error(`rm: cannot remove '${path}': No such file or directory`);
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
if (file.constructor === Object) {
|
|
52
|
+
if (!recursive) throw new Error(`rm: cannot remove '${path}': Is a directory`);
|
|
53
|
+
// Use readdirSync on real fs to catch all entries including dangling symlinks,
|
|
54
|
+
// which getFileByPath skips because statSync throws for them.
|
|
55
|
+
let children;
|
|
56
|
+
if (this.fs.isRealFilesystem) {
|
|
57
|
+
try { children = nodeFs.readdirSync(this.fs.absolutePath(path)); } catch(e) { children = []; }
|
|
58
|
+
} else {
|
|
59
|
+
children = Object.keys(file);
|
|
60
|
+
}
|
|
61
|
+
for (const key of children) {
|
|
62
|
+
const childPath = path.replace(/\/$/, '') + '/' + key;
|
|
63
|
+
this._remove(childPath, recursive, force);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
this.fs.deleteFileByPath(path);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
module.exports = Rm;
|
package/lib/local/which.js
CHANGED
|
@@ -8,13 +8,13 @@ class Which extends LocalCommand {
|
|
|
8
8
|
return Promise.reject('which requires a single argument');
|
|
9
9
|
}
|
|
10
10
|
|
|
11
|
-
const file = this.context.fs.getFileByPath(`${this.context.getVar('PATH')}/${this.arguments[0]}
|
|
11
|
+
const file = this.context.fs.getFileByPath(`${this.context.getVar('PATH')}/${this.arguments[0]}`);
|
|
12
12
|
|
|
13
13
|
if(!file || file.constructor === Object || file.constructor === String) {
|
|
14
14
|
return Promise.reject();
|
|
15
15
|
}
|
|
16
16
|
|
|
17
|
-
return Promise.resolve(
|
|
17
|
+
return Promise.resolve(`${this.context.getVar('PATH')}/${this.arguments[0]}`);
|
|
18
18
|
}
|
|
19
19
|
}
|
|
20
20
|
|