js-dev-tool 1.2.8 → 1.2.10
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 +1 -1
- package/tsconfig.json +1 -1
- package/utils.d.ts +15 -0
- package/utils.js +24 -2
package/package.json
CHANGED
package/tsconfig.json
CHANGED
package/utils.d.ts
CHANGED
|
@@ -84,6 +84,21 @@ 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
|
+
* @param {T} json file path.
|
|
90
|
+
* @param {string} path file path.
|
|
91
|
+
* @param {TWriteJsonCallback} [callback]
|
|
92
|
+
* @returns {void} description
|
|
93
|
+
*/
|
|
94
|
+
declare function writeJson<T>(json: T, path: string, callback?: TWriteJsonCallback): void;
|
|
95
|
+
/**
|
|
96
|
+
* @template T
|
|
97
|
+
* @param {T} json
|
|
98
|
+
* @param {number} [indent=2]
|
|
99
|
+
* @returns {string}
|
|
100
|
+
*/
|
|
101
|
+
declare function formatedStringify<T>(json: T, indent?: number): string;
|
|
87
102
|
/**
|
|
88
103
|
* @param path
|
|
89
104
|
* @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
|
|
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,9 @@ function readText(from, callback) {
|
|
|
150
150
|
* @template T
|
|
151
151
|
* @typedef {TBD<(err: any, data: TypedRecord<T>) => void>} TReadJsonCallback
|
|
152
152
|
*/
|
|
153
|
+
/**
|
|
154
|
+
* @typedef {TBD<() => void>} TWriteJsonCallback
|
|
155
|
+
*/
|
|
153
156
|
/**
|
|
154
157
|
* NOTE: when callback specified, returns undefined
|
|
155
158
|
*
|
|
@@ -171,6 +174,23 @@ function readJson(path, callback) {
|
|
|
171
174
|
}
|
|
172
175
|
return /** @type {R} */ (undefined);
|
|
173
176
|
}
|
|
177
|
+
/**
|
|
178
|
+
* @template T
|
|
179
|
+
* @param {T} json file path.
|
|
180
|
+
* @param {string} path file path.
|
|
181
|
+
* @param {TWriteJsonCallback} [callback]
|
|
182
|
+
* @returns {void} description
|
|
183
|
+
*/
|
|
184
|
+
function writeJson(json, path, callback) {
|
|
185
|
+
writeText(formatedStringify(json), path, callback);
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* @template T
|
|
189
|
+
* @param {T} json
|
|
190
|
+
* @param {number} [indent=2]
|
|
191
|
+
* @returns {string}
|
|
192
|
+
*/
|
|
193
|
+
const formatedStringify = (json, indent = 2) => JSON.stringify(json, null, indent);
|
|
174
194
|
/**
|
|
175
195
|
* use "rm-cstyle-cmts"
|
|
176
196
|
*
|
|
@@ -472,6 +492,8 @@ module.exports = {
|
|
|
472
492
|
writeText,
|
|
473
493
|
readText,
|
|
474
494
|
readJson,
|
|
495
|
+
writeJson,
|
|
496
|
+
formatedStringify,
|
|
475
497
|
walkDirSync,
|
|
476
498
|
compressScript,
|
|
477
499
|
compressScript2,
|