xjs-node 2.3.0 → 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.
@@ -10,29 +10,29 @@ 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.
@@ -52,7 +52,7 @@ var UFile;
52
52
  }
53
53
  UFile.mkdir = mkdir;
54
54
  function write(p, c) {
55
- fs.writeFileSync((0, u_1.joinPath)(p), c);
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.rmSync((0, u_1.joinPath)(p), { recursive: true, force: !errorIfAbsent });
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
- const f = (0, u_1.joinPath)(p);
81
- if (!fs.existsSync(f))
82
- throw new xjs_common_1.XjsErr(s_errCode, `No file found => ${f}`);
83
- return fs.readFileSync(f, encoding);
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
- const f = (0, u_1.joinPath)(from), t = (0, u_1.joinPath)(to);
98
- if (!fs.existsSync(f))
99
- throw new xjs_common_1.XjsErr(s_errCode, `No file found => ${f}`);
100
- fs.copyFileSync(f, t);
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
- const f = (0, u_1.joinPath)(from), t = (0, u_1.joinPath)(to);
105
- if (!fs.existsSync(f))
106
- throw new xjs_common_1.XjsErr(s_errCode, `No file found => ${f}`);
107
- fs.renameSync(f, t);
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) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xjs-node",
3
- "version": "2.3.0",
3
+ "version": "3.0.0-alpha.1",
4
4
  "description": "library modules for nodejs + typescript that bundled general-purpose implementations.",
5
5
  "repository": {
6
6
  "type": "git",