binary-collections 2.0.7 → 2.0.8

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.
Files changed (150) hide show
  1. package/bin/dir-tree.cmd +7 -0
  2. package/bin/git-diff +4 -0
  3. package/bin/git-diff.cmd +5 -2
  4. package/bin/{git-fix-encoding → git-fix} +1 -4
  5. package/bin/git-fix.cmd +7 -0
  6. package/bin/nodekill +0 -0
  7. package/bin/nodekill.ps1 +0 -0
  8. package/bin/{submodule → submodule.txt} +0 -0
  9. package/lib/binary-collections-config.cjs +14 -0
  10. package/lib/binary-collections-config.d.mts +18 -0
  11. package/lib/binary-collections-config.d.ts +16 -0
  12. package/lib/binary-collections-config.js +39 -0
  13. package/lib/binary-collections-config.mjs +6 -0
  14. package/lib/binary-collections.cjs +105 -0
  15. package/lib/binary-collections.d.cts +2 -0
  16. package/lib/binary-collections.d.mts +121 -0
  17. package/lib/binary-collections.d.ts +121 -0
  18. package/lib/binary-collections.mjs +108 -0
  19. package/lib/chunk-4BYBVEYC.mjs +30 -0
  20. package/lib/{chunk-FB6YIQYR.mjs → chunk-AASHBCRW.mjs} +17 -2
  21. package/lib/chunk-APBWENF6.mjs +135 -0
  22. package/lib/{chunk-4LEXWIIF.mjs → chunk-DPKAJKFO.mjs} +2 -4
  23. package/lib/chunk-EGSSKVDH.mjs +66 -0
  24. package/lib/{chunk-3LOB2P54.mjs → chunk-G3THLIDT.mjs} +3 -5
  25. package/lib/chunk-JGR2NW6D.mjs +187 -0
  26. package/lib/chunk-ONIBBBQ3.mjs +108 -0
  27. package/lib/chunk-SH3L6HHV.mjs +27 -0
  28. package/lib/chunk-VVEZVNIV.mjs +81 -0
  29. package/lib/{chunk-JL32QDSH.mjs → chunk-W3ENOM53.mjs} +2 -4
  30. package/lib/chunk-YV7DO3YV.mjs +48 -0
  31. package/lib/{chunk-BSD5CIRU.mjs → chunk-YX5U7XDR.mjs} +11 -5
  32. package/lib/chunk-ZYAQRPUL.mjs +28 -0
  33. package/lib/clean-github-actions-caches.cjs +162 -0
  34. package/lib/clean-github-actions-caches.d.cts +1 -0
  35. package/lib/clean-github-actions-caches.d.mts +169 -0
  36. package/lib/clean-github-actions-caches.d.ts +169 -0
  37. package/lib/clean-github-actions-caches.mjs +132 -0
  38. package/lib/del-gradle.cjs +87 -3
  39. package/lib/del-gradle.js +1 -1
  40. package/lib/del-gradle.mjs +4 -6
  41. package/lib/del-node-modules.cjs +86 -2
  42. package/lib/del-node-modules.mjs +3 -5
  43. package/lib/del-ps.cjs +89 -5
  44. package/lib/del-ps.js +2 -2
  45. package/lib/del-ps.mjs +6 -8
  46. package/lib/del-yarn-caches.cjs +86 -2
  47. package/lib/del-yarn-caches.mjs +3 -5
  48. package/lib/find-node-modules-cli.cjs +8 -0
  49. package/lib/find-node-modules-cli.mjs +2 -3
  50. package/lib/find-node-modules.cjs +8 -0
  51. package/lib/find-node-modules.d.mts +3 -0
  52. package/lib/find-node-modules.d.ts +3 -0
  53. package/lib/find-node-modules.js +12 -0
  54. package/lib/find-node-modules.mjs +2 -3
  55. package/lib/git/gitattributes.cjs +171 -0
  56. package/lib/git/gitattributes.d.mts +35 -0
  57. package/lib/git/gitattributes.d.ts +33 -0
  58. package/lib/git/gitattributes.js +223 -0
  59. package/lib/git/gitattributes.mjs +6 -0
  60. package/lib/git/line-endings.cjs +74 -0
  61. package/lib/git/line-endings.d.cts +7 -0
  62. package/lib/git/line-endings.d.mts +83 -0
  63. package/lib/git/line-endings.d.ts +83 -0
  64. package/lib/git/line-endings.mjs +8 -0
  65. package/lib/git/normalize.cjs +42 -0
  66. package/lib/git/normalize.d.cts +6 -0
  67. package/lib/git/normalize.d.mts +43 -0
  68. package/lib/git/normalize.d.ts +43 -0
  69. package/lib/git/normalize.mjs +6 -0
  70. package/lib/git/permissions.cjs +15 -0
  71. package/lib/git/permissions.d.cts +6 -0
  72. package/lib/git/permissions.d.mts +17 -0
  73. package/lib/git/permissions.d.ts +17 -0
  74. package/lib/git/permissions.mjs +7 -0
  75. package/lib/git/pull-strategy.cjs +13 -0
  76. package/lib/git/pull-strategy.d.cts +5 -0
  77. package/lib/git/pull-strategy.d.mts +15 -0
  78. package/lib/git/pull-strategy.d.ts +15 -0
  79. package/lib/git/pull-strategy.mjs +7 -0
  80. package/lib/git/user-config.cjs +100 -0
  81. package/lib/git/user-config.d.cts +10 -0
  82. package/lib/git/user-config.d.mts +105 -0
  83. package/lib/git/user-config.d.ts +105 -0
  84. package/lib/git/user-config.mjs +8 -0
  85. package/lib/git/utils.cjs +70 -0
  86. package/lib/git/utils.d.cts +20 -0
  87. package/lib/git/utils.d.mts +69 -0
  88. package/lib/git/utils.d.ts +69 -0
  89. package/lib/git/utils.mjs +6 -0
  90. package/lib/git-diff.cjs +23 -24
  91. package/lib/git-diff.d.mts +25 -28
  92. package/lib/git-diff.d.ts +25 -28
  93. package/lib/git-diff.mjs +32 -27
  94. package/lib/git-fix.cjs +129 -0
  95. package/lib/git-fix.d.cts +2 -0
  96. package/lib/git-fix.d.mts +141 -0
  97. package/lib/git-fix.d.ts +141 -0
  98. package/lib/git-fix.mjs +151 -0
  99. package/lib/git-purge.cjs +86 -2
  100. package/lib/git-purge.mjs +3 -5
  101. package/lib/index.cjs +8 -0
  102. package/lib/index.mjs +3 -5
  103. package/lib/npm-run-series.cjs +140 -1
  104. package/lib/npm-run-series.js +2 -1
  105. package/lib/npm-run-series.mjs +7 -5
  106. package/lib/package-resolutions-updater.cjs +274 -0
  107. package/lib/package-resolutions-updater.d.mts +1 -0
  108. package/lib/package-resolutions-updater.d.ts +326 -0
  109. package/lib/package-resolutions-updater.mjs +316 -0
  110. package/lib/print-directory-tree.cjs +241 -0
  111. package/lib/print-directory-tree.d.cts +1 -0
  112. package/lib/print-directory-tree.d.mts +234 -0
  113. package/lib/print-directory-tree.d.ts +234 -0
  114. package/lib/print-directory-tree.mjs +182 -0
  115. package/lib/ps/connected-domain.mjs +2 -3
  116. package/lib/ps/index.cjs +3 -3
  117. package/lib/ps/index.d.mjs +1 -2
  118. package/lib/ps/index.js +6 -3
  119. package/lib/ps/index.mjs +9 -11
  120. package/lib/ps/isWin.mjs +2 -3
  121. package/lib/ps/table-parser.mjs +3 -4
  122. package/lib/submodule-install.cjs +13 -31
  123. package/lib/submodule-install.d.mts +12 -32
  124. package/lib/submodule-install.d.ts +12 -32
  125. package/lib/submodule-install.mjs +16 -25
  126. package/lib/utils.cjs +86 -2
  127. package/lib/utils.d.mts +29 -9
  128. package/lib/utils.d.ts +28 -8
  129. package/lib/utils.js +139 -8
  130. package/lib/utils.mjs +2 -3
  131. package/lib/yarn-reinstall.cjs +9 -7
  132. package/lib/yarn-reinstall.d.mts +12 -8
  133. package/lib/yarn-reinstall.d.ts +12 -8
  134. package/lib/yarn-reinstall.mjs +14 -10
  135. package/package.json +109 -80
  136. package/readme.md +74 -11
  137. package/src/package-resolutions-updater.mjs +325 -0
  138. package/src/print-directory-tree.cjs +234 -0
  139. package/src/ps/index.js +4 -3
  140. package/src/yarn-reinstall.cjs +49 -0
  141. package/test-project/package.json +16 -0
  142. package/tmp/test-repo/package.json +7 -0
  143. package/bin/git-fix-encoding.cmd +0 -6
  144. package/lib/chunk-OKYLF2MU.mjs +0 -53
  145. package/lib/chunk-VXZQNLPU.mjs +0 -23
  146. package/lib/package-resolutions.cjs +0 -28
  147. package/lib/package-resolutions.d.mts +0 -25
  148. package/lib/package-resolutions.d.ts +0 -25
  149. package/lib/package-resolutions.mjs +0 -31
  150. /package/bin/{submodule-install → submodule-install.txt} +0 -0
