@zwa73/utils 1.0.102 → 1.0.104
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/UtilFileTools.d.ts +46 -12
- package/dist/UtilFileTools.js +37 -27
- package/package.json +3 -3
- package/scripts/compile.ps1 +4 -4
- package/scripts/postinstall.js +69 -53
- package/scripts/release.ps1 +4 -0
- package/scripts/watch.ps1 +9 -9
- package/src/UtilFileTools.ts +63 -27
- package/publish.bat +0 -2
- package/release.bat +0 -2
- package/release.js +0 -16
package/dist/UtilFileTools.d.ts
CHANGED
|
@@ -1,4 +1,28 @@
|
|
|
1
1
|
import { JToken } from "./UtilInterfaces";
|
|
2
|
+
/**创建文件选项 */
|
|
3
|
+
type CreatePathOpt = Partial<{
|
|
4
|
+
/**创建一个目录 默认 false
|
|
5
|
+
* 默认仅在以path.sep结尾时创建文件夹
|
|
6
|
+
*/
|
|
7
|
+
dir: boolean;
|
|
8
|
+
}>;
|
|
9
|
+
/**验证文件选项 */
|
|
10
|
+
type EnsurePathExistsOpt = Partial<{
|
|
11
|
+
/**验证一个目录 默认 false
|
|
12
|
+
* 默认仅在以path.sep结尾时创建文件夹
|
|
13
|
+
*/
|
|
14
|
+
dir: boolean;
|
|
15
|
+
}>;
|
|
16
|
+
/**glob搜索选项 */
|
|
17
|
+
type FileSearchGlobOpt = Partial<{
|
|
18
|
+
/**忽略的文件 默认 undefined */
|
|
19
|
+
ingore: string | string[];
|
|
20
|
+
}>;
|
|
21
|
+
/**regex搜索选项 */
|
|
22
|
+
type FileSearchRegexOpt = Partial<{
|
|
23
|
+
/**搜索子目录 默认 true */
|
|
24
|
+
relative: boolean;
|
|
25
|
+
}>;
|
|
2
26
|
/**文件工具 */
|
|
3
27
|
export declare namespace UtilFT {
|
|
4
28
|
/**验证路径 文件或文件夹 是否存在 异步
|
|
@@ -15,28 +39,33 @@ export declare namespace UtilFT {
|
|
|
15
39
|
/**路径不存在时创建路径 以path.sep结尾时创建文件夹 异步
|
|
16
40
|
* @async
|
|
17
41
|
* @param filePath - 待创建的路径
|
|
18
|
-
* @param
|
|
42
|
+
* @param opt - 可选参数
|
|
43
|
+
* @param opt.dir - 创建一个目录
|
|
19
44
|
* @returns 是否成功创建
|
|
20
45
|
*/
|
|
21
|
-
function createPath(filePath: string,
|
|
46
|
+
function createPath(filePath: string, opt?: CreatePathOpt): Promise<boolean>;
|
|
22
47
|
/**路径不存在时创建路径 以path.sep结尾时创建文件夹 同步
|
|
23
48
|
* @param filePath - 待创建的路径
|
|
24
|
-
* @param
|
|
49
|
+
* @param opt - 可选参数
|
|
50
|
+
* @param opt.dir - 创建一个目录
|
|
25
51
|
* @returns 是否成功创建
|
|
26
52
|
*/
|
|
27
|
-
function createPathSync(filePath: string,
|
|
53
|
+
function createPathSync(filePath: string, opt?: CreatePathOpt): boolean;
|
|
28
54
|
/**确保路径存在 不存在时创建 异步
|
|
29
55
|
* @async
|
|
30
56
|
* @param filePath - 待验证的路径
|
|
31
|
-
* @param
|
|
57
|
+
* @param opt - 可选参数
|
|
58
|
+
* @param opt.dir - 强制验证一个文件夹
|
|
32
59
|
* @returns 是否成功执行 创建或已存在
|
|
33
60
|
*/
|
|
34
|
-
function ensurePathExists(filePath: string,
|
|
61
|
+
function ensurePathExists(filePath: string, opt?: EnsurePathExistsOpt): Promise<boolean>;
|
|
35
62
|
/**确保路径存在 不存在时创建 同步
|
|
36
63
|
* @param filePath - 待验证的路径
|
|
64
|
+
* @param opt - 可选参数
|
|
65
|
+
* @param opt.dir - 强制验证一个文件夹
|
|
37
66
|
* @returns 是否成功执行 创建或已存在
|
|
38
67
|
*/
|
|
39
|
-
function ensurePathExistsSync(filePath: string,
|
|
68
|
+
function ensurePathExistsSync(filePath: string, opt?: EnsurePathExistsOpt): boolean;
|
|
40
69
|
/**加载json文件 同步
|
|
41
70
|
* @param filePath - 文件路径
|
|
42
71
|
* @returns 加载完成的对象或空{}
|
|
@@ -69,17 +98,21 @@ export declare namespace UtilFT {
|
|
|
69
98
|
*/
|
|
70
99
|
function writeJSONFile(filePath: string, token: JToken): Promise<void>;
|
|
71
100
|
/**搜索路径符合正则表达式的文件
|
|
72
|
-
* @param folder
|
|
73
|
-
* @param traitRegex
|
|
101
|
+
* @param folder - 文件夹路径
|
|
102
|
+
* @param traitRegex - 正则表达式
|
|
103
|
+
* @param opt - 可选参数
|
|
104
|
+
* @param opt.relative - 搜索子目录
|
|
74
105
|
* @returns 文件名路径数组
|
|
75
106
|
*/
|
|
76
|
-
function fileSearchRegex(folder: string, traitRegex: string): string[];
|
|
107
|
+
function fileSearchRegex(folder: string, traitRegex: string, opt?: FileSearchRegexOpt): string[];
|
|
77
108
|
/**搜索符合Glob匹配的文件
|
|
109
|
+
* @param folder - 文件夹路径
|
|
78
110
|
* @param globPattern - glob匹配
|
|
79
|
-
* @param
|
|
111
|
+
* @param opt - 可选参数
|
|
112
|
+
* @param opt.ignore - 忽略的文件
|
|
80
113
|
* @returns 文件绝对路径数组
|
|
81
114
|
*/
|
|
82
|
-
function fileSearchGlob(globPattern: string | string[],
|
|
115
|
+
function fileSearchGlob(folder: string, globPattern: string | string[], opt?: FileSearchGlobOpt): string[];
|
|
83
116
|
/**
|
|
84
117
|
* @deprecated 请使用 fileSearchRegex 或 fileSearchGlob
|
|
85
118
|
*/
|
|
@@ -89,3 +122,4 @@ export declare namespace UtilFT {
|
|
|
89
122
|
*/
|
|
90
123
|
function isValidFilePath(filePath: string): boolean;
|
|
91
124
|
}
|
|
125
|
+
export {};
|
package/dist/UtilFileTools.js
CHANGED
|
@@ -66,11 +66,12 @@ var UtilFT;
|
|
|
66
66
|
/**路径不存在时创建路径 以path.sep结尾时创建文件夹 异步
|
|
67
67
|
* @async
|
|
68
68
|
* @param filePath - 待创建的路径
|
|
69
|
-
* @param
|
|
69
|
+
* @param opt - 可选参数
|
|
70
|
+
* @param opt.dir - 创建一个目录
|
|
70
71
|
* @returns 是否成功创建
|
|
71
72
|
*/
|
|
72
|
-
async function createPath(filePath,
|
|
73
|
-
if (
|
|
73
|
+
async function createPath(filePath, opt) {
|
|
74
|
+
if (opt?.dir == true)
|
|
74
75
|
filePath = path.join(filePath, path.sep);
|
|
75
76
|
try {
|
|
76
77
|
if (filePath.endsWith(path.sep)) {
|
|
@@ -90,11 +91,12 @@ var UtilFT;
|
|
|
90
91
|
UtilFT.createPath = createPath;
|
|
91
92
|
/**路径不存在时创建路径 以path.sep结尾时创建文件夹 同步
|
|
92
93
|
* @param filePath - 待创建的路径
|
|
93
|
-
* @param
|
|
94
|
+
* @param opt - 可选参数
|
|
95
|
+
* @param opt.dir - 创建一个目录
|
|
94
96
|
* @returns 是否成功创建
|
|
95
97
|
*/
|
|
96
|
-
function createPathSync(filePath,
|
|
97
|
-
if (
|
|
98
|
+
function createPathSync(filePath, opt) {
|
|
99
|
+
if (opt?.dir == true)
|
|
98
100
|
filePath = path.join(filePath, path.sep);
|
|
99
101
|
try {
|
|
100
102
|
if (filePath.endsWith(path.sep)) {
|
|
@@ -114,23 +116,26 @@ var UtilFT;
|
|
|
114
116
|
/**确保路径存在 不存在时创建 异步
|
|
115
117
|
* @async
|
|
116
118
|
* @param filePath - 待验证的路径
|
|
117
|
-
* @param
|
|
119
|
+
* @param opt - 可选参数
|
|
120
|
+
* @param opt.dir - 强制验证一个文件夹
|
|
118
121
|
* @returns 是否成功执行 创建或已存在
|
|
119
122
|
*/
|
|
120
|
-
async function ensurePathExists(filePath,
|
|
123
|
+
async function ensurePathExists(filePath, opt) {
|
|
121
124
|
if (await pathExists(filePath))
|
|
122
125
|
return true;
|
|
123
|
-
return await createPath(filePath,
|
|
126
|
+
return await createPath(filePath, opt);
|
|
124
127
|
}
|
|
125
128
|
UtilFT.ensurePathExists = ensurePathExists;
|
|
126
129
|
/**确保路径存在 不存在时创建 同步
|
|
127
130
|
* @param filePath - 待验证的路径
|
|
131
|
+
* @param opt - 可选参数
|
|
132
|
+
* @param opt.dir - 强制验证一个文件夹
|
|
128
133
|
* @returns 是否成功执行 创建或已存在
|
|
129
134
|
*/
|
|
130
|
-
function ensurePathExistsSync(filePath,
|
|
135
|
+
function ensurePathExistsSync(filePath, opt) {
|
|
131
136
|
if (pathExistsSync(filePath))
|
|
132
137
|
return true;
|
|
133
|
-
return createPathSync(filePath,
|
|
138
|
+
return createPathSync(filePath, opt);
|
|
134
139
|
}
|
|
135
140
|
UtilFT.ensurePathExistsSync = ensurePathExistsSync;
|
|
136
141
|
function loadJSONFileSync(filePath, def) {
|
|
@@ -189,20 +194,23 @@ var UtilFT;
|
|
|
189
194
|
}
|
|
190
195
|
UtilFT.writeJSONFile = writeJSONFile;
|
|
191
196
|
/**搜索路径符合正则表达式的文件
|
|
192
|
-
* @param folder
|
|
193
|
-
* @param traitRegex
|
|
197
|
+
* @param folder - 文件夹路径
|
|
198
|
+
* @param traitRegex - 正则表达式
|
|
199
|
+
* @param opt - 可选参数
|
|
200
|
+
* @param opt.relative - 搜索子目录
|
|
194
201
|
* @returns 文件名路径数组
|
|
195
202
|
*/
|
|
196
|
-
function fileSearchRegex(folder, traitRegex) {
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
+
function fileSearchRegex(folder, traitRegex, opt) {
|
|
204
|
+
const relative = opt?.relative ?? true;
|
|
205
|
+
const outArray = [];
|
|
206
|
+
const subFiles = fs.readdirSync(folder, { withFileTypes: true });
|
|
207
|
+
const regex = new RegExp(traitRegex);
|
|
208
|
+
for (const subFile of subFiles) {
|
|
209
|
+
const subFilePath = path.join(folder, subFile.name);
|
|
203
210
|
//判断是否是文件夹,递归调用
|
|
204
|
-
if (
|
|
205
|
-
|
|
211
|
+
if (subFile.isDirectory()) {
|
|
212
|
+
if (relative)
|
|
213
|
+
outArray.push(...fileSearchRegex(path.join(subFilePath, path.sep), traitRegex));
|
|
206
214
|
continue;
|
|
207
215
|
}
|
|
208
216
|
if (regex.test(subFilePath))
|
|
@@ -212,15 +220,17 @@ var UtilFT;
|
|
|
212
220
|
}
|
|
213
221
|
UtilFT.fileSearchRegex = fileSearchRegex;
|
|
214
222
|
/**搜索符合Glob匹配的文件
|
|
223
|
+
* @param folder - 文件夹路径
|
|
215
224
|
* @param globPattern - glob匹配
|
|
216
|
-
* @param
|
|
225
|
+
* @param opt - 可选参数
|
|
226
|
+
* @param opt.ignore - 忽略的文件
|
|
217
227
|
* @returns 文件绝对路径数组
|
|
218
228
|
*/
|
|
219
|
-
function fileSearchGlob(globPattern,
|
|
229
|
+
function fileSearchGlob(folder, globPattern, opt) {
|
|
220
230
|
const fixedPath = typeof globPattern === "string"
|
|
221
|
-
? path.posix.normalize(globPattern)
|
|
222
|
-
: globPattern.map((p) => path.posix.normalize(p));
|
|
223
|
-
return (0, glob_1.globSync)(fixedPath, { ignore, absolute: true });
|
|
231
|
+
? path.join(folder, path.posix.normalize(globPattern))
|
|
232
|
+
: globPattern.map((p) => path.join(folder, path.posix.normalize(p)));
|
|
233
|
+
return (0, glob_1.globSync)(fixedPath, { ignore: opt?.ingore, absolute: true });
|
|
224
234
|
}
|
|
225
235
|
UtilFT.fileSearchGlob = fileSearchGlob;
|
|
226
236
|
/**
|
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zwa73/utils",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.104",
|
|
4
4
|
"description": "my utils",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
7
|
-
"test": "
|
|
7
|
+
"test": "jest",
|
|
8
8
|
"postinstall": "start node ./scripts/postinstall.js",
|
|
9
|
-
"release": "
|
|
9
|
+
"release": "powershell scripts/release",
|
|
10
10
|
"compile": "powershell scripts/compile",
|
|
11
11
|
"watch": "powershell scripts/watch"
|
|
12
12
|
},
|
package/scripts/compile.ps1
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
Write-Output 开始删除原dist
|
|
2
|
-
Remove-Item -Recurse -Force dist
|
|
3
|
-
Write-Output 开始编译
|
|
4
|
-
tsc -p tsconfig.compile.json
|
|
1
|
+
Write-Output 开始删除原dist
|
|
2
|
+
Remove-Item -Recurse -Force dist
|
|
3
|
+
Write-Output 开始编译
|
|
4
|
+
tsc -p tsconfig.compile.json
|
|
5
5
|
tsc-alias -p tsconfig.compile.json
|
package/scripts/postinstall.js
CHANGED
|
@@ -1,53 +1,69 @@
|
|
|
1
|
-
const
|
|
2
|
-
const
|
|
3
|
-
const
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
const
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
const
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
1
|
+
const path = require('path');
|
|
2
|
+
const { UtilFT, SLogger } = require("../index");
|
|
3
|
+
const readline = require('readline');
|
|
4
|
+
|
|
5
|
+
const PACKAGE_PATH = path.join(__dirname,'..','package.json');
|
|
6
|
+
const VERSION_PATH = path.join(__dirname,"..","..","version.json");
|
|
7
|
+
//提示表
|
|
8
|
+
const INFO_TABLE = {
|
|
9
|
+
"1.0.47":"UtilFT.fileSearch 函数与返回值发生变动, 请使用fileSearchRegex 或 fileSearchGlob",
|
|
10
|
+
"1.0.104":
|
|
11
|
+
"UtilFT.fleSearchGlob 更改参数 floder作为第一个参数 ingore 移入 可选参数对象 \n" +
|
|
12
|
+
"UtilFT.fleSearchRegex 添加了一个可选参数 \n" +
|
|
13
|
+
"UtilFT.createPath 更改参数 dir 移入 可选参数对象 \n" +
|
|
14
|
+
"UtilFT.createPathSync 更改参数 dir 移入 可选参数对象 \n" +
|
|
15
|
+
"UtilFT.ensurePathExists 更改参数 dir 移入 可选参数对象 \n" +
|
|
16
|
+
"UtilFT.ensurePathExistsSync 更改参数 dir 移入 可选参数对象 \n"
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// 将版本号转换为可以比较的数字
|
|
20
|
+
function versionToNumber(version) {
|
|
21
|
+
if(version==undefined) return 0;
|
|
22
|
+
return version.split('.').map(Number).reduce((acc, val) => acc * 1000 + val);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
//显示提示
|
|
26
|
+
function showUpgradeMessages(prevVersion, currentVersion) {
|
|
27
|
+
const prevVersionNumber = versionToNumber(prevVersion);
|
|
28
|
+
const currentVersionNumber = versionToNumber(currentVersion);
|
|
29
|
+
let hasOut = false;
|
|
30
|
+
// 遍历infoTable中的所有版本
|
|
31
|
+
for (const version in INFO_TABLE) {
|
|
32
|
+
const versionNumber = versionToNumber(version);
|
|
33
|
+
// 如果用户的上一个版本低于这个版本,而当前版本高于或等于这个版本
|
|
34
|
+
if (versionNumber > prevVersionNumber && versionNumber <= currentVersionNumber) {
|
|
35
|
+
// 显示这个版本的提示信息
|
|
36
|
+
SLogger.info(INFO_TABLE[version]);
|
|
37
|
+
hasOut=true;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return hasOut;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
async function main(){
|
|
46
|
+
const packageTable = await UtilFT.loadJSONFile(PACKAGE_PATH);
|
|
47
|
+
const currentVersion = packageTable.version;
|
|
48
|
+
await UtilFT.ensurePathExists(versionTable);
|
|
49
|
+
const versionTable = Object.assign({},
|
|
50
|
+
await UtilFT.loadJSONFile(VERSION_PATH,{}),
|
|
51
|
+
{utils:{version:currentVersion}});
|
|
52
|
+
const prevVersion = versionTable.utils.version;
|
|
53
|
+
|
|
54
|
+
SLogger.info(`${currentVersion} 版本已安装`);
|
|
55
|
+
// 使用这个函数来显示升级信息
|
|
56
|
+
const hasOut = showUpgradeMessages(prevVersion, currentVersion);
|
|
57
|
+
|
|
58
|
+
versionTable.utils.version = currentVersion;
|
|
59
|
+
await UtilFT.writeJSONFile(dataPath, versionTable);
|
|
60
|
+
|
|
61
|
+
if(!hasOut) return;
|
|
62
|
+
const rl = readline.createInterface({
|
|
63
|
+
input: process.stdin,
|
|
64
|
+
output: process.stdout
|
|
65
|
+
});
|
|
66
|
+
setTimeout(()=>rl.close(),10000);
|
|
67
|
+
rl.question('按任意键继续...', () => rl.close());
|
|
68
|
+
}
|
|
69
|
+
main()
|
package/scripts/watch.ps1
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
# 定义一个函数来运行脚本
|
|
2
|
-
function New-Script {
|
|
3
|
-
param($command)
|
|
4
|
-
# 在后台运行命令
|
|
5
|
-
Start-Process -NoNewWindow -FilePath "powershell" -ArgumentList "-Command", $command
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
# 开始运行脚本
|
|
9
|
-
New-Script "tsc -w -p tsconfig.compile.json"
|
|
1
|
+
# 定义一个函数来运行脚本
|
|
2
|
+
function New-Script {
|
|
3
|
+
param($command)
|
|
4
|
+
# 在后台运行命令
|
|
5
|
+
Start-Process -NoNewWindow -FilePath "powershell" -ArgumentList "-Command", $command
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
# 开始运行脚本
|
|
9
|
+
New-Script "tsc -w -p tsconfig.compile.json"
|
|
10
10
|
New-Script "tsc-alias -w -p tsconfig.compile.json"
|
package/src/UtilFileTools.ts
CHANGED
|
@@ -6,6 +6,33 @@ import * as JSON5 from 'json5';
|
|
|
6
6
|
import { globSync } from "glob";
|
|
7
7
|
import { UtilFunc } from "./UtilFunctions";
|
|
8
8
|
|
|
9
|
+
/**创建文件选项 */
|
|
10
|
+
type CreatePathOpt = Partial<{
|
|
11
|
+
/**创建一个目录 默认 false
|
|
12
|
+
* 默认仅在以path.sep结尾时创建文件夹
|
|
13
|
+
*/
|
|
14
|
+
dir:boolean
|
|
15
|
+
}>
|
|
16
|
+
/**验证文件选项 */
|
|
17
|
+
type EnsurePathExistsOpt = Partial<{
|
|
18
|
+
/**验证一个目录 默认 false
|
|
19
|
+
* 默认仅在以path.sep结尾时创建文件夹
|
|
20
|
+
*/
|
|
21
|
+
dir:boolean
|
|
22
|
+
}>
|
|
23
|
+
|
|
24
|
+
/**glob搜索选项 */
|
|
25
|
+
type FileSearchGlobOpt = Partial<{
|
|
26
|
+
/**忽略的文件 默认 undefined */
|
|
27
|
+
ingore:string|string[];
|
|
28
|
+
}>
|
|
29
|
+
|
|
30
|
+
/**regex搜索选项 */
|
|
31
|
+
type FileSearchRegexOpt = Partial<{
|
|
32
|
+
/**搜索子目录 默认 true */
|
|
33
|
+
relative:boolean;
|
|
34
|
+
}>
|
|
35
|
+
|
|
9
36
|
/**文件工具 */
|
|
10
37
|
export namespace UtilFT{
|
|
11
38
|
|
|
@@ -40,11 +67,12 @@ export function pathExistsSync(filePath: string):boolean{
|
|
|
40
67
|
/**路径不存在时创建路径 以path.sep结尾时创建文件夹 异步
|
|
41
68
|
* @async
|
|
42
69
|
* @param filePath - 待创建的路径
|
|
43
|
-
* @param
|
|
70
|
+
* @param opt - 可选参数
|
|
71
|
+
* @param opt.dir - 创建一个目录
|
|
44
72
|
* @returns 是否成功创建
|
|
45
73
|
*/
|
|
46
|
-
export async function createPath(filePath: string,
|
|
47
|
-
if(
|
|
74
|
+
export async function createPath(filePath: string, opt?:CreatePathOpt):Promise<boolean>{
|
|
75
|
+
if(opt?.dir==true)
|
|
48
76
|
filePath = path.join(filePath,path.sep);
|
|
49
77
|
|
|
50
78
|
try{
|
|
@@ -65,11 +93,12 @@ export async function createPath(filePath: string, isDir?:boolean):Promise<boole
|
|
|
65
93
|
|
|
66
94
|
/**路径不存在时创建路径 以path.sep结尾时创建文件夹 同步
|
|
67
95
|
* @param filePath - 待创建的路径
|
|
68
|
-
* @param
|
|
96
|
+
* @param opt - 可选参数
|
|
97
|
+
* @param opt.dir - 创建一个目录
|
|
69
98
|
* @returns 是否成功创建
|
|
70
99
|
*/
|
|
71
|
-
export function createPathSync(filePath: string,
|
|
72
|
-
if(
|
|
100
|
+
export function createPathSync(filePath: string, opt?:CreatePathOpt):boolean{
|
|
101
|
+
if(opt?.dir==true)
|
|
73
102
|
filePath = path.join(filePath,path.sep);
|
|
74
103
|
|
|
75
104
|
try{
|
|
@@ -90,23 +119,26 @@ export function createPathSync(filePath: string, isDir?:boolean):boolean{
|
|
|
90
119
|
/**确保路径存在 不存在时创建 异步
|
|
91
120
|
* @async
|
|
92
121
|
* @param filePath - 待验证的路径
|
|
93
|
-
* @param
|
|
122
|
+
* @param opt - 可选参数
|
|
123
|
+
* @param opt.dir - 强制验证一个文件夹
|
|
94
124
|
* @returns 是否成功执行 创建或已存在
|
|
95
125
|
*/
|
|
96
|
-
export async function ensurePathExists(filePath: string,
|
|
126
|
+
export async function ensurePathExists(filePath: string, opt?:EnsurePathExistsOpt):Promise<boolean>{
|
|
97
127
|
if(await pathExists(filePath))
|
|
98
128
|
return true;
|
|
99
|
-
return await createPath(filePath,
|
|
129
|
+
return await createPath(filePath,opt);
|
|
100
130
|
}
|
|
101
131
|
|
|
102
132
|
/**确保路径存在 不存在时创建 同步
|
|
103
133
|
* @param filePath - 待验证的路径
|
|
134
|
+
* @param opt - 可选参数
|
|
135
|
+
* @param opt.dir - 强制验证一个文件夹
|
|
104
136
|
* @returns 是否成功执行 创建或已存在
|
|
105
137
|
*/
|
|
106
|
-
export function ensurePathExistsSync(filePath: string,
|
|
138
|
+
export function ensurePathExistsSync(filePath: string, opt?:EnsurePathExistsOpt):boolean{
|
|
107
139
|
if(pathExistsSync(filePath))
|
|
108
140
|
return true;
|
|
109
|
-
return createPathSync(filePath,
|
|
141
|
+
return createPathSync(filePath,opt);
|
|
110
142
|
}
|
|
111
143
|
|
|
112
144
|
/**加载json文件 同步
|
|
@@ -196,20 +228,22 @@ export async function writeJSONFile(
|
|
|
196
228
|
}
|
|
197
229
|
|
|
198
230
|
/**搜索路径符合正则表达式的文件
|
|
199
|
-
* @param folder
|
|
200
|
-
* @param traitRegex
|
|
231
|
+
* @param folder - 文件夹路径
|
|
232
|
+
* @param traitRegex - 正则表达式
|
|
233
|
+
* @param opt - 可选参数
|
|
234
|
+
* @param opt.relative - 搜索子目录
|
|
201
235
|
* @returns 文件名路径数组
|
|
202
236
|
*/
|
|
203
|
-
export function fileSearchRegex(folder: string, traitRegex: string) {
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
237
|
+
export function fileSearchRegex(folder: string, traitRegex: string, opt?:FileSearchRegexOpt) {
|
|
238
|
+
const relative = opt?.relative ?? true;
|
|
239
|
+
const outArray: string[] = [];
|
|
240
|
+
const subFiles = fs.readdirSync(folder,{withFileTypes:true});
|
|
241
|
+
const regex = new RegExp(traitRegex);
|
|
242
|
+
for (const subFile of subFiles) {
|
|
243
|
+
const subFilePath = path.join(folder, subFile.name);
|
|
210
244
|
//判断是否是文件夹,递归调用
|
|
211
|
-
if (
|
|
212
|
-
outArray.push(...fileSearchRegex(path.join(subFilePath, path.sep), traitRegex));
|
|
245
|
+
if (subFile.isDirectory()) {
|
|
246
|
+
if(relative) outArray.push(...fileSearchRegex(path.join(subFilePath, path.sep), traitRegex));
|
|
213
247
|
continue;
|
|
214
248
|
}
|
|
215
249
|
if (regex.test(subFilePath)) outArray.push(subFilePath);
|
|
@@ -217,15 +251,17 @@ export function fileSearchRegex(folder: string, traitRegex: string) {
|
|
|
217
251
|
return outArray;
|
|
218
252
|
}
|
|
219
253
|
/**搜索符合Glob匹配的文件
|
|
254
|
+
* @param folder - 文件夹路径
|
|
220
255
|
* @param globPattern - glob匹配
|
|
221
|
-
* @param
|
|
256
|
+
* @param opt - 可选参数
|
|
257
|
+
* @param opt.ignore - 忽略的文件
|
|
222
258
|
* @returns 文件绝对路径数组
|
|
223
259
|
*/
|
|
224
|
-
export function fileSearchGlob(globPattern:string|string[],
|
|
260
|
+
export function fileSearchGlob(folder: string, globPattern:string|string[],opt?:FileSearchGlobOpt){
|
|
225
261
|
const fixedPath = typeof globPattern === "string"
|
|
226
|
-
? path.posix.normalize(globPattern)
|
|
227
|
-
: globPattern.map((p)=>path.posix.normalize(p));
|
|
228
|
-
return globSync(fixedPath,{ignore,absolute:true});
|
|
262
|
+
? path.join(folder,path.posix.normalize(globPattern))
|
|
263
|
+
: globPattern.map((p)=>path.join(folder,path.posix.normalize(p)));
|
|
264
|
+
return globSync(fixedPath,{ignore:opt?.ingore,absolute:true});
|
|
229
265
|
}
|
|
230
266
|
/**
|
|
231
267
|
* @deprecated 请使用 fileSearchRegex 或 fileSearchGlob
|
package/publish.bat
DELETED
package/release.bat
DELETED
package/release.js
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
const fs = require('fs');
|
|
2
|
-
const path = require('path');
|
|
3
|
-
|
|
4
|
-
// 读取并更新包配置文件中的版本号
|
|
5
|
-
function updateVersion() {
|
|
6
|
-
const packagePath = path.join(__dirname, 'package.json');
|
|
7
|
-
const packageData = JSON.parse(fs.readFileSync(packagePath));
|
|
8
|
-
const version = packageData.version.split('.');
|
|
9
|
-
version[2] = parseInt(version[2]) + 1;
|
|
10
|
-
packageData.version = version.join('.');
|
|
11
|
-
fs.writeFileSync(packagePath, JSON.stringify(packageData, null, 2));
|
|
12
|
-
return packageData.version;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
updateVersion();
|