js-dev-tool 1.2.19 → 1.2.20

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/README.md CHANGED
@@ -30,10 +30,11 @@ This section reflects the current `package.json#exports` and `package.json#files
30
30
  | --- | --- |
31
31
  | `js-dev-tool` | Root namespace object with `{ progress, common, utils }` |
32
32
  | `js-dev-tool/utils` | File, JSON, CLI, TSV/CSV, and small utility helpers |
33
- | `js-dev-tool/common` | File-system and terminal rendering primitives |
33
+ | `js-dev-tool/common` | File-system helpers, terminal rendering primitives, and `isMain` |
34
34
  | `js-dev-tool/progress` | Progress renderer, spinner helpers, webpack / browserify progress hooks |
35
35
  | `js-dev-tool/progress/progress-extras` | Environment and webpack-version helpers |
36
36
  | `js-dev-tool/extras/algorithms` | Binary-search helper |
37
+ | `js-dev-tool/extras/gzip-tool` | Deterministic gzip + Base64 source-literal helpers |
37
38
  | `js-dev-tool/extras/jsonl` | JSONL readers, JSONL-to-array/map helpers, and `jsonMinify` |
38
39
  | `js-dev-tool/extras/json-minify` | `jsonMinify` |
39
40
  | `js-dev-tool/extras/progress-light` | Small fixed-frame spinner |
@@ -93,6 +94,20 @@ console.log(map["1"], deps.slice(0, 5));
93
94
  - `createLogStreamAndResolvePath(logPath)`
94
95
  - `renderLine(msg?, row?)`
95
96
  - `cursor(enabled, output?)`
97
+ - `isMain(moduleOrPathOrMeta)`
98
+
99
+ `isMain(...)` is the cross-module-system entry-point check. It accepts:
100
+
101
+ - CommonJS: `module` or `__filename`
102
+ - ESM: `import.meta` or `fileURLToPath(import.meta.url)`
103
+
104
+ ```js
105
+ const { isMain } = require("js-dev-tool/common");
106
+
107
+ if (isMain(module)) {
108
+ console.log("run only when executed directly");
109
+ }
110
+ ```
96
111
 
97
112
  ## `progress`
98
113
 
@@ -190,6 +205,34 @@ async function main() {
190
205
  main().catch(console.error);
191
206
  ```
192
207
 