package/readme.md CHANGED
@@ -24,9 +24,6 @@ npm install binary-collections
24
24
  # Install globally
25
25
  npm install binary-collections -g
26
26
 
27
- # Install from Git
28
- npm install binary-collections@git+https://github.com/dimaslanjaka/bin.git
29
-
30
27
  # Install from release archive
31
28
  npm install binary-collections@https://github.com/dimaslanjaka/bin/raw/master/releases/bin.tgz
32
29
  ```
@@ -77,14 +74,32 @@ Create `.vscode/settings.json` to add binary tools to your PATH:
77
74
 
78
75
  | Category | Tools | Description |
79
76
  |----------|-------|-------------|
80
- | **Git** | `git-purge`, `git-diff`, `git-fix-encoding`, `git-reduce-size` | Git repository management and optimization |
77
+ | **Git** | `git-purge`, `git-diff`, `git-fix`, `git-reduce-size` | Git repository management and optimization |
81
78
  | **Submodules** | `submodule`, `submodule-install`, `submodule-remove`, `submodule-token` | Git submodule operations |
82
79
  | **NPM Scripts** | `nrs`, `run-s`, `run-series`, `npm-run-series` | Run npm scripts in series with pattern matching |
83
- | **Package Mgmt** | `yarn-reinstall`, `package-resolutions` | Yarn package management utilities |
80
+ | **Package Mgmt** | `yarn-reinstall`, `pkg-resolutions-updater`, `pkg-res-updater` | Yarn/package resolutions management utilities |
84
81
  | **Node.js Dev** | `find-node-modules`, `find-nodemodules`, `dev`, `prod`, `empty` | Node.js development helpers |
85
82
  | **Process Mgmt** | `kill-process`, `nodekill`, `javakill`, `del-ps` | Process management and termination |
86
- | **File System** | `rmfind`, `rmpath`, `rmx` | File system operations |
83
+ | **File System** | `rmfind`, `rmpath`, `rmx`, `print-tree`, `dir-tree` | File system operations |
87
84
  | **Cleanup** | `del-nodemodules`, `del-yarncaches`, `del-gradle` | Cache and build directory cleanup |
85
+ | **GitHub Actions** | `clean-github-actions-caches`, `clean-github-actions-cache`, `clear-github-actions-cache`, `clear-github-actions-caches`, `clear-gh-caches` | Remove old GitHub Actions caches, keep only the latest |
86
+
87
+
88
+ ---
89
+ #### Binary List Generation & Source Code
90
+
91
+ The list of available binaries and utilities is automatically generated by the build script (`build.js`).
92
+ All CLI tools and binaries are collected from the `bin/`, `lib/`, and other relevant folders. The build process updates the `bin` field in `package.json` to reflect all available executables.
93
+
94
+ Source code for utilities is located in the [`src/`](./src/) folder. To update the binary list, run:
95
+
96
+ ```bash
97
+ yarn run build
98
+ # or
99
+ node build.js
100
+ ```
101
+
102
+ This will scan the project and update `package.json` with all available binaries. The list may change as files are added or removed from the project.
88
103
 
89
104
  For a complete list of available binaries and utilities, see:
90
105
  - [Binary executables](https://github.com/dimaslanjaka/bin/tree/master/bin)
@@ -109,13 +124,42 @@ Enhanced git diff functionality:
109
124
  git-diff
110
125
  ```
