@jmcomic/jmcomic 2.6.20 → 2.6.21

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 (2) hide show
  1. package/index.js +192 -22
  2. package/package.json +5 -5
package/index.js CHANGED
@@ -1,50 +1,220 @@
1
1
  import path from "path"
2
- import { execFileSync } from "child_process"
2
+ import { spawn, execFileSync } from "child_process"
3
3
  import { createRequire } from "module"
4
4
 
5
5
  const require = createRequire(import.meta.url)
6
6
 
7
- // binary
8
- function getBinary() {
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
24
+
9
25
  const platform = process.platform
10
26
 
11
27
  if (platform === "win32") {
12
- const p = require.resolve("@kaguyajs/jmcomic-windows-x64")
13
- return path.join(path.dirname(p), "bin/jmcomic.exe")
28
+ const p = require.resolve("@jmcomic/jmcomic-windows-x64")
29
+ cachedBinaryPath = path.join(path.dirname(p), "bin/jmcomic.exe")
30
+ return cachedBinaryPath
14
31
  }
15
32
 
16
33
  if (platform === "linux") {
17
34
  try {
18
- const p = require.resolve("@kaguyajs/jmcomic-linux-gnu")
19
- return path.join(path.dirname(p), "bin/jmcomic")
35
+ const p = require.resolve("@jmcomic/jmcomic-linux-gnu")
36
+ cachedBinaryPath = path.join(path.dirname(p), "bin/jmcomic")
37
+ return cachedBinaryPath
20
38
  } catch {
21
- const p = require.resolve("@kaguyajs/jmcomic-linux-musl")
22
- return path.join(path.dirname(p), "bin/jmcomic")
39
+ const p = require.resolve("@jmcomic/jmcomic-linux-musl")
40
+ cachedBinaryPath = path.join(path.dirname(p), "bin/jmcomic")
41
+ return cachedBinaryPath
23
42
  }
24
43
  }
25
44
 
26
45
  if (platform === "darwin") {
27
- const p = require.resolve("@kaguyajs/jmcomic-macos-arm64")
28
- return path.join(path.dirname(p), "bin/jmcomic")
46
+ const p = require.resolve("@jmcomic/jmcomic-macos-arm64")
47
+ cachedBinaryPath = path.join(path.dirname(p), "bin/jmcomic")
48
+ return cachedBinaryPath
29
49
  }
30
50
 
31
- throw new Error("Unsupported platform")
51
+ throw new Error(`Unsupported platform: ${platform}`)
32
52
  }
33
53
 
34
- const bin = getBinary()
35
-
36
- export function run(input) {
37
- 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)
38
76
  ? input
39
- : String(input || "").trim().split(/\s+/)
77
+ : String(input).trim().split(/\s+/)
40
78
 
41
- execFileSync(bin, argv, {
42
- stdio: "inherit"
79
+ return spawn(getBinaryPath(), args, {
80
+ stdio: "inherit",
81
+ ...options
43
82
  })
44
83
  }
45
84
 
46
- export function jmcomic(input) {
47
- return run(input)
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
+ )
127
+
128
+ err.code = code
129
+ err.signal = signal
130
+
131
+ reject(err)
132
+ })
133
+ })
48
134
  }
49
135
 
50
- export default jmcomic
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)
177
+ }
178
+
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.20",
3
+ "version": "2.6.21",
4
4
  "keywords": [
5
5
  "JMComic",
6
6
  "禁漫",
@@ -21,9 +21,9 @@
21
21
  "index.js"
22
22
  ],
23
23
  "optionalDependencies": {
24
- "@jmcomic/jmcomic-linux-gnu": "2.6.20",
25
- "@jmcomic/jmcomic-linux-musl": "2.6.20",
26
- "@jmcomic/jmcomic-windows-x64": "2.6.20",
27
- "@jmcomic/jmcomic-macos-arm64": "2.6.20"
24
+ "@jmcomic/jmcomic-linux-gnu": "2.6.21",
25
+ "@jmcomic/jmcomic-linux-musl": "2.6.21",
26
+ "@jmcomic/jmcomic-windows-x64": "2.6.21",
27
+ "@jmcomic/jmcomic-macos-arm64": "2.6.21"
28
28
  }
29
29
  }