@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.
Files changed (2) hide show
  1. package/es/index.mjs +173 -163
  2. package/package.json +2 -2
package/es/index.mjs CHANGED
@@ -1,68 +1,68 @@
1
1
  #!/usr/bin/env node
2
- import { OutputConsoleTypeEnum as u, OutputLogFileTypeEnum as a, handleTableTypeConsole as w, createOutputConsole as D, createOutputLogFile as A } from "@done-coding/output-core";
3
- import { OutputConsoleTypeEnum as le, OutputLogFileTypeEnum as fe } from "@done-coding/output-core";
4
- import d from "chalk";
5
- import O from "pino";
6
- import h from "node:fs";
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 L extends Error {
9
- constructor(r, t) {
10
- super(`无效的输出类型: ${r}。有效的类型包括: ${t.join(", ")}`), this.name = "InvalidOutputTypeError";
8
+ class I extends Error {
9
+ constructor(t, r) {
10
+ super(`无效的输出类型: ${t}。有效的类型包括: ${r.join(", ")}`), this.name = "InvalidOutputTypeError";
11
11
  }
12
12
  }
13
- class g extends Error {
14
- constructor(r) {
15
- super(`配置错误: ${r}`), this.name = "InvalidConfigurationError";
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(r, t) {
20
- super(`文件权限不足: 无法${t}文件 "${r}"`), this.name = "FilePermissionError";
19
+ constructor(t, r) {
20
+ super(`文件权限不足: 无法${r}文件 "${t}"`), this.name = "FilePermissionError";
21
21
  }
22
22
  }
23
- class C extends Error {
24
- constructor(r, t) {
25
- super(`驱动初始化失败: ${r}${t ? ` - ${t.message}` : ""}`), this.name = "DriverInitializationError", t && (this.cause = t);
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 _(e) {
29
- const r = Object.values(u).filter(
30
- (t) => typeof t == "number"
28
+ function S(e) {
29
+ const t = Object.values(f).filter(
30
+ (r) => typeof r == "number"
31
31
  );
32
- if (typeof e != "number" || !r.includes(e)) {
33
- const t = Object.keys(u).filter(
34
- (o) => isNaN(Number(o))
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 L(e, t);
36
+ throw new I(e, r);
37
37
  }
38
38
  }
39
39
  function U(e) {
40
- const r = Object.values(a).filter(
41
- (t) => typeof t == "number"
40
+ const t = Object.values(d).filter(
41
+ (r) => typeof r == "number"
42
42
  );
43
- if (typeof e != "number" || !r.includes(e)) {
44
- const t = Object.keys(a).filter(
45
- (o) => isNaN(Number(o))
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 L(e, t);
47
+ throw new I(e, r);
48
48
  }
49
49
  }
50
- function T(e, r) {
51
- for (const t of r)
52
- if (!(t in e) || e[t] === void 0)
53
- throw new g(`缺少必需字段: ${t}`);
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 g("文件路径必须是非空字符串");
57
+ throw new _("文件路径必须是非空字符串");
58
58
  if (/[<>:"|?*]/.test(e))
59
- throw new g(`文件路径包含非法字符: ${e}`);
59
+ throw new _(`文件路径包含非法字符: ${e}`);
60
60
  }
61
- function c(e, r) {
61
+ function c(e, t) {
62
62
  try {
63
63
  return e();
64
- } catch (t) {
65
- return r ? r(t) : void 0;
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
- [u.DEBUG]: "gray",
74
- [u.SKIP]: "dim",
75
- [u.INFO]: "white",
76
- [u.TABLE]: "cyan",
77
- [u.STAGE]: "blue",
78
- [u.SUCCESS]: "greenBright",
79
- [u.WARN]: "yellow",
80
- [u.ERROR]: "redBright"
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
- }, b = {
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
- }, te = {
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 G(e) {
135
+ function B(e) {
136
136
  const {
137
- enableColor: r = R.enableColor,
138
- colorMap: t
139
- } = e, o = { ...$, ...t };
137
+ enableColor: t = R.enableColor,
138
+ colorMap: r
139
+ } = e, n = { ...$, ...r };
140
140
  try {
141
- if (T(e, []), t) {
142
- for (const [n, i] of Object.entries(t))
143
- if (_(Number(n)), typeof i != "string")
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 (n) {
147
- throw new C("控制台驱动", n);
146
+ } catch (o) {
147
+ throw new b("控制台驱动", o);
148
148
  }
149
- return (n, ...i) => {
150
- if (c(() => _(n)), n === u.TABLE) {
151
- c(() => w(...i));
149
+ return (o, ...i) => {
150
+ if (c(() => S(o)), o === f.TABLE) {
151
+ c(() => v(...i));
152
152
  return;
153
153
  }
154
- const l = o[n] || x.fallbackColor, f = i.map((s) => typeof s == "string" && r ? c(
155
- () => l in d && typeof d[l] == "function" ? d[l](s) : d.white(s),
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(...f));
158
+ c(() => console.log(...l));
159
159
  };
160
160
  }
161
161
  const N = /* @__PURE__ */ new Set();
162
- let I = !1;
163
- function Z() {
164
- for (const { logger: e, destination: r } of N)
162
+ let L = !1;
163
+ function G() {
164
+ for (const { destination: e } of N)
165
165
  try {
166
- e.info("进程退出,日志刷新完成"), B(r);
166
+ Z(e), k(e);
167
167
  } catch {
168
168
  }
169
- H();
169
+ X();
170
170
  }
171
- function B(e) {
172
- if (v(e))
171
+ function Z(e) {
172
+ if (w(e))
173
173
  try {
174
174
  e.flushSync(), j(e);
175
- } catch (r) {
176
- if (r instanceof Error && r.message.includes("sonic boom is not ready yet"))
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 r = e.fd;
182
- if (typeof r == "number" && r > 0)
181
+ const t = e.fd;
182
+ if (typeof t == "number" && t > 0)
183
183
  try {
184
- h.fsyncSync(r);
184
+ a.fsyncSync(t);
185
185
  } catch {
186
186
  }
187
187
  }
188
- function v(e) {
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 r = e;
192
- if (r.constructor && r.constructor.name === "SonicBoom")
193
- return k(r);
194
- const t = r.fd;
195
- return typeof t == "number" ? t > 0 || t === 1 || t === 2 : !(r.destroyed || r.closed || r.writable === !1);
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 k(e) {
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 H() {
210
+ function X() {
201
211
  try {
202
- process.stdout.fd !== void 0 && typeof process.stdout.fd == "number" && h.fsyncSync(process.stdout.fd);
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" && h.fsyncSync(process.stderr.fd);
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 X() {
211
- I || (P(() => {
212
- Z();
213
- }), I = !0);
220
+ function V() {
221
+ L || (P(() => {
222
+ G();
223
+ }), L = !0);
214
224
  }
215
- function V({
225
+ function W({
216
226
  logger: e,
217
- destination: r
227
+ destination: t
218
228
  }) {
219
- N.add({ logger: e, destination: r }), X();
229
+ N.add({ logger: e, destination: t }), V();
220
230
  }
221
- function W(e) {
231
+ function K(e) {
222
232
  const {
223
- logFilePath: r,
224
- sync: t = m.sync,
225
- bufferSize: o = m.bufferSize
233
+ logFilePath: t,
234
+ sync: r = m.sync,
235
+ bufferSize: n = m.bufferSize
226
236
  } = e;
227
237
  try {
228
- if (T(e, []), r !== void 0 && M(r), o <= 0)
229
- throw new Error(`缓冲区大小必须大于0: ${o}`);
230
- if (o < E.MIN_SIZE || o > E.MAX_SIZE)
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} 字节范围内: ${o}`
242
+ `缓冲区大小必须在 ${E.MIN_SIZE} - ${E.MAX_SIZE} 字节范围内: ${n}`
233
243
  );
234
- } catch (f) {
235
- throw new C("日志文件驱动", f);
244
+ } catch (l) {
245
+ throw new b("日志文件驱动", l);
236
246
  }
237
- const i = K({
238
- logFilePath: r,
239
- sync: t,
240
- bufferSize: o
247
+ const i = q({
248
+ logFilePath: t,
249
+ sync: r,
250
+ bufferSize: n
241
251
  });
242
- V(i);
243
- const { logger: l } = i;
244
- return (f, ...s) => {
245
- c(() => U(f));
246
- const y = s.length === 1 ? s[0] : s.join(" ");
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 S;
249
- const p = (a[f] || a[a.INFO]).toLowerCase();
250
- (S = l[p]) == null || S.call(l, y);
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 K(e) {
255
- const { logFilePath: r, sync: t, bufferSize: o } = e, n = O.destination({
256
- dest: r,
257
- sync: t,
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: o,
269
+ bufferSize: n,
260
270
  // 使用用户配置的缓冲区大小
261
271
  autoEnd: !1
262
272
  // 始终禁用自动结束,使用我们的防御性退出处理
263
- }), i = n.flushSync;
264
- return n.flushSync = function() {
265
- if (v(n))
273
+ }), i = o.flushSync;
274
+ return o.flushSync = function() {
275
+ if (w(o))
266
276
  return i.call(this);
267
- }, { logger: O(
277
+ }, { logger: h(
268
278
  {
269
- level: b.level
279
+ level: T.level
270
280
  },
271
- n
272
- ), destination: n };
281
+ o
282
+ ), destination: o };
273
283
  }
274
- function ne(e = {}) {
284
+ function ie(e = {}) {
275
285
  const {
276
- enableColor: r = z.enableColor,
277
- isSilent: t,
278
- colorMap: o,
279
- ...n
280
- } = e, i = G({
281
- enableColor: r,
282
- colorMap: o
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: t,
287
- colorMap: o,
288
- enableColor: r,
289
- ...n
296
+ isSilent: r,
297
+ colorMap: n,
298
+ enableColor: t,
299
+ ...o
290
300
  });
291
301
  }
292
- function ie(e) {
302
+ function se(e) {
293
303
  const {
294
- isSilent: r,
304
+ isSilent: t,
295
305
  // 不设置默认值,保持可选
296
- logFilePath: t,
306
+ logFilePath: r,
297
307
  // 现在是必传的绝对路径参数
298
- sync: o = F.sync,
308
+ sync: n = F.sync,
299
309
  // 默认异步写入
300
- bufferSize: n = F.bufferSize,
310
+ bufferSize: o = F.bufferSize,
301
311
  // 默认缓冲区大小 4KB
302
312
  ...i
303
- } = e, l = W({
304
- logFilePath: t,
305
- sync: o,
306
- bufferSize: n
313
+ } = e, u = K({
314
+ logFilePath: r,
315
+ sync: n,
316
+ bufferSize: o
307
317
  });
308
- let f;
318
+ let l;
309
319
  return A({
310
- outputImpl: l,
311
- isSilent: r,
320
+ outputImpl: u,
321
+ isSilent: t,
312
322
  // 内部使用pino 直接输出到控制台
313
- outputConsoleFn: (s, ...y) => {
314
- f || (f = O({
315
- level: b.level
323
+ outputConsoleFn: (s, ...p) => {
324
+ l || (l = h({
325
+ level: T.level
316
326
  }));
317
- const p = { type: s, messages: y };
318
- f.info(p);
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
- b as DEFAULT_PINO_CONFIG,
332
- te as DEFAULT_PROCESS_HANDLER_CONFIG,
333
- C as DriverInitializationError,
341
+ T as DEFAULT_PINO_CONFIG,
342
+ ne as DEFAULT_PROCESS_HANDLER_CONFIG,
343
+ b as DriverInitializationError,
334
344
  re as FilePermissionError,
335
- g as InvalidConfigurationError,
336
- L as InvalidOutputTypeError,
345
+ _ as InvalidConfigurationError,
346
+ I as InvalidOutputTypeError,
337
347
  le as OutputConsoleTypeEnum,
338
- fe as OutputLogFileTypeEnum,
339
- ne as createOutputConsole,
340
- ie as createOutputLogFile
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.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": "87831fe924a30d5f398a6687e92f5db72d6ea71b"
65
+ "gitHead": "1c1c135e6a8a313082f6f63faa46fef4e97e2fc0"
66
66
  }