@zwa73/utils 1.0.121 → 1.0.122

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.
@@ -134,10 +134,6 @@ export declare namespace UtilFT {
134
134
  * @returns 文件绝对路径数组
135
135
  */
136
136
  function fileSearchGlob(dir: string, globPattern: string | string[], opt?: FileSearchGlobOpt): string[];
137
- /**
138
- * @deprecated 请使用 fileSearchRegex 或 fileSearchGlob
139
- */
140
- function fileSearch(...patams: any[]): void;
141
137
  /**是一个有效的文件路径
142
138
  * @param filePath - 需要验证的文件路径
143
139
  */
@@ -34,7 +34,6 @@ const UtilLogger_1 = require("./UtilLogger");
34
34
  const JSON5 = __importStar(require("json5"));
35
35
  const glob_1 = require("glob");
36
36
  const UtilFunctions_1 = require("./UtilFunctions");
37
- const QuickFunction_1 = require("./QuickFunction");
38
37
  /**文件工具 */
39
38
  var UtilFT;
40
39
  (function (UtilFT) {
@@ -181,7 +180,7 @@ var UtilFT;
181
180
  * @param token - 所要写入的JToken
182
181
  */
183
182
  async function writeJSONFile(filePath, token) {
184
- let str = UtilFunctions_1.UtilFunc.stringifyJToken(token);
183
+ const str = UtilFunctions_1.UtilFunc.stringifyJToken(token);
185
184
  if (pathe_1.default.extname(filePath) !== '.json')
186
185
  filePath += '.json';
187
186
  // 判断文件路径是否存在 不存在则创建
@@ -205,7 +204,7 @@ var UtilFT;
205
204
  function stylizePath(filePath, style) {
206
205
  if (style === undefined)
207
206
  return filePath;
208
- return (0, QuickFunction_1.matchProc)(style, {
207
+ return UtilFunctions_1.UtilFunc.matchProc(style, {
209
208
  posix: (nor) => filePath.replaceAll("\\", "/"),
210
209
  win32: (nor) => filePath.replaceAll("/", "\\"),
211
210
  });
@@ -280,13 +279,6 @@ var UtilFT;
280
279
  .map((filePath) => pathe_1.default.normalize(filePath));
281
280
  }
282
281
  UtilFT.fileSearchGlob = fileSearchGlob;
283
- /**
284
- * @deprecated 请使用 fileSearchRegex 或 fileSearchGlob
285
- */
286
- function fileSearch(...patams) {
287
- UtilFunctions_1.UtilFunc.throwError("请使用 fileSearchRegex 或 fileSearchGlob", 'fatal');
288
- }
289
- UtilFT.fileSearch = fileSearch;
290
282
  /**是一个有效的文件路径
291
283
  * @param filePath - 需要验证的文件路径
292
284
  */
@@ -55,6 +55,8 @@ export declare class UtilFunc {
55
55
  * @returns
56
56
  */
57
57
  static sleep<T>(timeMs: number, result: T): Promise<T>;
58
+ /**是否需要检查环境 */
59
+ static checkEnv: boolean;
58
60
  /**封装的 cp.spawn 执行一段指令,指令运行时实时返回输出
59
61
  * @param command - 指令文本
60
62
  * @param opt - 可选参数
@@ -219,5 +221,9 @@ export declare class UtilFunc {
219
221
  filePath: string;
220
222
  lineNumber: `${number}:${number}`;
221
223
  } | undefined;
224
+ /**获取当前公网ipv4
225
+ * @returns undefined 时为错误
226
+ */
227
+ static getPublicIpv4(): Promise<string | undefined>;
222
228
  }
223
229
  export {};
@@ -31,6 +31,9 @@ var __importStar = (this && this.__importStar) || function (mod) {
31
31
  var __metadata = (this && this.__metadata) || function (k, v) {
32
32
  if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
33
33
  };
34
+ var __importDefault = (this && this.__importDefault) || function (mod) {
35
+ return (mod && mod.__esModule) ? mod : { "default": mod };
36
+ };
34
37
  Object.defineProperty(exports, "__esModule", { value: true });
35
38
  exports.UtilFunc = void 0;
36
39
  const crypto = __importStar(require("crypto"));
@@ -38,6 +41,8 @@ const cp = __importStar(require("child_process"));
38
41
  const UtilLogger_1 = require("./UtilLogger");
39
42
  const UtilSymbol_1 = require("./UtilSymbol");
40
43
  const UtilDecorators_1 = require("./UtilDecorators");
44
+ const path_1 = __importDefault(require("path"));
45
+ const UtilFileTools_1 = require("./UtilFileTools");
41
46
  const HashMethodList = ["md5", "sha1", "sha256", "sha512", "sha3", "blake2", "blake3"];
42
47
  /**永不完成的Promise单例 */
43
48
  const NeverResolvedPromise = new Promise(() => { });
@@ -103,6 +108,8 @@ class UtilFunc {
103
108
  }, timeMs);
104
109
  });
105
110
  }
111
+ /**是否需要检查环境 */
112
+ static checkEnv = true;
106
113
  /**封装的 cp.spawn 执行一段指令,指令运行时实时返回输出
107
114
  * @param command - 指令文本
108
115
  * @param opt - 可选参数
@@ -110,8 +117,16 @@ class UtilFunc {
110
117
  * @param opt.errlvl - 错误的日志等级
111
118
  */
112
119
  static exec(command, opt) {
113
- return new Promise((resolve, reject) => {
114
- const child = cp.spawn(command, { shell: true });
120
+ return new Promise(async (resolve, reject) => {
121
+ // 创建一个新的环境变量对象,并将项目的 node_modules/.bin 目录添加到 PATH 环境变量中
122
+ const env = Object.create(process.env);
123
+ const penv = path_1.default.join(process.cwd(), 'node_modules', '.bin');
124
+ if (UtilFunc.checkEnv && !await UtilFileTools_1.UtilFT.pathExists(penv)) {
125
+ UtilLogger_1.SLogger.warn('UtilFunc.exec 错误, 没有检测到 process.cwd()/node_modules/.bin 环境');
126
+ UtilFunc.checkEnv = false;
127
+ }
128
+ env.PATH = penv + path_1.default.delimiter + env.PATH;
129
+ const child = cp.spawn(command, { shell: true, env });
115
130
  let stdout = '';
116
131
  let stderr = '';
117
132
  child.stdout.on('data', (data) => {
@@ -589,6 +604,18 @@ class UtilFunc {
589
604
  }
590
605
  return undefined;
591
606
  }
607
+ /**获取当前公网ipv4
608
+ * @returns undefined 时为错误
609
+ */
610
+ static async getPublicIpv4() {
611
+ const { stdout } = await UtilFunc.exec('public-ip -4');
612
+ const ip = stdout.trim();
613
+ // 单独的 0-255 的正则表达式
614
+ const i255regex = /(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)/.source;
615
+ // 使用 .source 属性来拼接正则表达式
616
+ const ipv4Pattern = new RegExp(`^(${i255regex}\\.){3}${i255regex}$`);
617
+ return ipv4Pattern.test(ip) ? ip : undefined;
618
+ }
592
619
  }
593
620
  exports.UtilFunc = UtilFunc;
594
621
  __decorate([
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zwa73/utils",
3
- "version": "1.0.121",
3
+ "version": "1.0.122",
4
4
  "description": "my utils",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -26,6 +26,7 @@
26
26
  "https-proxy-agent": "^5.0.1",
27
27
  "json5": "^2.2.3",
28
28
  "pathe": "^1.1.2",
29
+ "public-ip-cli": "^3.0.0",
29
30
  "tiktoken": "^1.0.7",
30
31
  "winston": "^3.10.0",
31
32
  "winston-daily-rotate-file": "^4.7.1"
@@ -1,7 +1,7 @@
1
1
  import { UtilFT } from "./UtilFileTools";
2
2
  import { UtilFunc } from "./UtilFunctions";
3
3
  import { Outcome } from "./UtilInterfaces";
4
- import { Completed, Failed, FailedLike, None, NoneOut, StatusSymbol, Success, SuccessLike, Terminated, Timeout } from "./UtilSymbol";
4
+ import { Failed, None, NoneOut, Success } from "./UtilSymbol";
5
5
 
6
6
  export const {
7
7
  outcome,
@@ -6,7 +6,6 @@ import { SLogger } from "@src/UtilLogger";
6
6
  import * as JSON5 from 'json5';
7
7
  import { globSync } from "glob";
8
8
  import { UtilFunc } from "./UtilFunctions";
9
- import { matchProc } from "./QuickFunction";
10
9
 
11
10
  /**创建文件选项 */
12
11
  type CreatePathOpt = Partial<{
@@ -212,7 +211,7 @@ export async function writeJSONFile(
212
211
  filePath: string,
213
212
  token: JToken
214
213
  ): Promise<void> {
215
- let str = UtilFunc.stringifyJToken(token);
214
+ const str = UtilFunc.stringifyJToken(token);
216
215
  if (path.extname(filePath) !== '.json') filePath += '.json';
217
216
 
218
217
  // 判断文件路径是否存在 不存在则创建
@@ -234,7 +233,7 @@ export async function writeJSONFile(
234
233
  */
235
234
  export function stylizePath(filePath:string,style?:'win32'|'posix'){
236
235
  if(style===undefined) return filePath;
237
- return matchProc(style, {
236
+ return UtilFunc.matchProc(style, {
238
237
  posix: (nor) => filePath.replaceAll("\\", "/"),
239
238
  win32: (nor) => filePath.replaceAll("/", "\\"),
240
239
  });
@@ -308,12 +307,6 @@ export function fileSearchGlob(dir: string, globPattern:string|string[],opt?:Fil
308
307
  return globSync(fixedPath, { ignore: opt?.ingore, absolute: true })
309
308
  .map((filePath) => path.normalize(filePath));
310
309
  }
311
- /**
312
- * @deprecated 请使用 fileSearchRegex 或 fileSearchGlob
313
- */
314
- export function fileSearch(...patams:any[]){
315
- UtilFunc.throwError("请使用 fileSearchRegex 或 fileSearchGlob",'fatal');
316
- }
317
310
  /**是一个有效的文件路径
318
311
  * @param filePath - 需要验证的文件路径
319
312
  */
@@ -4,6 +4,8 @@ import * as cp from "child_process";
4
4
  import { LogLevel, SLogger } from "@src/UtilLogger";
5
5
  import { Completed, Failed, FailedLike, None, StatusSymbol, Success, SuccessLike, Terminated, Timeout } from "./UtilSymbol";
6
6
  import { Catch, DeferAsync, LogTimeAsync } from "./UtilDecorators";
7
+ import opath from 'path';
8
+ import { UtilFT } from "./UtilFileTools";
7
9
 
8
10
  const HashMethodList = ["md5","sha1","sha256","sha512","sha3","blake2","blake3"] as const;
9
11
  type HashMethod = typeof HashMethodList[number];
@@ -111,6 +113,8 @@ static async sleep<T>(timeMs: number, result?:T): Promise<T|undefined> {
111
113
  });
112
114
  }
113
115
 
116
+ /**是否需要检查环境 */
117
+ static checkEnv = true;
114
118
  /**封装的 cp.spawn 执行一段指令,指令运行时实时返回输出
115
119
  * @param command - 指令文本
116
120
  * @param opt - 可选参数
@@ -118,8 +122,19 @@ static async sleep<T>(timeMs: number, result?:T): Promise<T|undefined> {
118
122
  * @param opt.errlvl - 错误的日志等级
119
123
  */
120
124
  static exec(command: string, opt?:ExecOpt) {
121
- return new Promise<{ stdout: string, stderr: string }>((resolve, reject) => {
122
- const child = cp.spawn(command,{shell:true});
125
+ return new Promise<{ stdout: string, stderr: string }>(async (resolve, reject) => {
126
+ // 创建一个新的环境变量对象,并将项目的 node_modules/.bin 目录添加到 PATH 环境变量中
127
+ const env = Object.create(process.env);
128
+ const penv = opath.join(process.cwd(), 'node_modules', '.bin');
129
+
130
+ if(UtilFunc.checkEnv && ! await UtilFT.pathExists(penv)){
131
+ SLogger.warn('UtilFunc.exec 错误, 没有检测到 process.cwd()/node_modules/.bin 环境');
132
+ UtilFunc.checkEnv = false;
133
+ }
134
+
135
+ env.PATH = penv + opath.delimiter + env.PATH;
136
+
137
+ const child = cp.spawn(command, { shell: true, env });
123
138
 
124
139
  let stdout = '';
125
140
  let stderr = '';
@@ -669,4 +684,17 @@ static getFuncLoc(stack=1) {
669
684
  return undefined;
670
685
  }
671
686
 
687
+ /**获取当前公网ipv4
688
+ * @returns undefined 时为错误
689
+ */
690
+ static async getPublicIpv4(){
691
+ const {stdout} = await UtilFunc.exec('public-ip -4');
692
+ const ip = stdout.trim();
693
+ // 单独的 0-255 的正则表达式
694
+ const i255regex = /(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)/.source;
695
+ // 使用 .source 属性来拼接正则表达式
696
+ const ipv4Pattern = new RegExp(`^(${i255regex}\\.){3}${i255regex}$`);
697
+ return ipv4Pattern.test(ip) ? ip : undefined;
698
+ }
699
+
672
700
  }