@kokiito0926/cli2module 0.0.6 → 0.0.8
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 +11 -13
- package/index.js +1 -22
- package/package.json +1 -1
package/example.js
CHANGED
|
@@ -2,18 +2,16 @@
|
|
|
2
2
|
|
|
3
3
|
import { cli2module } from "./index.js";
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
// const result = await cli2module("zx/cli", []);
|
|
6
6
|
const result = await cli2module("zx/cli", ["--help"]);
|
|
7
|
-
|
|
8
|
-
console.log(
|
|
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);
|
|
9
12
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
console.log(
|
|
13
|
-
|
|
14
|
-
console.log(
|
|
15
|
-
try {
|
|
16
|
-
await cli2module("non-existent-module");
|
|
17
|
-
} catch (err) {
|
|
18
|
-
console.log(`Caught expected error: ${err.message}`);
|
|
19
|
-
}
|
|
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);
|
package/index.js
CHANGED
|
@@ -21,8 +21,6 @@ function runInWorker(scriptPath, args = [], input = "") {
|
|
|
21
21
|
try {
|
|
22
22
|
await import('file:///' + workerData.path.replace(/\\\\/g, '/'));
|
|
23
23
|
} catch (err) {
|
|
24
|
-
// ワーカースレッド内での例外(構文エラーやインポートエラー等)を確実に投げ、
|
|
25
|
-
// 親スレッドの 'error' イベントでキャッチできるようにする。
|
|
26
24
|
throw err;
|
|
27
25
|
}
|
|
28
26
|
`;
|
|
@@ -66,27 +64,8 @@ function runInWorker(scriptPath, args = [], input = "") {
|
|
|
66
64
|
});
|
|
67
65
|
}
|
|
68
66
|
|
|
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
67
|
export async function cli2module(specifier, args = [], input = "") {
|
|
78
|
-
|
|
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
|
-
}
|
|
89
|
-
|
|
68
|
+
const cliUrl = await import.meta.resolve(specifier);
|
|
90
69
|
const cliPath = fileURLToPath(cliUrl);
|
|
91
70
|
return await runInWorker(cliPath, args, input);
|
|
92
71
|
}
|