brsh 3.9.0 → 3.10.1
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 +14 -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 +4074 -3717
- package/dist/shell.es.js.map +1 -1
- package/dist/shell.min.js +26 -25
- package/dist/shell.min.js.map +1 -1
- package/dist/terminal.cjs.js +3 -3
- package/dist/terminal.cjs.js.map +1 -1
- package/dist/terminal.es.js +55 -33
- package/dist/terminal.es.js.map +1 -1
- package/dist/terminal.min.js +3 -3
- package/dist/terminal.min.js.map +1 -1
- package/dist/wbt.es.js +3082 -2710
- package/dist/wbt.es.js.map +1 -1
- package/dist/wbt.min.js +16 -12
- package/dist/wbt.min.js.map +1 -1
- package/index.js +102 -80
- package/lib/BaseFileSystem.js +57 -2
- package/lib/BinaryContainer.js +1 -1
- package/lib/Command.js +1 -1
- package/lib/Context.js +4 -3
- package/lib/FileSystem.js +4 -2
- package/lib/InteractiveCommand.js +9 -8
- package/lib/JsCommand.js +113 -0
- package/lib/NodeFileSystem.js +164 -30
- package/lib/Readme.md +1 -0
- package/lib/ScriptCommand.js +148 -8
- package/lib/Terminal.js +60 -6
- package/lib/TtyStream.js +36 -2
- package/lib/WasmCommand.js +313 -66
- package/lib/buildFilesystemStructure.js +38 -24
- package/lib/isUtf8Text.js +47 -0
- package/lib/local/which.js +8 -3
- package/lib/wbt/index.js +473 -175
- package/lib/wbt/registry.js +21 -1
- package/package.json +3 -2
- package/test/devices-signals.test.js +112 -0
- package/test/doom.test.js +7 -4
- 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/terminal.test.js +148 -0
- package/test/wasm-cli.test.js +76 -0
- package/test/wbt.test.js +418 -10
- package/test/wbtCli.test.js +160 -0
package/lib/wbt/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const
|
|
3
|
+
const path = require('path');
|
|
4
|
+
const Command = require('../Command');
|
|
4
5
|
const WasmCommand = require('../WasmCommand');
|
|
5
6
|
const ReplCommand = require('../ReplCommand');
|
|
6
7
|
const { DEFAULT_REGISTRY, resolvePackageName } = require('./registry');
|
|
@@ -8,7 +9,6 @@ const wllamaAdapter = require('./packages/wllama');
|
|
|
8
9
|
const { writeBytesToFs, createGenericWasmCommand } = WasmCommand;
|
|
9
10
|
const { createReplWasmCommand } = ReplCommand;
|
|
10
11
|
|
|
11
|
-
const STATE_FILE = '/var/lib/wbt/installed.json';
|
|
12
12
|
const CACHE_DIR = '/var/cache/wbt';
|
|
13
13
|
/**
|
|
14
14
|
* Creates a Command class that executes a custom installed WASM or WAT file at `wasmPath`.
|
|
@@ -55,18 +55,65 @@ function createCustomWasmBinCommand(binName, wasmPath, pkgMeta = {}) {
|
|
|
55
55
|
* const CustomWbt = Wbt.configure({ registry: { ... } });
|
|
56
56
|
* shell.context.setCommand(CustomWbt);
|
|
57
57
|
*/
|
|
58
|
-
class Wbt extends
|
|
58
|
+
class Wbt extends Command {
|
|
59
59
|
static get name() { return 'wbt'; }
|
|
60
60
|
get name() { return 'wbt'; }
|
|
61
61
|
get requiresFilesystem() { return true; }
|
|
62
62
|
|
|
63
|
+
get _fs() {
|
|
64
|
+
return this.fs || (this.context && this.context.fs) || null;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
get _pathDir() {
|
|
68
|
+
return (this.context && typeof this.context.getVar === 'function' && this.context.getVar('PATH')) || '/bin';
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
_withWritableParent(targetPath, fn) {
|
|
72
|
+
const fs = this._fs;
|
|
73
|
+
if (!fs) return;
|
|
74
|
+
const abs = typeof fs.absolutePath === 'function' ? fs.absolutePath(targetPath) : targetPath;
|
|
75
|
+
const parts = abs.split('/');
|
|
76
|
+
parts.pop();
|
|
77
|
+
const parentPath = parts.join('/') || '/';
|
|
78
|
+
const parentMode = typeof fs.getMode === 'function' ? fs.getMode(parentPath) : null;
|
|
79
|
+
const needsChmod = parentMode !== null && (parentMode & 0o200) === 0;
|
|
80
|
+
if (needsChmod) {
|
|
81
|
+
try { fs.setMode(parentPath, parentMode | 0o200); } catch { /* ignore */ }
|
|
82
|
+
}
|
|
83
|
+
try {
|
|
84
|
+
return fn(fs);
|
|
85
|
+
} finally {
|
|
86
|
+
if (needsChmod) {
|
|
87
|
+
try { fs.setMode(parentPath, parentMode); } catch { /* ignore */ }
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
_writeFileSafe(targetPath, content) {
|
|
93
|
+
this._withWritableParent(targetPath, (fs) => {
|
|
94
|
+
if (fs.exists(targetPath)) {
|
|
95
|
+
try { fs.setMode(targetPath, 0o755); } catch { /* ignore */ }
|
|
96
|
+
}
|
|
97
|
+
fs.setFileByPath(targetPath, content, true);
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
_writeBytesSafe(targetPath, bytes, mimeType = 'application/wasm') {
|
|
102
|
+
this._withWritableParent(targetPath, (fs) => {
|
|
103
|
+
if (fs.exists(targetPath)) {
|
|
104
|
+
try { fs.setMode(targetPath, 0o755); } catch { /* ignore */ }
|
|
105
|
+
}
|
|
106
|
+
writeBytesToFs(fs, targetPath, bytes, mimeType);
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
|
|
63
110
|
get interactive() {
|
|
64
111
|
if (this._activeSubCommand && this._activeSubCommand.interactive !== undefined) {
|
|
65
112
|
return Boolean(this._activeSubCommand.interactive);
|
|
66
113
|
}
|
|
67
114
|
if (this.arguments && this.arguments[0] === 'run' && this.arguments[1]) {
|
|
68
|
-
const
|
|
69
|
-
const pkgMeta = this._getRegistry()[
|
|
115
|
+
const resolvedName = resolvePackageName(this.arguments[1]);
|
|
116
|
+
const pkgMeta = this._getRegistry()[resolvedName];
|
|
70
117
|
if (pkgMeta && pkgMeta.interactive) {
|
|
71
118
|
const isHelpOrVersion = this.arguments.slice(2).some(a => a === '--help' || a === '-h' || a === '--version' || a === '-V' || a === '-v');
|
|
72
119
|
return !isHelpOrVersion;
|
|
@@ -82,6 +129,13 @@ class Wbt extends LocalCommand {
|
|
|
82
129
|
return this.interactive;
|
|
83
130
|
}
|
|
84
131
|
|
|
132
|
+
get isRaw() {
|
|
133
|
+
return Boolean(
|
|
134
|
+
(this.stdinStream && this.stdinStream.isRaw)
|
|
135
|
+
|| (this._activeSubCommand && (this._activeSubCommand.isRaw || (this._activeSubCommand.stdinStream && this._activeSubCommand.stdinStream.isRaw)))
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
|
|
85
139
|
onInput(key) {
|
|
86
140
|
if (this._activeSubCommand && typeof this._activeSubCommand.onInput === 'function') {
|
|
87
141
|
this._activeSubCommand.onInput(key);
|
|
@@ -117,22 +171,19 @@ class Wbt extends LocalCommand {
|
|
|
117
171
|
}
|
|
118
172
|
|
|
119
173
|
/**
|
|
120
|
-
* Restores any packages
|
|
121
|
-
*
|
|
174
|
+
* Restores any packages into the given Shell or Context.
|
|
175
|
+
* Retained as a safe no-op for backward compatibility: packages are self-contained on the filesystem.
|
|
122
176
|
*/
|
|
123
|
-
static restoreInstalled(
|
|
124
|
-
|
|
125
|
-
if (!ctx || !ctx.fs) return;
|
|
126
|
-
const instance = new (this.configure(config))([], ctx);
|
|
127
|
-
instance.fs = ctx.fs.sandbox();
|
|
128
|
-
instance._restoreCommands();
|
|
177
|
+
static restoreInstalled() {
|
|
178
|
+
// No-op: virtual filesystem is self-contained via /usr/lib/ and /bin symlinks
|
|
129
179
|
}
|
|
130
180
|
|
|
131
181
|
_getRegistry() {
|
|
132
182
|
const cfg = this.constructor._config || {};
|
|
133
183
|
const base = Object.assign({}, DEFAULT_REGISTRY, cfg.registry || {});
|
|
134
184
|
try {
|
|
135
|
-
const
|
|
185
|
+
const fs = this._fs;
|
|
186
|
+
const cachedRemote = fs ? fs.getFileByPath(`${CACHE_DIR}/remote-registry.json`) : null;
|
|
136
187
|
if (typeof cachedRemote === 'string' && cachedRemote.trim()) {
|
|
137
188
|
const parsed = JSON.parse(cachedRemote);
|
|
138
189
|
Object.assign(base, parsed);
|
|
@@ -143,23 +194,79 @@ class Wbt extends LocalCommand {
|
|
|
143
194
|
return base;
|
|
144
195
|
}
|
|
145
196
|
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
197
|
+
_getInstalledPackages() {
|
|
198
|
+
const pkgs = {};
|
|
199
|
+
const fs = this._fs;
|
|
200
|
+
if (!fs) return pkgs;
|
|
201
|
+
const usrLib = fs.getFileByPath('/usr/lib');
|
|
202
|
+
if (!usrLib || typeof usrLib !== 'object') {
|
|
203
|
+
return pkgs;
|
|
204
|
+
}
|
|
205
|
+
const registry = this._getRegistry();
|
|
206
|
+
|
|
207
|
+
for (const dirName of Object.keys(usrLib)) {
|
|
208
|
+
if (dirName.startsWith('.')) continue;
|
|
209
|
+
const pkgName = dirName.toLowerCase();
|
|
210
|
+
const reg = registry[pkgName] || Object.values(registry).find(p => p.name === pkgName || p.bin === pkgName);
|
|
211
|
+
const dirFiles = fs.getFileByPath(`/usr/lib/${dirName}`) || {};
|
|
212
|
+
const files = (typeof dirFiles === 'object') ? Object.keys(dirFiles) : [];
|
|
213
|
+
const wasmFile = files.find(f => f.endsWith('.wasm') || f.endsWith('.wat'));
|
|
214
|
+
const wasmPath = wasmFile ? `/usr/lib/${dirName}/${wasmFile}` : (fs.exists(`/usr/lib/${dirName}/${pkgName}.wasm`) ? `/usr/lib/${dirName}/${pkgName}.wasm` : null);
|
|
215
|
+
|
|
216
|
+
const primaryBin = (reg && reg.bin) || pkgName;
|
|
217
|
+
const bins = [primaryBin];
|
|
218
|
+
if (reg && reg.aliases) {
|
|
219
|
+
bins.push(...reg.aliases);
|
|
220
|
+
}
|
|
221
|
+
if (reg && reg.scripts) {
|
|
222
|
+
bins.push(...Object.keys(reg.scripts));
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
pkgs[pkgName] = Object.assign({
|
|
226
|
+
name: pkgName,
|
|
227
|
+
bin: primaryBin,
|
|
228
|
+
bins,
|
|
229
|
+
version: (reg && reg.version) || 'custom',
|
|
230
|
+
description: (reg && reg.description) || `Installed package ${pkgName}`,
|
|
231
|
+
type: (reg && reg.type) || (wasmPath ? 'custom-wasm' : 'generic-runtime'),
|
|
232
|
+
wasmPath
|
|
233
|
+
}, reg || {});
|
|
234
|
+
if (wasmPath) {
|
|
235
|
+
pkgs[pkgName].wasmPath = wasmPath;
|
|
154
236
|
}
|
|
155
|
-
} catch {
|
|
156
|
-
// ignore
|
|
157
237
|
}
|
|
158
|
-
return
|
|
238
|
+
return pkgs;
|
|
159
239
|
}
|
|
160
240
|
|
|
161
|
-
|
|
162
|
-
|
|
241
|
+
_generateJsCommandSource(pkgMeta, binName) {
|
|
242
|
+
if (pkgMeta.adapter === 'wllama' || pkgMeta.name === 'wllama') {
|
|
243
|
+
const wllamaPath = path.resolve(__dirname, 'packages/wllama.js');
|
|
244
|
+
return `'use strict';\n` +
|
|
245
|
+
`let wllamaModule;\n` +
|
|
246
|
+
`try {\n` +
|
|
247
|
+
` wllamaModule = require(${JSON.stringify(wllamaPath)});\n` +
|
|
248
|
+
`} catch (_) {\n` +
|
|
249
|
+
` try {\n` +
|
|
250
|
+
` wllamaModule = require('brsh/wbt/packages/wllama');\n` +
|
|
251
|
+
` } catch (e) {\n` +
|
|
252
|
+
` wllamaModule = require('./wllama');\n` +
|
|
253
|
+
` }\n` +
|
|
254
|
+
`}\n` +
|
|
255
|
+
`module.exports = wllamaModule.createWllamaCommand(${JSON.stringify(pkgMeta)}, {}, ${JSON.stringify(binName)});\n`;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
const serializedMeta = JSON.stringify(pkgMeta, null, 2);
|
|
259
|
+
if (pkgMeta.repl) {
|
|
260
|
+
return `'use strict';\n` +
|
|
261
|
+
`const pkgMeta = ${serializedMeta};\n` +
|
|
262
|
+
`const _Repl = typeof ReplCommand !== 'undefined' ? ReplCommand : (typeof require !== 'undefined' ? require('brsh/ReplCommand') : globalThis.ReplCommand);\n` +
|
|
263
|
+
`module.exports = _Repl.createReplWasmCommand(pkgMeta, {}, ${JSON.stringify(binName)});\n`;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
return `'use strict';\n` +
|
|
267
|
+
`const pkgMeta = ${serializedMeta};\n` +
|
|
268
|
+
`const _Wasm = typeof WasmCommand !== 'undefined' ? WasmCommand : (typeof require !== 'undefined' ? require('brsh/WasmCommand') : globalThis.WasmCommand);\n` +
|
|
269
|
+
`module.exports = _Wasm.createGenericWasmCommand(pkgMeta, {}, ${JSON.stringify(binName)});\n`;
|
|
163
270
|
}
|
|
164
271
|
|
|
165
272
|
_buildCommandsForPackage(pkgMeta) {
|
|
@@ -202,62 +309,111 @@ class Wbt extends LocalCommand {
|
|
|
202
309
|
}
|
|
203
310
|
|
|
204
311
|
_registerPackageIntoContext(pkgMeta) {
|
|
205
|
-
const
|
|
312
|
+
const fs = this._fs;
|
|
313
|
+
const cfg = this.constructor._config || {};
|
|
314
|
+
const pathDir = this._pathDir;
|
|
315
|
+
const pkgName = (pkgMeta.name || '').split('/').pop().toLowerCase();
|
|
316
|
+
const primaryBin = (pkgMeta.bin || pkgName).toLowerCase();
|
|
317
|
+
const targetDir = `/usr/lib/${pkgName}`;
|
|
206
318
|
const entries = this._buildCommandsForPackage(pkgMeta);
|
|
207
319
|
const installedBins = [];
|
|
320
|
+
const adapterKey = pkgMeta.adapter || pkgMeta.name;
|
|
321
|
+
const hasRuntimeClosure = Boolean(
|
|
322
|
+
cfg.moduleLoader
|
|
323
|
+
|| cfg.fetch
|
|
324
|
+
|| adapterKey === 'wllama'
|
|
325
|
+
|| (this.constructor._adapters && this.constructor._adapters.has(adapterKey))
|
|
326
|
+
);
|
|
327
|
+
|
|
328
|
+
// Check if pure wasm exists at targetDir
|
|
329
|
+
const wasmPath = pkgMeta.wasmPath || (fs.exists(`${targetDir}/${primaryBin}.wasm`) ? `${targetDir}/${primaryBin}.wasm` : null);
|
|
330
|
+
|
|
331
|
+
const metaPath = `${targetDir}/package.json`;
|
|
332
|
+
const pkgInfo = {
|
|
333
|
+
name: pkgMeta.name,
|
|
334
|
+
version: pkgMeta.version || '1.0.0',
|
|
335
|
+
bin: primaryBin,
|
|
336
|
+
aliases: pkgMeta.aliases || [],
|
|
337
|
+
description: pkgMeta.description || '',
|
|
338
|
+
...(pkgMeta.interactive ? { interactive: true } : {})
|
|
339
|
+
};
|
|
340
|
+
this._writeFileSafe(metaPath, JSON.stringify(pkgInfo, null, 2));
|
|
208
341
|
|
|
209
342
|
for (const { bin, Cmd } of entries) {
|
|
210
343
|
const binName = bin.toLowerCase();
|
|
211
|
-
|
|
344
|
+
let targetFile;
|
|
345
|
+
|
|
346
|
+
if (wasmPath && (!pkgMeta.type || pkgMeta.type === 'custom-wasm' || pkgMeta.type === 'wasm')) {
|
|
347
|
+
targetFile = wasmPath;
|
|
348
|
+
} else {
|
|
349
|
+
targetFile = `${targetDir}/${binName}.js`;
|
|
350
|
+
const jsSource = this._generateJsCommandSource(pkgMeta, binName);
|
|
351
|
+
if (typeof Cmd === 'function') {
|
|
352
|
+
Cmd.__source = jsSource;
|
|
353
|
+
}
|
|
354
|
+
if (!fs.isRealFilesystem && typeof Cmd === 'function' && hasRuntimeClosure) {
|
|
355
|
+
this._writeFileSafe(targetFile, Cmd);
|
|
356
|
+
} else {
|
|
357
|
+
this._writeFileSafe(targetFile, jsSource);
|
|
358
|
+
}
|
|
359
|
+
try { fs.setMode(targetFile, 0o755); } catch { /* ignore */ }
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
this._createBinSymlink(pathDir, binName, targetFile);
|
|
212
363
|
installedBins.push(binName);
|
|
213
364
|
}
|
|
214
365
|
|
|
215
|
-
// Also register any aliases not already created by the factory
|
|
216
366
|
for (const alias of (pkgMeta.aliases || [])) {
|
|
217
367
|
const aliasLower = alias.toLowerCase();
|
|
218
368
|
if (!installedBins.includes(aliasLower) && entries.length > 0) {
|
|
219
|
-
const
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
}
|
|
224
|
-
this._installBin(pathDir, aliasLower, AliasCommand);
|
|
369
|
+
const primaryTarget = wasmPath && (!pkgMeta.type || pkgMeta.type === 'custom-wasm' || pkgMeta.type === 'wasm')
|
|
370
|
+
? wasmPath
|
|
371
|
+
: `${targetDir}/${primaryBin}.js`;
|
|
372
|
+
this._createBinSymlink(pathDir, aliasLower, primaryTarget);
|
|
225
373
|
installedBins.push(aliasLower);
|
|
226
374
|
}
|
|
227
375
|
}
|
|
228
376
|
|
|
377
|
+
const scriptMap = pkgMeta.scriptPaths || {};
|
|
378
|
+
for (const [scriptBin, scriptTarget] of Object.entries(scriptMap)) {
|
|
379
|
+
const binLower = scriptBin.toLowerCase();
|
|
380
|
+
if (!installedBins.includes(binLower) && fs.exists(scriptTarget)) {
|
|
381
|
+
this._createBinSymlink(pathDir, binLower, scriptTarget);
|
|
382
|
+
installedBins.push(binLower);
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
if (pkgMeta.scripts && typeof pkgMeta.scripts === 'object') {
|
|
386
|
+
for (const scriptBin of Object.keys(pkgMeta.scripts)) {
|
|
387
|
+
const binLower = scriptBin.toLowerCase();
|
|
388
|
+
const fallbackTarget = `${targetDir}/${binLower}`;
|
|
389
|
+
if (!installedBins.includes(binLower) && fs.exists(fallbackTarget)) {
|
|
390
|
+
this._createBinSymlink(pathDir, binLower, fallbackTarget);
|
|
391
|
+
installedBins.push(binLower);
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
|
|
229
396
|
return installedBins;
|
|
230
397
|
}
|
|
231
398
|
|
|
232
|
-
|
|
233
|
-
const
|
|
234
|
-
|
|
235
|
-
this.
|
|
399
|
+
_createBinSymlink(pathDir, binName, targetFile) {
|
|
400
|
+
const fs = this._fs;
|
|
401
|
+
const symlinkPath = `${pathDir}/${binName.toLowerCase()}`;
|
|
402
|
+
this._removeBin(pathDir, binName);
|
|
403
|
+
try {
|
|
404
|
+
fs.createSymlink(targetFile, symlinkPath);
|
|
405
|
+
fs.setMode(symlinkPath, 0o755);
|
|
406
|
+
} catch {
|
|
407
|
+
this._writeFileSafe(symlinkPath, fs.getFileByPath(targetFile));
|
|
408
|
+
try { fs.setMode(symlinkPath, 0o755); } catch { /* ignore */ }
|
|
409
|
+
}
|
|
236
410
|
}
|
|
237
411
|
|
|
238
412
|
_removeBin(pathDir, binName) {
|
|
239
413
|
const location = `${pathDir}/${binName.toLowerCase()}`;
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
} else {
|
|
244
|
-
try { this.context.fs.deleteFileByPath(location); } catch { /* ignore */ }
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
_restoreCommands() {
|
|
249
|
-
const state = this._readState();
|
|
250
|
-
const registry = this._getRegistry();
|
|
251
|
-
for (const [pkgName, record] of Object.entries(state.packages)) {
|
|
252
|
-
const meta = registry[pkgName] || record;
|
|
253
|
-
if (meta) {
|
|
254
|
-
try {
|
|
255
|
-
this._registerPackageIntoContext(meta);
|
|
256
|
-
} catch {
|
|
257
|
-
// ignore
|
|
258
|
-
}
|
|
259
|
-
}
|
|
260
|
-
}
|
|
414
|
+
this._withWritableParent(location, (fs) => {
|
|
415
|
+
try { fs.deleteFileByPath(location); } catch { /* ignore */ }
|
|
416
|
+
});
|
|
261
417
|
}
|
|
262
418
|
|
|
263
419
|
async run() {
|
|
@@ -341,7 +497,7 @@ class Wbt extends LocalCommand {
|
|
|
341
497
|
);
|
|
342
498
|
|
|
343
499
|
const registry = this._getRegistry();
|
|
344
|
-
const
|
|
500
|
+
const installed = this._getInstalledPackages();
|
|
345
501
|
let printedHeader = false;
|
|
346
502
|
|
|
347
503
|
for (let i = 0; i < positional.length; i++) {
|
|
@@ -351,7 +507,7 @@ class Wbt extends LocalCommand {
|
|
|
351
507
|
if (!isUrl(rawName) && i + 1 < positional.length && isUrl(positional[i + 1])) {
|
|
352
508
|
const url = positional[++i];
|
|
353
509
|
const pkgName = (asName || rawName).toLowerCase();
|
|
354
|
-
await this._installCustomUrl(pkgName, url, quiet
|
|
510
|
+
await this._installCustomUrl(pkgName, url, quiet);
|
|
355
511
|
if (this.exitCode) return;
|
|
356
512
|
continue;
|
|
357
513
|
}
|
|
@@ -360,7 +516,7 @@ class Wbt extends LocalCommand {
|
|
|
360
516
|
if (isUrl(rawName)) {
|
|
361
517
|
const url = rawName;
|
|
362
518
|
const pkgName = (asName || deriveUrlBinName(url)).toLowerCase();
|
|
363
|
-
await this._installCustomUrl(pkgName, url, quiet
|
|
519
|
+
await this._installCustomUrl(pkgName, url, quiet);
|
|
364
520
|
if (this.exitCode) return;
|
|
365
521
|
continue;
|
|
366
522
|
}
|
|
@@ -396,7 +552,7 @@ class Wbt extends LocalCommand {
|
|
|
396
552
|
return;
|
|
397
553
|
}
|
|
398
554
|
|
|
399
|
-
if (
|
|
555
|
+
if (installed[pkgMeta.name] && !flags.includes('--reinstall') && !flags.includes('-f')) {
|
|
400
556
|
if (!quiet) {
|
|
401
557
|
this.stdOut = `${pkgMeta.name} is already the newest version (${pkgMeta.version}).`;
|
|
402
558
|
}
|
|
@@ -409,8 +565,10 @@ class Wbt extends LocalCommand {
|
|
|
409
565
|
}
|
|
410
566
|
|
|
411
567
|
let wasmPath = null;
|
|
568
|
+
let scriptPaths = null;
|
|
412
569
|
try {
|
|
413
570
|
wasmPath = await this._pullPackageWasm(pkgMeta, quiet, flags.includes('--preload'));
|
|
571
|
+
scriptPaths = await this._pullPackageScripts(pkgMeta, quiet);
|
|
414
572
|
} catch (err) {
|
|
415
573
|
this.stdErr = `E: Failed to fetch ${pkgMeta.name}: ${err.message || err}`;
|
|
416
574
|
this.exitCode = 1;
|
|
@@ -422,30 +580,54 @@ class Wbt extends LocalCommand {
|
|
|
422
580
|
this.flush();
|
|
423
581
|
}
|
|
424
582
|
|
|
425
|
-
const resolvedMeta = Object.assign(
|
|
583
|
+
const resolvedMeta = Object.assign(
|
|
584
|
+
{},
|
|
585
|
+
pkgMeta,
|
|
586
|
+
wasmPath ? { wasmPath } : {},
|
|
587
|
+
scriptPaths ? { scriptPaths } : {}
|
|
588
|
+
);
|
|
426
589
|
const bins = this._registerPackageIntoContext(resolvedMeta);
|
|
427
|
-
state.packages[pkgMeta.name] = {
|
|
428
|
-
name: pkgMeta.name,
|
|
429
|
-
bin: pkgMeta.bin,
|
|
430
|
-
bins,
|
|
431
|
-
aliases: pkgMeta.aliases || [],
|
|
432
|
-
version: pkgMeta.version,
|
|
433
|
-
description: pkgMeta.description,
|
|
434
|
-
cdnUrl: pkgMeta.cdnUrl,
|
|
435
|
-
wasmUrl: pkgMeta.wasmUrl,
|
|
436
|
-
wasmPath: wasmPath || pkgMeta.wasmPath,
|
|
437
|
-
type: pkgMeta.type,
|
|
438
|
-
adapter: pkgMeta.adapter,
|
|
439
|
-
repl: pkgMeta.repl,
|
|
440
|
-
installedAt: new Date().toISOString()
|
|
441
|
-
};
|
|
442
590
|
|
|
443
591
|
if (!quiet) {
|
|
444
592
|
this.stdOut = `Setting up ${pkgMeta.name} (${pkgMeta.version}) -> /bin/${bins.join(', /bin/')} ...`;
|
|
445
593
|
}
|
|
446
594
|
}
|
|
595
|
+
}
|
|
447
596
|
|
|
448
|
-
|
|
597
|
+
async _pullPackageScripts(pkgMeta, quiet = false, targetDir = null) {
|
|
598
|
+
if (!pkgMeta || !pkgMeta.scripts || typeof pkgMeta.scripts !== 'object') {
|
|
599
|
+
return null;
|
|
600
|
+
}
|
|
601
|
+
const cfg = this.constructor._config || {};
|
|
602
|
+
const fetchFn = cfg.fetch || (typeof fetch === 'function' ? fetch : null);
|
|
603
|
+
if (!fetchFn) return null;
|
|
604
|
+
|
|
605
|
+
const pkgName = (pkgMeta.name || '').split('/').pop().toLowerCase();
|
|
606
|
+
const destDir = targetDir || `/usr/lib/${pkgName}`;
|
|
607
|
+
const installed = {};
|
|
608
|
+
|
|
609
|
+
for (const [scriptName, scriptUrl] of Object.entries(pkgMeta.scripts)) {
|
|
610
|
+
if (!scriptUrl) continue;
|
|
611
|
+
const binLower = scriptName.toLowerCase();
|
|
612
|
+
const res = await fetchFn(scriptUrl);
|
|
613
|
+
if (!res || !res.ok) {
|
|
614
|
+
throw new Error(`HTTP ${res ? res.status : 'error'} fetching script ${scriptUrl}`);
|
|
615
|
+
}
|
|
616
|
+
let text;
|
|
617
|
+
if (typeof res.text === 'function') {
|
|
618
|
+
text = await res.text();
|
|
619
|
+
} else if (typeof res.arrayBuffer === 'function') {
|
|
620
|
+
text = new TextDecoder().decode(new Uint8Array(await res.arrayBuffer()));
|
|
621
|
+
} else {
|
|
622
|
+
text = '';
|
|
623
|
+
}
|
|
624
|
+
const targetPath = `${destDir}/${binLower}`;
|
|
625
|
+
this._writeFileSafe(targetPath, text);
|
|
626
|
+
try { this._fs.setMode(targetPath, 0o755); } catch { /* ignore */ }
|
|
627
|
+
installed[binLower] = targetPath;
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
return Object.keys(installed).length > 0 ? installed : null;
|
|
449
631
|
}
|
|
450
632
|
|
|
451
633
|
async _readResponseBytes(res, label, quiet = false) {
|
|
@@ -580,7 +762,7 @@ class Wbt extends LocalCommand {
|
|
|
580
762
|
return null;
|
|
581
763
|
}
|
|
582
764
|
|
|
583
|
-
async _pullPackageWasm(pkgMeta, quiet = false, requireModuleLoad = false) {
|
|
765
|
+
async _pullPackageWasm(pkgMeta, quiet = false, requireModuleLoad = false, targetDir = null) {
|
|
584
766
|
const cfg = this.constructor._config || {};
|
|
585
767
|
const fetchFn = cfg.fetch || (typeof fetch === 'function' ? fetch : null);
|
|
586
768
|
const { loadModule } = require('./utils');
|
|
@@ -681,16 +863,19 @@ class Wbt extends LocalCommand {
|
|
|
681
863
|
}
|
|
682
864
|
|
|
683
865
|
if (wasmBytes) {
|
|
684
|
-
const
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
866
|
+
const pkgName = (pkgMeta.name || '').split('/').pop().toLowerCase();
|
|
867
|
+
const binName = (pkgMeta.bin || pkgName).toLowerCase();
|
|
868
|
+
const destDir = targetDir || `/usr/lib/${pkgName}`;
|
|
869
|
+
const targetPath = `${destDir}/${binName}.wasm`;
|
|
870
|
+
this._writeBytesSafe(targetPath, wasmBytes, 'application/wasm');
|
|
871
|
+
try { this._fs.setMode(targetPath, 0o755); } catch { /* ignore */ }
|
|
872
|
+
return targetPath;
|
|
688
873
|
}
|
|
689
874
|
|
|
690
875
|
return null;
|
|
691
876
|
}
|
|
692
877
|
|
|
693
|
-
async _installCustomUrl(pkgName, url, quiet = false
|
|
878
|
+
async _installCustomUrl(pkgName, url, quiet = false) {
|
|
694
879
|
// If URL is an ESM JS module (.mjs / .js or esm.sh URL without .wasm/.wat), wrap with GenericWasmCommand
|
|
695
880
|
const isBinaryWasmOrWat = /\.(wasm|wat)(\?.*)?$/i.test(url);
|
|
696
881
|
if (!isBinaryWasmOrWat && (/^https?:\/\/esm\.sh\//i.test(url) || /\.(mjs|js)(\?.*)?$/i.test(url))) {
|
|
@@ -707,9 +892,6 @@ class Wbt extends LocalCommand {
|
|
|
707
892
|
const wasmPath = await this._pullPackageWasm(pkgMeta, quiet, false);
|
|
708
893
|
if (wasmPath) pkgMeta.wasmPath = wasmPath;
|
|
709
894
|
this._registerPackageIntoContext(pkgMeta);
|
|
710
|
-
const state = existingState || this._readState();
|
|
711
|
-
state.packages[pkgName] = pkgMeta;
|
|
712
|
-
this._writeState(state);
|
|
713
895
|
if (!quiet) {
|
|
714
896
|
this.stdOut = `Setting up ${pkgName} (generic runtime) -> /bin/${pkgName} ...`;
|
|
715
897
|
}
|
|
@@ -739,8 +921,6 @@ class Wbt extends LocalCommand {
|
|
|
739
921
|
try {
|
|
740
922
|
res = await fetchFn(normalizedUrl);
|
|
741
923
|
} catch (err) {
|
|
742
|
-
// GitHub `/releases/download/...` redirects to objects.githubusercontent.com without CORS headers in browsers;
|
|
743
|
-
// fall back to a CORS proxy if direct browser fetch throws a network/CORS TypeError
|
|
744
924
|
if (/^https?:\/\/github\.com\/[^/]+\/[^/]+\/releases\/download\//i.test(normalizedUrl)) {
|
|
745
925
|
try {
|
|
746
926
|
res = await fetchFn(`https://corsproxy.io/?${encodeURIComponent(normalizedUrl)}`);
|
|
@@ -764,16 +944,17 @@ class Wbt extends LocalCommand {
|
|
|
764
944
|
|
|
765
945
|
const isWat = url.endsWith('.wat');
|
|
766
946
|
const ext = isWat ? '.wat' : '.wasm';
|
|
767
|
-
const
|
|
947
|
+
const targetDir = `/usr/lib/${pkgName}`;
|
|
948
|
+
const wasmPath = `${targetDir}/${pkgName}${ext}`;
|
|
768
949
|
|
|
769
950
|
if (isWat) {
|
|
770
951
|
const text = await res.text();
|
|
771
|
-
this.
|
|
952
|
+
this._writeFileSafe(wasmPath, text);
|
|
772
953
|
} else {
|
|
773
954
|
const ab = await res.arrayBuffer();
|
|
774
|
-
|
|
955
|
+
this._writeBytesSafe(wasmPath, new Uint8Array(ab), 'application/wasm');
|
|
775
956
|
}
|
|
776
|
-
this.
|
|
957
|
+
try { this._fs.setMode(wasmPath, 0o755); } catch { /* ignore */ }
|
|
777
958
|
|
|
778
959
|
const pkgMeta = {
|
|
779
960
|
name: pkgName,
|
|
@@ -788,9 +969,6 @@ class Wbt extends LocalCommand {
|
|
|
788
969
|
};
|
|
789
970
|
|
|
790
971
|
this._registerPackageIntoContext(pkgMeta);
|
|
791
|
-
const state = existingState || this._readState();
|
|
792
|
-
state.packages[pkgName] = pkgMeta;
|
|
793
|
-
this._writeState(state);
|
|
794
972
|
|
|
795
973
|
if (!quiet) {
|
|
796
974
|
this.stdOut = `Setting up ${pkgName} (custom) -> /bin/${pkgName} ...`;
|
|
@@ -805,12 +983,13 @@ class Wbt extends LocalCommand {
|
|
|
805
983
|
return;
|
|
806
984
|
}
|
|
807
985
|
|
|
808
|
-
const
|
|
809
|
-
const
|
|
986
|
+
const installed = this._getInstalledPackages();
|
|
987
|
+
const registry = this._getRegistry();
|
|
988
|
+
const pathDir = this._pathDir;
|
|
810
989
|
|
|
811
990
|
for (const rawName of positional) {
|
|
812
991
|
const pkgName = resolvePackageName(rawName);
|
|
813
|
-
const record =
|
|
992
|
+
const record = installed[pkgName] || installed[rawName.toLowerCase()];
|
|
814
993
|
|
|
815
994
|
if (!record) {
|
|
816
995
|
this.stdErr = `E: Package '${rawName}' is not installed, so not removed`;
|
|
@@ -818,36 +997,46 @@ class Wbt extends LocalCommand {
|
|
|
818
997
|
return;
|
|
819
998
|
}
|
|
820
999
|
|
|
821
|
-
const
|
|
1000
|
+
const reg = registry[pkgName] || {};
|
|
1001
|
+
const binsToRemove = new Set([
|
|
1002
|
+
record.bin || record.name,
|
|
1003
|
+
...(record.aliases || []),
|
|
1004
|
+
...(reg.aliases || []),
|
|
1005
|
+
rawName.toLowerCase(),
|
|
1006
|
+
pkgName
|
|
1007
|
+
]);
|
|
1008
|
+
if (record.scripts) {
|
|
1009
|
+
for (const scriptKey of Object.keys(record.scripts)) {
|
|
1010
|
+
binsToRemove.add(scriptKey);
|
|
1011
|
+
}
|
|
1012
|
+
}
|
|
1013
|
+
|
|
822
1014
|
for (const b of binsToRemove) {
|
|
823
1015
|
this._removeBin(pathDir, b);
|
|
824
1016
|
}
|
|
825
1017
|
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
}
|
|
1018
|
+
// Remove /usr/lib/<pkgName>
|
|
1019
|
+
const pkgDir = `/usr/lib/${pkgName}`;
|
|
1020
|
+
try { this._fs.deleteFileByPath(pkgDir); } catch { /* ignore */ }
|
|
829
1021
|
|
|
830
|
-
delete state.packages[record.name];
|
|
831
1022
|
this.stdOut = `Removing ${record.name} (${record.version}) ...`;
|
|
832
1023
|
}
|
|
833
|
-
|
|
834
|
-
this._writeState(state);
|
|
835
1024
|
}
|
|
836
1025
|
|
|
837
1026
|
_handleList(args) {
|
|
838
1027
|
const installedOnly = args.includes('--installed') || args.includes('-i');
|
|
839
1028
|
const registry = this._getRegistry();
|
|
840
|
-
const
|
|
1029
|
+
const installed = this._getInstalledPackages();
|
|
841
1030
|
|
|
842
1031
|
this.stdOut = 'Listing...';
|
|
843
1032
|
|
|
844
1033
|
const names = installedOnly
|
|
845
|
-
? Object.keys(
|
|
846
|
-
: Array.from(new Set([...Object.keys(registry), ...Object.keys(
|
|
1034
|
+
? Object.keys(installed).sort()
|
|
1035
|
+
: Array.from(new Set([...Object.keys(registry), ...Object.keys(installed)])).sort();
|
|
847
1036
|
|
|
848
1037
|
for (const name of names) {
|
|
849
1038
|
const reg = registry[name];
|
|
850
|
-
const inst =
|
|
1039
|
+
const inst = installed[name];
|
|
851
1040
|
const meta = inst || reg;
|
|
852
1041
|
if (!meta) continue;
|
|
853
1042
|
|
|
@@ -860,7 +1049,7 @@ class Wbt extends LocalCommand {
|
|
|
860
1049
|
_handleSearch(args) {
|
|
861
1050
|
const query = args.filter(a => !a.startsWith('-')).join(' ').toLowerCase();
|
|
862
1051
|
const registry = this._getRegistry();
|
|
863
|
-
const
|
|
1052
|
+
const installed = this._getInstalledPackages();
|
|
864
1053
|
|
|
865
1054
|
this.stdOut = 'Sorting... Done';
|
|
866
1055
|
this.stdOut = 'Full Text Search... Done';
|
|
@@ -871,13 +1060,14 @@ class Wbt extends LocalCommand {
|
|
|
871
1060
|
pkg.name.toLowerCase().includes(query) ||
|
|
872
1061
|
(pkg.bin && pkg.bin.toLowerCase().includes(query)) ||
|
|
873
1062
|
(pkg.aliases && pkg.aliases.some(a => a.toLowerCase().includes(query))) ||
|
|
1063
|
+
(pkg.scripts && Object.keys(pkg.scripts).some(s => s.toLowerCase().includes(query))) ||
|
|
874
1064
|
(pkg.description && pkg.description.toLowerCase().includes(query)) ||
|
|
875
1065
|
(pkg.details && pkg.details.toLowerCase().includes(query))
|
|
876
1066
|
);
|
|
877
1067
|
});
|
|
878
1068
|
|
|
879
1069
|
for (const pkg of entries) {
|
|
880
|
-
const inst =
|
|
1070
|
+
const inst = installed[pkg.name] ? ' [installed]' : '';
|
|
881
1071
|
this.stdOut = `${pkg.name}/wasm ${pkg.version} wasm${inst}`;
|
|
882
1072
|
this.stdOut = ` ${pkg.description}`;
|
|
883
1073
|
}
|
|
@@ -893,8 +1083,8 @@ class Wbt extends LocalCommand {
|
|
|
893
1083
|
|
|
894
1084
|
const pkgName = resolvePackageName(rawName);
|
|
895
1085
|
const registry = this._getRegistry();
|
|
896
|
-
const
|
|
897
|
-
const meta =
|
|
1086
|
+
const installed = this._getInstalledPackages();
|
|
1087
|
+
const meta = installed[pkgName] || registry[pkgName];
|
|
898
1088
|
|
|
899
1089
|
if (!meta) {
|
|
900
1090
|
this.stdErr = `E: No packages found matching ${rawName}`;
|
|
@@ -903,7 +1093,7 @@ class Wbt extends LocalCommand {
|
|
|
903
1093
|
}
|
|
904
1094
|
|
|
905
1095
|
const full = Object.assign({}, registry[pkgName] || {}, meta);
|
|
906
|
-
const isInstalled = Boolean(
|
|
1096
|
+
const isInstalled = Boolean(installed[full.name]);
|
|
907
1097
|
|
|
908
1098
|
this.stdOut = `Package: ${full.name}`;
|
|
909
1099
|
this.stdOut = `Version: ${full.version}`;
|
|
@@ -912,6 +1102,9 @@ class Wbt extends LocalCommand {
|
|
|
912
1102
|
if (full.aliases && full.aliases.length > 0) {
|
|
913
1103
|
this.stdOut = `Aliases: ${full.aliases.join(', ')}`;
|
|
914
1104
|
}
|
|
1105
|
+
if (full.scripts && Object.keys(full.scripts).length > 0) {
|
|
1106
|
+
this.stdOut = `Scripts: ${Object.keys(full.scripts).join(', ')}`;
|
|
1107
|
+
}
|
|
915
1108
|
if (full.size) {
|
|
916
1109
|
this.stdOut = `Download-Size: ${full.size}`;
|
|
917
1110
|
}
|
|
@@ -958,12 +1151,9 @@ class Wbt extends LocalCommand {
|
|
|
958
1151
|
}
|
|
959
1152
|
|
|
960
1153
|
if (Object.keys(mergedRemote).length > 0) {
|
|
961
|
-
this.
|
|
1154
|
+
this._writeFileSafe(`${CACHE_DIR}/remote-registry.json`, JSON.stringify(mergedRemote, null, 2));
|
|
962
1155
|
}
|
|
963
1156
|
|
|
964
|
-
// Also ensure any installed packages in state are registered in PATH
|
|
965
|
-
this._restoreCommands();
|
|
966
|
-
|
|
967
1157
|
const registry = this._getRegistry();
|
|
968
1158
|
this.stdOut = `Reading package lists... Done (${Object.keys(registry).length} packages available)`;
|
|
969
1159
|
}
|
|
@@ -977,8 +1167,9 @@ class Wbt extends LocalCommand {
|
|
|
977
1167
|
const rawName = args.shift();
|
|
978
1168
|
const pkgName = resolvePackageName(rawName);
|
|
979
1169
|
const registry = this._getRegistry();
|
|
980
|
-
const
|
|
981
|
-
|
|
1170
|
+
const installed = this._getInstalledPackages();
|
|
1171
|
+
const isInstalled = Boolean(installed[pkgName]);
|
|
1172
|
+
let pkgMeta = installed[pkgName] || registry[pkgName];
|
|
982
1173
|
|
|
983
1174
|
if (!pkgMeta && (rawName.startsWith('npm:') || rawName.startsWith('esm:'))) {
|
|
984
1175
|
const cleanSpec = rawName.replace(/^(npm|esm):/i, '');
|
|
@@ -998,66 +1189,172 @@ class Wbt extends LocalCommand {
|
|
|
998
1189
|
return;
|
|
999
1190
|
}
|
|
1000
1191
|
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1192
|
+
const fs = this._fs;
|
|
1193
|
+
let tempDir = null;
|
|
1194
|
+
const origPath = this._pathDir;
|
|
1195
|
+
try {
|
|
1196
|
+
if (!isInstalled) {
|
|
1197
|
+
tempDir = `/tmp/.wbt-run-${pkgName}-${Date.now()}`;
|
|
1198
|
+
}
|
|
1199
|
+
|
|
1200
|
+
if ((pkgMeta.type === 'custom-wasm' || pkgMeta.type === 'wasm') && !pkgMeta.wasmPath) {
|
|
1201
|
+
try {
|
|
1202
|
+
const wasmPath = await this._pullPackageWasm(pkgMeta, true, false, tempDir);
|
|
1203
|
+
if (wasmPath) {
|
|
1204
|
+
pkgMeta = Object.assign({}, pkgMeta, { wasmPath });
|
|
1205
|
+
}
|
|
1206
|
+
} catch (err) {
|
|
1207
|
+
this.stdErr = `E: Failed to fetch ${pkgMeta.name}: ${err.message || err}`;
|
|
1208
|
+
this.exitCode = 1;
|
|
1209
|
+
return;
|
|
1006
1210
|
}
|
|
1007
|
-
} catch (err) {
|
|
1008
|
-
this.stdErr = `E: Failed to fetch ${pkgMeta.name}: ${err.message || err}`;
|
|
1009
|
-
this.exitCode = 1;
|
|
1010
|
-
return;
|
|
1011
1211
|
}
|
|
1012
|
-
}
|
|
1013
1212
|
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1213
|
+
const rawLower = rawName.toLowerCase();
|
|
1214
|
+
let instance;
|
|
1215
|
+
if (pkgMeta.scripts && pkgMeta.scripts[rawLower]) {
|
|
1216
|
+
let scriptPath = (pkgMeta.scriptPaths && pkgMeta.scriptPaths[rawLower]) || `/usr/lib/${pkgName}/${rawLower}`;
|
|
1217
|
+
if (!fs.exists(scriptPath)) {
|
|
1218
|
+
try {
|
|
1219
|
+
const pulledScripts = await this._pullPackageScripts(pkgMeta, true, tempDir);
|
|
1220
|
+
if (pulledScripts && pulledScripts[rawLower]) {
|
|
1221
|
+
scriptPath = pulledScripts[rawLower];
|
|
1222
|
+
}
|
|
1223
|
+
} catch (err) {
|
|
1224
|
+
this.stdErr = `E: Failed to fetch script ${rawLower}: ${err.message || err}`;
|
|
1225
|
+
this.exitCode = 1;
|
|
1226
|
+
return;
|
|
1227
|
+
}
|
|
1228
|
+
}
|
|
1229
|
+
const getCtxVar = (v) => (this.context && typeof this.context.getVar === 'function') ? this.context.getVar(v) : '';
|
|
1230
|
+
const effectivePath = tempDir ? `${tempDir}:${origPath}` : origPath;
|
|
1231
|
+
const baseVars = (this.context && this.context.vars) ? Object.assign({}, this.context.vars) : {
|
|
1232
|
+
HOME: getCtxVar('HOME') || '/root',
|
|
1233
|
+
HOST: getCtxVar('HOST') || 'browser',
|
|
1234
|
+
COLUMNS: String((this.stdout && this.stdout.columns) || getCtxVar('COLUMNS') || 80),
|
|
1235
|
+
LINES: String((this.stdout && this.stdout.rows) || getCtxVar('LINES') || 24),
|
|
1236
|
+
};
|
|
1237
|
+
baseVars.PATH = effectivePath;
|
|
1238
|
+
const subContext = {
|
|
1239
|
+
vars: baseVars,
|
|
1240
|
+
aliases: (this.context && this.context.aliases) || {},
|
|
1241
|
+
getVar: (v) => (v === 'PATH' ? effectivePath : (baseVars[v] || getCtxVar(v))),
|
|
1242
|
+
getAlias: (a) => (this.context && typeof this.context.getAlias === 'function') ? this.context.getAlias(a) : null,
|
|
1243
|
+
getCommand: (c) => (this.context && typeof this.context.getCommand === 'function') ? this.context.getCommand(c) : null,
|
|
1244
|
+
};
|
|
1245
|
+
const ScriptCommand = require('../ScriptCommand');
|
|
1246
|
+
const scriptContent = fs.getFileByPath(scriptPath);
|
|
1247
|
+
instance = new ScriptCommand(scriptContent, args, subContext);
|
|
1248
|
+
instance.fs = fs;
|
|
1249
|
+
} else {
|
|
1250
|
+
const entries = this._buildCommandsForPackage(pkgMeta);
|
|
1251
|
+
// Find matching entry if user specified e.g. `wbt run asrun` or `wbt run wasm2wat`
|
|
1252
|
+
const matched = entries.find(e => e.bin === rawLower) || entries[0];
|
|
1253
|
+
const Cmd = matched.Cmd;
|
|
1254
|
+
instance = new Cmd(args, this.context);
|
|
1255
|
+
if (instance.requiresFilesystem) {
|
|
1256
|
+
instance.fs = typeof fs.sandbox === 'function' ? fs.sandbox() : fs;
|
|
1257
|
+
}
|
|
1258
|
+
}
|
|
1018
1259
|
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1260
|
+
// Pipe I/O and forward interactivity
|
|
1261
|
+
this._activeSubCommand = instance;
|
|
1262
|
+
if (this._onStdinStreamData && this.stdinStream && typeof this.stdinStream.off === 'function') {
|
|
1263
|
+
this.stdinStream.off('data', this._onStdinStreamData);
|
|
1264
|
+
}
|
|
1265
|
+
instance.stdinStream = this.stdinStream;
|
|
1266
|
+
instance.stdin = (this.stdin && !this.stdin.isTTY) ? this.stdin : this.stdinStream;
|
|
1267
|
+
if (instance._onStdinStreamData && this.stdinStream && typeof this.stdinStream.on === 'function') {
|
|
1268
|
+
this.stdinStream.on('data', instance._onStdinStreamData);
|
|
1269
|
+
}
|
|
1023
1270
|
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1271
|
+
if (this.stdout && instance.stdout) {
|
|
1272
|
+
if (this.stdout.columns) instance.stdout.columns = this.stdout.columns;
|
|
1273
|
+
if (this.stdout.rows) instance.stdout.rows = this.stdout.rows;
|
|
1274
|
+
}
|
|
1275
|
+
if (this.stderr && instance.stderr) {
|
|
1276
|
+
if (this.stderr.columns) instance.stderr.columns = this.stderr.columns;
|
|
1277
|
+
if (this.stderr.rows) instance.stderr.rows = this.stderr.rows;
|
|
1278
|
+
}
|
|
1279
|
+
const onParentStdoutResize = () => {
|
|
1280
|
+
if (!this.stdout || !instance.stdout) return;
|
|
1281
|
+
if (typeof instance.stdout.resize === 'function') {
|
|
1282
|
+
instance.stdout.resize(this.stdout.columns, this.stdout.rows);
|
|
1283
|
+
} else {
|
|
1284
|
+
instance.stdout.columns = this.stdout.columns;
|
|
1285
|
+
instance.stdout.rows = this.stdout.rows;
|
|
1286
|
+
}
|
|
1287
|
+
};
|
|
1288
|
+
if (this.stdout && typeof this.stdout.on === 'function') {
|
|
1289
|
+
this.stdout.on('resize', onParentStdoutResize);
|
|
1290
|
+
}
|
|
1028
1291
|
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1292
|
+
const onStdout = (chunk) => {
|
|
1293
|
+
if (this.stdout && typeof this.stdout.write === 'function') this.stdout.write(chunk);
|
|
1294
|
+
};
|
|
1295
|
+
const onStderr = (chunk) => {
|
|
1296
|
+
if (this.stderr && typeof this.stderr.write === 'function') this.stderr.write(chunk);
|
|
1297
|
+
};
|
|
1298
|
+
const onFlush = (flushed) => {
|
|
1299
|
+
const lines = Array.isArray(flushed)
|
|
1300
|
+
? flushed
|
|
1301
|
+
: (flushed && typeof flushed.getStdOutput === 'function' ? flushed.getStdOutput() : []);
|
|
1302
|
+
for (const line of lines) {
|
|
1303
|
+
if (line.type === 'out') this.stdOut = line.string;
|
|
1304
|
+
else if (line.type === 'err') this.stdErr = line.string;
|
|
1305
|
+
}
|
|
1306
|
+
this.flush();
|
|
1307
|
+
};
|
|
1308
|
+
if (instance.stdout && typeof instance.stdout.on === 'function') {
|
|
1309
|
+
instance.stdout.on('data', onStdout);
|
|
1310
|
+
}
|
|
1311
|
+
if (instance.stderr && typeof instance.stderr.on === 'function') {
|
|
1312
|
+
instance.stderr.on('data', onStderr);
|
|
1313
|
+
}
|
|
1314
|
+
if (typeof instance.on === 'function') {
|
|
1315
|
+
instance.on('flush', onFlush);
|
|
1316
|
+
}
|
|
1041
1317
|
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1318
|
+
let res;
|
|
1319
|
+
try {
|
|
1320
|
+
res = await instance.runCommand();
|
|
1321
|
+
} finally {
|
|
1322
|
+
this._activeSubCommand = null;
|
|
1323
|
+
if (this.stdout && typeof this.stdout.off === 'function') {
|
|
1324
|
+
this.stdout.off('resize', onParentStdoutResize);
|
|
1325
|
+
}
|
|
1326
|
+
if (instance._onStdinStreamData && this.stdinStream && typeof this.stdinStream.off === 'function') {
|
|
1327
|
+
this.stdinStream.off('data', instance._onStdinStreamData);
|
|
1328
|
+
}
|
|
1329
|
+
if (this._onStdinStreamData && this.stdinStream && typeof this.stdinStream.on === 'function') {
|
|
1330
|
+
this.stdinStream.on('data', this._onStdinStreamData);
|
|
1331
|
+
}
|
|
1332
|
+
if (instance.stdout && typeof instance.stdout.off === 'function') {
|
|
1333
|
+
instance.stdout.off('data', onStdout);
|
|
1334
|
+
}
|
|
1335
|
+
if (instance.stderr && typeof instance.stderr.off === 'function') {
|
|
1336
|
+
instance.stderr.off('data', onStderr);
|
|
1337
|
+
}
|
|
1338
|
+
if (typeof instance.removeListener === 'function') {
|
|
1339
|
+
instance.removeListener('flush', onFlush);
|
|
1340
|
+
}
|
|
1049
1341
|
}
|
|
1050
|
-
if (instance.
|
|
1051
|
-
|
|
1342
|
+
if (!instance.interactive && res) {
|
|
1343
|
+
for (const line of res.getStdOutput()) {
|
|
1344
|
+
if (line.type === 'out') this.stdOut = line.string;
|
|
1345
|
+
else if (line.type === 'err') this.stdErr = line.string;
|
|
1346
|
+
}
|
|
1052
1347
|
}
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1348
|
+
this.exitCode = res ? res.getExitCode() : 0;
|
|
1349
|
+
} finally {
|
|
1350
|
+
if (tempDir && fs) {
|
|
1351
|
+
try {
|
|
1352
|
+
fs.deleteFileByPath(tempDir);
|
|
1353
|
+
} catch {
|
|
1354
|
+
// ignore
|
|
1355
|
+
}
|
|
1058
1356
|
}
|
|
1059
1357
|
}
|
|
1060
|
-
this.exitCode = res ? res.getExitCode() : 0;
|
|
1061
1358
|
}
|
|
1062
1359
|
|
|
1063
1360
|
_printHelp() {
|
|
@@ -1086,7 +1383,8 @@ class Wbt extends LocalCommand {
|
|
|
1086
1383
|
' quickjs Sandboxed QuickJS JavaScript engine (qjs)',
|
|
1087
1384
|
' lua Lua 5.4 WebAssembly interpreter (lua)',
|
|
1088
1385
|
' wllama WASM & WebGPU LLM inference engine (wllama, llama, llama-cli)',
|
|
1089
|
-
' nano GNU nano 8.0 text editor with ncurses (nano, nano-wasm)'
|
|
1386
|
+
' nano GNU nano 8.0 text editor with ncurses (nano, nano-wasm)',
|
|
1387
|
+
' doom Classic Doom (1993) with 24-bit ANSI rendering (doom, freedoom)'
|
|
1090
1388
|
].forEach(l => { this.stdOut = l; });
|
|
1091
1389
|
}
|
|
1092
1390
|
}
|