xjs-node 2.1.0 → 2.1.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.
@@ -12,9 +12,11 @@ export declare namespace UFile {
12
12
  function mkdir(p: MaybeArray<string>): boolean;
13
13
  function write(p: MaybeArray<string>, c: string): void;
14
14
  /**
15
- * remove a file. no error if the file to be removed doesn't exist.
15
+ * remove a file. default is no error if the file to be removed doesn't exist.
16
+ * @param p path of the file. if passed as an array those are joined.
17
+ * @param errorIfAbsent raise an error if the file to be removed doesn't exist.
16
18
  */
17
- function rm(p: MaybeArray<string>): void;
19
+ function rm(p: MaybeArray<string>, errorIfAbsent?: boolean): void;
18
20
  function exists(p: MaybeArray<string>): boolean;
19
21
  /**
20
22
  * return a file status. if the file of the status doesn't exist, this returns `null`.
@@ -22,6 +24,13 @@ export declare namespace UFile {
22
24
  function status(p: MaybeArray<string>): FileStatus;
23
25
  function read(p: MaybeArray<string>): Buffer;
24
26
  function read(p: MaybeArray<string>, encoding: BufferEncoding): string;
27
+ /**
28
+ * read specified file path as a json object.
29
+ * @param p file path
30
+ * @param d default value if the file path doesn't exist. default of this is `{}`.
31
+ * @param encoding encoding used by file reading.
32
+ */
33
+ function readAsJson<T>(p: MaybeArray<string>, d?: any, encoding?: BufferEncoding): T;
25
34
  function cp(from: MaybeArray<string>, to: MaybeArray<string>): void;
26
35
  function mv(from: MaybeArray<string>, to: MaybeArray<string>): void;
27
36
  function ls(p: MaybeArray<string>): string[];
@@ -56,12 +56,12 @@ var UFile;
56
56
  }
57
57
  UFile.write = write;
58
58
  /**
59
- * remove a file. no error if the file to be removed doesn't exist.
59
+ * remove a file. default is no error if the file to be removed doesn't exist.
60
+ * @param p path of the file. if passed as an array those are joined.
61
+ * @param errorIfAbsent raise an error if the file to be removed doesn't exist.
60
62
  */
61
- function rm(p) {
62
- const pt = (0, u_1.joinPath)(p);
63
- if (fs.existsSync(pt))
64
- fs.rmSync(pt, { recursive: true });
63
+ function rm(p, errorIfAbsent) {
64
+ fs.rmSync((0, u_1.joinPath)(p), { recursive: true, force: !errorIfAbsent });
65
65
  }
66
66
  UFile.rm = rm;
67
67
  function exists(p) {
@@ -83,6 +83,16 @@ var UFile;
83
83
  return fs.readFileSync(f, encoding);
84
84
  }
85
85
  UFile.read = read;
86
+ /**
87
+ * read specified file path as a json object.
88
+ * @param p file path
89
+ * @param d default value if the file path doesn't exist. default of this is `{}`.
90
+ * @param encoding encoding used by file reading.
91
+ */
92
+ function readAsJson(p, d = {}, encoding = "utf-8") {
93
+ return UFile.exists(p) ? JSON.parse(UFile.read(p, encoding)) : d;
94
+ }
95
+ UFile.readAsJson = readAsJson;
86
96
  function cp(from, to) {
87
97
  const f = (0, u_1.joinPath)(from), t = (0, u_1.joinPath)(to);
88
98
  if (!fs.existsSync(f))
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xjs-node",
3
- "version": "2.1.0",
3
+ "version": "2.1.1",
4
4
  "description": "library modules for nodejs + typescript that bundled general-purpose implementations.",
5
5
  "repository": {
6
6
  "type": "git",