@zwa73/utils 1.0.60 → 1.0.61

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 (42) hide show
  1. package/dist/UtilCodecs.d.ts +16 -16
  2. package/dist/UtilCodecs.js +16 -16
  3. package/dist/UtilCom.d.ts +22 -25
  4. package/dist/UtilCom.js +35 -38
  5. package/dist/UtilDecorators.d.ts +15 -8
  6. package/dist/UtilDecorators.js +79 -25
  7. package/dist/UtilFP.d.ts +46 -0
  8. package/dist/UtilFP.js +52 -0
  9. package/dist/UtilFfmpegTools.d.ts +30 -30
  10. package/dist/UtilFfmpegTools.js +32 -32
  11. package/dist/UtilFileTools.d.ts +30 -35
  12. package/dist/UtilFileTools.js +17 -18
  13. package/dist/UtilFunctions.d.ts +37 -78
  14. package/dist/UtilFunctions.js +27 -62
  15. package/dist/UtilInterfaces.d.ts +11 -11
  16. package/dist/UtilInterfaces.js +2 -2
  17. package/dist/UtilLogger.d.ts +55 -55
  18. package/dist/UtilLogger.js +55 -55
  19. package/dist/index.d.ts +1 -0
  20. package/dist/index.js +1 -0
  21. package/dist/test/composeTest.d.ts +21 -0
  22. package/dist/test/composeTest.js +33 -0
  23. package/dist/test/importtest.d.ts +1 -0
  24. package/dist/test/importtest.js +4 -0
  25. package/dist/test/test.js +5 -3
  26. package/package.json +1 -1
  27. package/src/UtilClass.ts +1051 -1051
  28. package/src/UtilCodecs.ts +117 -117
  29. package/src/UtilCom.ts +171 -174
  30. package/src/UtilDecorators.ts +174 -116
  31. package/src/UtilFP.ts +98 -0
  32. package/src/UtilFfmpegTools.ts +271 -271
  33. package/src/UtilFileTools.ts +231 -236
  34. package/src/UtilFunctions.ts +289 -364
  35. package/src/UtilInterfaces.ts +137 -137
  36. package/src/UtilLogger.ts +386 -386
  37. package/src/index.ts +10 -9
  38. package/src/test/composeTest.ts +37 -0
  39. package/src/test/importtest.ts +5 -0
  40. package/src/test/test.ts +8 -6
  41. package/src/test/test2.ts +2 -3
  42. package/tsconfig.json +2 -7
