@zwa73/utils 1.0.16 → 1.0.18

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.
@@ -13,6 +13,7 @@ export declare function getTime(): number;
13
13
  */
14
14
  export declare function initField<T>(obj: Record<string, T>, field: string, defaultVal: T): T;
15
15
  /**验证路径 文件或文件夹 是否存在 异步
16
+ * @async
16
17
  * @param {string} filePath - 待验证的路径
17
18
  * @returns {Promise<boolean>} - 是否存在
18
19
  */
@@ -22,16 +23,31 @@ export declare function pathExists(filePath: string): Promise<boolean>;
22
23
  * @returns {boolean} - 是否存在
23
24
  */
24
25
  export declare function pathExistsSync(filePath: string): boolean;
25
- /**创建路径 path.sep 结尾时创建文件夹 异步
26
+ /**路径不存在时创建路径 path.sep结尾时创建文件夹 异步
27
+ * @async
26
28
  * @param {string} filePath - 待创建的路径
29
+ * @param {boolean} isDir - 强制创建一个文件夹
27
30
  * @returns {Promise<boolean>} - 是否成功创建
28
31
  */
29
- export declare function createPath(filePath: string): Promise<boolean>;
30
- /**创建路径 path.sep 结尾时创建文件夹 同步
32
+ export declare function createPath(filePath: string, isDir?: boolean): Promise<boolean>;
33
+ /**路径不存在时创建路径 path.sep结尾时创建文件夹 同步
31
34
  * @param {string} filePath - 待创建的路径
35
+ * @param {boolean} isDir - 强制验证一个文件夹
32
36
  * @returns {boolean} - 是否成功创建
33
37
  */
34
- export declare function createPathSync(filePath: string): boolean;
38
+ export declare function createPathSync(filePath: string, isDir?: boolean): boolean;
39
+ /**确保路径存在 不存在时创建 异步
40
+ * @async
41
+ * @param {string} filePath - 待验证的路径
42
+ * @param {boolean} isDir - 强制验证一个文件夹
43
+ * @returns {Promise<boolean>} - 是否成功执行 创建或已存在
44
+ */
45
+ export declare function ensurePathExists(filePath: string, isDir?: boolean): Promise<boolean>;
46
+ /**确保路径存在 不存在时创建 同步
47
+ * @param {string} filePath - 待验证的路径
48
+ * @returns {boolean} - 是否成功执行 创建或已存在
49
+ */
50
+ export declare function ensurePathExistsSync(filePath: string, isDir?: boolean): boolean;
35
51
  /**加载json文件 同步
36
52
  * Object (string)
37
53
  * @param {string} filePath - 文件路径
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.fileSearch = exports.sleep = exports.isSafeNumber = exports.deepClone = exports.genUUID = exports.writeJSONFile = exports.loadJSONFile = exports.loadJSONFileSync = exports.createPathSync = exports.createPath = exports.pathExistsSync = exports.pathExists = exports.initField = exports.getTime = void 0;
3
+ exports.fileSearch = exports.sleep = exports.isSafeNumber = exports.deepClone = exports.genUUID = exports.writeJSONFile = exports.loadJSONFile = exports.loadJSONFileSync = exports.ensurePathExistsSync = exports.ensurePathExists = exports.createPathSync = exports.createPath = exports.pathExistsSync = exports.pathExists = exports.initField = exports.getTime = void 0;
4
4
  const fs = require("fs");
5
5
  const crypto = require("crypto");
6
6
  const path = require("path");
@@ -26,6 +26,7 @@ function initField(obj, field, defaultVal) {
26
26
  }
27
27
  exports.initField = initField;
28
28
  /**验证路径 文件或文件夹 是否存在 异步
29
+ * @async
29
30
  * @param {string} filePath - 待验证的路径
30
31
  * @returns {Promise<boolean>} - 是否存在
31
32
  */
@@ -54,11 +55,15 @@ function pathExistsSync(filePath) {
54
55
  }
55
56
  }
56
57
  exports.pathExistsSync = pathExistsSync;
57
- /**创建路径 path.sep 结尾时创建文件夹 异步
58
+ /**路径不存在时创建路径 path.sep结尾时创建文件夹 异步
59
+ * @async
58
60
  * @param {string} filePath - 待创建的路径
61
+ * @param {boolean} isDir - 强制创建一个文件夹
59
62
  * @returns {Promise<boolean>} - 是否成功创建
60
63
  */
