brsh 3.9.0 → 3.10.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 +7 -0
- package/Readme.md +22 -4
- package/bin/Readme.md +33 -3
- package/bin/exportFileSystem.js +127 -110
- package/bin/wbt.js +118 -0
- package/dist/shell.es.js +6852 -6616
- package/dist/shell.es.js.map +1 -1
- package/dist/shell.min.js +23 -22
- package/dist/shell.min.js.map +1 -1
- package/dist/wbt.es.js +2534 -2342
- package/dist/wbt.es.js.map +1 -1
- package/dist/wbt.min.js +15 -11
- package/dist/wbt.min.js.map +1 -1
- package/index.js +66 -71
- package/lib/BaseFileSystem.js +55 -2
- package/lib/Context.js +1 -1
- package/lib/FileSystem.js +4 -2
- package/lib/JsCommand.js +113 -0
- package/lib/NodeFileSystem.js +164 -30
- package/lib/Readme.md +1 -0
- package/lib/ScriptCommand.js +110 -7
- package/lib/WasmCommand.js +85 -25
- package/lib/buildFilesystemStructure.js +38 -24
- package/lib/isUtf8Text.js +47 -0
- package/lib/local/which.js +8 -3
- package/lib/wbt/index.js +344 -160
- package/lib/wbt/registry.js +21 -1
- package/package.json +3 -2
- package/test/exportFileSystem.test.js +37 -15
- package/test/js-command.test.js +125 -0
- package/test/node-filesystem.test.js +7 -0
- package/test/scripts.test.js +30 -0
- package/test/wbt.test.js +308 -10
- package/test/wbtCli.test.js +160 -0
package/index.js
CHANGED
|
@@ -4,6 +4,7 @@ const Context = require('./lib/Context');
|
|
|
4
4
|
const LocalCommand = require('./lib/local/LocalCommand');
|
|
5
5
|
const ScriptCommand = require('./lib/ScriptCommand');
|
|
6
6
|
const WasmCommand = require('./lib/WasmCommand');
|
|
7
|
+
const JsCommand = require('./lib/JsCommand');
|
|
7
8
|
const Command = require('./lib/Command');
|
|
8
9
|
const InteractiveCommand = require('./lib/InteractiveCommand');
|
|
9
10
|
const { TtyStream } = require('./lib/TtyStream');
|
|
@@ -1294,81 +1295,86 @@ class Shell extends EventEmitter {
|
|
|
1294
1295
|
return segments;
|
|
1295
1296
|
}
|
|
1296
1297
|
|
|
1298
|
+
_loadJsCommand(scriptPath, content) {
|
|
1299
|
+
if (!this._jsCommandCache) {
|
|
1300
|
+
this._jsCommandCache = new Map();
|
|
1301
|
+
}
|
|
1302
|
+
return JsCommand.load(this.context.fs, scriptPath, content, this._jsCommandCache);
|
|
1303
|
+
}
|
|
1304
|
+
|
|
1305
|
+
_findExecutablePath(basePath) {
|
|
1306
|
+
if (this.context.fs.exists(basePath)) return basePath;
|
|
1307
|
+
if (!basePath.endsWith('.wasm') && this.context.fs.exists(`${basePath}.wasm`)) return `${basePath}.wasm`;
|
|
1308
|
+
if (!basePath.endsWith('.js') && this.context.fs.exists(`${basePath}.js`)) return `${basePath}.js`;
|
|
1309
|
+
return null;
|
|
1310
|
+
}
|
|
1311
|
+
|
|
1312
|
+
_resolveCommandFromPath(binLocation) {
|
|
1313
|
+
const targetPath = this.context.fs.realpath(binLocation);
|
|
1314
|
+
if (!this.context.fs.isExecutable(binLocation) && !this.context.fs.isExecutable(targetPath)) {
|
|
1315
|
+
throw new Error(`${binLocation}: permission denied`);
|
|
1316
|
+
}
|
|
1317
|
+
|
|
1318
|
+
const handle = this.context.fs.getFileByPath(targetPath);
|
|
1319
|
+
|
|
1320
|
+
if (JsCommand.isCommandClass(handle)) {
|
|
1321
|
+
return handle;
|
|
1322
|
+
}
|
|
1323
|
+
if (typeof handle === 'string' && handle.startsWith('#!')) {
|
|
1324
|
+
return ScriptCommand.bind(this, handle);
|
|
1325
|
+
}
|
|
1326
|
+
if (WasmCommand.isWasmFile(targetPath, handle)) {
|
|
1327
|
+
return WasmCommand.bind(null, targetPath);
|
|
1328
|
+
}
|
|
1329
|
+
if (JsCommand.isJsFile(targetPath, handle)) {
|
|
1330
|
+
return this._loadJsCommand(targetPath, handle);
|
|
1331
|
+
}
|
|
1332
|
+
|
|
1333
|
+
return null;
|
|
1334
|
+
}
|
|
1335
|
+
|
|
1297
1336
|
_parseCommand(command) {
|
|
1298
|
-
if(command.startsWith('./') || command.startsWith('/')) {
|
|
1299
|
-
const
|
|
1300
|
-
if(!
|
|
1337
|
+
if (command.startsWith('./') || command.startsWith('/') || command.startsWith('../') || command.includes('/')) {
|
|
1338
|
+
const absPath = this._findExecutablePath(this.context.fs.absolutePath(command));
|
|
1339
|
+
if (!absPath) {
|
|
1301
1340
|
throw new Error(`${command}: No such file or directory`);
|
|
1302
1341
|
}
|
|
1303
1342
|
|
|
1304
|
-
|
|
1343
|
+
const stat = this.context.fs.stat(absPath);
|
|
1344
|
+
if (stat && stat.isDirectory) {
|
|
1305
1345
|
throw new Error(`${command}: is a directory`);
|
|
1306
1346
|
}
|
|
1307
1347
|
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
}
|
|
1312
|
-
return handle;
|
|
1313
|
-
}
|
|
1314
|
-
|
|
1315
|
-
if(handle.constructor === String && handle.startsWith('#!')) {
|
|
1316
|
-
if (!this.context.fs.isExecutable(command)) {
|
|
1317
|
-
throw new Error(`${command}: permission denied`);
|
|
1318
|
-
}
|
|
1319
|
-
return ScriptCommand.bind(this, handle);
|
|
1320
|
-
}
|
|
1321
|
-
|
|
1322
|
-
if(this._isWasmFile(command, handle)) {
|
|
1323
|
-
if (!this.context.fs.isExecutable(command)) {
|
|
1324
|
-
throw new Error(`${command}: permission denied`);
|
|
1325
|
-
}
|
|
1326
|
-
return WasmCommand.bind(null, command);
|
|
1348
|
+
const cmd = this._resolveCommandFromPath(absPath);
|
|
1349
|
+
if (!cmd) {
|
|
1350
|
+
throw new Error(`${command}: permission denied`);
|
|
1327
1351
|
}
|
|
1352
|
+
return cmd;
|
|
1353
|
+
}
|
|
1328
1354
|
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
}
|
|
1334
|
-
const abs = this.context.fs.absolutePath(command);
|
|
1335
|
-
try {
|
|
1336
|
-
const mod = require(abs);
|
|
1337
|
-
const Cls = mod.default || mod;
|
|
1338
|
-
if (typeof Cls === 'function' && typeof Cls.prototype.run === 'function' && typeof Cls.prototype.runCommand === 'function') {
|
|
1339
|
-
return Cls;
|
|
1340
|
-
}
|
|
1341
|
-
} catch(e) {
|
|
1342
|
-
// Not a loadable command module — fall through
|
|
1343
|
-
}
|
|
1344
|
-
}
|
|
1355
|
+
// 1. Built-in registered commands take precedence (mkdir, echo, cat, etc.)
|
|
1356
|
+
const builtin = this.context.getCommand(command)
|
|
1357
|
+
|| (!command.endsWith('.wasm') && this.context.getCommand(`${command}.wasm`))
|
|
1358
|
+
|| (!command.endsWith('.js') && this.context.getCommand(`${command}.js`));
|
|
1345
1359
|
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
let cmd = this.context.getCommand(command);
|
|
1349
|
-
let cmdName = command;
|
|
1350
|
-
if (!cmd && !command.endsWith('.wasm')) {
|
|
1351
|
-
cmd = this.context.getCommand(`${command}.wasm`);
|
|
1352
|
-
if (cmd) cmdName = `${command}.wasm`;
|
|
1360
|
+
if (JsCommand.isCommandClass(builtin)) {
|
|
1361
|
+
return builtin;
|
|
1353
1362
|
}
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
throw new Error(`${command}: Command not found`);
|
|
1363
|
+
if (typeof builtin === 'string' && builtin.startsWith('#!')) {
|
|
1364
|
+
return ScriptCommand.bind(this, builtin);
|
|
1357
1365
|
}
|
|
1358
1366
|
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1367
|
+
// 2. Search PATH directories for installed binaries, symlinks, scripts, and wasm
|
|
1368
|
+
const pathDirs = (this.context.getVar('PATH') || '/bin').split(':').filter(Boolean);
|
|
1369
|
+
for (const dir of pathDirs) {
|
|
1370
|
+
const binLocation = this._findExecutablePath(`${dir}/${command}`);
|
|
1371
|
+
if (binLocation) {
|
|
1372
|
+
const cmd = this._resolveCommandFromPath(binLocation);
|
|
1373
|
+
if (cmd) return cmd;
|
|
1363
1374
|
}
|
|
1364
|
-
return WasmCommand.bind(null, binLocation);
|
|
1365
1375
|
}
|
|
1366
1376
|
|
|
1367
|
-
|
|
1368
|
-
return ScriptCommand.bind(this, cmd);
|
|
1369
|
-
}
|
|
1370
|
-
|
|
1371
|
-
return cmd;
|
|
1377
|
+
throw new Error(`${command}: Command not found`);
|
|
1372
1378
|
}
|
|
1373
1379
|
|
|
1374
1380
|
async _parseCommandLine(string) {
|
|
@@ -1614,18 +1620,6 @@ class Shell extends EventEmitter {
|
|
|
1614
1620
|
return parts;
|
|
1615
1621
|
}
|
|
1616
1622
|
|
|
1617
|
-
_isWasmFile(path, content) {
|
|
1618
|
-
if (path.endsWith('.wasm') || path.endsWith('.wat')) return true;
|
|
1619
|
-
if (content instanceof Uint8Array) {
|
|
1620
|
-
return content.length >= 4 && content[0] === 0x00 && content[1] === 0x61 && content[2] === 0x73 && content[3] === 0x6d;
|
|
1621
|
-
}
|
|
1622
|
-
if (typeof content !== 'string') return false;
|
|
1623
|
-
// Binary WASM: magic bytes \0asm
|
|
1624
|
-
if (content.charCodeAt(0) === 0 && content.slice(1, 4) === 'asm') return true;
|
|
1625
|
-
// WAT text format
|
|
1626
|
-
return content.trimStart().startsWith('(module');
|
|
1627
|
-
}
|
|
1628
|
-
|
|
1629
1623
|
static _escapeRegExp(string) {
|
|
1630
1624
|
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
1631
1625
|
}
|
|
@@ -1664,6 +1658,7 @@ Shell.InteractiveCommand = InteractiveCommand;
|
|
|
1664
1658
|
Shell.WasmCommand = WasmCommand;
|
|
1665
1659
|
Shell.createWasmCommand = WasmCommand.createGenericWasmCommand || WasmCommand.createCommand;
|
|
1666
1660
|
Shell.ReplCommand = require('./lib/ReplCommand');
|
|
1661
|
+
Shell.JsCommand = JsCommand;
|
|
1667
1662
|
Shell.FS = BaseFileSystem;
|
|
1668
1663
|
Shell.BinaryContainer = require('./lib/BinaryContainer');
|
|
1669
1664
|
Shell.Storage = Shell.BinaryContainer.FilesystemStorage;
|
package/lib/BaseFileSystem.js
CHANGED
|
@@ -9,8 +9,8 @@ const SANDBOX_ALLOWED = new Set([
|
|
|
9
9
|
'getMode', 'setMode',
|
|
10
10
|
'isReadable', 'isWritable', 'isExecutable',
|
|
11
11
|
'getSize', 'autoComplete',
|
|
12
|
-
'isRealFilesystem', 'isSymlink', 'readSymlink', 'createSymlink',
|
|
13
|
-
'setTtyDevice',
|
|
12
|
+
'isRealFilesystem', 'isSymlink', 'readSymlink', 'createSymlink', 'realpath',
|
|
13
|
+
'setTtyDevice', 'loadModule',
|
|
14
14
|
]);
|
|
15
15
|
|
|
16
16
|
/**
|
|
@@ -34,6 +34,35 @@ const SANDBOX_ALLOWED = new Set([
|
|
|
34
34
|
* addFiles(commands)
|
|
35
35
|
*/
|
|
36
36
|
class BaseFileSystem {
|
|
37
|
+
/**
|
|
38
|
+
* Loads and evaluates a JavaScript module from the filesystem at `path`,
|
|
39
|
+
* returning its default or CommonJS export.
|
|
40
|
+
* Subclasses (like NodeFileSystem) can override this to use native module loaders.
|
|
41
|
+
*/
|
|
42
|
+
loadModule(path) {
|
|
43
|
+
const content = this.getFileByPath(path);
|
|
44
|
+
if (content === null || content === undefined) {
|
|
45
|
+
throw new Error(`${path}: No such file or directory`);
|
|
46
|
+
}
|
|
47
|
+
if (typeof content === 'function') {
|
|
48
|
+
return content;
|
|
49
|
+
}
|
|
50
|
+
let code = typeof content === 'string' ? content : new TextDecoder().decode(content);
|
|
51
|
+
if (/^\s*export\s+default\s+/m.test(code)) {
|
|
52
|
+
code = code.replace(/^\s*export\s+default\s+/m, 'module.exports = ');
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const moduleObj = { exports: {} };
|
|
56
|
+
const Command = require('./Command');
|
|
57
|
+
const LocalCommand = require('./local/LocalCommand');
|
|
58
|
+
const WasmCommand = require('./WasmCommand');
|
|
59
|
+
const InteractiveCommand = require('./InteractiveCommand');
|
|
60
|
+
const ReplCommand = require('./ReplCommand');
|
|
61
|
+
const fn = new Function('module', 'exports', 'require', 'Command', 'LocalCommand', 'WasmCommand', 'InteractiveCommand', 'ReplCommand', code);
|
|
62
|
+
fn(moduleObj, moduleObj.exports, require, Command, LocalCommand, WasmCommand, InteractiveCommand, ReplCommand);
|
|
63
|
+
return moduleObj.exports.default !== undefined ? moduleObj.exports.default : moduleObj.exports;
|
|
64
|
+
}
|
|
65
|
+
|
|
37
66
|
isReadable(path) {
|
|
38
67
|
const mode = this.getMode(path);
|
|
39
68
|
if (mode === null) return false;
|
|
@@ -52,6 +81,22 @@ class BaseFileSystem {
|
|
|
52
81
|
return (mode & 0o100) !== 0;
|
|
53
82
|
}
|
|
54
83
|
|
|
84
|
+
/**
|
|
85
|
+
* Resolves symbolic links along `path`, returning the target absolute path.
|
|
86
|
+
*/
|
|
87
|
+
realpath(path, maxHops = 10) {
|
|
88
|
+
let current = this.absolutePath(path);
|
|
89
|
+
let hops = 0;
|
|
90
|
+
while (hops++ < maxHops && typeof this.isSymlink === 'function' && this.isSymlink(current)) {
|
|
91
|
+
const link = this.readSymlink(current);
|
|
92
|
+
if (!link) break;
|
|
93
|
+
current = link.startsWith('/')
|
|
94
|
+
? this.absolutePath(link)
|
|
95
|
+
: this.absolutePath(`${current.split('/').slice(0, -1).join('/')}/${link}`);
|
|
96
|
+
}
|
|
97
|
+
return current;
|
|
98
|
+
}
|
|
99
|
+
|
|
55
100
|
// Returns a sandboxed proxy of this filesystem for use by commands.
|
|
56
101
|
// Only the public API is accessible; raw/internal methods are blocked.
|
|
57
102
|
sandbox() {
|
|
@@ -117,6 +162,10 @@ class SymlinkRef {
|
|
|
117
162
|
constructor(target) {
|
|
118
163
|
this.target = target;
|
|
119
164
|
}
|
|
165
|
+
|
|
166
|
+
toJSON() {
|
|
167
|
+
return { $l: this.target };
|
|
168
|
+
}
|
|
120
169
|
}
|
|
121
170
|
|
|
122
171
|
/**
|
|
@@ -137,7 +186,11 @@ class DeviceFile {
|
|
|
137
186
|
}
|
|
138
187
|
}
|
|
139
188
|
|
|
189
|
+
const isUtf8Text = require('./isUtf8Text');
|
|
190
|
+
|
|
140
191
|
module.exports = BaseFileSystem;
|
|
141
192
|
module.exports.BinaryFile = BinaryFile;
|
|
142
193
|
module.exports.SymlinkRef = SymlinkRef;
|
|
143
194
|
module.exports.DeviceFile = DeviceFile;
|
|
195
|
+
module.exports.isUtf8Text = isUtf8Text;
|
|
196
|
+
|
package/lib/Context.js
CHANGED
|
@@ -9,7 +9,7 @@ class Context {
|
|
|
9
9
|
|
|
10
10
|
this.vars = {};
|
|
11
11
|
this.aliases = {};
|
|
12
|
-
this.fs = new FileSystem(cwd, {}, {}, onFsChange || null);
|
|
12
|
+
this.fs = (this.options && this.options.fs) ? this.options.fs : new FileSystem(cwd, {}, {}, onFsChange || null);
|
|
13
13
|
this._clearFn = clearFn;
|
|
14
14
|
this._destroyFn = destroyFn;
|
|
15
15
|
}
|
package/lib/FileSystem.js
CHANGED
|
@@ -12,7 +12,9 @@ const { BinaryFile, SymlinkRef, DeviceFile } = BaseFileSystem;
|
|
|
12
12
|
function decodeBinaryFiles(node) {
|
|
13
13
|
for (const key of Object.keys(node)) {
|
|
14
14
|
const value = node[key];
|
|
15
|
-
if (value
|
|
15
|
+
if (value && typeof value === 'object' && ('$l' in value)) {
|
|
16
|
+
node[key] = new SymlinkRef(value.$l);
|
|
17
|
+
} else if (value !== null && typeof value === 'object' && !(value instanceof Uint8Array) && !(value instanceof SymlinkRef) && !(value instanceof DeviceFile)) {
|
|
16
18
|
decodeBinaryFiles(value);
|
|
17
19
|
} else if (typeof value === 'string' && value.startsWith('data:')) {
|
|
18
20
|
const semicolon = value.indexOf(';');
|
|
@@ -165,7 +167,7 @@ class FileSystem extends BaseFileSystem {
|
|
|
165
167
|
return file instanceof SymlinkRef ? file.target : null;
|
|
166
168
|
}
|
|
167
169
|
createSymlink(target, linkPath) {
|
|
168
|
-
this._setFileRaw(linkPath, new SymlinkRef(target));
|
|
170
|
+
this._setFileRaw(linkPath, new SymlinkRef(target), true);
|
|
169
171
|
}
|
|
170
172
|
|
|
171
173
|
// Returns type metadata without following the final symlink (like lstat).
|
package/lib/JsCommand.js
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const WasmCommand = require('./WasmCommand');
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Handles loading, evaluating, and wrapping JavaScript commands and modules
|
|
7
|
+
* stored on the brsh virtual or real filesystem.
|
|
8
|
+
*/
|
|
9
|
+
class JsCommand {
|
|
10
|
+
static isCommandClass(fn) {
|
|
11
|
+
return typeof fn === 'function'
|
|
12
|
+
&& Boolean(fn.prototype)
|
|
13
|
+
&& typeof fn.prototype.run === 'function'
|
|
14
|
+
&& typeof fn.prototype.runCommand === 'function';
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
static isJsFile(scriptPath, content) {
|
|
18
|
+
if (typeof content === 'function') return true;
|
|
19
|
+
if (scriptPath && scriptPath.endsWith('.js')) return true;
|
|
20
|
+
return typeof content === 'string' && (content.includes('export') || content.includes('module.exports'));
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Loads a JavaScript module from the filesystem, evaluates it,
|
|
25
|
+
* and returns an executable Command class.
|
|
26
|
+
*
|
|
27
|
+
* @param {Object} fs - The filesystem instance (BaseFileSystem / NodeFileSystem)
|
|
28
|
+
* @param {string} scriptPath - Absolute path to the .js script
|
|
29
|
+
* @param {string|Function|null} content - Optional pre-read content or function
|
|
30
|
+
* @param {Map} [cache] - Optional cache Map to store evaluated classes by scriptPath
|
|
31
|
+
* @returns {Function} Executable Command constructor
|
|
32
|
+
*/
|
|
33
|
+
static load(fs, scriptPath, content = null, cache = null) {
|
|
34
|
+
const fileContent = content !== null && content !== undefined
|
|
35
|
+
? content
|
|
36
|
+
: (fs && typeof fs.getFileByPath === 'function' ? fs.getFileByPath(scriptPath) : null);
|
|
37
|
+
|
|
38
|
+
if (cache && cache.has(scriptPath)) {
|
|
39
|
+
const cached = cache.get(scriptPath);
|
|
40
|
+
if (cached && cached.content === fileContent) {
|
|
41
|
+
return cached.Cmd;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (typeof fileContent === 'function') {
|
|
46
|
+
const Cmd = JsCommand.isCommandClass(fileContent)
|
|
47
|
+
? fileContent
|
|
48
|
+
: JsCommand.wrapModule(fs, scriptPath, fileContent);
|
|
49
|
+
if (cache) {
|
|
50
|
+
cache.set(scriptPath, { content: fileContent, Cmd });
|
|
51
|
+
}
|
|
52
|
+
return Cmd;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
let exported = null;
|
|
56
|
+
try {
|
|
57
|
+
if (fs && typeof fs.loadModule === 'function') {
|
|
58
|
+
exported = fs.loadModule(scriptPath);
|
|
59
|
+
} else {
|
|
60
|
+
const nodeReq = typeof require !== 'undefined' ? require : null;
|
|
61
|
+
if (nodeReq) {
|
|
62
|
+
exported = nodeReq(scriptPath);
|
|
63
|
+
} else {
|
|
64
|
+
throw new Error('loadModule is not available on filesystem');
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
} catch (e) {
|
|
68
|
+
throw new Error(`${scriptPath}: Failed to evaluate script: ${e.message}`);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const Cmd = JsCommand.isCommandClass(exported)
|
|
72
|
+
? exported
|
|
73
|
+
: JsCommand.wrapModule(fs, scriptPath, exported);
|
|
74
|
+
|
|
75
|
+
if (cache) {
|
|
76
|
+
cache.set(scriptPath, { content: fileContent, Cmd });
|
|
77
|
+
}
|
|
78
|
+
return Cmd;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Wraps a non-Command export (e.g. exported function or module object)
|
|
83
|
+
* as an executable GenericWasmCommand, automatically detecting any sibling .wasm binary.
|
|
84
|
+
*
|
|
85
|
+
* @param {Object} fs - The filesystem instance
|
|
86
|
+
* @param {string} scriptPath - Absolute path to the .js file
|
|
87
|
+
* @param {*} exported - The module's exported value
|
|
88
|
+
* @returns {Function} Executable Command constructor
|
|
89
|
+
*/
|
|
90
|
+
static wrapModule(fs, scriptPath, exported) {
|
|
91
|
+
const binName = scriptPath.split('/').pop().replace(/\.js$/i, '').toLowerCase();
|
|
92
|
+
const siblingWasmPath = scriptPath.replace(/\.js$/i, '.wasm');
|
|
93
|
+
const hasSiblingWasm = fs && typeof fs.exists === 'function' && fs.exists(siblingWasmPath);
|
|
94
|
+
const wasmBytes = (hasSiblingWasm && typeof fs.getFileByPath === 'function')
|
|
95
|
+
? fs.getFileByPath(siblingWasmPath)
|
|
96
|
+
: null;
|
|
97
|
+
|
|
98
|
+
const pkgMeta = {
|
|
99
|
+
name: binName,
|
|
100
|
+
bin: binName,
|
|
101
|
+
wasmPath: hasSiblingWasm ? siblingWasmPath : null,
|
|
102
|
+
wasmBytes: wasmBytes || null
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
const config = {
|
|
106
|
+
moduleLoader: async () => exported
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
return WasmCommand.createGenericWasmCommand(pkgMeta, config, binName);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
module.exports = JsCommand;
|