@kokiito0926/cli2module 0.0.5
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/LICENSE +21 -0
- package/README.md +39 -0
- package/example.js +17 -0
- package/index.js +73 -0
- package/package.json +37 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Koki Ito
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
## cli2module
|
|
2
|
+
|
|
3
|
+
cli2moduleは、CLIのツールをワーカースレッドで実行することができるライブラリです。
|
|
4
|
+
通常、CLIのツールをimportして実行すると、内部でprocess.exitが呼び出されたときに、呼び出し元のプログラムも終了してしまいます。
|
|
5
|
+
このライブラリはワーカースレッドを使用することで、プロセスを終了させることなく、CLIのツールを実行することができます。
|
|
6
|
+
|
|
7
|
+
## インストール
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
$ npm install @kokiito0926/cli2module
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## 使用方法
|
|
14
|
+
|
|
15
|
+
CLIのツールを指定して、コマンドライン引数を配列で渡します。
|
|
16
|
+
|
|
17
|
+
```javascript
|
|
18
|
+
import { cli2module } from "@kokiito0926/cli2module";
|
|
19
|
+
|
|
20
|
+
const result = await cli2module("zx/cli", ["--help"]);
|
|
21
|
+
console.log(result.code);
|
|
22
|
+
console.log(result.stdout);
|
|
23
|
+
console.log(result.stderr);
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
第3引数に文字列を渡すことで、CLIのツールの標準入力として扱われます。
|
|
27
|
+
|
|
28
|
+
```javascript
|
|
29
|
+
import { cli2module } from "@kokiito0926/cli2module";
|
|
30
|
+
|
|
31
|
+
const result = await cli2module("zx/cli", [], 'console.log("Hello, world!")');
|
|
32
|
+
console.log(result.code);
|
|
33
|
+
console.log(result.stdout);
|
|
34
|
+
console.log(result.stderr);
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## ライセンス
|
|
38
|
+
|
|
39
|
+
[MIT](LICENSE)
|
package/example.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import { cli2module } from "./index.js";
|
|
4
|
+
|
|
5
|
+
// const result = await cli2module("zx/cli", []);
|
|
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);
|
|
12
|
+
|
|
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
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { fileURLToPath } from "node:url";
|
|
2
|
+
import { Worker } from "node:worker_threads";
|
|
3
|
+
|
|
4
|
+
// zxのCLIのパスを取得しなければいけない。
|
|
5
|
+
// 通常通りにzxをimportしてしまうと、ライブラリとして解決されてしまう。
|
|
6
|
+
/*
|
|
7
|
+
"bin": {
|
|
8
|
+
"zx": "build/cli.js"
|
|
9
|
+
},
|
|
10
|
+
*/
|
|
11
|
+
// >> C:\Users\kouki\root\playground\node\node_modules\zx\package.json
|
|
12
|
+
// >> 2026/01/28 10:43.
|
|
13
|
+
|
|
14
|
+
function runInWorker(scriptPath, args = [], input = "") {
|
|
15
|
+
return new Promise((resolve, reject) => {
|
|
16
|
+
const workerCode = `
|
|
17
|
+
import { workerData } from 'node:worker_threads';
|
|
18
|
+
// console.log(workerData.path);
|
|
19
|
+
// console.log(workerData.args);
|
|
20
|
+
|
|
21
|
+
process.argv = ['node', workerData.path, ...workerData.args];
|
|
22
|
+
|
|
23
|
+
await import('file://' + workerData.path);
|
|
24
|
+
`;
|
|
25
|
+
|
|
26
|
+
const worker = new Worker(workerCode, {
|
|
27
|
+
eval: true,
|
|
28
|
+
type: "module",
|
|
29
|
+
workerData: { path: scriptPath, args },
|
|
30
|
+
stdin: true,
|
|
31
|
+
stdout: true,
|
|
32
|
+
stderr: true,
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
let output = "";
|
|
36
|
+
let errorOutput = "";
|
|
37
|
+
|
|
38
|
+
if (input) {
|
|
39
|
+
worker.stdin.write(input);
|
|
40
|
+
worker.stdin.end();
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
worker.stdout.on("data", (chunk) => {
|
|
44
|
+
output += chunk.toString();
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
worker.stderr.on("data", (chunk) => {
|
|
48
|
+
errorOutput += chunk.toString();
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
worker.on("exit", (code) => {
|
|
52
|
+
resolve({
|
|
53
|
+
code,
|
|
54
|
+
stdout: output.trim(),
|
|
55
|
+
stderr: errorOutput.trim(),
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
worker.on("error", reject);
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
|
|
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
|
+
|
|
70
|
+
const result = await runInWorker(cliPath2, options, input);
|
|
71
|
+
// console.log(result);
|
|
72
|
+
return result;
|
|
73
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@kokiito0926/cli2module",
|
|
3
|
+
"version": "0.0.5",
|
|
4
|
+
"private": false,
|
|
5
|
+
"description": "CLIのツールをワーカースレッドで実行することができるライブラリです。",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"cli",
|
|
8
|
+
"ctm",
|
|
9
|
+
"module",
|
|
10
|
+
"nodejs",
|
|
11
|
+
"cli2module"
|
|
12
|
+
],
|
|
13
|
+
"homepage": "https://github.com/kokiito0926/cli2module#readme",
|
|
14
|
+
"publishConfig": {
|
|
15
|
+
"access": "public"
|
|
16
|
+
},
|
|
17
|
+
"main": "./index.js",
|
|
18
|
+
"bugs": {
|
|
19
|
+
"url": "https://github.com/kokiito0926/cli2module/issues"
|
|
20
|
+
},
|
|
21
|
+
"repository": {
|
|
22
|
+
"type": "git",
|
|
23
|
+
"url": "git+https://github.com/kokiito0926/cli2module.git"
|
|
24
|
+
},
|
|
25
|
+
"license": "MIT",
|
|
26
|
+
"author": "Koki Ito <kokiito0926@gmail.com>",
|
|
27
|
+
"type": "module",
|
|
28
|
+
"scripts": {
|
|
29
|
+
"test": "node --test"
|
|
30
|
+
},
|
|
31
|
+
"engines": {
|
|
32
|
+
"node": ">=24.0.0"
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"zx": "^8.8.5"
|
|
36
|
+
}
|
|
37
|
+
}
|