61
- async function createPath(filePath) {
64
+ async function createPath(filePath, isDir) {
65
+ if (isDir == true)
66
+ filePath = path.join(filePath, path.sep);
62
67
  try {
63
68
  if (filePath.endsWith(path.sep)) {
64
69
  await fs.promises.mkdir(filePath, { recursive: true });
@@ -75,11 +80,14 @@ async function createPath(filePath) {
75
80
  }
76
81
  }
77
82
  exports.createPath = createPath;
78
- /**创建路径 path.sep 结尾时创建文件夹 同步
83
+ /**路径不存在时创建路径 path.sep结尾时创建文件夹 同步
79
84
  * @param {string} filePath - 待创建的路径
85
+ * @param {boolean} isDir - 强制验证一个文件夹
80
86
  * @returns {boolean} - 是否成功创建
81
87
  */
82
- function createPathSync(filePath) {
88
+ function createPathSync(filePath, isDir) {
89
+ if (isDir == true)
90
+ filePath = path.join(filePath, path.sep);
83
91
  try {
84
92
  if (filePath.endsWith(path.sep)) {
85
93
  fs.mkdirSync(filePath, { recursive: true });
@@ -96,6 +104,28 @@ function createPathSync(filePath) {
96
104
  }
97
105
  }
98
106
  exports.createPathSync = createPathSync;
107
+ /**确保路径存在 不存在时创建 异步
108
+ * @async
109
+ * @param {string} filePath - 待验证的路径
110
+ * @param {boolean} isDir - 强制验证一个文件夹
111
+ * @returns {Promise<boolean>} - 是否成功执行 创建或已存在
112
+ */
113
+ async function ensurePathExists(filePath, isDir) {
114
+ if (await pathExists(filePath))
115
+ return true;
116
+ return await createPath(filePath, isDir);
117
+ }
118
+ exports.ensurePathExists = ensurePathExists;
119
+ /**确保路径存在 不存在时创建 同步
120
+ * @param {string} filePath - 待验证的路径
121
+ * @returns {boolean} - 是否成功执行 创建或已存在
122
+ */
123
+ function ensurePathExistsSync(filePath, isDir) {
124
+ if (pathExistsSync(filePath))
125
+ return true;
126
+ return createPathSync(filePath, isDir);
127
+ }
128
+ exports.ensurePathExistsSync = ensurePathExistsSync;
99
129
  /**加载json文件 同步
100
130
  * Object (string)
101
131
  * @param {string} filePath - 文件路径
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zwa73/utils",
3
- "version": "1.0.16",
3
+ "version": "1.0.18",
4
4
  "description": "my utils",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -28,6 +28,7 @@ export function initField<T>(
28
28
  }
29
29
 
30
30
  /**验证路径 文件或文件夹 是否存在 异步
31
+ * @async
31
32
  * @param {string} filePath - 待验证的路径
32
33
  * @returns {Promise<boolean>} - 是否存在
33
34
  */
@@ -54,11 +55,16 @@ export function pathExistsSync(filePath: string):boolean{
54
55
  }
55
56
  }
56
57
 
57
- /**创建路径 path.sep 结尾时创建文件夹 异步
58
+ /**路径不存在时创建路径 path.sep结尾时创建文件夹 异步
59
+ * @async
58
60
  * @param {string} filePath - 待创建的路径
61
+ * @param {boolean} isDir - 强制创建一个文件夹
59
62
  * @returns {Promise<boolean>} - 是否成功创建
60
63
  */
61
- export async function createPath(filePath: string):Promise<boolean>{
64
+ export async function createPath(filePath: string, isDir?:boolean):Promise<boolean>{
65
+ if(isDir==true)
66
+ filePath = path.join(filePath,path.sep);
67
+
62
68
  try{
63
69
  if(filePath.endsWith(path.sep)){
64
70
  await fs.promises.mkdir(filePath, {recursive: true});
@@ -75,11 +81,15 @@ export async function createPath(filePath: string):Promise<boolean>{
75
81
  }
76
82
  }
77
83
 
78
- /**创建路径 path.sep 结尾时创建文件夹 同步
84
+ /**路径不存在时创建路径 path.sep结尾时创建文件夹 同步
79
85
  * @param {string} filePath - 待创建的路径
86
+ * @param {boolean} isDir - 强制验证一个文件夹
80
87
  * @returns {boolean} - 是否成功创建
81
88
  */
82
- export function createPathSync(filePath: string):boolean{
89
+ export function createPathSync(filePath: string, isDir?:boolean):boolean{
90
+ if(isDir==true)
91
+ filePath = path.join(filePath,path.sep);
92
+
83
93
  try{
84
94
  if(filePath.endsWith(path.sep)){
85
95
  fs.mkdirSync(filePath, {recursive: true});
@@ -96,6 +106,28 @@ export function createPathSync(filePath: string):boolean{
96
106
  }
97
107
  }
98
108
 
109
+ /**确保路径存在 不存在时创建 异步
110
+ * @async
111
+ * @param {string} filePath - 待验证的路径
112
+ * @param {boolean} isDir - 强制验证一个文件夹
113
+ * @returns {Promise<boolean>} - 是否成功执行 创建或已存在
114
+ */
115
+ export async function ensurePathExists(filePath: string, isDir?:boolean):Promise<boolean>{
116
+ if(await pathExists(filePath))
117
+ return true;
118
+ return await createPath(filePath,isDir);
119
+ }
120
+
121
+ /**确保路径存在 不存在时创建 同步
122
+ * @param {string} filePath - 待验证的路径
123
+ * @returns {boolean} - 是否成功执行 创建或已存在
124
+ */
125
+ export function ensurePathExistsSync(filePath: string, isDir?:boolean):boolean{
126
+ if(pathExistsSync(filePath))
127
+ return true;
128
+ return createPathSync(filePath,isDir);
129
+ }
130
+
99
131
  /**加载json文件 同步
100
132
  * Object (string)
101
133
  * @param {string} filePath - 文件路径