@@ -1,237 +1,232 @@
1
- import * as fs from "fs";
2
- import * as path from "path";
3
- import { JObject, JToken, stringifyJToken } from "./UtilInterfaces";
4
- import { SLogger } from "./UtilLogger";
5
- import * as JSON5 from 'json5';
6
- import { globSync } from "glob";
7
-
8
- /**文件工具 */
9
- export namespace UtilFT{
10
-
11
- /**验证路径 文件或文件夹 是否存在 异步
12
- * @async
13
- * @param {string} filePath - 待验证的路径
14
- * @returns {Promise<boolean>} - 是否存在
15
- */
16
- export async function pathExists(filePath: string):Promise<boolean>{
17
- try {
18
- const stats = await fs.promises.stat(filePath);
19
- await fs.promises.access(filePath);
20
- return true;
21
- } catch (e) {
22
- return false;
23
- }
24
- }
25
-
26
- /**验证路径 文件或文件夹 是否存在 同步
27
- * @param {string} filePath - 待验证的路径
28
- * @returns {boolean} - 是否存在
29
- */
30
- export function pathExistsSync(filePath: string):boolean{
31
- try {
32
- fs.accessSync(filePath);
33
- return true;
34
- } catch (e) {
35
- return false;
36
- }
37
- }
38
-
39
- /**路径不存在时创建路径 以path.sep结尾时创建文件夹 异步
40
- * @async
41
- * @param {string} filePath - 待创建的路径
42
- * @param {boolean} isDir - 强制创建一个文件夹
43
- * @returns {Promise<boolean>} - 是否成功创建
44
- */
45
- export async function createPath(filePath: string, isDir?:boolean):Promise<boolean>{
46
- if(isDir==true)
47
- filePath = path.join(filePath,path.sep);
48
-
49
- try{
50
- if(filePath.endsWith(path.sep)){
51
- await fs.promises.mkdir(filePath, {recursive: true});
52
- return true;
53
- }
54
- await fs.promises.mkdir(path.dirname(filePath), {recursive: true});
55
- const filehandle = await fs.promises.open(filePath, 'w');
56
- await filehandle.close();
57
- return true;
58
- }
59
- catch(e){
60
- SLogger.error("createPath 错误",e);
61
- return false;
62
- }
63
- }
64
-
65
- /**路径不存在时创建路径 以path.sep结尾时创建文件夹 同步
66
- * @param {string} filePath - 待创建的路径
67
- * @param {boolean} isDir - 强制验证一个文件夹
68
- * @returns {boolean} - 是否成功创建
69
- */
70
- export function createPathSync(filePath: string, isDir?:boolean):boolean{
71
- if(isDir==true)
72
- filePath = path.join(filePath,path.sep);
73
-
74
- try{
75
- if(filePath.endsWith(path.sep)){
76
- fs.mkdirSync(filePath, {recursive: true});
77
- return true;
78
- }
79
- fs.mkdirSync(path.dirname(filePath), {recursive: true});
80
- fs.openSync(filePath, 'w');
81
- return true;
82
- }
83
- catch(e){
84
- SLogger.error("createPathSync 错误",e);
85
- return false;
86
- }
87
- }
88
-
89
- /**确保路径存在 不存在时创建 异步
90
- * @async
91
- * @param {string} filePath - 待验证的路径
92
- * @param {boolean} isDir - 强制验证一个文件夹
93
- * @returns {Promise<boolean>} - 是否成功执行 创建或已存在
94
- */
95
- export async function ensurePathExists(filePath: string, isDir?:boolean):Promise<boolean>{
96
- if(await pathExists(filePath))
97
- return true;
98
- return await createPath(filePath,isDir);
99
- }
100
-
101
- /**确保路径存在 不存在时创建 同步
102
- * @param {string} filePath - 待验证的路径
103
- * @returns {boolean} - 是否成功执行 创建或已存在
104
- */
105
- export function ensurePathExistsSync(filePath: string, isDir?:boolean):boolean{
106
- if(pathExistsSync(filePath))
107
- return true;
108
- return createPathSync(filePath,isDir);
109
- }
110
-
111
- /**加载json文件 同步
112
- * Object (string)
113
- * @param {string} filePath - 文件路径
114
- * @returns {JObject} - 加载完成的对象或空{}
115
- */
116
- export function loadJSONFileSync(filePath: string): JObject
117
- /**加载json文件 同步
118
- * Object (string)
119
- * @param {string} filePath - 文件路径
120
- * @param {T} def - 默认值
121
- * @returns {T} - 加载完成的对象或默认值
122
- */
123
- export function loadJSONFileSync<T extends JToken>(filePath: string,def: T): T
124
- export function loadJSONFileSync<T extends JToken>(filePath: string,def?: T): T {
125
- if (path.extname(filePath) !== '.json') filePath += '.json';
126
-
127
- let str = "";
128
-
129
- // 判断文件路径是否存在
130
- if(pathExistsSync(filePath))
131
- str = fs.readFileSync(filePath, "utf-8");
132
-
133
- // 如果不存在则返回默认值
134
- if (str == "" || str == null){
135
- if(def!==undefined)
136
- return def;
137
- return {} as T;
138
- }
139
- return JSON5.parse(str);
140
- }
141
-
142
- /**加载json文件 异步
143
- * Object (string)
144
- * @async
145
- * @param {string} filePath - 文件路径
146
- * @returns {Promise<JObject>} - 加载完成的对象或空{}
147
- */
148
- export async function loadJSONFile(filePath: string): Promise<JObject>
149
- /**加载json文件 异步
150
- * Object (string)
151
- * @async
152
- * @param {string} filePath - 文件路径
153
- * @param {T} T - 默认值
154
- * @returns {Promise<T>} - 加载完成的对象或默认值
155
- */
156
- export async function loadJSONFile<T extends JToken>(filePath: string,def: T): Promise<T>
157
- export async function loadJSONFile<T extends JToken>(filePath: string,def?: T): Promise<T> {
158
- if (path.extname(filePath) !== '.json') filePath += '.json';
159
-
160
- let str = "";
161
-
162
- // 判断文件路径是否存在
163
- if(await pathExists(filePath))
164
- str = await fs.promises.readFile(filePath, "utf-8");
165
-
166
- // 如果不存在则返回默认值
167
- if (str == "" || str == null){
168
- if(def!==undefined)
169
- return def;
170
- return {} as T;
171
- }
172
- return JSON5.parse(str);
173
- }
174
-
175
- /**写入JSON文件
176
- * void (string,Object)
177
- * @async
178
- * @param {string} filePath - 文件路径
179
- * @param {JToken} token - 所要写入的JToken
180
- * @returns {Promise<void>}
181
- */
182
- export async function writeJSONFile(
183
- filePath: string,
184
- token: JToken
185
- ): Promise<void> {
186
- let str = stringifyJToken(token);
187
- if (path.extname(filePath) !== '.json') filePath += '.json';
188
-
189
- // 判断文件路径是否存在 不存在则创建
190
- if(!(await pathExists(filePath)))
191
- await createPath(filePath);
192
-
193
- // 写入文件
194
- try {
195
- await fs.promises.writeFile(filePath, str);
196
- SLogger.verbose(`${filePath} writeJSONFile 成功`);
197
- } catch (err) {
198
- SLogger.error(`${filePath} writeJSONFile 错误`,err);
199
- }
200
- }
201
-
202
- /**搜索路径符合正则表达式的文件
203
- * @param folder - 文件夹路径
204
- * @param traitRegex - 正则表达式
205
- * @returns 文件名路径数组
206
- */
207
- export function fileSearchRegex(folder: string, traitRegex: string) {
208
- let outArray: string[] = [];
209
- let subFiles = fs.readdirSync(folder);
210
- let regex = new RegExp(traitRegex);
211
- for (let subFile of subFiles) {
212
- let subFilePath = path.join(folder, subFile);
213
- let stat = fs.lstatSync(subFilePath);
214
- //判断是否是文件夹,递归调用
215
- if (stat.isDirectory()) {
216
- outArray.push(...fileSearchRegex(path.join(subFilePath, path.sep), traitRegex));
217
- continue;
218
- }
219
- if (regex.test(subFilePath)) outArray.push(subFilePath);
220
- }
221
- return outArray;
222
- }
223
- /**搜索符合Glob匹配的文件
224
- * @param globPattern - glob匹配
225
- * @param ignore - 忽略的文件
226
- * @returns 文件绝对路径数组
227
- */
228
- export function fileSearchGlob(globPattern:string|string[],ignore?:string|string[]){
229
- return globSync(globPattern,{ignore,absolute:true});
230
- }
231
- /**
232
- * @deprecated 请使用 fileSearchRegex 或 fileSearchGlob
233
- */
234
- export function fileSearch(...patams:any[]){
235
- throw "请使用 fileSearchRegex 或 fileSearchGlob"
236
- }
1
+ import * as fs from "fs";
2
+ import * as path from "path";
3
+ import { JToken, stringifyJToken } from "@src/UtilInterfaces";
4
+ import { SLogger } from "@src/UtilLogger";
5
+ import * as JSON5 from 'json5';
6
+ import { globSync } from "glob";
7
+
8
+ /**文件工具 */
9
+ export namespace UtilFT{
10
+
11
+ /**验证路径 文件或文件夹 是否存在 异步
12
+ * @async
13
+ * @param filePath - 待验证的路径
14
+ * @returns 是否存在
15
+ */
16
+ export async function pathExists(filePath: string):Promise<boolean>{
17
+ try {
18
+ const stats = await fs.promises.stat(filePath);
19
+ await fs.promises.access(filePath);
20
+ return true;
21
+ } catch (e) {
22
+ return false;
23
+ }
24
+ }
25
+
26
+ /**验证路径 文件或文件夹 是否存在 同步
27
+ * @param filePath - 待验证的路径
28
+ * @returns - 是否存在
29
+ */
30
+ export function pathExistsSync(filePath: string):boolean{
31
+ try {
32
+ fs.accessSync(filePath);
33
+ return true;
34
+ } catch (e) {
35
+ return false;
36
+ }
37
+ }
38
+
39
+ /**路径不存在时创建路径 以path.sep结尾时创建文件夹 异步
40
+ * @async
41
+ * @param filePath - 待创建的路径
42
+ * @param isDir - 强制创建一个文件夹
43
+ * @returns 是否成功创建
44
+ */
45
+ export async function createPath(filePath: string, isDir?:boolean):Promise<boolean>{
46
+ if(isDir==true)
47
+ filePath = path.join(filePath,path.sep);
48
+
49
+ try{
50
+ if(filePath.endsWith(path.sep)){
51
+ await fs.promises.mkdir(filePath, {recursive: true});
52
+ return true;
53
+ }
54
+ await fs.promises.mkdir(path.dirname(filePath), {recursive: true});
55
+ const filehandle = await fs.promises.open(filePath, 'w');
56
+ await filehandle.close();
57
+ return true;
58
+ }
59
+ catch(e){
60
+ SLogger.error("createPath 错误",e);
61
+ return false;
62
+ }
63
+ }
64
+
65
+ /**路径不存在时创建路径 以path.sep结尾时创建文件夹 同步
66
+ * @param filePath - 待创建的路径
67
+ * @param isDir - 强制验证一个文件夹
68
+ * @returns 是否成功创建
69
+ */
70
+ export function createPathSync(filePath: string, isDir?:boolean):boolean{
71
+ if(isDir==true)
72
+ filePath = path.join(filePath,path.sep);
73
+
74
+ try{
75
+ if(filePath.endsWith(path.sep)){
76
+ fs.mkdirSync(filePath, {recursive: true});
77
+ return true;
78
+ }
79
+ fs.mkdirSync(path.dirname(filePath), {recursive: true});
80
+ fs.openSync(filePath, 'w');
81
+ return true;
82
+ }
83
+ catch(e){
84
+ SLogger.error("createPathSync 错误",e);
85
+ return false;
86
+ }
87
+ }
88
+
89
+ /**确保路径存在 不存在时创建 异步
90
+ * @async
91
+ * @param filePath - 待验证的路径
92
+ * @param isDir - 强制验证一个文件夹
93
+ * @returns 是否成功执行 创建或已存在
94
+ */
95
+ export async function ensurePathExists(filePath: string, isDir?:boolean):Promise<boolean>{
96
+ if(await pathExists(filePath))
97
+ return true;
98
+ return await createPath(filePath,isDir);
99
+ }
100
+
101
+ /**确保路径存在 不存在时创建 同步
102
+ * @param filePath - 待验证的路径
103
+ * @returns 是否成功执行 创建或已存在
104
+ */
105
+ export function ensurePathExistsSync(filePath: string, isDir?:boolean):boolean{
106
+ if(pathExistsSync(filePath))
107
+ return true;
108
+ return createPathSync(filePath,isDir);
109
+ }
110
+
111
+ /**加载json文件 同步
112
+ * @param filePath - 文件路径
113
+ * @returns 加载完成的对象或空{}
114
+ */
115
+ export function loadJSONFileSync(filePath: string): JToken
116
+ /**加载json文件 同步
117
+ * @param filePath - 文件路径
118
+ * @param def - 默认值
119
+ * @returns 加载完成的对象或默认值
120
+ */
121
+ export function loadJSONFileSync<T extends JToken>(filePath: string,def: T): T
122
+ export function loadJSONFileSync<T extends JToken>(filePath: string,def?: T): T {
123
+ if (path.extname(filePath) !== '.json') filePath += '.json';
124
+
125
+ let str = "";
126
+
127
+ // 判断文件路径是否存在
128
+ if(pathExistsSync(filePath))
129
+ str = fs.readFileSync(filePath, "utf-8");
130
+
131
+ // 如果不存在则返回默认值
132
+ if (str == "" || str == null){
133
+ if(def!==undefined)
134
+ return def;
135
+ return {} as T;
136
+ }
137
+ return JSON5.parse(str);
138
+ }
139
+
140
+ /**加载json文件 异步
141
+ * @async
142
+ * @param filePath - 文件路径
143
+ * @returns 加载完成的对象或空{}
144
+ */
145
+ export async function loadJSONFile(filePath: string): Promise<JToken>
146
+ /**加载json文件 异步
147
+ * @async
148
+ * @param filePath - 文件路径
149
+ * @param def - 默认值
150
+ * @returns 加载完成的对象或默认值
151
+ */
152
+ export async function loadJSONFile<T extends JToken>(filePath: string,def: T): Promise<T>
153
+ export async function loadJSONFile<T extends JToken>(filePath: string,def?: T): Promise<T> {
154
+ if (path.extname(filePath) !== '.json') filePath += '.json';
155
+
156
+ let str = "";
157
+
158
+ // 判断文件路径是否存在
159
+ if(await pathExists(filePath))
160
+ str = await fs.promises.readFile(filePath, "utf-8");
161
+
162
+ // 如果不存在则返回默认值
163
+ if (str == "" || str == null){
164
+ if(def!==undefined)
165
+ return def;
166
+ return {} as T;
167
+ }
168
+ return JSON5.parse(str);
169
+ }
170
+
171
+ /**写入JSON文件
172
+ * void (string,Object)
173
+ * @async
174
+ * @param filePath - 文件路径
175
+ * @param token - 所要写入的JToken
176
+ */
177
+ export async function writeJSONFile(
178
+ filePath: string,
179
+ token: JToken
180
+ ): Promise<void> {
181
+ let str = stringifyJToken(token);
182
+ if (path.extname(filePath) !== '.json') filePath += '.json';
183
+
184
+ // 判断文件路径是否存在 不存在则创建
185
+ if(!(await pathExists(filePath)))
186
+ await createPath(filePath);
187
+
188
+ // 写入文件
189
+ try {
190
+ await fs.promises.writeFile(filePath, str);
191
+ SLogger.verbose(`${filePath} writeJSONFile 成功`);
192
+ } catch (err) {
193
+ SLogger.error(`${filePath} writeJSONFile 错误`,err);
194
+ }
195
+ }
196
+
197
+ /**搜索路径符合正则表达式的文件
198
+ * @param folder - 文件夹路径
199
+ * @param traitRegex - 正则表达式
200
+ * @returns 文件名路径数组
201
+ */
202
+ export function fileSearchRegex(folder: string, traitRegex: string) {
203
+ let outArray: string[] = [];
204
+ let subFiles = fs.readdirSync(folder);
205
+ let regex = new RegExp(traitRegex);
206
+ for (let subFile of subFiles) {
207
+ let subFilePath = path.join(folder, subFile);
208
+ let stat = fs.lstatSync(subFilePath);
209
+ //判断是否是文件夹,递归调用
210
+ if (stat.isDirectory()) {
211
+ outArray.push(...fileSearchRegex(path.join(subFilePath, path.sep), traitRegex));
212
+ continue;
213
+ }
214
+ if (regex.test(subFilePath)) outArray.push(subFilePath);
215
+ }
216
+ return outArray;
217
+ }
218
+ /**搜索符合Glob匹配的文件
219
+ * @param globPattern - glob匹配
220
+ * @param ignore - 忽略的文件
221
+ * @returns 文件绝对路径数组
222
+ */
223
+ export function fileSearchGlob(globPattern:string|string[],ignore?:string|string[]){
224
+ return globSync(globPattern,{ignore,absolute:true});
225
+ }
226
+ /**
227
+ * @deprecated 请使用 fileSearchRegex 或 fileSearchGlob
228
+ */
229
+ export function fileSearch(...patams:any[]){
230
+ throw "请使用 fileSearchRegex 或 fileSearchGlob"
231
+ }
237
232
  }