@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/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
- export async function run (componentPath, args, opts) {
11
- // Ensure that `args` is an array
12
- args = [...args];
13
- return runComponent(componentPath, args, opts, `
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 (componentPath, args, opts) {
32
- let tryFindPort = false;
33
- let { port, host } = opts;
34
- if (port === undefined) {
35
- tryFindPort = true;
36
- port = '8000';
37
- }
38
- // Ensure that `args` is an array
39
- args = [...args];
40
- return runComponent(componentPath, args, opts, `
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
- ${tryFindPort ? `
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
- ` : `server.listen(port, ${JSON.stringify(host)})`}
56
- console.error(\`Server listening on port...\`);
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 (componentPath, args, opts, executor) {
61
- const jcoImport = opts.jcoImport ? resolve(opts.jcoImport) : null;
77
+ async function runComponent(componentPath, args, opts, executor) {
78
+ const jcoImport = opts.jcoImport ? resolve(opts.jcoImport) : null;
62
79
 
63
- const name = basename(componentPath.slice(0, -extname(componentPath).length || Infinity));
64
- const outDir = opts.jcoDir || await getTmpDir();
65
- if (opts.jcoDir) {
66
- await mkdir(outDir, { recursive: true });
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
- await transpile(componentPath, {
72
- name,
73
- quiet: true,
74
- noTypescript: true,
75
- wasiShim: true,
76
- outDir,
77
- tracing: opts.jcoTrace,
78
- map: opts.jcoMap,
79
- importBindings: opts.jcoImportBindings,
80
- });
81
- }
82
- catch (e) {
83
- throw new Error('Unable to transpile command for execution', { cause: e });
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
- await writeFile(resolve(outDir, 'package.json'), JSON.stringify({ type: 'module' }));
106
+ await writeFile(
107
+ resolve(outDir, 'package.json'),
108
+ JSON.stringify({ type: 'module' })
109
+ );
87
110
 
88
- let preview2ShimPath;
89
- try {
90
- preview2ShimPath = resolve(fileURLToPath(import.meta.resolve('@bytecodealliance/preview2-shim')), '../../../');
91
- } catch {
92
- throw c`Unable to locate the {bold @bytecodealliance/preview2-shim} package, make sure it is installed.`;
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
- const modulesDir = resolve(outDir, 'node_modules', '@bytecodealliance');
96
- await mkdir(modulesDir, { recursive: true });
125
+ const modulesDir = resolve(outDir, 'node_modules', '@bytecodealliance');
126
+ await mkdir(modulesDir, { recursive: true });
97
127
 
98
- try {
99
- await symlink(preview2ShimPath, resolve(modulesDir, 'preview2-shim'), 'dir');
100
- } catch (e) {
101
- if (e.code !== 'EEXIST')
102
- throw e;
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
- const runPath = resolve(outDir, '_run.js');
106
- await writeFile(runPath, `
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
- const nodePath = process.env.JCO_RUN_PATH || process.argv[0];
152
+ const nodePath = process.env.JCO_RUN_PATH || process.argv[0];
117
153
 
118
- process.exitCode = await new Promise((resolve, reject) => {
119
- const cp = spawn(nodePath, [...process.env.JCO_RUN_ARGS ? process.env.JCO_RUN_ARGS.split(' ') : [], runPath, ...args], { stdio: 'inherit' });
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
- cp.on('error', reject);
122
- cp.on('exit', resolve);
123
- });
124
- }
125
- finally {
126
- try {
127
- if (!opts.jcoDir)
128
- await rm(outDir, { recursive: true });
129
- } catch {
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":"AAgCA,8DAGC;AAED,mEAGC;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,CAA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