js-dev-tool 1.2.7 → 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/basic-types.d.ts CHANGED
@@ -24,12 +24,12 @@ type TBD<T> = T | undefined;
24
24
  type TBC<T> = T | null;
25
25
  type Maybe<T> = T | null | undefined;
26
26
  /**
27
- * T is falsy then return A, trusy then B
28
- *
27
+ * T is falsy then return A, truthy then B
28
+ *
29
29
  * ```ts
30
30
  * type ConditionalX<T, A, B> = T extends (void | false | undefined) ? A : B // <- maybe not works
31
31
  * type Conditional<T, A, B> = void extends T ? A : T extends (void | false | undefined) ? A : B;
32
- *
32
+ *
33
33
  * function x<T extends true | void, R extends ConditionalX<T, string, string[]>>(need?: T): R {
34
34
  * return (need? "": [""]) as R;
35
35
  * }
@@ -80,15 +80,18 @@ type Prettify<T> = { [K in keyof T]: T[K] } & {};
80
80
  type Constructor<T> = new (...args: any[]) => T;
81
81
  /**
82
82
  * mark a specific property as `required`
83
- *
84
- * + About `Merge` parameter - Accepts values ​​of `1` or `0`.
83
+ *
84
+ * + About `Flatten` parameter - Accepts values ​​of `1` or `0`.
85
85
  * - If it is **1**, the types are displayed concatenated.
86
86
  * - If it is **0**, the types are displayed separated by "&".
87
+ *
88
+ * @date Renamed due to reasons. (`RequireThese` to `RequireKeys`)
87
89
  */
88
- type RequireThese<
89
- T, K extends keyof T, Merge extends 1 | 0 = 0,
90
- Intersected = T & Required<Pick<T, K>>
91
- > = Merge extends 1 ? Prettify<Intersected> : Intersected;
90
+ type RequireKeys<
91
+ T, K extends keyof T = keyof T,
92
+ Flatten extends 1 | 0 = 0,
93
+ Intersected = T & { [P in K]-?: T[P] }
94
+ > = Flatten extends 1 ? Prettify<Intersected> : Intersected;
92
95
  /**
93
96
  * pick any `property` as excludes
94
97
  */
@@ -118,8 +121,9 @@ type TypedCtor<T> = {
118
121
  interface ITypedCtor<T> extends TypedCtor<T> {}
119
122
  /**
120
123
  * Array.sort etc...
124
+ * @date 2026/03/03 - Improved to allow comparison of different types
121
125
  */
122
- declare type TComparator<T> = (a: T, b: T) => number;
126
+ declare type TComparator<T, T2 = T> = (a: T, b: T2) => number;
123
127
  /**
124
128
  * Generates all combinations of the string S. The order of characters does not matter.
125
129
  * @template S - The target string type
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "js-dev-tool",
3
- "version": "1.2.7",
3
+ "version": "1.2.9",
4
4
  "bin": {
5
5
  "jstool": "tools.js"
6
6
  },
package/tools.js CHANGED
@@ -9,16 +9,14 @@
9
9
  */
10
10
  // @ts-check
11
11
  "use strict";
12
+ require("colors.ts");
12
13
  const fs = require("fs");
13
14
  const path = require("path");
14
15
  const rmc = require("rm-cstyle-cmts");
15
16
  /* utilities module by own. */
16
17
  const utils = require("./utils");
17
- // @ts-expect- error
18
18
  global.params = require("tin-args")();
19
- // @ts-expect- error
20
19
  global.verbose = params.verb;
21
- // @ts-expect- error
22
20
  global.isArray = Array.isArray;
23
21
  require("./tool-lib/ps").globalize(fs, utils);
24
22
  utils.log(params);
@@ -83,21 +81,28 @@ const ToolFunctions = {
83
81
  fn() {
84
82
  const re = params.regex;
85
83
  const after = params.after || "";
84
+ let targets = params.args || params.targets
85
+ if (typeof targets === "string") targets = [targets];
86
+ params.targets = targets;
86
87
  if (re) {
87
88
  processSources(this.taskName, (data) => data.replace(re, after), {
88
89
  base: "",
89
- targets: params.args || params.targets,
90
90
  });
91
91
  }
92
92
  },
93
- help: `jstool -cmd replace [-after <replacement>] -regex \"/^\\s+<!--[\\s\\S]+?-->(?:\\r?\\n)?/gm\" [-targets "<path>,<path>,..." | <args: file, file file...>]
94
- note:
95
- targets - must be array type arg, "['<path>', '<path>',...]" or "<path>,<path>,..."
96
- note: targets - can use args parameter instead
97
- It is better to use the <args: file, file file...>
98
- e.g - jstool -cmd replace -after ".." -regex "re/(?<=reference path=\")(\\.)(?=\\/index.d.ts\")/" build/**/*.js
99
- ^^^^^^^^^^^^^
100
- `,
93
+ get help() {
94
+ return `jstool -cmd replace [-after <replacement>] -regex \"/^\\s+<!--[\\s\\S]+?-->(?:\\r?\\n)?/gm\" [-targets "<path>,<path>,..." | <args: file, file file...>]
95
+
96
+ ${"Options:".red}
97
+ ${"after".cyan} ${`: If omitted, "" (empty string) is used, meaning that the matched string is deleted.`.gray(16)}
98
+ ${"targets".cyan}${`: Single path string or Array type arg, "['<path>', '<path>',...]" or "<path>,<path>,..."`.gray(16)}
99
+
100
+ ${"remarks".magenta}${": targets - can use args parameter instead".gray(16)}${`
101
+ It is better to use the <args: file, file file...>
102
+ e.g - ${"jstool".yellow} ${"-cmd".green} ${"replace".cyan} ${"-after".green} ${`".."`.red} ${"-regex".green} ${String.raw`'re/(?<=reference path=")(\.)(?=\/index.d.ts")/'`.magenta} ${"build/**/*.js".gray(16)}
103
+ ^^^^^^^^^^^^^
104
+ `.gray(16)}`;
105
+ },
101
106
  },
102
107
  version: {
103
108
  taskName: "version",
@@ -287,19 +292,11 @@ function isJSToolEntry(entry) {
287
292
  if (typeof entry !== "object") return false;
288
293
  return typeof entry.fn === "function" && typeof entry.help === "string";
289
294
  }
290
- let colorEnable = false;
291
- function enableColors() {
292
- if (!colorEnable) {
293
- require("colors.ts");
294
- colorEnable = true;
295
- }
296
- }
297
295
  /**
298
296
  * @param {string} cmd
299
297
  * @returns {void}
300
298
  */
301
299
  function printHelp(cmd) {
302
- enableColors();
303
300
  const entry = ToolFunctions[cmd];
304
301
  console.log(`${cmd.yellow + " help:".green} ${entry.help.gray(8)}`);
305
302
  }
@@ -318,7 +315,6 @@ if (params.cmd) {
318
315
  process.cwd(),
319
316
  );
320
317
  } else if (params.v) {
321
- enableColors();
322
318
  const thisVersion = require("./package.json").version;
323
319
  console.log(
324
320
  `${"jstool".magenta} in "js-dev-scripts", version: ${thisVersion.green}
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,