js-dev-tool 1.2.8 → 1.2.9

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "js-dev-tool",
3
- "version": "1.2.8",
3
+ "version": "1.2.9",
4
4
  "bin": {
5
5
  "jstool": "tools.js"
6
6
  },
package/tsconfig.json CHANGED
@@ -13,7 +13,7 @@
13
13
  "checkJs": true,
14
14
  "target": "es2019",
15
15
  "module": "nodenext",
16
- "outDir": "./build",
16
+ "outDir": "./dts",
17
17
  // "baseUrl": ".",
18
18
  "rootDir": ".",
19
19
  "paths": {
package/utils.d.ts CHANGED
@@ -84,6 +84,22 @@ export function readText<C extends TBD<TFsCallback>, R extends undefined extends
84
84
  * @param [callback]
85
85
  */
86
86
  export function readJson<T, C extends TBD<TReadJsonCallback<string>>, R extends undefined extends C ? TypedRecord<T> : void>(path: string, callback?: C): R;
87
+ /**
88
+ * @template T
89
+ * @template {TWriteJsonCallback<T>} C description
90
+ * @param {TypedRecord<T>} json file path.
91
+ * @param {string} path file path.
92
+ * @param {C} [callback]
93
+ * @returns {void} description
94
+ */
95
+ declare function writeJson<T, C extends TWriteJsonCallback<T>>(json: TypedRecord<T>, path: string, callback?: C): void;
96
+ /**
97
+ * @template T
98
+ * @param {TypedRecord<T>} json
99
+ * @param {number} [indent=2]
100
+ * @returns
101
+ */
102
+ declare function formatedStringify<T>(json: TypedRecord<T>, indent?: number): string;
87
103
  /**
88
104
  * @param path
89
105
  * @param handler
package/utils.js CHANGED
@@ -103,12 +103,12 @@ function writeText(content, dest, callback) {
103
103
  lib.checkParentDirectory(dest);
104
104
  const ws = fs.createWriteStream(dest);
105
105
  ws.on("error", function (err) {
106
- log("WriteStream.error evnet!", arguments);
106
+ log("WriteStream.error event!", err);
107
107
  }).on("close", function (/*no args*/) {
108
108
  callback && callback();
109
109
  });
110
110
  if (content instanceof Buffer) {
111
- content = content.toString();
111
+ content = content.toString("utf8");
112
112
  }
113
113
  if (typeof content === "string") {
114
114
  const success = ws.write(content);
@@ -150,6 +150,10 @@ function readText(from, callback) {
150
150
  * @template T
151
151
  * @typedef {TBD<(err: any, data: TypedRecord<T>) => void>} TReadJsonCallback
152
152
  */
153
+ /**
154
+ * @template T
155
+ * @typedef {TBD<() => void>} TWriteJsonCallback
156
+ */
153
157
  /**
154
158
  * NOTE: when callback specified, returns undefined
155
159
  *
@@ -171,6 +175,24 @@ function readJson(path, callback) {
171
175
  }
172
176
  return /** @type {R} */ (undefined);
173
177
  }
178
+ /**
179
+ * @template T
180
+ * @template {TWriteJsonCallback<T>} C description
181
+ * @param {TypedRecord<T>} json file path.
182
+ * @param {string} path file path.
183
+ * @param {C} [callback]
184
+ * @returns {void} description
185
+ */
186
+ function writeJson(json, path, callback) {
187
+ writeText(formatedStringify(json), path, callback);
188
+ }
189
+ /**
190
+ * @template T
191
+ * @param {TypedRecord<T>} json
192
+ * @param {number} [indent=2]
193
+ * @returns
194
+ */
195
+ const formatedStringify = (json, indent = 2) => JSON.stringify(json, null, indent);
174
196
  /**
175
197
  * use "rm-cstyle-cmts"
176
198
  *
@@ -472,6 +494,8 @@ module.exports = {
472
494
  writeText,
473
495
  readText,
474
496
  readJson,
497
+ writeJson,
498
+ formatedStringify,
475
499
  walkDirSync,
476
500
  compressScript,
477
501
  compressScript2,