111
126
 
112
- #### Git Encoding Fixer
113
- Fix git file encoding issues:
127
+ #### Git Fix Utility
128
+ Comprehensive Git configuration fixer for cross-platform development (replaces the old `git-fix-encoding`):
114
129
 
115
130
  ```bash
116
- git-fix-encoding
131
+ git-fix # Apply all fixes
132
+ git-fix --lf-only # Force LF line endings only
133
+ git-fix --permissions # Ignore file permissions only
134
+ git-fix --normalize # Normalize existing files only
135
+ git-fix --user # Configure Git user from environment
136
+ git-fix --user NAME EMAIL # Configure Git user with specific values
137
+ git-fix --user --update-remote # Also update remote URL to match user
138
+ git-fix --user NAME EMAIL --update-remote # Configure user and update remote URL
117
139
  ```
118
140
 
141
+ Features:
142
+ - Forces LF line endings (`core.autocrlf = false`)
143
+ - Ignores file permission changes (`core.filemode = false`)
144
+ - Sets pull strategy to false (prevents auto-rebase)
145
+ - Normalizes existing line endings
146
+ - Creates/updates `.gitattributes` with proper line ending rules
147
+ - Configures Git user from environment variables or CLI arguments
148
+ - **Non-interactive:** All configuration is now argument-driven; no interactive prompts
149
+ - `--update-remote` flag: Update remote URL to match the configured user (for HTTPS remotes)
150
+
151
+ User Configuration:
152
+ - Environment variables: `GITHUB_USER`, `GITHUB_EMAIL`
153
+ - CLI arguments take precedence over environment variables
154
+ - Use `--update-remote` to update the remote URL with the configured user
155
+ - Examples:
156
+ ```bash
157
+ git-fix --user "John Doe" "john@example.com" # Use CLI args
158
+ git-fix --user --update-remote # Use env vars and update remote
159
+ git-fix --user "Jane" "jane@example.com" --update-remote # CLI args and update remote
160
+ GITHUB_USER="Jane" GITHUB_EMAIL="jane@example.com" git-fix --user # Use env vars
161
+ ```
162
+
119
163
  #### Git Repository Size Reducer
120
164
  Reduce git repository size by cleaning up history:
121
165
 
@@ -175,10 +219,11 @@ yarn-reinstall <packageName> [--dev|-D|--peer|-P|--optional|-O]
175
219
  ```
176
220
 
177
221
  #### Package Resolutions Manager
178
- Manage package resolutions in package.json:
222
+ Manage package resolutions in package.json (aliases: `pkg-resolutions-updater`, `pkg-res-updater`):
179
223
 
180
224
  ```bash
