@jmcomic/jmcomic 2.6.19-hotfix → 2.6.20-rc.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.
Files changed (3) hide show
  1. package/cli.js +1 -0
  2. package/index.js +191 -20
  3. package/package.json +6 -5
package/cli.js CHANGED
@@ -1,3 +1,4 @@
1
+ #!/usr/bin/env node
1
2
  import { run } from "./index.js"
2
3
 
3
4
  // CLI 参数
package/index.js CHANGED
@@ -1,49 +1,220 @@
1
1
  import path from "path"
2
- import { execFileSync } from "child_process"
2
+ import { spawn, execFileSync } from "child_process"
3
+ import { createRequire } from "module"
4
+
5
+ const require = createRequire(import.meta.url)
6
+
7
+ let cachedBinaryPath = null
8
+
9
+ /**
10
+ * @typedef {Object} RunResult
11
+ * @property {number|null} code 退出码
12
+ * @property {NodeJS.Signals|null} signal 结束信号
13
+ * @property {import("child_process").ChildProcess} child 子进程对象
14
+ */
15
+
16
+ /**
17
+ * 获取当前平台对应的 jmcomic 可执行文件路径
18
+ *
19
+ * @returns {string} 可执行文件绝对路径
20
+ * @throws {Error} 当前平台不受支持
21
+ */
22
+ export function getBinaryPath() {
23
+ if (cachedBinaryPath) return cachedBinaryPath
3
24
 
4
- // 找 binary
5
- function getBinary() {
6
25
  const platform = process.platform
7
26
 
8
27
  if (platform === "win32") {
9
28
  const p = require.resolve("@kaguyajs/jmcomic-windows-x64")
10
- return path.join(path.dirname(p), "bin/jmcomic.exe")
29
+ cachedBinaryPath = path.join(path.dirname(p), "bin/jmcomic.exe")
30
+ return cachedBinaryPath
11
31
  }
12
32
 
13
33
  if (platform === "linux") {
14
34
  try {
15
35
  const p = require.resolve("@kaguyajs/jmcomic-linux-gnu")
16
- return path.join(path.dirname(p), "bin/jmcomic")
36
+ cachedBinaryPath = path.join(path.dirname(p), "bin/jmcomic")
37
+ return cachedBinaryPath
17
38
  } catch {
18
39
  const p = require.resolve("@kaguyajs/jmcomic-linux-musl")
19
- return path.join(path.dirname(p), "bin/jmcomic")
40
+ cachedBinaryPath = path.join(path.dirname(p), "bin/jmcomic")
41
+ return cachedBinaryPath
20
42
  }
21
43
  }
22
44
 
23
45
  if (platform === "darwin") {
24
46
  const p = require.resolve("@kaguyajs/jmcomic-macos-arm64")
25
- return path.join(path.dirname(p), "bin/jmcomic")
47
+ cachedBinaryPath = path.join(path.dirname(p), "bin/jmcomic")
48
+ return cachedBinaryPath
26
49
  }
27
50
 
28
- throw new Error("Unsupported platform")
51
+ throw new Error(`Unsupported platform: ${platform}`)
29
52
  }
30
53
 
31
- const bin = getBinary()
32
-
33
- // 核心执行函数(API)
34
- export function run(input) {
35
- const argv = Array.isArray(input)
54
+ /**
55
+ * 启动 jmcomic 进程
56
+ *
57
+ * 返回原始 ChildProcess 对象,
58
+ * 可自行监听 stdout、stderr、close、exit、error 等事件。
59
+ *
60
+ * @param {string|string[]} input
61
+ * 命令参数。
62
+ *
63
+ * 示例:
64
+ * ```js
65
+ * spawnProcess(["123456"])
66
+ * spawnProcess("123456 --option=option.yml")
67
+ * ```
68
+ *
69
+ * @param {import("child_process").SpawnOptions} [options]
70
+ * spawn 配置项
71
+ *
72
+ * @returns {import("child_process").ChildProcess}
73
+ */
74
+ export function spawnProcess(input, options = {}) {
75
+ const args = Array.isArray(input)
36
76
  ? input
37
- : String(input || "").trim().split(/\s+/)
77
+ : String(input).trim().split(/\s+/)
78
+
79
+ return spawn(getBinaryPath(), args, {
80
+ stdio: "inherit",
81
+ ...options
82
+ })
83
+ }
84
+
85
+ /**
86
+ * 异步执行 jmcomic
87
+ *
88
+ * Promise 在退出码为 0 时 resolve,
89
+ * 非 0 时 reject。
90
+ *
91
+ * @param {string|string[]} input
92
+ * 命令参数
93
+ *
94
+ * @param {import("child_process").SpawnOptions} [options]
95
+ * spawn 配置项
96
+ *
97
+ * @returns {Promise<RunResult>}
98
+ *
99
+ * @example
100
+ * await run(["123456"])
101
+ *
102
+ * @example
103
+ * await run([
104
+ * "123456",
105
+ * "--option=option.yml"
106
+ * ])
107
+ */
108
+ export function run(input, options = {}) {
109
+ return new Promise((resolve, reject) => {
110
+ const child = spawnProcess(input, options)
111
+
112
+ child.on("error", reject)
113
+
114
+ child.on("close", (code, signal) => {
115
+ if (code === 0) {
116
+ resolve({
117
+ code,
118
+ signal,
119
+ child
120
+ })
121
+ return
122
+ }
123
+
124
+ const err = new Error(
125
+ `jmcomic exited with code ${code}`
126
+ )
38
127
 
39
- execFileSync(bin, argv, {
40
- stdio: "inherit"
128
+ err.code = code
129
+ err.signal = signal
130
+
131
+ reject(err)
132
+ })
41
133
  })
42
134
  }
43
135
 
44
- // 给用户用的 API
45
- export function jmcomic(input) {
46
- return run(input)
136
+ /**
137
+ * 同步执行 jmcomic
138
+ *
139
+ * 当前线程会被阻塞直到程序退出。
140
+ *
141
+ * @param {string|string[]} input
142
+ * 命令参数
143
+ *
144
+ * @param {import("child_process").ExecFileSyncOptions} [options]
145
+ * execFileSync 配置项
146
+ *
147
+ * @returns {Buffer|string}
148
+ *
149
+ * @example
150
+ * runSync(["123456"])
151
+ */
152
+ export function runSync(input, options = {}) {
153
+ const args = Array.isArray(input)
154
+ ? input
155
+ : String(input).trim().split(/\s+/)
156
+
157
+ return execFileSync(
158
+ getBinaryPath(),
159
+ args,
160
+ {
161
+ stdio: "inherit",
162
+ ...options
163
+ }
164
+ )
165
+ }
166
+
167
+ /**
168
+ * run 的别名
169
+ *
170
+ * @param {string|string[]} input
171
+ * @param {import("child_process").SpawnOptions} [options]
172
+ *
173
+ * @returns {Promise<RunResult>}
174
+ */
175
+ export function exec(input, options = {}) {
176
+ return run(input, options)
47
177
  }
48
178
 
49
- export default jmcomic
179
+ /**
180
+ * 默认导出
181
+ *
182
+ * 等价于:
183
+ *
184
+ * ```js
185
+ * await run(...)
186
+ * ```
187
+ *
188
+ * @param {string|string[]} input
189
+ * @param {import("child_process").SpawnOptions} [options]
190
+ *
191
+ * @returns {Promise<RunResult>}
192
+ */
193
+ export default function jmcomic(input, options = {}) {
194
+ return run(input, options)
195
+ }
196
+
197
+ /**
198
+ * 获取可执行文件路径
199
+ */
200
+ jmcomic.getBinaryPath = getBinaryPath
201
+
202
+ /**
203
+ * 启动子进程
204
+ */
205
+ jmcomic.spawn = spawnProcess
206
+
207
+ /**
208
+ * 异步执行
209
+ */
210
+ jmcomic.run = run
211
+
212
+ /**
213
+ * run 的别名
214
+ */
215
+ jmcomic.exec = exec
216
+
217
+ /**
218
+ * 同步执行
219
+ */
220
+ jmcomic.runSync = runSync
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jmcomic/jmcomic",
3
- "version": "2.6.19-hotfix",
3
+ "version": "2.6.20-rc.1",
4
4
  "keywords": [
5
5
  "JMComic",
6
6
  "禁漫",
@@ -16,13 +16,14 @@
16
16
  "url": "https://github.com/hect0x7/JMComic-Crawler-Python"
17
17
  },
18
18
  "license": "MIT",
19
+ "type": "module",
19
20
  "files": [
20
21
  "index.js"
21
22
  ],
22
23
  "optionalDependencies": {
23
- "@jmcomic/jmcomic-linux-gnu": "2.6.19-hotfix",
24
- "@jmcomic/jmcomic-linux-musl": "2.6.19-hotfix",
25
- "@jmcomic/jmcomic-windows-x64": "2.6.19-hotfix",
26
- "@jmcomic/jmcomic-macos-arm64": "2.6.19-hotfix"
24
+ "@jmcomic/jmcomic-linux-gnu": "2.6.20-rc.1",
25
+ "@jmcomic/jmcomic-linux-musl": "2.6.20-rc.1",
26
+ "@jmcomic/jmcomic-windows-x64": "2.6.20-rc.1",
27
+ "@jmcomic/jmcomic-macos-arm64": "2.6.20-rc.1"
27
28
  }
28
29
  }