xjs-node 2.2.1 → 3.0.0-alpha.1
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/build/func/u-file.d.ts +8 -9
- package/build/func/u-file.js +30 -20
- package/package.json +1 -1
package/build/func/u-file.d.ts
CHANGED
|
@@ -10,36 +10,35 @@ interface FileStatus {
|
|
|
10
10
|
}
|
|
11
11
|
export declare namespace UFile {
|
|
12
12
|
function mkdir(p: MaybeArray<string>): boolean;
|
|
13
|
-
function write(p: MaybeArray<string>, c: string): void
|
|
13
|
+
function write(p: MaybeArray<string>, c: string): Promise<void>;
|
|
14
14
|
/**
|
|
15
15
|
* remove a file. default is no error if the file to be removed doesn't exist.
|
|
16
16
|
* @param p path of the file. if passed as an array those are joined.
|
|
17
17
|
* @param errorIfAbsent raise an error if the file to be removed doesn't exist.
|
|
18
18
|
*/
|
|
19
|
-
function rm(p: MaybeArray<string>, errorIfAbsent?: boolean): void
|
|
19
|
+
function rm(p: MaybeArray<string>, errorIfAbsent?: boolean): Promise<void>;
|
|
20
20
|
function exists(p: MaybeArray<string>): boolean;
|
|
21
21
|
/**
|
|
22
22
|
* return a file status. if the file of the status doesn't exist, this returns `null`.
|
|
23
23
|
*/
|
|
24
24
|
function status(p: MaybeArray<string>): FileStatus;
|
|
25
|
-
function read(p: MaybeArray<string>): Buffer
|
|
26
|
-
function read(p: MaybeArray<string>, encoding: BufferEncoding): string
|
|
25
|
+
function read(p: MaybeArray<string>): Promise<Buffer>;
|
|
26
|
+
function read(p: MaybeArray<string>, encoding: BufferEncoding): Promise<string>;
|
|
27
27
|
/**
|
|
28
28
|
* read specified file path as a json object.
|
|
29
29
|
* @param p file path
|
|
30
30
|
* @param d default value if the file path doesn't exist. default of this is `{}`.
|
|
31
31
|
* @param encoding encoding used by file reading. default is `utf-8`.
|
|
32
32
|
*/
|
|
33
|
-
function readAsJson<T>(p: MaybeArray<string>, d?: any, encoding?: BufferEncoding): T
|
|
34
|
-
function cp(from: MaybeArray<string>, to: MaybeArray<string>): void
|
|
35
|
-
function mv(from: MaybeArray<string>, to: MaybeArray<string>): void
|
|
33
|
+
function readAsJson<T>(p: MaybeArray<string>, d?: any, encoding?: BufferEncoding): Promise<T>;
|
|
34
|
+
function cp(from: MaybeArray<string>, to: MaybeArray<string>): Promise<void>;
|
|
35
|
+
function mv(from: MaybeArray<string>, to: MaybeArray<string>): Promise<void>;
|
|
36
36
|
function ls(p: MaybeArray<string>): string[];
|
|
37
37
|
/**
|
|
38
38
|
* check availability to export a file with specified directory and file name.
|
|
39
|
-
* if it doesn't,
|
|
39
|
+
* if it doesn't, this retries to check after appending incremental number to the filename excluding an extension (e.g. `aaa_1.txt`).
|
|
40
40
|
* @param dir destination directory path.
|
|
41
41
|
* @param fname file name wanna export to.
|
|
42
|
-
* @returns exportable file path.
|
|
43
42
|
*/
|
|
44
43
|
function reserveFilePath(dir: MaybeArray<string>, fname: string): string;
|
|
45
44
|
/**
|
package/build/func/u-file.js
CHANGED
|
@@ -52,7 +52,7 @@ var UFile;
|
|
|
52
52
|
}
|
|
53
53
|
UFile.mkdir = mkdir;
|
|
54
54
|
function write(p, c) {
|
|
55
|
-
fs.
|
|
55
|
+
return new Promise((rs, rj) => fs.writeFile((0, u_1.joinPath)(p), c, e => e ? rj(e) : rs()));
|
|
56
56
|
}
|
|
57
57
|
UFile.write = write;
|
|
58
58
|
/**
|
|
@@ -61,7 +61,7 @@ var UFile;
|
|
|
61
61
|
* @param errorIfAbsent raise an error if the file to be removed doesn't exist.
|
|
62
62
|
*/
|
|
63
63
|
function rm(p, errorIfAbsent) {
|
|
64
|
-
fs.
|
|
64
|
+
return new Promise((rs, rj) => fs.rm((0, u_1.joinPath)(p), { recursive: true, force: !errorIfAbsent }, e => e ? rj(e) : rs()));
|
|
65
65
|
}
|
|
66
66
|
UFile.rm = rm;
|
|
67
67
|
function exists(p) {
|
|
@@ -77,10 +77,13 @@ var UFile;
|
|
|
77
77
|
}
|
|
78
78
|
UFile.status = status;
|
|
79
79
|
function read(p, encoding) {
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
80
|
+
return new Promise((rs, rj) => {
|
|
81
|
+
const f = (0, u_1.joinPath)(p);
|
|
82
|
+
if (fs.existsSync(f))
|
|
83
|
+
fs.readFile(f, encoding, (e, d) => e ? rj(e) : rs(d));
|
|
84
|
+
else
|
|
85
|
+
rj(new xjs_common_1.XjsErr(s_errCode, `No file found => ${f}`));
|
|
86
|
+
});
|
|
84
87
|
}
|
|
85
88
|
UFile.read = read;
|
|
86
89
|
/**
|
|
@@ -89,22 +92,28 @@ var UFile;
|
|
|
89
92
|
* @param d default value if the file path doesn't exist. default of this is `{}`.
|
|
90
93
|
* @param encoding encoding used by file reading. default is `utf-8`.
|
|
91
94
|
*/
|
|
92
|
-
function readAsJson(p, d = {}, encoding = "utf-8") {
|
|
93
|
-
return UFile.exists(p) ? JSON.parse(UFile.read(p, encoding)) : d;
|
|
95
|
+
async function readAsJson(p, d = {}, encoding = "utf-8") {
|
|
96
|
+
return UFile.exists(p) ? JSON.parse(await UFile.read(p, encoding)) : d;
|
|
94
97
|
}
|
|
95
98
|
UFile.readAsJson = readAsJson;
|
|
96
99
|
function cp(from, to) {
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
100
|
+
return new Promise((rs, rj) => {
|
|
101
|
+
const f = (0, u_1.joinPath)(from), t = (0, u_1.joinPath)(to);
|
|
102
|
+
if (fs.existsSync(f))
|
|
103
|
+
fs.copyFile(f, t, e => e ? rj(e) : rs());
|
|
104
|
+
else
|
|
105
|
+
rj(new xjs_common_1.XjsErr(s_errCode, `No file found => ${f}`));
|
|
106
|
+
});
|
|
101
107
|
}
|
|
102
108
|
UFile.cp = cp;
|
|
103
109
|
function mv(from, to) {
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
110
|
+
return new Promise((rs, rj) => {
|
|
111
|
+
const f = (0, u_1.joinPath)(from), t = (0, u_1.joinPath)(to);
|
|
112
|
+
if (fs.existsSync(f))
|
|
113
|
+
fs.rename(f, t, e => e ? rj(e) : rs());
|
|
114
|
+
else
|
|
115
|
+
rj(new xjs_common_1.XjsErr(s_errCode, `No file found => ${f}`));
|
|
116
|
+
});
|
|
108
117
|
}
|
|
109
118
|
UFile.mv = mv;
|
|
110
119
|
function ls(p) {
|
|
@@ -116,10 +125,9 @@ var UFile;
|
|
|
116
125
|
UFile.ls = ls;
|
|
117
126
|
/**
|
|
118
127
|
* check availability to export a file with specified directory and file name.
|
|
119
|
-
* if it doesn't,
|
|
128
|
+
* if it doesn't, this retries to check after appending incremental number to the filename excluding an extension (e.g. `aaa_1.txt`).
|
|
120
129
|
* @param dir destination directory path.
|
|
121
130
|
* @param fname file name wanna export to.
|
|
122
|
-
* @returns exportable file path.
|
|
123
131
|
*/
|
|
124
132
|
function reserveFilePath(dir, fname) {
|
|
125
133
|
const pt = (0, u_1.joinPath)(dir);
|
|
@@ -128,8 +136,10 @@ var UFile;
|
|
|
128
136
|
if (!fname || fname.match(/[\\/:*?"<>|]/))
|
|
129
137
|
throw new xjs_common_1.XjsErr(s_errCode, "Specified filename is invalid due to empty or including disallowed characters.");
|
|
130
138
|
let dest = (0, u_1.joinPath)(pt, fname), i = 1;
|
|
131
|
-
while (fs.existsSync(dest))
|
|
132
|
-
|
|
139
|
+
while (fs.existsSync(dest)) {
|
|
140
|
+
const ext = fname.match(/^(.+)(\.[^\.]+)$/);
|
|
141
|
+
dest = (0, u_1.joinPath)(pt, `${ext ? ext[1] : fname}_${i++}${ext ? ext[2] : ""}`);
|
|
142
|
+
}
|
|
133
143
|
return dest;
|
|
134
144
|
}
|
|
135
145
|
UFile.reserveFilePath = reserveFilePath;
|