@zwa73/utils 1.0.119 → 1.0.121

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.
@@ -1,4 +1,4 @@
1
1
  import { UtilFT } from "./UtilFileTools";
2
2
  import { UtilFunc } from "./UtilFunctions";
3
3
  export declare const outcome: typeof UtilFunc.outcome, matchProc: typeof UtilFunc.matchProc, sucesProc: typeof UtilFunc.sucesProc, isFailed: typeof UtilFunc.isFailed, isSafeNumber: typeof UtilFunc.isSafeNumber, isSuccess: typeof UtilFunc.isSuccess, likeNone: typeof UtilFunc.likeNone, expect: typeof UtilFunc.expect, assertType: typeof UtilFunc.assertType, assertLiteral: typeof UtilFunc.assertLiteral, deepClone: typeof UtilFunc.deepClone, sleep: typeof UtilFunc.sleep, stringifyJToken: typeof UtilFunc.stringifyJToken, getTime: typeof UtilFunc.getTime, mapEntries: typeof UtilFunc.mapEntries, dedent: typeof UtilFunc.dedent, throwError: typeof UtilFunc.throwError;
4
- export declare const stylizePath: typeof UtilFT.stylizePath, posixizePath: typeof UtilFT.posixizePath, win32izePath: typeof UtilFT.win32izePath;
4
+ export declare const stylizePath: typeof UtilFT.stylizePath, posixizePath: typeof UtilFT.posixizePath, win32izePath: typeof UtilFT.win32izePath, currosizePath: typeof UtilFT.currosizePath;
@@ -1,11 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.win32izePath = exports.posixizePath = exports.stylizePath = exports.throwError = exports.dedent = exports.mapEntries = exports.getTime = exports.stringifyJToken = exports.sleep = exports.deepClone = exports.assertLiteral = exports.assertType = exports.expect = exports.likeNone = exports.isSuccess = exports.isSafeNumber = exports.isFailed = exports.sucesProc = exports.matchProc = exports.outcome = void 0;
3
+ exports.currosizePath = exports.win32izePath = exports.posixizePath = exports.stylizePath = exports.throwError = exports.dedent = exports.mapEntries = exports.getTime = exports.stringifyJToken = exports.sleep = exports.deepClone = exports.assertLiteral = exports.assertType = exports.expect = exports.likeNone = exports.isSuccess = exports.isSafeNumber = exports.isFailed = exports.sucesProc = exports.matchProc = exports.outcome = void 0;
4
4
  const UtilFileTools_1 = require("./UtilFileTools");
5
5
  const UtilFunctions_1 = require("./UtilFunctions");
6
6
  const UtilSymbol_1 = require("./UtilSymbol");
7
7
  exports.outcome = UtilFunctions_1.UtilFunc.outcome, exports.matchProc = UtilFunctions_1.UtilFunc.matchProc, exports.sucesProc = UtilFunctions_1.UtilFunc.sucesProc, exports.isFailed = UtilFunctions_1.UtilFunc.isFailed, exports.isSafeNumber = UtilFunctions_1.UtilFunc.isSafeNumber, exports.isSuccess = UtilFunctions_1.UtilFunc.isSuccess, exports.likeNone = UtilFunctions_1.UtilFunc.likeNone, exports.expect = UtilFunctions_1.UtilFunc.expect, exports.assertType = UtilFunctions_1.UtilFunc.assertType, exports.assertLiteral = UtilFunctions_1.UtilFunc.assertLiteral, exports.deepClone = UtilFunctions_1.UtilFunc.deepClone, exports.sleep = UtilFunctions_1.UtilFunc.sleep, exports.stringifyJToken = UtilFunctions_1.UtilFunc.stringifyJToken, exports.getTime = UtilFunctions_1.UtilFunc.getTime, exports.mapEntries = UtilFunctions_1.UtilFunc.mapEntries, exports.dedent = UtilFunctions_1.UtilFunc.dedent, exports.throwError = UtilFunctions_1.UtilFunc.throwError;
