@bytecodealliance/jco 1.10.2 → 1.11.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/obj/js-component-bindgen-component.core.wasm +0 -0
- package/obj/js-component-bindgen-component.js +2 -2
- package/obj/wasm-tools.core.wasm +0 -0
- package/obj/wasm-tools.js +2 -2
- package/package.json +18 -6
- package/src/cmd/componentize.js +64 -18
- package/src/cmd/opt.js +233 -202
- package/src/cmd/run.js +117 -73
- package/src/cmd/transpile.d.ts.map +1 -1
- package/src/cmd/transpile.js +643 -530
- package/src/cmd/wasm-tools.js +130 -95
- package/src/jco.js +5 -1
package/src/cmd/run.js
CHANGED
|
@@ -7,10 +7,16 @@ import process from 'node:process';
|
|
|
7
7
|
import { fileURLToPath, pathToFileURL } from 'node:url';
|
|
8
8
|
import c from 'chalk-template';
|
|
9
9
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
10
|
+
const DEFAULT_SERVE_HOST = 'localhost';
|
|
11
|
+
|
|
12
|
+
export async function run(componentPath, args, opts) {
|
|
13
|
+
// Ensure that `args` is an array
|
|
14
|
+
args = [...args];
|
|
15
|
+
return runComponent(
|
|
16
|
+
componentPath,
|
|
17
|
+
args,
|
|
18
|
+
opts,
|
|
19
|
+
`
|
|
14
20
|
if (!mod.run || !mod.run.run) {
|
|
15
21
|
console.error('Not a valid command component to execute.');
|
|
16
22
|
process.exit(1);
|
|
@@ -25,23 +31,31 @@ export async function run (componentPath, args, opts) {
|
|
|
25
31
|
console.error(e);
|
|
26
32
|
process.exit(1);
|
|
27
33
|
}
|
|
28
|
-
`
|
|
34
|
+
`
|
|
35
|
+
);
|
|
29
36
|
}
|
|
30
37
|
|
|
31
|
-
export async function serve
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
38
|
+
export async function serve(componentPath, args, opts) {
|
|
39
|
+
let tryFindPort = false;
|
|
40
|
+
let { port, host } = opts;
|
|
41
|
+
if (port === undefined) {
|
|
42
|
+
tryFindPort = true;
|
|
43
|
+
port = '8000';
|
|
44
|
+
}
|
|
45
|
+
// Ensure that `args` is an array
|
|
46
|
+
args = [...args];
|
|
47
|
+
host = host ?? DEFAULT_SERVE_HOST;
|
|
48
|
+
return runComponent(
|
|
49
|
+
componentPath,
|
|
50
|
+
args,
|
|
51
|
+
opts,
|
|
52
|
+
`
|
|
41
53
|
import { HTTPServer } from '@bytecodealliance/preview2-shim/http';
|
|
42
54
|
const server = new HTTPServer(mod.incomingHandler);
|
|
43
55
|
let port = ${port};
|
|
44
|
-
${
|
|
56
|
+
${
|
|
57
|
+
tryFindPort
|
|
58
|
+
? `
|
|
45
59
|
while (true) {
|
|
46
60
|
try {
|
|
47
61
|
server.listen(port, ${JSON.stringify(host)});
|
|
@@ -52,58 +66,79 @@ export async function serve (componentPath, args, opts) {
|
|
|
52
66
|
}
|
|
53
67
|
port++;
|
|
54
68
|
}
|
|
55
|
-
`
|
|
56
|
-
|
|
57
|
-
|
|
69
|
+
`
|
|
70
|
+
: `server.listen(port, ${JSON.stringify(host)})`
|
|
71
|
+
}
|
|
72
|
+
console.error(\`Server listening @ ${host}:${port}...\`);
|
|
73
|
+
`
|
|
74
|
+
);
|
|
58
75
|
}
|
|
59
76
|
|
|
60
|
-
async function runComponent
|
|
61
|
-
|
|
77
|
+
async function runComponent(componentPath, args, opts, executor) {
|
|
78
|
+
const jcoImport = opts.jcoImport ? resolve(opts.jcoImport) : null;
|
|
62
79
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
80
|
+
const name = basename(
|
|
81
|
+
componentPath.slice(0, -extname(componentPath).length || Infinity)
|
|
82
|
+
);
|
|
83
|
+
const outDir = opts.jcoDir || (await getTmpDir());
|
|
84
|
+
if (opts.jcoDir) {
|
|
85
|
+
await mkdir(outDir, { recursive: true });
|
|
86
|
+
}
|
|
68
87
|
|
|
69
|
-
try {
|
|
70
88
|
try {
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
89
|
+
try {
|
|
90
|
+
await transpile(componentPath, {
|
|
91
|
+
name,
|
|
92
|
+
quiet: true,
|
|
93
|
+
noTypescript: true,
|
|
94
|
+
wasiShim: true,
|
|
95
|
+
outDir,
|
|
96
|
+
tracing: opts.jcoTrace,
|
|
97
|
+
map: opts.jcoMap,
|
|
98
|
+
importBindings: opts.jcoImportBindings,
|
|
99
|
+
});
|
|
100
|
+
} catch (e) {
|
|
101
|
+
throw new Error('Unable to transpile command for execution', {
|
|
102
|
+
cause: e,
|
|
103
|
+
});
|
|
104
|
+
}
|
|
85
105
|
|
|
86
|
-
|
|
106
|
+
await writeFile(
|
|
107
|
+
resolve(outDir, 'package.json'),
|
|
108
|
+
JSON.stringify({ type: 'module' })
|
|
109
|
+
);
|
|
87
110
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
111
|
+
let preview2ShimPath;
|
|
112
|
+
try {
|
|
113
|
+
preview2ShimPath = resolve(
|
|
114
|
+
fileURLToPath(
|
|
115
|
+
import.meta.resolve('@bytecodealliance/preview2-shim')
|
|
116
|
+
),
|
|
117
|
+
'../../../'
|
|
118
|
+
);
|
|
119
|
+
} catch (err) {
|
|
120
|
+
const msg = c`{red.bold error} Failed to resolve {bold @bytecodealliance/preview2-shim}, ensure it is installed.`;
|
|
121
|
+
msg += `\nERROR:\n${err.toString()}`;
|
|
122
|
+
throw new Error(msg);
|
|
123
|
+
}
|
|
94
124
|
|
|
95
|
-
|
|
96
|
-
|
|
125
|
+
const modulesDir = resolve(outDir, 'node_modules', '@bytecodealliance');
|
|
126
|
+
await mkdir(modulesDir, { recursive: true });
|
|
97
127
|
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
128
|
+
try {
|
|
129
|
+
await symlink(
|
|
130
|
+
preview2ShimPath,
|
|
131
|
+
resolve(modulesDir, 'preview2-shim'),
|
|
132
|
+
'dir'
|
|
133
|
+
);
|
|
134
|
+
} catch (e) {
|
|
135
|
+
if (e.code !== 'EEXIST') throw e;
|
|
136
|
+
}
|
|
104
137
|
|
|
105
|
-
|
|
106
|
-
|
|
138
|
+
const runPath = resolve(outDir, '_run.js');
|
|
139
|
+
await writeFile(
|
|
140
|
+
runPath,
|
|
141
|
+
`
|
|
107
142
|
${jcoImport ? `import ${JSON.stringify(pathToFileURL(jcoImport))}` : ''}
|
|
108
143
|
import process from 'node:process';
|
|
109
144
|
try {
|
|
@@ -111,23 +146,32 @@ async function runComponent (componentPath, args, opts, executor) {
|
|
|
111
146
|
} catch {}
|
|
112
147
|
const mod = await import('./${name}.js');
|
|
113
148
|
${executor}
|
|
114
|
-
`
|
|
149
|
+
`
|
|
150
|
+
);
|
|
115
151
|
|
|
116
|
-
|
|
152
|
+
const nodePath = process.env.JCO_RUN_PATH || process.argv[0];
|
|
117
153
|
|
|
118
|
-
|
|
119
|
-
|
|
154
|
+
process.exitCode = await new Promise((resolve, reject) => {
|
|
155
|
+
const cp = spawn(
|
|
156
|
+
nodePath,
|
|
157
|
+
[
|
|
158
|
+
...(process.env.JCO_RUN_ARGS
|
|
159
|
+
? process.env.JCO_RUN_ARGS.split(' ')
|
|
160
|
+
: []),
|
|
161
|
+
runPath,
|
|
162
|
+
...args,
|
|
163
|
+
],
|
|
164
|
+
{ stdio: 'inherit' }
|
|
165
|
+
);
|
|
120
166
|
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
// empty
|
|
167
|
+
cp.on('error', reject);
|
|
168
|
+
cp.on('exit', resolve);
|
|
169
|
+
});
|
|
170
|
+
} finally {
|
|
171
|
+
try {
|
|
172
|
+
if (!opts.jcoDir) await rm(outDir, { recursive: true });
|
|
173
|
+
} catch {
|
|
174
|
+
// empty
|
|
175
|
+
}
|
|
131
176
|
}
|
|
132
|
-
}
|
|
133
177
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"transpile.d.ts","sourceRoot":"","sources":["transpile.js"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"transpile.d.ts","sourceRoot":"","sources":["transpile.js"],"names":[],"mappings":"AA+CA,8DAGC;AAED,mEAMC;AAED;;;;;;;;;;;;;;;GAeG;AACH,wCAfW,MAAM,QACN;IACN,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,aAAa,CAAC,EAAE,OAAO,GAAG,MAAM,CAAC;IACjC,SAAS,CAAC,EAAE,IAAI,CAAC;IACjB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,YAAY,CAAC,EAAE,MAAM,EAAE,CAAC;IACxB,YAAY,CAAC,EAAE,MAAM,EAAE,CAAC;IACxB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,QAAQ,CAAC,EAAE,MAAM,EAAE,GAAG,KAAK,CAAC;IAC5B,KAAK,CAAC,EAAE,IAAI,CAAC;CACd,GACS,OAAO,CAAC;IAAE,CAAC,QAAQ,EAAE,MAAM,GAAG,UAAU,CAAA;CAAE,CAAC,CA+DvD;AAyCD,sFAoCC;AAmBD;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,8CAzBW,UAAU,SACV;IACN,IAAI,EAAE,MAAM,CAAC;IACb,aAAa,CAAC,EAAE,OAAO,GAAG,MAAM,CAAC;IACjC,cAAc,CAAC,EAAE,IAAI,GAAG,WAAW,GAAG,QAAQ,GAAG,kBAAkB,CAAC;IACpE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