@zwa73/utils 1.0.202 → 1.0.203
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/dist/index.d.ts +0 -1
- package/dist/index.js +0 -1
- package/package.json +7 -4
- package/compile.bat +0 -2
- package/dist/UtilFfmpegTools.d.ts +0 -86
- package/dist/UtilFfmpegTools.js +0 -291
- package/input.wav +0 -0
- package/jest.config.js +0 -16
- package/scripts/compile.ps1 +0 -6
- package/scripts/expand-macro.ps1 +0 -2
- package/scripts/postinstall.js +0 -61
- package/scripts/release.ps1 +0 -3
- package/scripts/watch.ps1 +0 -10
- package/src/QuickExport.ts +0 -70
- package/src/UtilClass.ts +0 -259
- package/src/UtilCodecs.ts +0 -102
- package/src/UtilCom.macro.ts +0 -28
- package/src/UtilCom.ts +0 -456
- package/src/UtilCom_bak.txt +0 -345
- package/src/UtilDecorators.ts +0 -223
- package/src/UtilFP.macro.ts +0 -29
- package/src/UtilFP.ts +0 -222
- package/src/UtilFfmpegTools.ts +0 -320
- package/src/UtilFileTools.ts +0 -410
- package/src/UtilFunctions.ts +0 -938
- package/src/UtilI18n.ts +0 -278
- package/src/UtilInterfaces.ts +0 -264
- package/src/UtilLogger.ts +0 -390
- package/src/UtilSymbol.ts +0 -45
- package/src/backup.txt +0 -102
- package/src/index.ts +0 -13
- package/test.bat +0 -2
- package/watch.bat +0 -1
package/src/UtilFileTools.ts
DELETED
|
@@ -1,410 +0,0 @@
|
|
|
1
|
-
import * as fs from "fs";
|
|
2
|
-
import path from 'pathe';
|
|
3
|
-
import * as os from "os";
|
|
4
|
-
import { JObject, JToken } from "@/src/UtilInterfaces";
|
|
5
|
-
import { SLogger } from "@/src/UtilLogger";
|
|
6
|
-
import * as JSON5 from 'json5';
|
|
7
|
-
import { globSync, glob } from "glob";
|
|
8
|
-
import { UtilFunc } from "./UtilFunctions";
|
|
9
|
-
|
|
10
|
-
/**创建文件选项 */
|
|
11
|
-
type CreatePathOpt = Partial<{
|
|
12
|
-
/**创建一个目录 默认 false
|
|
13
|
-
* 默认仅在以path.sep结尾时创建文件夹
|
|
14
|
-
*/
|
|
15
|
-
dir:boolean
|
|
16
|
-
}>
|
|
17
|
-
/**验证文件选项 */
|
|
18
|
-
type EnsurePathExistsOpt = Partial<{
|
|
19
|
-
/**验证一个目录 默认 false
|
|
20
|
-
* 默认仅在以path.sep结尾时创建文件夹
|
|
21
|
-
*/
|
|
22
|
-
dir:boolean
|
|
23
|
-
}>
|
|
24
|
-
|
|
25
|
-
/**glob搜索选项 */
|
|
26
|
-
type FileSearchGlobOpt = Partial<{
|
|
27
|
-
/**忽略的文件 默认 undefined */
|
|
28
|
-
ingore:string|string[];
|
|
29
|
-
}>
|
|
30
|
-
|
|
31
|
-
/**regex搜索选项 */
|
|
32
|
-
type FileSearchRegexOpt = Partial<{
|
|
33
|
-
/**搜索子目录 默认 true */
|
|
34
|
-
relative:boolean;
|
|
35
|
-
}>
|
|
36
|
-
|
|
37
|
-
/**json文件加载选项 */
|
|
38
|
-
type LoadJsonFileOpt<T> = Partial<{
|
|
39
|
-
/**默认值 默认{} */
|
|
40
|
-
default:T;
|
|
41
|
-
/**以json5模式加载 略慢 默认false */
|
|
42
|
-
json5:boolean;
|
|
43
|
-
/**不自动修改扩展名为json */
|
|
44
|
-
forceExt:boolean;
|
|
45
|
-
}>
|
|
46
|
-
|
|
47
|
-
/**json文件写入选项 */
|
|
48
|
-
type WriteJsonFileOpt = Partial<{
|
|
49
|
-
/**使用紧凑风格 将会用/\uF121\uF122\uF123.+\uF121\uF122\uF123/作为特殊标记, 原始文本内若出现相同格式将会产生错误 */
|
|
50
|
-
compress:boolean;
|
|
51
|
-
/**紧凑风格 压缩阈值 默认100 */
|
|
52
|
-
compressThreshold:number;
|
|
53
|
-
/**不自动修改扩展名为json */
|
|
54
|
-
forceExt:boolean;
|
|
55
|
-
}>
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
/**文件工具 */
|
|
59
|
-
export namespace UtilFT{
|
|
60
|
-
|
|
61
|
-
/**验证路径 文件或文件夹 是否存在 异步
|
|
62
|
-
* @async
|
|
63
|
-
* @param filePath - 待验证的路径
|
|
64
|
-
* @returns 是否存在
|
|
65
|
-
*/
|
|
66
|
-
export async function pathExists(filePath: string):Promise<boolean>{
|
|
67
|
-
try {
|
|
68
|
-
await fs.promises.access(filePath);
|
|
69
|
-
return true;
|
|
70
|
-
} catch (e) {
|
|
71
|
-
return false;
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
/**验证路径 文件或文件夹 是否存在 同步
|
|
76
|
-
* @param filePath - 待验证的路径
|
|
77
|
-
* @returns - 是否存在
|
|
78
|
-
*/
|
|
79
|
-
export function pathExistsSync(filePath: string):boolean{
|
|
80
|
-
try {
|
|
81
|
-
fs.accessSync(filePath);
|
|
82
|
-
return true;
|
|
83
|
-
} catch (e) {
|
|
84
|
-
return false;
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
/**路径不存在时创建路径 以path.sep结尾时创建文件夹 异步
|
|
89
|
-
* @async
|
|
90
|
-
* @param filePath - 待创建的路径
|
|
91
|
-
* @param opt - 可选参数
|
|
92
|
-
* @param opt.dir - 创建一个目录
|
|
93
|
-
* @returns 是否成功创建
|
|
94
|
-
*/
|
|
95
|
-
export async function createPath(filePath: string, opt?:CreatePathOpt):Promise<boolean>{
|
|
96
|
-
if(opt?.dir==true)
|
|
97
|
-
filePath = path.join(filePath,path.sep);
|
|
98
|
-
|
|
99
|
-
try{
|
|
100
|
-
if(filePath.endsWith(path.sep)){
|
|
101
|
-
await fs.promises.mkdir(filePath, {recursive: true});
|
|
102
|
-
return true;
|
|
103
|
-
}
|
|
104
|
-
await fs.promises.mkdir(path.dirname(filePath), {recursive: true});
|
|
105
|
-
const filehandle = await fs.promises.open(filePath, 'w');
|
|
106
|
-
await filehandle.close();
|
|
107
|
-
return true;
|
|
108
|
-
}
|
|
109
|
-
catch(e){
|
|
110
|
-
SLogger.error("createPath 错误",e);
|
|
111
|
-
return false;
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
/**路径不存在时创建路径 以path.sep结尾时创建文件夹 同步
|
|
116
|
-
* @param filePath - 待创建的路径
|
|
117
|
-
* @param opt - 可选参数
|
|
118
|
-
* @param opt.dir - 创建一个目录
|
|
119
|
-
* @returns 是否成功创建
|
|
120
|
-
*/
|
|
121
|
-
export function createPathSync(filePath: string, opt?:CreatePathOpt):boolean{
|
|
122
|
-
if(opt?.dir==true)
|
|
123
|
-
filePath = path.join(filePath,path.sep);
|
|
124
|
-
|
|
125
|
-
try{
|
|
126
|
-
if(filePath.endsWith(path.sep)){
|
|
127
|
-
fs.mkdirSync(filePath, {recursive: true});
|
|
128
|
-
return true;
|
|
129
|
-
}
|
|
130
|
-
fs.mkdirSync(path.dirname(filePath), {recursive: true});
|
|
131
|
-
fs.openSync(filePath, 'w');
|
|
132
|
-
return true;
|
|
133
|
-
}
|
|
134
|
-
catch(e){
|
|
135
|
-
SLogger.error("createPathSync 错误",e);
|
|
136
|
-
return false;
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
/**确保路径存在 不存在时创建 异步
|
|
141
|
-
* @async
|
|
142
|
-
* @param filePath - 待验证的路径
|
|
143
|
-
* @param opt - 可选参数
|
|
144
|
-
* @param opt.dir - 强制验证一个文件夹
|
|
145
|
-
* @returns 是否成功执行 创建或已存在
|
|
146
|
-
*/
|
|
147
|
-
export async function ensurePathExists(filePath: string, opt?:EnsurePathExistsOpt):Promise<boolean>{
|
|
148
|
-
if(await pathExists(filePath))
|
|
149
|
-
return true;
|
|
150
|
-
return await createPath(filePath,opt);
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
/**确保路径存在 不存在时创建 同步
|
|
154
|
-
* @param filePath - 待验证的路径
|
|
155
|
-
* @param opt - 可选参数
|
|
156
|
-
* @param opt.dir - 强制验证一个文件夹
|
|
157
|
-
* @returns 是否成功执行 创建或已存在
|
|
158
|
-
*/
|
|
159
|
-
export function ensurePathExistsSync(filePath: string, opt?:EnsurePathExistsOpt):boolean{
|
|
160
|
-
if(pathExistsSync(filePath))
|
|
161
|
-
return true;
|
|
162
|
-
return createPathSync(filePath,opt);
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
/**加载json文件 同步
|
|
166
|
-
* @param filePath - 文件路径
|
|
167
|
-
* @returns 加载完成的对象或空{}
|
|
168
|
-
*/
|
|
169
|
-
export function loadJSONFileSync(filePath: string): JToken
|
|
170
|
-
/**加载json文件 同步
|
|
171
|
-
* @param filePath - 文件路径
|
|
172
|
-
* @param opt - 可选参数
|
|
173
|
-
* @param opt.default - 默认值
|
|
174
|
-
* @param opt.json5 - json5模式
|
|
175
|
-
* @param opt.forceExt - 不自动修改扩展名为json
|
|
176
|
-
* @returns 加载完成的对象或默认值
|
|
177
|
-
*/
|
|
178
|
-
export function loadJSONFileSync<T extends JToken>(filePath: string,opt:LoadJsonFileOpt<T>): T
|
|
179
|
-
export function loadJSONFileSync<T extends JToken>(filePath: string,opt?:LoadJsonFileOpt<T>): T {
|
|
180
|
-
if(opt?.forceExt!==true && path.extname(filePath) !== '.json')
|
|
181
|
-
filePath += '.json';
|
|
182
|
-
|
|
183
|
-
const str = pathExistsSync(filePath)
|
|
184
|
-
? fs.readFileSync(filePath, "utf-8")
|
|
185
|
-
: "";
|
|
186
|
-
|
|
187
|
-
// 如果不存在则返回默认值
|
|
188
|
-
if (str == "" || str == null){
|
|
189
|
-
if(opt?.default!==undefined)
|
|
190
|
-
return opt.default;
|
|
191
|
-
return {} as T;
|
|
192
|
-
}
|
|
193
|
-
if(opt?.json5)
|
|
194
|
-
return JSON5.parse(str);
|
|
195
|
-
return JSON.parse(str);
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
/**加载json文件 异步
|
|
199
|
-
* @async
|
|
200
|
-
* @param filePath - 文件路径
|
|
201
|
-
* @returns 加载完成的对象或空{}
|
|
202
|
-
*/
|
|
203
|
-
export async function loadJSONFile(filePath: string): Promise<JToken>
|
|
204
|
-
/**加载json文件 异步
|
|
205
|
-
* @async
|
|
206
|
-
* @param filePath - 文件路径
|
|
207
|
-
* @param opt - 可选参数
|
|
208
|
-
* @param opt.default - 默认值
|
|
209
|
-
* @param opt.json5 - json5模式
|
|
210
|
-
* @param opt.forceExt - 不自动修改扩展名为json
|
|
211
|
-
* @returns 加载完成的对象或默认值
|
|
212
|
-
*/
|
|
213
|
-
export async function loadJSONFile<T extends JToken>(filePath: string,opt:LoadJsonFileOpt<T>): Promise<T>
|
|
214
|
-
export async function loadJSONFile<T extends JToken>(filePath: string,opt?:LoadJsonFileOpt<T>): Promise<T> {
|
|
215
|
-
if(opt?.forceExt!==true && path.extname(filePath) !== '.json')
|
|
216
|
-
filePath += '.json';
|
|
217
|
-
|
|
218
|
-
const str = (await pathExists(filePath))
|
|
219
|
-
? await fs.promises.readFile(filePath, "utf-8")
|
|
220
|
-
: "";
|
|
221
|
-
|
|
222
|
-
// 如果不存在则返回默认值
|
|
223
|
-
if (str == "" || str == null){
|
|
224
|
-
if(opt?.default!==undefined)
|
|
225
|
-
return opt.default;
|
|
226
|
-
return {} as T;
|
|
227
|
-
}
|
|
228
|
-
if(opt?.json5)
|
|
229
|
-
return JSON5.parse(str);
|
|
230
|
-
return JSON.parse(str);
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
/**写入JSON文件
|
|
234
|
-
* @async
|
|
235
|
-
* @param filePath - 文件路径
|
|
236
|
-
* @param token - 所要写入的JToken
|
|
237
|
-
* @param opt - 可选参数
|
|
238
|
-
* @param opt.compress - 使用紧凑风格 将会用/\uF121\uF122\uF123.+\uF121\uF122\uF123/作为特殊标记, 原始文本内若出现相同格式将会产生错误
|
|
239
|
-
* @param opt.forceExt - 不自动修改扩展名为json
|
|
240
|
-
* @param opt.compressThreshold - 紧凑风格 压缩阈值 默认100
|
|
241
|
-
*/
|
|
242
|
-
export async function writeJSONFile(
|
|
243
|
-
filePath: string,
|
|
244
|
-
token: JToken,
|
|
245
|
-
opt?: WriteJsonFileOpt
|
|
246
|
-
): Promise<void> {
|
|
247
|
-
const str = UtilFunc.stringifyJToken(token,opt);
|
|
248
|
-
if(opt?.forceExt!==true && path.extname(filePath) !== '.json')
|
|
249
|
-
filePath += '.json';
|
|
250
|
-
|
|
251
|
-
// 判断文件路径是否存在 不存在则创建
|
|
252
|
-
if(!(await pathExists(filePath)))
|
|
253
|
-
await createPath(filePath);
|
|
254
|
-
|
|
255
|
-
// 写入文件
|
|
256
|
-
try {
|
|
257
|
-
await fs.promises.writeFile(filePath, str);
|
|
258
|
-
SLogger.verbose(`${filePath} writeJSONFile 成功`);
|
|
259
|
-
} catch (err) {
|
|
260
|
-
SLogger.error(`${filePath} writeJSONFile 错误`,err);
|
|
261
|
-
}
|
|
262
|
-
}
|
|
263
|
-
/**保证路径为某个风格
|
|
264
|
-
* @param filePath - 输入路径
|
|
265
|
-
* @param style - 目标风格 undefined 时不改变
|
|
266
|
-
* @returns 新的符合目标风格的路径
|
|
267
|
-
*/
|
|
268
|
-
export function stylizePath(filePath:string,style?:'win32'|'posix'){
|
|
269
|
-
if(style===undefined) return filePath;
|
|
270
|
-
return UtilFunc.matchProc(style, {
|
|
271
|
-
posix: (nor) => filePath.replaceAll("\\", "/"),
|
|
272
|
-
win32: (nor) => filePath.replaceAll("/", "\\"),
|
|
273
|
-
});
|
|
274
|
-
}
|
|
275
|
-
/**保证路径为POSIX风格
|
|
276
|
-
* @param filePath - 输入路径
|
|
277
|
-
* @returns 新的符合POSIX风格的路径
|
|
278
|
-
*/
|
|
279
|
-
export function posixizePath(filePath: string): string {
|
|
280
|
-
return stylizePath(filePath, 'posix');
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
/**保证路径为Win32风格
|
|
284
|
-
* @param filePath - 输入路径
|
|
285
|
-
* @returns 新的符合Win32风格的路径
|
|
286
|
-
*/
|
|
287
|
-
export function win32izePath(filePath: string): string {
|
|
288
|
-
return stylizePath(filePath, 'win32');
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
/**保证路径为当前系统风格
|
|
292
|
-
* @param filePath - 输入路径
|
|
293
|
-
* @returns 新的符合当前系统风格的路径
|
|
294
|
-
*/
|
|
295
|
-
export function currosizePath(filePath: string): string {
|
|
296
|
-
const system = os.platform();
|
|
297
|
-
if (system === 'win32')
|
|
298
|
-
return win32izePath(filePath);
|
|
299
|
-
return posixizePath(filePath);
|
|
300
|
-
}
|
|
301
|
-
/**逐级寻找node_modules文件夹
|
|
302
|
-
* @param basePath - 基础位置
|
|
303
|
-
*/
|
|
304
|
-
export async function findNodeModulesDir(basePath:string){
|
|
305
|
-
let currentDir = basePath;
|
|
306
|
-
while (currentDir) {
|
|
307
|
-
const nodeModulesPath = path.join(currentDir, 'node_modules');
|
|
308
|
-
if (await UtilFT.pathExists(nodeModulesPath))
|
|
309
|
-
return nodeModulesPath;
|
|
310
|
-
const parentDir = path.dirname(currentDir);
|
|
311
|
-
if (parentDir === currentDir) break;
|
|
312
|
-
currentDir = parentDir;
|
|
313
|
-
}
|
|
314
|
-
return undefined;
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
/**搜索路径符合正则表达式的文件
|
|
321
|
-
* @param dir - 起始目录
|
|
322
|
-
* @param traitRegex - 正则表达式
|
|
323
|
-
* @param opt - 可选参数
|
|
324
|
-
* @param opt.relative - 搜索子目录
|
|
325
|
-
* @returns 文件名路径数组
|
|
326
|
-
*/
|
|
327
|
-
export async function fileSearchRegex(dir: string, traitRegex: string, opt?:FileSearchRegexOpt) {
|
|
328
|
-
const relative = opt?.relative ?? true;
|
|
329
|
-
const outArray: string[] = [];
|
|
330
|
-
const subFiles = await fs.promises.readdir(dir, { withFileTypes: true });
|
|
331
|
-
//如果使用 g 会导致 RegExp.lastIndex 更改, 将会导致匹配错误
|
|
332
|
-
const regex = new RegExp(traitRegex);
|
|
333
|
-
await Promise.all(subFiles.map(async (subFile)=>{
|
|
334
|
-
const subFilePath = path.join(dir, subFile.name);
|
|
335
|
-
//判断是否是文件夹,递归调用
|
|
336
|
-
if (subFile.isDirectory()) {
|
|
337
|
-
if (relative)
|
|
338
|
-
outArray.push(... await fileSearchRegex(path.join(subFilePath, path.sep), traitRegex));
|
|
339
|
-
return;
|
|
340
|
-
}
|
|
341
|
-
if (regex.test(subFilePath)) outArray.push(subFilePath);
|
|
342
|
-
}));
|
|
343
|
-
return outArray.map((filePath) => path.normalize(filePath));
|
|
344
|
-
}
|
|
345
|
-
/**搜索路径符合正则表达式的文件 同步版本
|
|
346
|
-
* @param dir - 起始目录
|
|
347
|
-
* @param traitRegex - 正则表达式
|
|
348
|
-
* @param opt - 可选参数
|
|
349
|
-
* @param opt.relative - 搜索子目录
|
|
350
|
-
* @returns 文件名路径数组
|
|
351
|
-
*/
|
|
352
|
-
export function fileSearchRegexSync(dir: string, traitRegex: string, opt?:FileSearchRegexOpt) {
|
|
353
|
-
const relative = opt?.relative ?? true;
|
|
354
|
-
const outArray: string[] = [];
|
|
355
|
-
const subFiles = fs.readdirSync(dir, { withFileTypes: true });
|
|
356
|
-
//如果使用 g 会导致 RegExp.lastIndex 更改, 将会导致匹配错误
|
|
357
|
-
const regex = new RegExp(traitRegex);
|
|
358
|
-
for (const subFile of subFiles) {
|
|
359
|
-
const subFilePath = path.join(dir, subFile.name);
|
|
360
|
-
//判断是否是文件夹,递归调用
|
|
361
|
-
if (subFile.isDirectory()) {
|
|
362
|
-
if (relative)
|
|
363
|
-
outArray.push(...fileSearchRegexSync(path.join(subFilePath, path.sep), traitRegex));
|
|
364
|
-
continue;
|
|
365
|
-
}
|
|
366
|
-
if (regex.test(subFilePath)) outArray.push(subFilePath);
|
|
367
|
-
}
|
|
368
|
-
return outArray.map((filePath) => path.normalize(filePath));
|
|
369
|
-
}
|
|
370
|
-
/**搜索符合Glob匹配的文件
|
|
371
|
-
* @param dir - 起始目录
|
|
372
|
-
* @param globPattern - glob匹配
|
|
373
|
-
* @param opt - 可选参数
|
|
374
|
-
* @param opt.ignore - 忽略的文件
|
|
375
|
-
* @returns 文件绝对路径数组
|
|
376
|
-
*/
|
|
377
|
-
export async function fileSearchGlob(dir: string, globPattern:string|string[],opt?:FileSearchGlobOpt){
|
|
378
|
-
const fixedPath = typeof globPattern === "string"
|
|
379
|
-
? path.join(dir,globPattern)
|
|
380
|
-
: globPattern.map((p)=>path.join(dir,p));
|
|
381
|
-
return (await glob(fixedPath, { ignore: opt?.ingore, absolute: true }))
|
|
382
|
-
.map((filePath) => path.normalize(filePath));
|
|
383
|
-
}
|
|
384
|
-
/**搜索符合Glob匹配的文件 同步版本
|
|
385
|
-
* @param dir - 起始目录
|
|
386
|
-
* @param globPattern - glob匹配
|
|
387
|
-
* @param opt - 可选参数
|
|
388
|
-
* @param opt.ignore - 忽略的文件
|
|
389
|
-
* @returns 文件绝对路径数组
|
|
390
|
-
*/
|
|
391
|
-
export function fileSearchGlobSync(dir: string, globPattern:string|string[],opt?:FileSearchGlobOpt){
|
|
392
|
-
const fixedPath = typeof globPattern === "string"
|
|
393
|
-
? path.join(dir,globPattern)
|
|
394
|
-
: globPattern.map((p)=>path.join(dir,p));
|
|
395
|
-
return globSync(fixedPath, { ignore: opt?.ingore, absolute: true })
|
|
396
|
-
.map((filePath) => path.normalize(filePath));
|
|
397
|
-
}
|
|
398
|
-
/**是一个有效的文件路径
|
|
399
|
-
* @param filePath - 需要验证的文件路径
|
|
400
|
-
*/
|
|
401
|
-
export function isValidFilePath(filePath:string){
|
|
402
|
-
if(filePath.length>255) return false;
|
|
403
|
-
const invalidCharacters = ['<', '>', '"', '|', '?', '*'];
|
|
404
|
-
for (const invalidCharacter of invalidCharacters) {
|
|
405
|
-
if (filePath.includes(invalidCharacter))
|
|
406
|
-
return false;
|
|
407
|
-
}
|
|
408
|
-
return true;
|
|
409
|
-
}
|
|
410
|
-
}
|