8
- exports.stylizePath = UtilFileTools_1.UtilFT.stylizePath, exports.posixizePath = UtilFileTools_1.UtilFT.posixizePath, exports.win32izePath = UtilFileTools_1.UtilFT.win32izePath;
8
+ exports.stylizePath = UtilFileTools_1.UtilFT.stylizePath, exports.posixizePath = UtilFileTools_1.UtilFT.posixizePath, exports.win32izePath = UtilFileTools_1.UtilFT.win32izePath, exports.currosizePath = UtilFileTools_1.UtilFT.currosizePath;
9
9
  if (false) {
10
10
  let aaser = (0, exports.assertLiteral)({ a: 1 });
11
11
  let a = null;
@@ -28,7 +28,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
28
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
29
  exports.SFfmpegTool = void 0;
30
30
  const fluent_ffmpeg_1 = __importDefault(require("fluent-ffmpeg"));
31
- const path = __importStar(require("path"));
31
+ const pathe_1 = __importDefault(require("pathe"));
32
32
  const fs = __importStar(require("fs"));
33
33
  const UtilLogger_1 = require("./UtilLogger");
34
34
  const UtilClass_1 = require("./UtilClass");
@@ -39,7 +39,7 @@ class SFfmpegTool {
39
39
  static init() {
40
40
  const ffmpegPath = process.env.FFMPEG_PATH;
41
41
  if (ffmpegPath != null) {
42
- const exepath = path.join(ffmpegPath, "ffmpeg.exe");
42
+ const exepath = pathe_1.default.join(ffmpegPath, "ffmpeg.exe");
43
43
  SFfmpegTool.setFfmpegPath(exepath);
44
44
  }
45
45
  }
@@ -70,7 +70,7 @@ class SFfmpegTool {
70
70
  * @param quality - 质量
71
71
  */
72
72
  static async flac2ogg(inputFlacFile, outputOggPath, quality = 10) {
73
- let wavPath = path.join(path.dirname(inputFlacFile), `tmp_${path.basename(inputFlacFile, ".flac")}.wav`);
73
+ let wavPath = pathe_1.default.join(pathe_1.default.dirname(inputFlacFile), `tmp_${pathe_1.default.basename(inputFlacFile, ".flac")}.wav`);
74
74
  await new Promise((resolve, reject) => {
75
75
  (0, fluent_ffmpeg_1.default)(inputFlacFile)
76
76
  .audioCodec("pcm_s16le")
@@ -17,15 +17,11 @@ type EnsurePathExistsOpt = Partial<{
17
17
  type FileSearchGlobOpt = Partial<{
18
18
  /**忽略的文件 默认 undefined */
19
19
  ingore: string | string[];
20
- /**输出的路径风格 默认跟随系统 */
21
- style: "posix" | "win32";
22
20
  }>;
23
21
  /**regex搜索选项 */
24
22
  type FileSearchRegexOpt = Partial<{
25
23
  /**搜索子目录 默认 true */
26
24
  relative: boolean;
27
- /**输出的路径风格 默认跟随系统 */
28
- style: "posix" | "win32";
29
25
  }>;
30
26
  /**文件工具 */
31
27
  export declare namespace UtilFT {
@@ -117,12 +113,16 @@ export declare namespace UtilFT {
117
113
  * @returns 新的符合Win32风格的路径
118
114
  */
119
115
  function win32izePath(filePath: string): string;
116
+ /**保证路径为当前系统风格
117
+ * @param filePath - 输入路径
118
+ * @returns 新的符合当前系统风格的路径
119
+ */
120
+ function currosizePath(filePath: string): string;
120
121
  /**搜索路径符合正则表达式的文件
121
122
  * @param dir - 起始目录
122
123
  * @param traitRegex - 正则表达式
123
124
  * @param opt - 可选参数
124
125
  * @param opt.relative - 搜索子目录
125
- * @param opt.style - 输出的路径风格 默认跟随系统
126
126
  * @returns 文件名路径数组
127
127
  */
128
128
  function fileSearchRegex(dir: string, traitRegex: string, opt?: FileSearchRegexOpt): string[];
@@ -130,8 +130,7 @@ export declare namespace UtilFT {
130
130
  * @param dir - 起始目录
131
131
  * @param globPattern - glob匹配
132
132
  * @param opt - 可选参数
133
- * @param opt.ignore - 忽略的文件
134
- * @param opt.style - 输出的路径风格 默认跟随系统
133
+ * @param opt.ignore - 忽略的文件
135
134
  * @returns 文件绝对路径数组
136
135
  */
137
136
  function fileSearchGlob(dir: string, globPattern: string | string[], opt?: FileSearchGlobOpt): string[];
@@ -22,10 +22,14 @@ var __importStar = (this && this.__importStar) || function (mod) {
22
22
  __setModuleDefault(result, mod);
23
23
  return result;
24
24
  };
25
+ var __importDefault = (this && this.__importDefault) || function (mod) {
26
+ return (mod && mod.__esModule) ? mod : { "default": mod };
27
+ };
25
28
  Object.defineProperty(exports, "__esModule", { value: true });
26
29
  exports.UtilFT = void 0;
27
30
  const fs = __importStar(require("fs"));
28
- const path = __importStar(require("path"));
31
+ const pathe_1 = __importDefault(require("pathe"));
32
+ const os = __importStar(require("os"));
29
33
  const UtilLogger_1 = require("./UtilLogger");
30
34
  const JSON5 = __importStar(require("json5"));
31
35
  const glob_1 = require("glob");
@@ -72,13 +76,13 @@ var UtilFT;
72
76
  */
73
77
  async function createPath(filePath, opt) {
74
78
  if (opt?.dir == true)
75
- filePath = path.join(filePath, path.sep);
79
+ filePath = pathe_1.default.join(filePath, pathe_1.default.sep);
76
80
  try {
77
- if (filePath.endsWith(path.sep)) {
81
+ if (filePath.endsWith(pathe_1.default.sep)) {
78
82
  await fs.promises.mkdir(filePath, { recursive: true });
79
83
  return true;
80
84
  }
81
- await fs.promises.mkdir(path.dirname(filePath), { recursive: true });
85
+ await fs.promises.mkdir(pathe_1.default.dirname(filePath), { recursive: true });
82
86
  const filehandle = await fs.promises.open(filePath, 'w');
83
87
  await filehandle.close();
84
88
  return true;
@@ -97,13 +101,13 @@ var UtilFT;
97
101
  */
98
102
  function createPathSync(filePath, opt) {
99
103
  if (opt?.dir == true)
100
- filePath = path.join(filePath, path.sep);
104
+ filePath = pathe_1.default.join(filePath, pathe_1.default.sep);
101
105
  try {
102
- if (filePath.endsWith(path.sep)) {
106
+ if (filePath.endsWith(pathe_1.default.sep)) {
103
107
  fs.mkdirSync(filePath, { recursive: true });
104
108
  return true;
105
109
  }
106
- fs.mkdirSync(path.dirname(filePath), { recursive: true });
110
+ fs.mkdirSync(pathe_1.default.dirname(filePath), { recursive: true });
107
111
  fs.openSync(filePath, 'w');
108
112
  return true;
109
113
  }
@@ -139,7 +143,7 @@ var UtilFT;
139
143
  }
140
144
  UtilFT.ensurePathExistsSync = ensurePathExistsSync;
141
145
  function loadJSONFileSync(filePath, def) {
142
- if (path.extname(filePath) !== '.json')
146
+ if (pathe_1.default.extname(filePath) !== '.json')
143
147
  filePath += '.json';
144
148
  let str = "";
145
149
  // 判断文件路径是否存在
@@ -155,7 +159,7 @@ var UtilFT;
155
159
  }
156
160
  UtilFT.loadJSONFileSync = loadJSONFileSync;
157
161
  async function loadJSONFile(filePath, def) {
158
- if (path.extname(filePath) !== '.json')
162
+ if (pathe_1.default.extname(filePath) !== '.json')
159
163
  filePath += '.json';
160
164
  let str = "";
161
165
  // 判断文件路径是否存在
@@ -178,7 +182,7 @@ var UtilFT;
178
182
  */
179
183
  async function writeJSONFile(filePath, token) {
180
184
  let str = UtilFunctions_1.UtilFunc.stringifyJToken(token);
181
- if (path.extname(filePath) !== '.json')
185
+ if (pathe_1.default.extname(filePath) !== '.json')
182
186
  filePath += '.json';
183
187
  // 判断文件路径是否存在 不存在则创建
184
188
  if (!(await pathExists(filePath)))
@@ -223,54 +227,57 @@ var UtilFT;
223
227
  return stylizePath(filePath, 'win32');
224
228
  }
225
229
  UtilFT.win32izePath = win32izePath;
230
+ /**保证路径为当前系统风格
231
+ * @param filePath - 输入路径
232
+ * @returns 新的符合当前系统风格的路径
233
+ */
234
+ function currosizePath(filePath) {
235
+ const system = os.platform();
236
+ if (system === 'win32')
237
+ return win32izePath(filePath);
238
+ return posixizePath(filePath);
239
+ }
240
+ UtilFT.currosizePath = currosizePath;
226
241
  /**搜索路径符合正则表达式的文件
227
242
  * @param dir - 起始目录
228
243
  * @param traitRegex - 正则表达式
229
244
  * @param opt - 可选参数
230
245
  * @param opt.relative - 搜索子目录
231
- * @param opt.style - 输出的路径风格 默认跟随系统
232
246
  * @returns 文件名路径数组
233
247
  */
234
248
  function fileSearchRegex(dir, traitRegex, opt) {
235
- dir = stylizePath(dir, opt?.style);
236
- const fixpath = opt?.style === undefined ? path :
237
- opt.style === 'posix' ? path.posix : path.win32;
238
249
  const relative = opt?.relative ?? true;
239
250
  const outArray = [];
240
251
  const subFiles = fs.readdirSync(dir, { withFileTypes: true });
241
252
  //如果使用 g 会导致 RegExp.lastIndex 更改, 将会导致匹配错误
242
253
  const regex = new RegExp(traitRegex);
243
254
  for (const subFile of subFiles) {
244
- const subFilePath = fixpath.join(dir, subFile.name);
255
+ const subFilePath = pathe_1.default.join(dir, subFile.name);
245
256
  //判断是否是文件夹,递归调用
246
257
  if (subFile.isDirectory()) {
247
258
  if (relative)
248
- outArray.push(...fileSearchRegex(fixpath.join(subFilePath, fixpath.sep), traitRegex));
259
+ outArray.push(...fileSearchRegex(pathe_1.default.join(subFilePath, pathe_1.default.sep), traitRegex));
249
260
  continue;
250
261
  }
251
262
  if (regex.test(subFilePath))
252
263
  outArray.push(subFilePath);
253
264
  }
254
- return outArray.map((filePath) => fixpath.normalize(stylizePath(filePath, opt?.style)));
265
+ return outArray.map((filePath) => pathe_1.default.normalize(filePath));
255
266
  }
256
267
  UtilFT.fileSearchRegex = fileSearchRegex;
257
268
  /**搜索符合Glob匹配的文件
258
269
  * @param dir - 起始目录
259
270
  * @param globPattern - glob匹配
260
271
  * @param opt - 可选参数
261
- * @param opt.ignore - 忽略的文件
262
- * @param opt.style - 输出的路径风格 默认跟随系统
272
+ * @param opt.ignore - 忽略的文件
263
273
  * @returns 文件绝对路径数组
264
274
  */
265
275
  function fileSearchGlob(dir, globPattern, opt) {
266
- dir = stylizePath(dir, opt?.style);
267
- const fixpath = opt?.style === undefined ? path :
268
- opt.style === 'posix' ? path.posix : path.win32;
269
276
  const fixedPath = typeof globPattern === "string"
270
- ? fixpath.join(dir, globPattern)
271
- : globPattern.map((p) => fixpath.join(dir, p));
277
+ ? pathe_1.default.join(dir, globPattern)
278
+ : globPattern.map((p) => pathe_1.default.join(dir, p));
272
279
  return (0, glob_1.globSync)(fixedPath, { ignore: opt?.ingore, absolute: true })
273
- .map((filePath) => fixpath.normalize(stylizePath(filePath, opt?.style)));
280
+ .map((filePath) => pathe_1.default.normalize(filePath));
274
281
  }
275
282
  UtilFT.fileSearchGlob = fileSearchGlob;
276
283
  /**
@@ -27,7 +27,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
27
27
  };
28
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
29
  exports.SLogger = void 0;
30
- const path = __importStar(require("path"));
30
+ const pathe_1 = __importDefault(require("pathe"));
31
31
  const winston = __importStar(require("winston"));
32
32
  const winston_daily_rotate_file_1 = __importDefault(require("winston-daily-rotate-file"));
33
33
  const util_1 = require("util");
@@ -83,7 +83,7 @@ class SLogger {
83
83
  return `[${info.timestamp}] [${level.toUpperCase()}]: ${messageList.join("\n")}`;
84
84
  }));
85
85
  transports.push(new winston_daily_rotate_file_1.default({
86
- filename: path.join(outFloder, 'log-%DATE%.txt'),
86
+ filename: pathe_1.default.join(outFloder, 'log-%DATE%.txt'),
87
87
  datePattern: 'YYYY-MM-DD',
88
88
  level: fileLevel,
89
89
  format: fileFormat,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zwa73/utils",
3
- "version": "1.0.119",
3
+ "version": "1.0.121",
4
4
  "description": "my utils",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -25,6 +25,7 @@
25
25
  "http-proxy-agent": "^5.0.0",
26
26
  "https-proxy-agent": "^5.0.1",
27
27
  "json5": "^2.2.3",
28
+ "pathe": "^1.1.2",
28
29
  "tiktoken": "^1.0.7",
29
30
  "winston": "^3.10.0",
30
31
  "winston-daily-rotate-file": "^4.7.1"
@@ -24,9 +24,10 @@ export const {
24
24
  } = UtilFunc;
25
25
 
26
26
  export const {
27
- stylizePath ,
28
- posixizePath,
29
- win32izePath,
27
+ stylizePath ,
28
+ posixizePath ,
29
+ win32izePath ,
30
+ currosizePath,
30
31
  } = UtilFT;
31
32
 
32
33
  if(false){
@@ -1,6 +1,6 @@
1
1
  import { FfprobeData } from "fluent-ffmpeg";
2
2
  import fluentFfmpeg from "fluent-ffmpeg";
3
- import * as path from "path";
3
+ import path from "pathe";
4
4
  import * as fs from "fs";
5
5
  import { SLogger } from "@src/UtilLogger";
6
6
  import { Stream } from "./UtilClass";
@@ -1,5 +1,6 @@
1
1
  import * as fs from "fs";
2
- import * as path from "path";
2
+ import path from 'pathe';
3
+ import * as os from "os";
3
4
  import { JObject, JToken } from "@src/UtilInterfaces";
4
5
  import { SLogger } from "@src/UtilLogger";
5
6
  import * as JSON5 from 'json5';
@@ -26,16 +27,12 @@ type EnsurePathExistsOpt = Partial<{
26
27
  type FileSearchGlobOpt = Partial<{
27
28
  /**忽略的文件 默认 undefined */
28
29
  ingore:string|string[];
29
- /**输出的路径风格 默认跟随系统 */
30
- style:"posix"|"win32";
31
30
  }>
32
31
 
33
32
  /**regex搜索选项 */
34
33
  type FileSearchRegexOpt = Partial<{
35
34
  /**搜索子目录 默认 true */
36
35
  relative:boolean;
37
- /**输出的路径风格 默认跟随系统 */
38
- style:"posix"|"win32";
39
36
  }>
40
37
 
41
38
  /**文件工具 */
@@ -258,6 +255,18 @@ export function win32izePath(filePath: string): string {
258
255
  return stylizePath(filePath, 'win32');
259
256
  }
260
257
 
258
+ /**保证路径为当前系统风格
259
+ * @param filePath - 输入路径
260
+ * @returns 新的符合当前系统风格的路径
261
+ */
262
+ export function currosizePath(filePath: string): string {
263
+ const system = os.platform();
264
+ if (system === 'win32')
265
+ return win32izePath(filePath);
266
+ return posixizePath(filePath);
267
+ }
268
+
269
+
261
270
 
262
271
 
263
272
  /**搜索路径符合正则表达式的文件
@@ -265,49 +274,39 @@ export function win32izePath(filePath: string): string {
265
274
  * @param traitRegex - 正则表达式
266
275
  * @param opt - 可选参数
267
276
  * @param opt.relative - 搜索子目录
268
- * @param opt.style - 输出的路径风格 默认跟随系统
269
277
  * @returns 文件名路径数组
270
278
  */
271
279
  export function fileSearchRegex(dir: string, traitRegex: string, opt?:FileSearchRegexOpt) {
272
- dir = stylizePath(dir,opt?.style);
273
- const fixpath = opt?.style === undefined ? path :
274
- opt.style ==='posix' ? path.posix : path.win32;
275
-
276
280
  const relative = opt?.relative ?? true;
277
281
  const outArray: string[] = [];
278
282
  const subFiles = fs.readdirSync(dir, { withFileTypes: true });
279
283
  //如果使用 g 会导致 RegExp.lastIndex 更改, 将会导致匹配错误
280
284
  const regex = new RegExp(traitRegex);
281
285
  for (const subFile of subFiles) {
282
- const subFilePath = fixpath.join(dir, subFile.name);
286
+ const subFilePath = path.join(dir, subFile.name);
283
287
  //判断是否是文件夹,递归调用
284
288
  if (subFile.isDirectory()) {
285
289
  if (relative)
286
- outArray.push(...fileSearchRegex(fixpath.join(subFilePath, fixpath.sep), traitRegex));
290
+ outArray.push(...fileSearchRegex(path.join(subFilePath, path.sep), traitRegex));
287
291
  continue;
288
292
  }
289
293
  if (regex.test(subFilePath)) outArray.push(subFilePath);
290
294
  }
291
- return outArray.map((filePath) => fixpath.normalize(stylizePath(filePath,opt?.style)));
295
+ return outArray.map((filePath) => path.normalize(filePath));
292
296
  }
293
297
  /**搜索符合Glob匹配的文件
294
298
  * @param dir - 起始目录
295
299
  * @param globPattern - glob匹配
296
300
  * @param opt - 可选参数
297
- * @param opt.ignore - 忽略的文件
298
- * @param opt.style - 输出的路径风格 默认跟随系统
301
+ * @param opt.ignore - 忽略的文件
299
302
  * @returns 文件绝对路径数组
300
303
  */
301
304
  export function fileSearchGlob(dir: string, globPattern:string|string[],opt?:FileSearchGlobOpt){
302
- dir = stylizePath(dir,opt?.style);
303
- const fixpath = opt?.style === undefined ? path :
304
- opt.style ==='posix' ? path.posix : path.win32;
305
-
306
305
  const fixedPath = typeof globPattern === "string"
307
- ? fixpath.join(dir,globPattern)
308
- : globPattern.map((p)=>fixpath.join(dir,p));
306
+ ? path.join(dir,globPattern)
307
+ : globPattern.map((p)=>path.join(dir,p));
309
308
  return globSync(fixedPath, { ignore: opt?.ingore, absolute: true })
310
- .map((filePath) => fixpath.normalize(stylizePath(filePath,opt?.style)));
309
+ .map((filePath) => path.normalize(filePath));
311
310
  }
312
311
  /**
313
312
  * @deprecated 请使用 fileSearchRegex 或 fileSearchGlob
package/src/UtilLogger.ts CHANGED
@@ -1,4 +1,4 @@
1
- import * as path from 'path';
1
+ import path from 'pathe';
2
2
  import * as winston from 'winston';
3
3
  import DailyRotateFile from 'winston-daily-rotate-file';
4
4
  import {inspect} from 'util';