208
+ ### `js-dev-tool/extras/gzip-tool`
209
+
210
+ Exports:
211
+
212
+ - `gzip(data, useZeroTimestamp?)`
213
+ - `toBase64SourceLiteral(rawData, wrapSize?)`
214
+
215
+ Use this module when you want deterministic gzip output and a clean way to embed the compressed bytes into JavaScript source.
216
+
217
+ Key behaviors:
218
+
219
+ - `gzip(...)` compresses with gzip level `9`
220
+ - `gzip(..., true)` zeroes the gzip `mtime` header so repeated builds with the same input stay byte-stable
221
+ - `toBase64SourceLiteral(...)` wraps Base64 output with trailing `\` line continuations so the result can be pasted into a multi-line JavaScript string literal
222
+ - `wrapSize` must be a finite integer between `1` and `2048`
223
+
224
+ Example:
225
+
226
+ ```js
227
+ const { gzip, toBase64SourceLiteral } = require("js-dev-tool/extras/gzip-tool");
228
+
229
+ const compressed = gzip("console.log(\"hello\");");
230
+ const sourceLiteral = toBase64SourceLiteral(compressed, 76);
231
+
232
+ console.log(compressed.length);
233
+ console.log(sourceLiteral);
234
+ ```
235
+
193
236
  ### `js-dev-tool/extras/progress-light`
194
237
 
195
238
  Exports:
package/common/index.d.ts CHANGED
@@ -43,17 +43,29 @@ export function renderLine(msg?: string, row?: number): void;
43
43
  */
44
44
  export function cursor(enabled: boolean, output?: NodeJS.WriteStream): void;
45
45
  /**
46
- * Returns `true` if the given module is the entry point of the current process.
46
+ * Minimal `import.meta`-compatible shape accepted by `isMain`.
47
47
  *
48
- * Accepts either a `module` object (CommonJS) or a file path string,
49
- * making it usable from both CommonJS and ESM contexts.
48
+ * Only `url` is required. `resolve` stays optional so the same shape works
49
+ * across older and newer Node.js ESM runtimes.
50
+ */
51
+ declare type TImportMeta = {
52
+ url: string;
53
+ resolve?: (filePath: string) => string;
54
+ };
55
+ /**
56
+ * Returns `true` when the provided CommonJS module, absolute file path, or
57
+ * ESM `import.meta` object represents the current process entry point.
58
+ *
59
+ * This gives one API that works across both module systems:
60
+ * - CommonJS: `module` or `__filename`
61
+ * - ESM: `import.meta` or `fileURLToPath(import.meta.url)`
50
62
  *
51
63
  * Equivalent to `import.meta.main` in Deno / Bun,
52
64
  * or `process.argv[1] === fileURLToPath(import.meta.url)` in ESM.
53
65
  *
54
- * @param {Partial<NodeJS.Module> | string} modOrFilePath
55
- * Pass `module` or `__filename` (CommonJS),
56
- * or `fileURLToPath(import.meta.url)` (ESM).
66
+ * @param {Partial<NodeJS.Module> | TImportMeta | string} modOrPathOrMeta
67
+ * Pass `module` or `__filename` from CommonJS,
68
+ * or `import.meta` / `fileURLToPath(import.meta.url)` from ESM.
57
69
  * @returns {boolean} `true` if the calling module is the entry point.
58
70
  *
59
71
  * @example
@@ -67,7 +79,12 @@ export function cursor(enabled: boolean, output?: NodeJS.WriteStream): void;
67
79
  * if (isMain(__filename)) { main(); }
68
80
  *
69
81
  * @example
70
- * // ESM — via fileURLToPath:
82
+ * // ESM — via import.meta object:
83
+ * import { isMain } from "js-dev-tool/common";
84
+ * if (isMain(import.meta)) { main(); }
85
+ *
86
+ * @example
87
+ * // ESM — via fileURLToPath(import.meta.url):
71
88
  * import { isMain } from "js-dev-tool/common";
72
89
  * import { fileURLToPath } from "node:url";
73
90
  * if (isMain(fileURLToPath(import.meta.url))) { main(); }
@@ -76,4 +93,4 @@ export function cursor(enabled: boolean, output?: NodeJS.WriteStream): void;
76
93
  * @verified CJS: Confirmed working on Node.js v11.15.0 — v26.3.0.
77
94
  * @verified ESM: Confirmed working on Node.js v12.22.11 — v26.3.0.
78
95
  */
79
- export function isMain(modOrFilePath: Partial<NodeJS.Module> | string): boolean;
96
+ export function isMain(modOrPathOrMeta: Partial<NodeJS.Module> | TImportMeta | string): boolean;
package/common/index.js CHANGED
@@ -13,6 +13,7 @@
13
13
  const fs = require("fs");
14
14
  const path = require("path");
15
15
  const rl = require("readline");
16
+ const { fileURLToPath } = require("url");
16
17
  /**
17
18
  * @param {string} dest
18
19
  */
@@ -56,17 +57,30 @@ const cursor = (enabled, output = process.stderr) => {
56
57
  }
57
58
  };
58
59
  /**
59
- * Returns `true` if the given module is the entry point of the current process.
60
+ * Minimal `import.meta`-compatible shape accepted by {@link isMain}.
60
61
  *
61
- * Accepts either a `module` object (CommonJS) or a file path string,
62
- * making it usable from both CommonJS and ESM contexts.
62
+ * Only `url` is required. `resolve` stays optional so the same shape works
63
+ * across older and newer Node.js ESM runtimes.
64
+ *
65
+ * @typedef {{
66
+ * url: string;
67
+ * resolve?: (filePath: string) => string;
68
+ * }} TImportMeta
69
+ */
70
+ /**
71
+ * Returns `true` when the provided CommonJS module, absolute file path, or
72
+ * ESM `import.meta` object represents the current process entry point.
73
+ *
74
+ * This gives one API that works across both module systems:
75
+ * - CommonJS: `module` or `__filename`
76
+ * - ESM: `import.meta` or `fileURLToPath(import.meta.url)`
63
77
  *
64
78
  * Equivalent to `import.meta.main` in Deno / Bun,
65
79
  * or `process.argv[1] === fileURLToPath(import.meta.url)` in ESM.
66
80
  *
67
- * @param {Partial<NodeJS.Module> | string} modOrFilePath
68
- * Pass `module` or `__filename` (CommonJS),
69
- * or `fileURLToPath(import.meta.url)` (ESM).
81
+ * @param {Partial<NodeJS.Module> | TImportMeta | string} modOrPathOrMeta
82
+ * Pass `module` or `__filename` from CommonJS,
83
+ * or `import.meta` / `fileURLToPath(import.meta.url)` from ESM.
70
84
  * @returns {boolean} `true` if the calling module is the entry point.
71
85
  *
72
86
  * @example
@@ -80,7 +94,12 @@ const cursor = (enabled, output = process.stderr) => {
80
94
  * if (isMain(__filename)) { main(); }
81
95
  *
82
96
  * @example
83
- * // ESM — via fileURLToPath:
97
+ * // ESM — via import.meta object:
98
+ * import { isMain } from "js-dev-tool/common";
99
+ * if (isMain(import.meta)) { main(); }
100
+ *
101
+ * @example
102
+ * // ESM — via fileURLToPath(import.meta.url):
84
103
  * import { isMain } from "js-dev-tool/common";
85
104
  * import { fileURLToPath } from "node:url";
86
105
  * if (isMain(fileURLToPath(import.meta.url))) { main(); }
@@ -89,11 +108,18 @@ const cursor = (enabled, output = process.stderr) => {
89
108
  * @verified CJS: Confirmed working on Node.js v11.15.0 — v26.3.0.
90
109
  * @verified ESM: Confirmed working on Node.js v12.22.11 — v26.3.0.
91
110
  */
92
- function isMain(modOrFilePath) {
93
- if (typeof modOrFilePath === "string") {
94
- return process.argv[1] === modOrFilePath;
111
+ function isMain(modOrPathOrMeta) {
112
+ /** @type {string=} */
113
+ let pathString;
114
+ if (typeof modOrPathOrMeta === "string") {
115
+ pathString = modOrPathOrMeta;
116
+ } else if ("url" in modOrPathOrMeta /* && "resolve" in modOrPathOrMeta */) {
117
+ pathString = fileURLToPath(modOrPathOrMeta.url);
118
+ }
119
+ if (pathString) {
120
+ return process.argv[1] === pathString;
95
121
  }
96
- return require.main === modOrFilePath;
122
+ return require.main === modOrPathOrMeta;
97
123
  }
98
124
  module.exports = {
99
125
  checkParentDirectory,
@@ -49,11 +49,16 @@ function toBase64SourceLiteral(rawData, wrapSize = 100) {
49
49
  if (wrapSize > 2048) throw new Error(`"wrapSize" is expected to be 2048 or less - ${wrapSize}`);
50
50
  if (wrapSize < 1) throw new Error(`A wrapSize of 0 or less will result in an infinite loop, so please make it 1 or greater. - ${wrapSize}`);
51
51
  const base64 = Buffer.from(rawData).toString("base64");
52
- let wrapped = "";
53
- for (let offset = 0, L = base64.length; offset < L;) {
54
- wrapped += `${base64.slice(offset, offset += wrapSize)}\\\n`;
52
+ const L = base64.length;
53
+ if (!L) return "";
54
+ let wrapped = "", lineSuffix = "\\\n";
55
+ for (let offset = 0; offset < L;) {
56
+ if (
57
+ (offset + wrapSize) >= L
58
+ ) lineSuffix = "";
59
+ wrapped += (base64.slice(offset, offset += wrapSize) + lineSuffix);
55
60
  }
56
- return wrapped.slice(0, -2);
61
+ return wrapped;
57
62
  }
58
63
  module.exports = {
59
64
  gzip, toBase64SourceLiteral
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "js-dev-tool",
3
- "version": "1.2.19",
3
+ "version": "1.2.20",
4
4
  "type": "commonjs",
5
5
  "bin": {
6
6
  "jstool": "tools.js",
@@ -84,7 +84,7 @@
84
84
  "!tool.sh"
85
85
  ],
86
86
  "peerDependencies": {
87
- "archiver": "^4.0.1",
87
+ "archiver": "^7.0.1",
88
88
  "webpack": "*"
89
89
  },
90
90
  "dependencies": {
@@ -95,7 +95,7 @@
95
95
  "replace": "^1.2.2",
96
96
  "rm-cstyle-cmts": "^3.4.4",
97
97
  "terser": "^5.48.0",
98
- "tin-args": "^0.1.6",
98
+ "tin-args": "^0.1.7",
99
99
  "ts-cc-map": "^1.1.0"
100
100
  }
101
101
  }