@done-coding/output-node 0.1.1 → 0.1.2-alpha.0
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/es/index.mjs +173 -163
- package/package.json +2 -2
package/es/index.mjs
CHANGED
|
@@ -1,68 +1,68 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import { OutputConsoleTypeEnum as
|
|
3
|
-
import { OutputConsoleTypeEnum as le, OutputLogFileTypeEnum as
|
|
4
|
-
import
|
|
5
|
-
import
|
|
6
|
-
import
|
|
2
|
+
import { OutputConsoleTypeEnum as f, OutputLogFileTypeEnum as d, handleTableTypeConsole as v, createOutputConsole as D, createOutputLogFile as A } from "@done-coding/output-core";
|
|
3
|
+
import { OutputConsoleTypeEnum as le, OutputLogFileTypeEnum as ce } from "@done-coding/output-core";
|
|
4
|
+
import y from "chalk";
|
|
5
|
+
import h from "pino";
|
|
6
|
+
import a from "node:fs";
|
|
7
7
|
import { onExit as P } from "signal-exit";
|
|
8
|
-
class
|
|
9
|
-
constructor(
|
|
10
|
-
super(`无效的输出类型: ${
|
|
8
|
+
class I extends Error {
|
|
9
|
+
constructor(t, r) {
|
|
10
|
+
super(`无效的输出类型: ${t}。有效的类型包括: ${r.join(", ")}`), this.name = "InvalidOutputTypeError";
|
|
11
11
|
}
|
|
12
12
|
}
|
|
13
|
-
class
|
|
14
|
-
constructor(
|
|
15
|
-
super(`配置错误: ${
|
|
13
|
+
class _ extends Error {
|
|
14
|
+
constructor(t) {
|
|
15
|
+
super(`配置错误: ${t}`), this.name = "InvalidConfigurationError";
|
|
16
16
|
}
|
|
17
17
|
}
|
|
18
18
|
class re extends Error {
|
|
19
|
-
constructor(
|
|
20
|
-
super(`文件权限不足: 无法${
|
|
19
|
+
constructor(t, r) {
|
|
20
|
+
super(`文件权限不足: 无法${r}文件 "${t}"`), this.name = "FilePermissionError";
|
|
21
21
|
}
|
|
22
22
|
}
|
|
23
|
-
class
|
|
24
|
-
constructor(
|
|
25
|
-
super(`驱动初始化失败: ${
|
|
23
|
+
class b extends Error {
|
|
24
|
+
constructor(t, r) {
|
|
25
|
+
super(`驱动初始化失败: ${t}${r ? ` - ${r.message}` : ""}`), this.name = "DriverInitializationError", r && (this.cause = r);
|
|
26
26
|
}
|
|
27
27
|
}
|
|
28
|
-
function
|
|
29
|
-
const
|
|
30
|
-
(
|
|
28
|
+
function S(e) {
|
|
29
|
+
const t = Object.values(f).filter(
|
|
30
|
+
(r) => typeof r == "number"
|
|
31
31
|
);
|
|
32
|
-
if (typeof e != "number" || !
|
|
33
|
-
const
|
|
34
|
-
(
|
|
32
|
+
if (typeof e != "number" || !t.includes(e)) {
|
|
33
|
+
const r = Object.keys(f).filter(
|
|
34
|
+
(n) => isNaN(Number(n))
|
|
35
35
|
);
|
|
36
|
-
throw new
|
|
36
|
+
throw new I(e, r);
|
|
37
37
|
}
|
|
38
38
|
}
|
|
39
39
|
function U(e) {
|
|
40
|
-
const
|
|
41
|
-
(
|
|
40
|
+
const t = Object.values(d).filter(
|
|
41
|
+
(r) => typeof r == "number"
|
|
42
42
|
);
|
|
43
|
-
if (typeof e != "number" || !
|
|
44
|
-
const
|
|
45
|
-
(
|
|
43
|
+
if (typeof e != "number" || !t.includes(e)) {
|
|
44
|
+
const r = Object.keys(d).filter(
|
|
45
|
+
(n) => isNaN(Number(n))
|
|
46
46
|
);
|
|
47
|
-
throw new
|
|
47
|
+
throw new I(e, r);
|
|
48
48
|
}
|
|
49
49
|
}
|
|
50
|
-
function
|
|
51
|
-
for (const
|
|
52
|
-
if (!(
|
|
53
|
-
throw new
|
|
50
|
+
function C(e, t) {
|
|
51
|
+
for (const r of t)
|
|
52
|
+
if (!(r in e) || e[r] === void 0)
|
|
53
|
+
throw new _(`缺少必需字段: ${r}`);
|
|
54
54
|
}
|
|
55
55
|
function M(e) {
|
|
56
56
|
if (typeof e != "string" || e.trim() === "")
|
|
57
|
-
throw new
|
|
57
|
+
throw new _("文件路径必须是非空字符串");
|
|
58
58
|
if (/[<>:"|?*]/.test(e))
|
|
59
|
-
throw new
|
|
59
|
+
throw new _(`文件路径包含非法字符: ${e}`);
|
|
60
60
|
}
|
|
61
|
-
function c(e,
|
|
61
|
+
function c(e, t) {
|
|
62
62
|
try {
|
|
63
63
|
return e();
|
|
64
|
-
} catch (
|
|
65
|
-
return
|
|
64
|
+
} catch (r) {
|
|
65
|
+
return t ? t(r) : void 0;
|
|
66
66
|
}
|
|
67
67
|
}
|
|
68
68
|
const R = {
|
|
@@ -70,14 +70,14 @@ const R = {
|
|
|
70
70
|
/** 默认启用颜色输出:是 */
|
|
71
71
|
enableColor: !0
|
|
72
72
|
}, $ = {
|
|
73
|
-
[
|
|
74
|
-
[
|
|
75
|
-
[
|
|
76
|
-
[
|
|
77
|
-
[
|
|
78
|
-
[
|
|
79
|
-
[
|
|
80
|
-
[
|
|
73
|
+
[f.DEBUG]: "gray",
|
|
74
|
+
[f.SKIP]: "dim",
|
|
75
|
+
[f.INFO]: "white",
|
|
76
|
+
[f.TABLE]: "cyan",
|
|
77
|
+
[f.STAGE]: "blue",
|
|
78
|
+
[f.SUCCESS]: "greenBright",
|
|
79
|
+
[f.WARN]: "yellow",
|
|
80
|
+
[f.ERROR]: "redBright"
|
|
81
81
|
}, x = {
|
|
82
82
|
/** 默认颜色格式化失败时的回退颜色 */
|
|
83
83
|
fallbackColor: "white"
|
|
@@ -87,7 +87,7 @@ const R = {
|
|
|
87
87
|
sync: !1,
|
|
88
88
|
/** 默认缓冲区大小:4KB */
|
|
89
89
|
bufferSize: 4096
|
|
90
|
-
},
|
|
90
|
+
}, T = {
|
|
91
91
|
/** 默认日志级别:trace */
|
|
92
92
|
level: "trace",
|
|
93
93
|
/** 默认时间格式:yyyy-mm-dd HH:MM:ss */
|
|
@@ -117,7 +117,7 @@ const R = {
|
|
|
117
117
|
sync: !1,
|
|
118
118
|
/** 默认缓冲区大小:4KB */
|
|
119
119
|
bufferSize: 4096
|
|
120
|
-
},
|
|
120
|
+
}, ne = {
|
|
121
121
|
/** 默认静默模式:是(避免控制台输出) */
|
|
122
122
|
silentMode: !0,
|
|
123
123
|
/** 默认最大重试次数:3次 */
|
|
@@ -132,190 +132,200 @@ const R = {
|
|
|
132
132
|
/** 默认错误重试延迟:50ms */
|
|
133
133
|
retryDelay: 50
|
|
134
134
|
};
|
|
135
|
-
function
|
|
135
|
+
function B(e) {
|
|
136
136
|
const {
|
|
137
|
-
enableColor:
|
|
138
|
-
colorMap:
|
|
139
|
-
} = e,
|
|
137
|
+
enableColor: t = R.enableColor,
|
|
138
|
+
colorMap: r
|
|
139
|
+
} = e, n = { ...$, ...r };
|
|
140
140
|
try {
|
|
141
|
-
if (
|
|
142
|
-
for (const [
|
|
143
|
-
if (
|
|
141
|
+
if (C(e, []), r) {
|
|
142
|
+
for (const [o, i] of Object.entries(r))
|
|
143
|
+
if (S(Number(o)), typeof i != "string")
|
|
144
144
|
throw new Error(`颜色值必须是字符串: ${i}`);
|
|
145
145
|
}
|
|
146
|
-
} catch (
|
|
147
|
-
throw new
|
|
146
|
+
} catch (o) {
|
|
147
|
+
throw new b("控制台驱动", o);
|
|
148
148
|
}
|
|
149
|
-
return (
|
|
150
|
-
if (c(() =>
|
|
151
|
-
c(() =>
|
|
149
|
+
return (o, ...i) => {
|
|
150
|
+
if (c(() => S(o)), o === f.TABLE) {
|
|
151
|
+
c(() => v(...i));
|
|
152
152
|
return;
|
|
153
153
|
}
|
|
154
|
-
const
|
|
155
|
-
() =>
|
|
154
|
+
const u = n[o] || x.fallbackColor, l = i.map((s) => typeof s == "string" && t ? c(
|
|
155
|
+
() => u in y && typeof y[u] == "function" ? y[u](s) : y.white(s),
|
|
156
156
|
() => s
|
|
157
157
|
) : s);
|
|
158
|
-
c(() => console.log(...
|
|
158
|
+
c(() => console.log(...l));
|
|
159
159
|
};
|
|
160
160
|
}
|
|
161
161
|
const N = /* @__PURE__ */ new Set();
|
|
162
|
-
let
|
|
163
|
-
function
|
|
164
|
-
for (const {
|
|
162
|
+
let L = !1;
|
|
163
|
+
function G() {
|
|
164
|
+
for (const { destination: e } of N)
|
|
165
165
|
try {
|
|
166
|
-
e
|
|
166
|
+
Z(e), k(e);
|
|
167
167
|
} catch {
|
|
168
168
|
}
|
|
169
|
-
|
|
169
|
+
X();
|
|
170
170
|
}
|
|
171
|
-
function
|
|
172
|
-
if (
|
|
171
|
+
function Z(e) {
|
|
172
|
+
if (w(e))
|
|
173
173
|
try {
|
|
174
174
|
e.flushSync(), j(e);
|
|
175
|
-
} catch (
|
|
176
|
-
if (
|
|
175
|
+
} catch (t) {
|
|
176
|
+
if (t instanceof Error && t.message.includes("sonic boom is not ready yet"))
|
|
177
177
|
return;
|
|
178
178
|
}
|
|
179
179
|
}
|
|
180
180
|
function j(e) {
|
|
181
|
-
const
|
|
182
|
-
if (typeof
|
|
181
|
+
const t = e.fd;
|
|
182
|
+
if (typeof t == "number" && t > 0)
|
|
183
183
|
try {
|
|
184
|
-
|
|
184
|
+
a.fsyncSync(t);
|
|
185
185
|
} catch {
|
|
186
186
|
}
|
|
187
187
|
}
|
|
188
|
-
function
|
|
188
|
+
function k(e) {
|
|
189
|
+
try {
|
|
190
|
+
const t = e, r = t.file;
|
|
191
|
+
if (!r || typeof r != "string" || !a.existsSync(r))
|
|
192
|
+
return;
|
|
193
|
+
const o = a.statSync(r).size, i = t._bufs && t._bufs.length > 0 || typeof t._len == "number" && t._len > 0 || t._writingBuf && t._writingBuf.length > 0;
|
|
194
|
+
o === 0 && !i && a.unlinkSync(r);
|
|
195
|
+
} catch {
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
function w(e) {
|
|
189
199
|
if (!e || !("flushSync" in e) || typeof e.flushSync != "function")
|
|
190
200
|
return !1;
|
|
191
|
-
const
|
|
192
|
-
if (
|
|
193
|
-
return
|
|
194
|
-
const
|
|
195
|
-
return typeof
|
|
201
|
+
const t = e;
|
|
202
|
+
if (t.constructor && t.constructor.name === "SonicBoom")
|
|
203
|
+
return H(t);
|
|
204
|
+
const r = t.fd;
|
|
205
|
+
return typeof r == "number" ? r > 0 || r === 1 || r === 2 : !(t.destroyed || t.closed || t.writable === !1);
|
|
196
206
|
}
|
|
197
|
-
function
|
|
207
|
+
function H(e) {
|
|
198
208
|
return !(!e.fd || e.fd === null || e.fd === void 0 || e.fd < 0 || e._writing === !0 || e.destroyed === !0 || e._reopening === !0 || e._ending === !0 || e._asyncDrainScheduled === !0 || e._buf && e._buf.length > 0 && e._writing);
|
|
199
209
|
}
|
|
200
|
-
function
|
|
210
|
+
function X() {
|
|
201
211
|
try {
|
|
202
|
-
process.stdout.fd !== void 0 && typeof process.stdout.fd == "number" &&
|
|
212
|
+
process.stdout.fd !== void 0 && typeof process.stdout.fd == "number" && a.fsyncSync(process.stdout.fd);
|
|
203
213
|
} catch {
|
|
204
214
|
}
|
|
205
215
|
try {
|
|
206
|
-
process.stderr.fd !== void 0 && typeof process.stderr.fd == "number" &&
|
|
216
|
+
process.stderr.fd !== void 0 && typeof process.stderr.fd == "number" && a.fsyncSync(process.stderr.fd);
|
|
207
217
|
} catch {
|
|
208
218
|
}
|
|
209
219
|
}
|
|
210
|
-
function
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
}),
|
|
220
|
+
function V() {
|
|
221
|
+
L || (P(() => {
|
|
222
|
+
G();
|
|
223
|
+
}), L = !0);
|
|
214
224
|
}
|
|
215
|
-
function
|
|
225
|
+
function W({
|
|
216
226
|
logger: e,
|
|
217
|
-
destination:
|
|
227
|
+
destination: t
|
|
218
228
|
}) {
|
|
219
|
-
N.add({ logger: e, destination:
|
|
229
|
+
N.add({ logger: e, destination: t }), V();
|
|
220
230
|
}
|
|
221
|
-
function
|
|
231
|
+
function K(e) {
|
|
222
232
|
const {
|
|
223
|
-
logFilePath:
|
|
224
|
-
sync:
|
|
225
|
-
bufferSize:
|
|
233
|
+
logFilePath: t,
|
|
234
|
+
sync: r = m.sync,
|
|
235
|
+
bufferSize: n = m.bufferSize
|
|
226
236
|
} = e;
|
|
227
237
|
try {
|
|
228
|
-
if (
|
|
229
|
-
throw new Error(`缓冲区大小必须大于0: ${
|
|
230
|
-
if (
|
|
238
|
+
if (C(e, []), t !== void 0 && M(t), n <= 0)
|
|
239
|
+
throw new Error(`缓冲区大小必须大于0: ${n}`);
|
|
240
|
+
if (n < E.MIN_SIZE || n > E.MAX_SIZE)
|
|
231
241
|
throw new Error(
|
|
232
|
-
`缓冲区大小必须在 ${E.MIN_SIZE} - ${E.MAX_SIZE} 字节范围内: ${
|
|
242
|
+
`缓冲区大小必须在 ${E.MIN_SIZE} - ${E.MAX_SIZE} 字节范围内: ${n}`
|
|
233
243
|
);
|
|
234
|
-
} catch (
|
|
235
|
-
throw new
|
|
244
|
+
} catch (l) {
|
|
245
|
+
throw new b("日志文件驱动", l);
|
|
236
246
|
}
|
|
237
|
-
const i =
|
|
238
|
-
logFilePath:
|
|
239
|
-
sync:
|
|
240
|
-
bufferSize:
|
|
247
|
+
const i = q({
|
|
248
|
+
logFilePath: t,
|
|
249
|
+
sync: r,
|
|
250
|
+
bufferSize: n
|
|
241
251
|
});
|
|
242
|
-
|
|
243
|
-
const { logger:
|
|
244
|
-
return (
|
|
245
|
-
c(() => U(
|
|
246
|
-
const
|
|
252
|
+
W(i);
|
|
253
|
+
const { logger: u } = i;
|
|
254
|
+
return (l, ...s) => {
|
|
255
|
+
c(() => U(l));
|
|
256
|
+
const p = s.length === 1 ? s[0] : s.join(" ");
|
|
247
257
|
c(() => {
|
|
248
|
-
var
|
|
249
|
-
const
|
|
250
|
-
(
|
|
258
|
+
var g;
|
|
259
|
+
const O = (d[l] || d[d.INFO]).toLowerCase();
|
|
260
|
+
(g = u[O]) == null || g.call(u, p);
|
|
251
261
|
});
|
|
252
262
|
};
|
|
253
263
|
}
|
|
254
|
-
function
|
|
255
|
-
const { logFilePath:
|
|
256
|
-
dest:
|
|
257
|
-
sync:
|
|
264
|
+
function q(e) {
|
|
265
|
+
const { logFilePath: t, sync: r, bufferSize: n } = e, o = h.destination({
|
|
266
|
+
dest: t,
|
|
267
|
+
sync: r,
|
|
258
268
|
// 使用用户配置的同步/异步模式
|
|
259
|
-
bufferSize:
|
|
269
|
+
bufferSize: n,
|
|
260
270
|
// 使用用户配置的缓冲区大小
|
|
261
271
|
autoEnd: !1
|
|
262
272
|
// 始终禁用自动结束,使用我们的防御性退出处理
|
|
263
|
-
}), i =
|
|
264
|
-
return
|
|
265
|
-
if (
|
|
273
|
+
}), i = o.flushSync;
|
|
274
|
+
return o.flushSync = function() {
|
|
275
|
+
if (w(o))
|
|
266
276
|
return i.call(this);
|
|
267
|
-
}, { logger:
|
|
277
|
+
}, { logger: h(
|
|
268
278
|
{
|
|
269
|
-
level:
|
|
279
|
+
level: T.level
|
|
270
280
|
},
|
|
271
|
-
|
|
272
|
-
), destination:
|
|
281
|
+
o
|
|
282
|
+
), destination: o };
|
|
273
283
|
}
|
|
274
|
-
function
|
|
284
|
+
function ie(e = {}) {
|
|
275
285
|
const {
|
|
276
|
-
enableColor:
|
|
277
|
-
isSilent:
|
|
278
|
-
colorMap:
|
|
279
|
-
...
|
|
280
|
-
} = e, i =
|
|
281
|
-
enableColor:
|
|
282
|
-
colorMap:
|
|
286
|
+
enableColor: t = z.enableColor,
|
|
287
|
+
isSilent: r,
|
|
288
|
+
colorMap: n,
|
|
289
|
+
...o
|
|
290
|
+
} = e, i = B({
|
|
291
|
+
enableColor: t,
|
|
292
|
+
colorMap: n
|
|
283
293
|
});
|
|
284
294
|
return D({
|
|
285
295
|
outputImpl: i,
|
|
286
|
-
isSilent:
|
|
287
|
-
colorMap:
|
|
288
|
-
enableColor:
|
|
289
|
-
...
|
|
296
|
+
isSilent: r,
|
|
297
|
+
colorMap: n,
|
|
298
|
+
enableColor: t,
|
|
299
|
+
...o
|
|
290
300
|
});
|
|
291
301
|
}
|
|
292
|
-
function
|
|
302
|
+
function se(e) {
|
|
293
303
|
const {
|
|
294
|
-
isSilent:
|
|
304
|
+
isSilent: t,
|
|
295
305
|
// 不设置默认值,保持可选
|
|
296
|
-
logFilePath:
|
|
306
|
+
logFilePath: r,
|
|
297
307
|
// 现在是必传的绝对路径参数
|
|
298
|
-
sync:
|
|
308
|
+
sync: n = F.sync,
|
|
299
309
|
// 默认异步写入
|
|
300
|
-
bufferSize:
|
|
310
|
+
bufferSize: o = F.bufferSize,
|
|
301
311
|
// 默认缓冲区大小 4KB
|
|
302
312
|
...i
|
|
303
|
-
} = e,
|
|
304
|
-
logFilePath:
|
|
305
|
-
sync:
|
|
306
|
-
bufferSize:
|
|
313
|
+
} = e, u = K({
|
|
314
|
+
logFilePath: r,
|
|
315
|
+
sync: n,
|
|
316
|
+
bufferSize: o
|
|
307
317
|
});
|
|
308
|
-
let
|
|
318
|
+
let l;
|
|
309
319
|
return A({
|
|
310
|
-
outputImpl:
|
|
311
|
-
isSilent:
|
|
320
|
+
outputImpl: u,
|
|
321
|
+
isSilent: t,
|
|
312
322
|
// 内部使用pino 直接输出到控制台
|
|
313
|
-
outputConsoleFn: (s, ...
|
|
314
|
-
|
|
315
|
-
level:
|
|
323
|
+
outputConsoleFn: (s, ...p) => {
|
|
324
|
+
l || (l = h({
|
|
325
|
+
level: T.level
|
|
316
326
|
}));
|
|
317
|
-
const
|
|
318
|
-
|
|
327
|
+
const O = { type: s, messages: p };
|
|
328
|
+
l.info(O);
|
|
319
329
|
},
|
|
320
330
|
...i
|
|
321
331
|
});
|
|
@@ -328,14 +338,14 @@ export {
|
|
|
328
338
|
m as DEFAULT_LOG_FILE_OUTPUT_IMPL_OPTIONS,
|
|
329
339
|
z as DEFAULT_NODE_CREATE_OUTPUT_CONSOLE_OPTIONS,
|
|
330
340
|
F as DEFAULT_NODE_CREATE_OUTPUT_LOG_FILE_OPTIONS,
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
341
|
+
T as DEFAULT_PINO_CONFIG,
|
|
342
|
+
ne as DEFAULT_PROCESS_HANDLER_CONFIG,
|
|
343
|
+
b as DriverInitializationError,
|
|
334
344
|
re as FilePermissionError,
|
|
335
|
-
|
|
336
|
-
|
|
345
|
+
_ as InvalidConfigurationError,
|
|
346
|
+
I as InvalidOutputTypeError,
|
|
337
347
|
le as OutputConsoleTypeEnum,
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
348
|
+
ce as OutputLogFileTypeEnum,
|
|
349
|
+
ie as createOutputConsole,
|
|
350
|
+
se as createOutputLogFile
|
|
341
351
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@done-coding/output-node",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2-alpha.0",
|
|
4
4
|
"description": "node相关输出",
|
|
5
5
|
"private": false,
|
|
6
6
|
"module": "es/index.mjs",
|
|
@@ -62,5 +62,5 @@
|
|
|
62
62
|
"engines": {
|
|
63
63
|
"node": ">=18.0.0"
|
|
64
64
|
},
|
|
65
|
-
"gitHead": "
|
|
65
|
+
"gitHead": "1c1c135e6a8a313082f6f63faa46fef4e97e2fc0"
|
|
66
66
|
}
|