181
- package-resolutions
225
+ pkg-resolutions-updater
226
+ pkg-res-updater
182
227
  ```
183
228
 
184
229
  ### Node.js Development Tools
@@ -216,6 +261,24 @@ del-ps # Kill processes by command name
216
261
 
217
262
  ### Cleanup Tools
218
263
 
264
+ ### GitHub Actions Cache Cleaner
265
+
266
+ Remove old GitHub Actions caches, keeping only the most recent cache for each prefix:
267
+
268
+ ```bash
269
+ clean-github-actions-caches
270
+ ```
271
+
272
+ Features:
273
+ - Authenticates using `ACCESS_TOKEN` or `GITHUB_TOKEN` from your `.env` file
274
+ - Automatically groups and deletes caches by prefix, keeping only the latest
275
+ - Works for the current repository (origin remote)
276
+
277
+ Environment setup:
278
+ - Ensure your `.env` file contains `ACCESS_TOKEN` or `GITHUB_TOKEN`
279
+
280
+ Source: [`src/clean-github-actions-caches.cjs`](./src/clean-github-actions-caches.cjs)
281
+
219
282
  #### Node Modules Cleaner
220
283
  Remove node_modules directories recursively:
221
284
 
@@ -0,0 +1,325 @@
1
+ /**
2
+ * 📦 GitHub Package Resolver
3
+ *
4
+ * This script updates the commit hashes in `package.json`'s `resolutions` field
5
+ * for GitHub tarball URLs (typically using `raw/branch-name/...`) to point to the
6
+ * latest commit SHA of the corresponding repository and branch.
7
+ *
8
+ * 🔍 Features:
9
+ * - Parses GitHub URLs to extract repository owner, name, and branch.
10
+ * - Fetches the latest commit SHA across all branches using GitHub's API.
11
+ * - Replaces the old branch or commit in the URL with the latest SHA.
12
+ * - Overwrites `package.json` with the updated URLs.
13
+ *
14
+ * 🛠 Requirements:
15
+ * - GitHub Personal Access Token (GITHUB_TOKEN) via `.env`
16
+ * - ESM support (`type: "module"` in `package.json`)
17
+ * - Node.js v18+ recommended for ESM and `fetch` fallback compatibility
18
+ *
19
+ * 🧩 Dependencies:
20
+ * - `ansi-colors` – for styled terminal output
21
+ * - `dotenv` – to load GitHub token from `.env`
22
+ *
23
+ * ✅ Use case:
24
+ * - Ensures package resolutions always use immutable SHAs instead of mutable branch names.
25
+ * - Helps achieve deterministic builds in monorepos or projects with internal GitHub packages.
26
+ */
27
+
28
+ import ansiColors from "ansi-colors";
29
+ import * as dotenv from "dotenv";
30
+ import fs from "fs";
31
+ import https from "https";
32
+ import os from "os";
33
+ import path from "path";
34
+
35
+ const projectDir = process.cwd();
36
+ const envPath = path.join(projectDir, ".env");
37
+
38
+ // Load the .env file using dotenv (ESM import)
39
+ if (fs.existsSync(envPath)) dotenv.config({ path: envPath });
40
+
41
+ // 📌 Static override rules
42
+ const specialPackageOverrides = [
43
+ // SBG packages
44
+ { pkg: "sbg-utility", branch: "sbg-utility", repo: "static-blog-generator", owner: "dimaslanjaka" },
45
+ { pkg: "sbg-api", branch: "sbg-api", repo: "static-blog-generator", owner: "dimaslanjaka" },
46
+ { pkg: "instant-indexing", branch: "instant-indexing", repo: "static-blog-generator", owner: "dimaslanjaka" },
47
+ { pkg: "sbg-server", branch: "master", repo: "static-blog-generator", owner: "dimaslanjaka" },
48
+ { pkg: "sbg-cli", branch: "master", repo: "static-blog-generator", owner: "dimaslanjaka" },
49
+ { pkg: "static-blog-generator", branch: "master", repo: "static-blog-generator", owner: "dimaslanjaka" },
50
+ // Hexo family
51
+ { pkg: "hexo", branch: "monorepo-v7", repo: "hexo", owner: "dimaslanjaka" },
52
+ { pkg: "hexo-util", branch: "monorepo-v7", repo: "hexo", owner: "dimaslanjaka" },
53
+ { pkg: "warehouse", branch: "monorepo-v7", repo: "hexo", owner: "dimaslanjaka" },
54
+ { pkg: "hexo-server", branch: "monorepo-v7", repo: "hexo", owner: "dimaslanjaka" },
55
+ { pkg: "hexo-log", branch: "monorepo-v7", repo: "hexo", owner: "dimaslanjaka" },
56
+ { pkg: "hexo-front-matter", branch: "monorepo-v7", repo: "hexo", owner: "dimaslanjaka" },
57
+ { pkg: "hexo-cli", branch: "monorepo-v7", repo: "hexo", owner: "dimaslanjaka" },
58
+ { pkg: "hexo-asset-link", branch: "monorepo-v7", repo: "hexo", owner: "dimaslanjaka" },
59
+ { pkg: "hexo-post-parser", branch: "pre-release", repo: "hexo-post-parser", owner: "dimaslanjaka" },
60
+ { pkg: "hexo-seo", branch: "pre-release", repo: "hexo-seo", owner: "dimaslanjaka" },
61
+ { pkg: "hexo-is", branch: "master", repo: "hexo-is", owner: "dimaslanjaka" },
62
+ { pkg: "markdown-it", branch: "master", repo: "markdown-it", owner: "dimaslanjaka" },
63
+ { pkg: "hexo-renderers", branch: "pre-release", repo: "hexo-renderers", owner: "dimaslanjaka" },
64
+ { pkg: "hexo-shortcodes", branch: "pre-release", repo: "hexo-shortcodes", owner: "dimaslanjaka" },
65
+ { pkg: "google-news-sitemap", branch: "master", repo: "google-news-sitemap", owner: "dimaslanjaka" },
66
+ { pkg: "git-command-helper", branch: "pre-release", repo: "git-command-helper", owner: "dimaslanjaka" },
67
+ {
68
+ pkg: "nodejs-package-types",
69
+ branch: "main",
70
+ repo: "nodejs-package-types",
71
+ owner: "dimaslanjaka"
72
+ },
73
+ { pkg: "cross-spawn", branch: "private", repo: "node-cross-spawn", owner: "dimaslanjaka" },
74
+ { pkg: "hexo-generator-redirect", branch: "master", repo: "hexo-generator-redirect", owner: "dimaslanjaka" },
75
+ { pkg: "binary-collections", branch: "master", repo: "bin", owner: "dimaslanjaka" },
76
+ { pkg: "@types/hexo", branch: "monorepo-v7", repo: "hexo", owner: "dimaslanjaka" },
77
+ { pkg: "@types/git-command-helper", branch: "pre-release", repo: "git-command-helper", owner: "dimaslanjaka" }
78
+ ];
79
+
80
+ // --- Optimized: Load package.json once at the top ---
81
+ const pkgPath = path.join(process.cwd(), "package.json");
82
+ let pkg;
83
+ try {
84
+ pkg = JSON.parse(fs.readFileSync(pkgPath, "utf-8"));
85
+ } catch (e) {
86
+ console.error(ansiColors.red(`Failed to read package.json: ${e.message}`));
87
+ process.exit(1);
88
+ }
89
+
90
+ // --- Use a random User-Agent for GitHub API requests ---
91
+ const GITHUB_USER_AGENTS = [
92
+ "octokit-rest.js/19.0.7",
93
+ "GitHub CLI/2.40.0",
94
+ "Mozilla/5.0 (compatible; GitHubCopilot/1.0)",
95
+ "PostmanRuntime/7.32.3",
96
+ "binary-collections-resolver/1.0 (+https://github.com/dimaslanjaka/bin)"
97
+ ];
98
+
99
+ // --- User-Agent persistence in system temp folder ---
100
+ const userAgentDir = path.join(os.tmpdir(), "nodejs");
101
+ const userAgentFile = path.join(userAgentDir, "useragent.txt");
102
+ let selectedUserAgent;
103
+ try {
104
+ if (!fs.existsSync(userAgentDir)) fs.mkdirSync(userAgentDir, { recursive: true });
105
+ if (fs.existsSync(userAgentFile)) {
106
+ const fileAgent = fs.readFileSync(userAgentFile, "utf-8").trim();
107
+ if (GITHUB_USER_AGENTS.includes(fileAgent)) {
108
+ selectedUserAgent = fileAgent;
109
+ }
110
+ }
111
+ if (!selectedUserAgent) {
112
+ selectedUserAgent = GITHUB_USER_AGENTS[Math.floor(Math.random() * GITHUB_USER_AGENTS.length)];
113
+ fs.writeFileSync(userAgentFile, selectedUserAgent, "utf-8");
114
+ }
115
+ } catch (_e) {
116
+ // fallback to random if any error
117
+ selectedUserAgent = GITHUB_USER_AGENTS[Math.floor(Math.random() * GITHUB_USER_AGENTS.length)];
118
+ }
119
+
120
+ /**
121
+ * Fetch JSON from a URL with GitHub headers.
122
+ * @param {string} url
123
+ * @returns {Promise<any>}
124
+ */
125
+ function fetchJson(url) {
126
+ const headers = {
127
+ "User-Agent": selectedUserAgent,
128
+ Accept: "application/vnd.github.v3+json",
129
+ "X-GitHub-Api-Version": "2022-11-28",
130
+ ...(process.env.GITHUB_TOKEN ? { Authorization: `token ${process.env.GITHUB_TOKEN}` } : {})
131
+ };
132
+ return new Promise((resolve, reject) => {
133
+ https
134
+ .get(url, { headers }, (res) => {
135
+ let data = "";
136
+ res.on("data", (chunk) => (data += chunk));
137
+ res.on("end", () => {
138
+ try {
139
+ const json = JSON.parse(data);
140
+ if (res.statusCode < 200 || res.statusCode >= 300) {
141
+ return reject(
142
+ new Error(`GitHub API Error ${res.statusCode}: ${json.message || "Unknown error"}\nURL: ${url}`)
143
+ );
144
+ }
145
+ resolve(json);
146
+ } catch {
147
+ reject(new Error(`Invalid JSON from: ${url}`));
148
+ }
149
+ });
150
+ })
151
+ .on("error", reject);
152
+ });
153
+ }
154
+
155
+ /**
156
+ * Get latest commit SHA from a specific branch.
157
+ */
158
+ async function getLatestCommit(owner, repo, branch = "main") {
159
+ const url = `https://api.github.com/repos/${owner}/${repo}/commits/${branch}`;
160
+ const json = await fetchJson(url);
161
+
162
+ const sha = json.sha;
163
+ const dateStr = json.commit?.committer?.date || json.commit?.author?.date;
164
+
165
+ if (!sha || !dateStr) {
166
+ console.log(json);
167
+ throw new Error(`Missing SHA or date for ${owner}/${repo}@${branch}`);
168
+ }
169
+
170
+ return {
171
+ owner,
172
+ repo,
173
+ branch,
174
+ sha,
175
+ date: new Date(dateStr).toISOString()
176
+ };
177
+ }
178
+
179
+ /**
180
+ * Get latest commit SHA from all branches and pick the latest.
181
+ */
182
+ async function getLatestCommitAcrossBranches(owner, repo) {
183
+ const branches = await fetchJson(`https://api.github.com/repos/${owner}/${repo}/branches`);
184
+
185
+ const commits = await Promise.all(
186
+ branches.map(async ({ name, commit }) => {
187
+ const commitSha = commit?.sha;
188
+ if (!commitSha) {
189
+ console.warn(`No commit SHA for '${owner}/${repo}' branch: ${name}`);
190
+ return { branch: name, sha: "", date: new Date(0) };
191
+ }
192
+
193
+ try {
194
+ const commitData = await fetchJson(`https://api.github.com/repos/${owner}/${repo}/commits/${commitSha}`);
195
+ const dateStr = commitData.commit?.committer?.date || commitData.commit?.author?.date;
196
+ const date = dateStr ? new Date(dateStr) : new Date(0);
197
+ return { branch: name, sha: commitData.sha, date };
198
+ } catch (e) {
199
+ console.warn(`Failed to fetch commit for ${name}: ${e.message}`);
200
+ return { branch: name, sha: commitSha, date: new Date(0) };
201
+ }
202
+ })
203
+ );
204
+
205
+ const latest = commits.reduce((a, b) => (a.date > b.date ? a : b), { date: new Date(0) });
206
+
207
+ return {
208
+ owner,
209
+ repo,
210
+ branch: latest.branch,
211
+ sha: latest.sha,
212
+ date: latest.date.toISOString()
213
+ };
214
+ }
215
+
216
+ /**
217
+ * Replace the branch or commit in a GitHub raw URL with the latest hash.
218
+ */
219
+ function replaceRawWithLatestHash(url, latestHash) {
220
+ const match = url.match(/^https:\/\/github\.com\/([^/]+)\/([^/]+)\/raw\/([^/]+)\/(.+)$/);
221
+ if (!match) throw new Error("Invalid GitHub raw URL");
222
+
223
+ const [, owner, repo, _oldHash, path] = match;
224
+ return `https://github.com/${owner}/${repo}/raw/${latestHash}/${path}`;
225
+ }
226
+
227
+ /**
228
+ * Parse GitHub URLs and extract owner, repo, branch, and original URL.
229
+ */
230
+ function parseGitHubUrl(url) {
231
+ const ghRepoRoot = /^https:\/\/github\.com\/([^/]+)\/([^/]+)\/?$/;
232
+ const ghTreeOrBlob = /^https:\/\/github\.com\/([^/]+)\/([^/]+)\/(tree|blob)\/([^/]+(?:\/[^/]+)*)/;
233
+ const ghRaw = /^https:\/\/raw\.githubusercontent\.com\/([^/]+)\/([^/]+)\/([^/]+)(\/.+)?$/;
234
+ const ghDotComRaw = /^https:\/\/github\.com\/([^/]+)\/([^/]+)\/raw\/([^/]+)\/.+/;
235
+
236
+ let match;
237
+
238
+ if ((match = url.match(ghRaw))) {
239
+ const [, owner, repo, branch] = match;
240
+ return { owner, repo, branch, url };
241
+ }
242
+
243
+ if ((match = url.match(ghDotComRaw))) {
244
+ const [, owner, repo, branch] = match;
245
+ return { owner, repo, branch, url };
246
+ }
247
+
248
+ if ((match = url.match(ghTreeOrBlob))) {
249
+ const [, owner, repo, , branchPath] = match;
250
+ return { owner, repo, branch: branchPath, url };
251
+ }
252
+
253
+ if ((match = url.match(ghRepoRoot))) {
254
+ const [, owner, repo] = match;
255
+ return { owner, repo, url };
256
+ }
257
+
258
+ throw new Error(`Unsupported GitHub URL: ${url}`);
259
+ }
260
+
261
+ // --- Main logic ---
262
+ (async () => {
263
+ const entries = Object.entries(pkg.resolutions || {});
264
+ if (entries.length === 0) {
265
+ console.log(ansiColors.yellow("No resolutions found in package.json"));
266
+ return;
267
+ }
268
+ console.log(`Processing ${entries.length} resolution(s)...`);
269
+ const updates = [];
270
+ for (const [currentPkgName, url] of entries) {
271
+ // Validate if URL is a GitHub URL
272
+ let repo;
273
+ try {
274
+ repo = parseGitHubUrl(url);
275
+ console.log(`✅ Valid GitHub URL for ${ansiColors.cyan(currentPkgName)}: ${url}`);
276
+ } catch (error) {
277
+ console.log(`⏭️ Skipping ${ansiColors.yellow(currentPkgName)}: ${error.message}`);
278
+ continue;
279
+ }
280
+ try {
281
+ const override = specialPackageOverrides.find((p) => p.pkg === currentPkgName);
282
+ const latest = override
283
+ ? await getLatestCommit(override.owner, override.repo, override.branch)
284
+ : await getLatestCommitAcrossBranches(repo.owner, repo.repo);
285
+ const new_url = replaceRawWithLatestHash(url, latest.sha);
286
+ updates.push({
287
+ currentPkgName,
288
+ url,
289
+ new_url,
290
+ repo,
291
+ latest
292
+ });
293
+ } catch (error) {
294
+ console.log(`❌ Failed to process ${ansiColors.red(currentPkgName)}: ${error.message}`);
295
+ }
296
+ }
297
+ if (updates.length === 0) {
298
+ console.log(ansiColors.yellow("No GitHub URLs were processed"));
299
+ return;
300
+ }
301
+ console.log(`\n📝 Applying updates to ${updates.length} GitHub URL(s)...`);
302
+ let changed = false;
303
+ for (const { currentPkgName, url, new_url, repo, latest } of updates) {
304
+ if (url !== new_url) {
305
+ console.log(`\n${ansiColors.cyan(currentPkgName)}:`);
306
+ console.log(" from:", url.replace(repo.branch, ansiColors.red(repo.branch)));
307
+ console.log(" to:", new_url.replace(latest.sha, ansiColors.green(latest.sha)));
308
+ pkg.resolutions[currentPkgName] = new_url;
309
+ changed = true;
310
+ } else {
311
+ console.log(`\n${ansiColors.cyan(currentPkgName)}: ${ansiColors.gray("already up-to-date")}`);
312
+ }
313
+ }
314
+ if (changed) {
315
+ try {
316
+ fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + "\n");
317
+ console.log(`\n✅ package.json updated successfully`);
318
+ } catch (e) {
319
+ console.error(ansiColors.red(`Failed to write package.json: ${e.message}`));
320
+ process.exit(1);
321
+ }
322
+ } else {
323
+ console.log(ansiColors.green("No changes to package.json were necessary."));
324
+ }
325
+ })();
@@ -0,0 +1,234 @@
1
+ const fs = require("fs").promises;
2
+ const path = require("path");
3
+ const crypto = require("crypto");
4
+ const { execSync } = require("child_process");
5
+ const glob = require("glob");
6
+ const { getArgs } = require("./utils.js");
7
+ const sbgUtil = require("sbg-utility");
8
+ const dotenv = require("dotenv");
9
+
10
+ const projectDir = process.cwd();
11
+ const envPath = path.join(projectDir, ".env");
12
+
13
+ // Load the .env file using dotenv (ESM import)
14
+ if (fs.existsSync(envPath)) dotenv.config({ path: envPath });
15
+
16
+ // Parse CLI arguments
17
+ const argv = getArgs();
18
+
19
+ // Main logic wrapped in an async function
20
+ async function main() {
21
+ // Determine output file from CLI args
22
+ let relativeOutputFile = "tmp/directory-structure.txt";
23
+ if (argv.output || argv.o) {
24
+ relativeOutputFile = argv.output || argv.o;
25
+ }
26
+ // If not absolute, resolve relative to projectDir
27
+ const outputFile = path.isAbsolute(relativeOutputFile)
28
+ ? relativeOutputFile
29
+ : path.join(projectDir, relativeOutputFile);
30
+
31
+ // Create or clear the hash file
32
+ sbgUtil.writefile(outputFile, "");
33
+
34
+ /**
35
+ * List of file extensions to include
36
+ * @type {string[]}
37
+ */
38
+ let extensions = [];
39
+ if (argv.ext) {
40
+ extensions = argv.ext
41
+ .split(",")
42
+ .map((e) => e.trim().replace(/^\./, ""))
43
+ .filter(Boolean);
44
+ }
45
+
46
+ // Directories to exclude
47
+ let excludeDirs = [
48
+ "node_modules",
49
+ "vendor",
50
+ "venv",
51
+ ".venv",
52
+ ".git",
53
+ ".hg",
54
+ ".svn",
55
+ ".idea",
56
+ ".vscode",
57
+ "dist",
58
+ "build",
59
+ "out",
60
+ "coverage",
61
+ ".DS_Store"
62
+ ];
63
+ if (argv.exclude) {
64
+ const userExcludes = argv.exclude
65
+ .split(",")
66
+ .map((d) => d.trim())
67
+ .filter(Boolean);
68
+ if (argv["override-exclude"] || argv.we) {
69
+ // Override the default excludes with user-provided ones
70
+ excludeDirs = userExcludes;
71
+ } else {
72
+ // Append user-provided excludes to the default ones
73
+ excludeDirs = excludeDirs.concat(userExcludes);
74
+ }
75
+ }
76
+
77
+ // Convert excludeDirs into glob ignore patterns
78
+ const ignorePatterns = excludeDirs.map((dir) => `**/${dir}/**`);
79
+
80
+ // Initialize an array to hold the formatted outputs
81
+ let hashArray = [];
82
+
83
+ // Helper function to hash and add file if not already processed
84
+ const processedFiles = new Set();
85
+ /**
86
+ * Hashes a file and pushes its relative path and hash to the hashArray.
87
+ * @param {string} file - The absolute path to the file.
88
+ * @returns {Promise<void>} Resolves when the file has been processed and added to hashArray.
89
+ */
90
+ async function hashAndPush(file) {
91
+ if (processedFiles.has(file)) return;
92
+ processedFiles.add(file);
93
+ let relativePath = path.relative(projectDir, file);
94
+ relativePath = relativePath.split(path.sep).join("/");
95
+ try {
96
+ const stats = await fs.stat(file);
97
+ const pseudoHash = `${stats.size}-${stats.mtimeMs}`;
98
+ const hash = crypto.createHash("sha256").update(pseudoHash).digest("hex");
99
+ hashArray.push(`${relativePath} ${hash.slice(0, 8)}`);
100
+ } catch (err) {
101
+ console.error(`Error processing file: ${file}`, err instanceof Error ? err.message : "<unknown error>");
102
+ if (err && err.code === "ENOENT") {
103
+ hashArray.push(`${relativePath} <file not found>`);
104
+ } else {
105
+ hashArray.push(`${relativePath} <error: ${err && err.code ? `code ${err.code}` : "unknown"}>`);
106
+ }
107
+ }
108
+ }
109
+
110
+ // Collect all files to process (extensions + special files)
111
+ const initialFiles = [
112
+ path.join(projectDir, "package.json"),
113
+ path.join(projectDir, "composer.json"),
114
+ path.join(projectDir, "requirements.txt")
115
+ ];
116
+ let patterns = [];
117
+ if (argv.pattern) {
118
+ if (Array.isArray(argv.pattern)) {
119
+ patterns = argv.pattern.map((p) => p.trim()).filter(Boolean);
120
+ } else {
121
+ patterns = [argv.pattern.trim()];
122
+ }
123
+ } else if (extensions.length === 0) {
124
+ patterns = ["**/*.*"];
125
+ } else {
126
+ patterns = extensions.map((ext) => `**/*.${ext}`);
127
+ }
128
+ const globFiles = glob.sync(patterns.length === 1 ? patterns[0] : `{${patterns.join(",")}}`, {
129
+ cwd: projectDir,
130
+ ignore: ignorePatterns,
131
+ absolute: true,
132
+ nodir: true
133
+ });
134
+ const allFiles = new Set([...initialFiles, ...globFiles]);
135
+
136
+ // Hash all unique files
137
+ await Promise.all(Array.from(allFiles).map(hashAndPush));
138
+
139
+ // Sort the hashArray by file paths
140
+ hashArray.sort((a, b) => a.localeCompare(b));
141
+
142
+ /**
143
+ * Generates a directory/file tree string from a hash array of file paths and hashes.
144
+ *
145
+ * @param {string[]} hashArray - Array of strings in the format 'relative/path/to/file hash'.
146
+ * @returns {string} The directory/file tree as a string, with file hashes.
147
+ */
148
+ function getFileTreeString(hashArray) {
149
+ const tree = {};
150
+ // Map file paths to hashes for quick lookup
151
+ const hashMap = {};
152
+ for (const entry of hashArray) {
153
+ const [filePath, hash] = entry.split(" ");
154
+ hashMap[filePath] = hash;
155
+ const parts = filePath.split("/");
156
+ let current = tree;
157
+ for (let i = 0; i < parts.length; i++) {
158
+ const part = parts[i];
159
+ if (i === parts.length - 1) {
160
+ current[part] = null; // file
161
+ } else {
162
+ current[part] = current[part] || {};
163
+ current = current[part];
164
+ }
165
+ }
166
+ }
167
+ /**
168
+ * Recursively builds the tree string for a given node.
169
+ *
170
+ * @param {Object} node - The current node in the tree.
171
+ * @param {string} prefix - The prefix for the current tree level.
172
+ * @param {string} parentPath - The path to the current node.
173
+ * @returns {string[]} Array of lines representing the tree structure.
174
+ */
175
+ function printNode(node, prefix = "", parentPath = "") {
176
+ const keys = Object.keys(node).sort();
177
+ let lines = [];
178
+ keys.forEach((key, idx) => {
179
+ const isLast = idx === keys.length - 1;
180
+ const branch = isLast ? "└── " : "├── ";
181
+ const currentPath = parentPath ? parentPath + "/" + key : key;
182
+ if (node[key] === null) {
183
+ // file: show hash
184
+ lines.push(prefix + branch + key + " [" + (hashMap[currentPath] || "") + "]");
185
+ } else {
186
+ lines.push(prefix + branch + key + "/");
187
+ lines = lines.concat(printNode(node[key], prefix + (isLast ? " " : "│ "), currentPath));
188
+ }
189
+ });
190
+ return lines;
191
+ }
192
+ return printNode(tree, "", "").join("\n");
193
+ }
194
+
195
+ // Write directory/file tree to the output file (hashes are included in the tree)
196
+ const fileTreeString = getFileTreeString(hashArray);
197
+ await fs.writeFile(outputFile, fileTreeString + "\n", "utf-8");
198
+
199
+ // Add the hash file to the commit if --git-add is present
200
+ if (argv["git-add"]) {
201
+ execSync(`git add ${relativeOutputFile}`);
202
+ console.log(`Directory tree written to ${relativeOutputFile} and staged for git.`);
203
+ } else {
204
+ console.log(`Directory tree written to ${relativeOutputFile}.`);
205
+ }
206
+ }
207
+
208
+ if (argv.help || argv.h) {
209
+ console.log(`
210
+ Usage: node print-directory-tree.cjs [options]
211
+
212
+ Options:
213
+ --output, -o <file> Output file path (default: tmp/directory-structure.txt)
214
+ --ext <exts> Comma-separated list of file extensions (no dot, e.g. js,ts)
215
+ --pattern <glob> Glob pattern(s) for files (can be repeated)
216
+ --exclude <dirs> Comma-separated list of directories to exclude (appends to default)
217
+ --override-exclude, -we Override default exclude directories with --exclude
218
+ --git-add Add output file to git after writing
219
+ --help, -h Show this help message
220
+
221
+ Examples:
222
+ node print-directory-tree.cjs --ext=js,ts
223
+ node print-directory-tree.cjs --pattern=src/**/*.js --pattern=test/**/*.js
224
+ node print-directory-tree.cjs --exclude=dist,build
225
+ node print-directory-tree.cjs --output=tmp/tree.txt
226
+ `);
227
+ process.exit(0);
228
+ }
229
+
230
+ // Execute the main function
231
+ main().catch((err) => {
232
+ console.error(err);
233
+ process.exit(1);
234
+ });