@kokiito0926/cli2module 0.0.5 → 0.0.7

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.
Files changed (3) hide show
  1. package/example.js +13 -11
  2. package/index.js +25 -14
  3. package/package.json +1 -2
package/example.js CHANGED
@@ -2,16 +2,18 @@
2
2
 
3
3
  import { cli2module } from "./index.js";
4
4
 
5
- // const result = await cli2module("zx/cli", []);
5
+ console.log('--- Case 1: Running zx help ---');
6
6
  const result = await cli2module("zx/cli", ["--help"]);
7
- // const result = await cli2module("zx/cli", ["--version"]);
8
- console.log(result);
9
- console.log(result?.code);
10
- console.log(result?.stdout);
11
- console.log(result?.stderr);
7
+ console.log(`Code: ${result.code}`);
8
+ console.log(`Stdout length: ${result.stdout.length}`);
12
9
 
13
- const result2 = await cli2module("zx/cli", [], 'console.log("Hello, world!")');
14
- console.log(result2);
15
- console.log(result2?.code);
16
- console.log(result2?.stdout);
17
- console.log(result2?.stderr);
10
+ console.log('\n--- Case 2: Passing stdin to zx ---');
11
+ const result2 = await cli2module("zx/cli", [], 'console.log("Hello, zx!")');
12
+ console.log(`Stdout: ${result2.stdout}`);
13
+
14
+ console.log('\n--- Case 3: Error handling (invalid module) ---');
15
+ try {
16
+ await cli2module("non-existent-module");
17
+ } catch (err) {
18
+ console.log(`Caught expected error: ${err.message}`);
19
+ }
package/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { fileURLToPath } from "node:url";
1
+ import { fileURLToPath, pathToFileURL } from "node:url";
2
2
  import { Worker } from "node:worker_threads";
3
3
 
4
4
  // zxのCLIのパスを取得しなければいけない。
@@ -15,12 +15,16 @@ function runInWorker(scriptPath, args = [], input = "") {
15
15
  return new Promise((resolve, reject) => {
16
16
  const workerCode = `
17
17
  import { workerData } from 'node:worker_threads';
18
- // console.log(workerData.path);
19
- // console.log(workerData.args);
20
18
 
21
19
  process.argv = ['node', workerData.path, ...workerData.args];
22
20
 
23
- await import('file://' + workerData.path);
21
+ try {
22
+ await import('file:///' + workerData.path.replace(/\\\\/g, '/'));
23
+ } catch (err) {
24
+ // ワーカースレッド内での例外(構文エラーやインポートエラー等)を確実に投げ、
25
+ // 親スレッドの 'error' イベントでキャッチできるようにする。
26
+ throw err;
27
+ }
24
28
  `;
25
29
 
26
30
  const worker = new Worker(workerCode, {
@@ -56,18 +60,25 @@ function runInWorker(scriptPath, args = [], input = "") {
56
60
  });
57
61
  });
58
62
 
59
- worker.on("error", reject);
63
+ worker.on("error", (err) => {
64
+ reject(err);
65
+ });
60
66
  });
61
67
  }
62
68
 
63
- export async function cli2module(specifier, options = [], input = "") {
64
- const cliPath = await import.meta.resolve(specifier);
65
- // console.log(cliPath);
66
-
67
- const cliPath2 = fileURLToPath(cliPath);
68
- // console.log(cliPath2);
69
+ export async function cli2module(specifier, args = [], input = "") {
70
+ let cliUrl;
71
+ try {
72
+ // 相対パス(./ か ../)で始まる場合は、直感的な動作のために process.cwd() を基準に解決を試みる
73
+ if (specifier.startsWith("./") || specifier.startsWith("../")) {
74
+ cliUrl = pathToFileURL(path.resolve(process.cwd(), specifier)).href;
75
+ } else {
76
+ cliUrl = await import.meta.resolve(specifier);
77
+ }
78
+ } catch (err) {
79
+ throw new Error(`Failed to resolve specifier "${specifier}": ${err.message}`);
80
+ }
69
81
 
70
- const result = await runInWorker(cliPath2, options, input);
71
- // console.log(result);
72
- return result;
82
+ const cliPath = fileURLToPath(cliUrl);
83
+ return await runInWorker(cliPath, args, input);
73
84
  }
package/package.json CHANGED
@@ -1,11 +1,10 @@
1
1
  {
2
2
  "name": "@kokiito0926/cli2module",
3
- "version": "0.0.5",
3
+ "version": "0.0.7",
4
4
  "private": false,
5
5
  "description": "CLIのツールをワーカースレッドで実行することができるライブラリです。",
6
6
  "keywords": [
7
7
  "cli",
8
- "ctm",
9
8
  "module",
10
9
  "nodejs",
11
10
  "cli2module"