@kokiito0926/cli2module 0.0.5 → 0.0.6
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/example.js +13 -11
- package/index.js +33 -14
- 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
|
-
|
|
5
|
+
console.log('--- Case 1: Running zx help ---');
|
|
6
6
|
const result = await cli2module("zx/cli", ["--help"]);
|
|
7
|
-
|
|
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
|
-
|
|
14
|
-
console.log(
|
|
15
|
-
console.log(result2
|
|
16
|
-
|
|
17
|
-
console.log(
|
|
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
|
-
|
|
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,33 @@ function runInWorker(scriptPath, args = [], input = "") {
|
|
|
56
60
|
});
|
|
57
61
|
});
|
|
58
62
|
|
|
59
|
-
worker.on("error",
|
|
63
|
+
worker.on("error", (err) => {
|
|
64
|
+
reject(err);
|
|
65
|
+
});
|
|
60
66
|
});
|
|
61
67
|
}
|
|
62
68
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
+
/**
|
|
70
|
+
* CLIツールをワーカースレッドで実行します。
|
|
71
|
+
*
|
|
72
|
+
* @param {string} specifier 実行するCLIツールのパスまたはモジュール名
|
|
73
|
+
* @param {string[]} [args=[]] コマンドライン引数の配列
|
|
74
|
+
* @param {string} [input=""] 標準入力に渡す文字列
|
|
75
|
+
* @returns {Promise<{code: number, stdout: string, stderr: string}>}
|
|
76
|
+
*/
|
|
77
|
+
export async function cli2module(specifier, args = [], input = "") {
|
|
78
|
+
let cliUrl;
|
|
79
|
+
try {
|
|
80
|
+
// 相対パス(./ か ../)で始まる場合は、直感的な動作のために process.cwd() を基準に解決を試みる
|
|
81
|
+
if (specifier.startsWith("./") || specifier.startsWith("../")) {
|
|
82
|
+
cliUrl = pathToFileURL(path.resolve(process.cwd(), specifier)).href;
|
|
83
|
+
} else {
|
|
84
|
+
cliUrl = await import.meta.resolve(specifier);
|
|
85
|
+
}
|
|
86
|
+
} catch (err) {
|
|
87
|
+
throw new Error(`Failed to resolve specifier "${specifier}": ${err.message}`);
|
|
88
|
+
}
|
|
69
89
|
|
|
70
|
-
const
|
|
71
|
-
|
|
72
|
-
return result;
|
|
90
|
+
const cliPath = fileURLToPath(cliUrl);
|
|
91
|
+
return await runInWorker(cliPath, args, input);
|
|
73
92
|
}
|
package/package.json
CHANGED