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.
- package/bin/dir-tree.cmd +7 -0
- package/bin/git-diff +4 -0
- package/bin/git-diff.cmd +5 -2
- package/bin/{git-fix-encoding → git-fix} +1 -4
- package/bin/git-fix.cmd +7 -0
- package/bin/nodekill +0 -0
- package/bin/nodekill.ps1 +0 -0
- package/bin/{submodule → submodule.txt} +0 -0
- package/lib/binary-collections-config.cjs +14 -0
- package/lib/binary-collections-config.d.mts +18 -0
- package/lib/binary-collections-config.d.ts +16 -0
- package/lib/binary-collections-config.js +39 -0
- package/lib/binary-collections-config.mjs +6 -0
- package/lib/binary-collections.cjs +105 -0
- package/lib/binary-collections.d.cts +2 -0
- package/lib/binary-collections.d.mts +121 -0
- package/lib/binary-collections.d.ts +121 -0
- package/lib/binary-collections.mjs +108 -0
- package/lib/chunk-4BYBVEYC.mjs +30 -0
- package/lib/{chunk-FB6YIQYR.mjs → chunk-AASHBCRW.mjs} +17 -2
- package/lib/chunk-APBWENF6.mjs +135 -0
- package/lib/{chunk-4LEXWIIF.mjs → chunk-DPKAJKFO.mjs} +2 -4
- package/lib/chunk-EGSSKVDH.mjs +66 -0
- package/lib/{chunk-3LOB2P54.mjs → chunk-G3THLIDT.mjs} +3 -5
- package/lib/chunk-JGR2NW6D.mjs +187 -0
- package/lib/chunk-ONIBBBQ3.mjs +108 -0
- package/lib/chunk-SH3L6HHV.mjs +27 -0
- package/lib/chunk-VVEZVNIV.mjs +81 -0
- package/lib/{chunk-JL32QDSH.mjs → chunk-W3ENOM53.mjs} +2 -4
- package/lib/chunk-YV7DO3YV.mjs +48 -0
- package/lib/{chunk-BSD5CIRU.mjs → chunk-YX5U7XDR.mjs} +11 -5
- package/lib/chunk-ZYAQRPUL.mjs +28 -0
- package/lib/clean-github-actions-caches.cjs +162 -0
- package/lib/clean-github-actions-caches.d.cts +1 -0
- package/lib/clean-github-actions-caches.d.mts +169 -0
- package/lib/clean-github-actions-caches.d.ts +169 -0
- package/lib/clean-github-actions-caches.mjs +132 -0
- package/lib/del-gradle.cjs +87 -3
- package/lib/del-gradle.js +1 -1
- package/lib/del-gradle.mjs +4 -6
- package/lib/del-node-modules.cjs +86 -2
- package/lib/del-node-modules.mjs +3 -5
- package/lib/del-ps.cjs +89 -5
- package/lib/del-ps.js +2 -2
- package/lib/del-ps.mjs +6 -8
- package/lib/del-yarn-caches.cjs +86 -2
- package/lib/del-yarn-caches.mjs +3 -5
- package/lib/find-node-modules-cli.cjs +8 -0
- package/lib/find-node-modules-cli.mjs +2 -3
- package/lib/find-node-modules.cjs +8 -0
- package/lib/find-node-modules.d.mts +3 -0
- package/lib/find-node-modules.d.ts +3 -0
- package/lib/find-node-modules.js +12 -0
- package/lib/find-node-modules.mjs +2 -3
- package/lib/git/gitattributes.cjs +171 -0
- package/lib/git/gitattributes.d.mts +35 -0
- package/lib/git/gitattributes.d.ts +33 -0
- package/lib/git/gitattributes.js +223 -0
- package/lib/git/gitattributes.mjs +6 -0
- package/lib/git/line-endings.cjs +74 -0
- package/lib/git/line-endings.d.cts +7 -0
- package/lib/git/line-endings.d.mts +83 -0
- package/lib/git/line-endings.d.ts +83 -0
- package/lib/git/line-endings.mjs +8 -0
- package/lib/git/normalize.cjs +42 -0
- package/lib/git/normalize.d.cts +6 -0
- package/lib/git/normalize.d.mts +43 -0
- package/lib/git/normalize.d.ts +43 -0
- package/lib/git/normalize.mjs +6 -0
- package/lib/git/permissions.cjs +15 -0
- package/lib/git/permissions.d.cts +6 -0
- package/lib/git/permissions.d.mts +17 -0
- package/lib/git/permissions.d.ts +17 -0
- package/lib/git/permissions.mjs +7 -0
- package/lib/git/pull-strategy.cjs +13 -0
- package/lib/git/pull-strategy.d.cts +5 -0
- package/lib/git/pull-strategy.d.mts +15 -0
- package/lib/git/pull-strategy.d.ts +15 -0
- package/lib/git/pull-strategy.mjs +7 -0
- package/lib/git/user-config.cjs +100 -0
- package/lib/git/user-config.d.cts +10 -0
- package/lib/git/user-config.d.mts +105 -0
- package/lib/git/user-config.d.ts +105 -0
- package/lib/git/user-config.mjs +8 -0
- package/lib/git/utils.cjs +70 -0
- package/lib/git/utils.d.cts +20 -0
- package/lib/git/utils.d.mts +69 -0
- package/lib/git/utils.d.ts +69 -0
- package/lib/git/utils.mjs +6 -0
- package/lib/git-diff.cjs +23 -24
- package/lib/git-diff.d.mts +25 -28
- package/lib/git-diff.d.ts +25 -28
- package/lib/git-diff.mjs +32 -27
- package/lib/git-fix.cjs +129 -0
- package/lib/git-fix.d.cts +2 -0
- package/lib/git-fix.d.mts +141 -0
- package/lib/git-fix.d.ts +141 -0
- package/lib/git-fix.mjs +151 -0
- package/lib/git-purge.cjs +86 -2
- package/lib/git-purge.mjs +3 -5
- package/lib/index.cjs +8 -0
- package/lib/index.mjs +3 -5
- package/lib/npm-run-series.cjs +140 -1
- package/lib/npm-run-series.js +2 -1
- package/lib/npm-run-series.mjs +7 -5
- package/lib/package-resolutions-updater.cjs +274 -0
- package/lib/package-resolutions-updater.d.mts +1 -0
- package/lib/package-resolutions-updater.d.ts +326 -0
- package/lib/package-resolutions-updater.mjs +316 -0
- package/lib/print-directory-tree.cjs +241 -0
- package/lib/print-directory-tree.d.cts +1 -0
- package/lib/print-directory-tree.d.mts +234 -0
- package/lib/print-directory-tree.d.ts +234 -0
- package/lib/print-directory-tree.mjs +182 -0
- package/lib/ps/connected-domain.mjs +2 -3
- package/lib/ps/index.cjs +3 -3
- package/lib/ps/index.d.mjs +1 -2
- package/lib/ps/index.js +6 -3
- package/lib/ps/index.mjs +9 -11
- package/lib/ps/isWin.mjs +2 -3
- package/lib/ps/table-parser.mjs +3 -4
- package/lib/submodule-install.cjs +13 -31
- package/lib/submodule-install.d.mts +12 -32
- package/lib/submodule-install.d.ts +12 -32
- package/lib/submodule-install.mjs +16 -25
- package/lib/utils.cjs +86 -2
- package/lib/utils.d.mts +29 -9
- package/lib/utils.d.ts +28 -8
- package/lib/utils.js +139 -8
- package/lib/utils.mjs +2 -3
- package/lib/yarn-reinstall.cjs +9 -7
- package/lib/yarn-reinstall.d.mts +12 -8
- package/lib/yarn-reinstall.d.ts +12 -8
- package/lib/yarn-reinstall.mjs +14 -10
- package/package.json +109 -80
- package/readme.md +74 -11
- package/src/package-resolutions-updater.mjs +325 -0
- package/src/print-directory-tree.cjs +234 -0
- package/src/ps/index.js +4 -3
- package/src/yarn-reinstall.cjs +49 -0
- package/test-project/package.json +16 -0
- package/tmp/test-repo/package.json +7 -0
- package/bin/git-fix-encoding.cmd +0 -6
- package/lib/chunk-OKYLF2MU.mjs +0 -53
- package/lib/chunk-VXZQNLPU.mjs +0 -23
- package/lib/package-resolutions.cjs +0 -28
- package/lib/package-resolutions.d.mts +0 -25
- package/lib/package-resolutions.d.ts +0 -25
- package/lib/package-resolutions.mjs +0 -31
- /package/bin/{submodule-install → submodule-install.txt} +0 -0
|
@@ -0,0 +1,316 @@
|
|
|
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
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
28
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
29
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
30
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
31
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
32
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
33
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
34
|
+
});
|
|
35
|
+
};
|
|
36
|
+
import ansiColors from "ansi-colors";
|
|
37
|
+
import * as dotenv from "dotenv";
|
|
38
|
+
import fs from "fs";
|
|
39
|
+
import https from "https";
|
|
40
|
+
import os from "os";
|
|
41
|
+
import path from "path";
|
|
42
|
+
const projectDir = process.cwd();
|
|
43
|
+
const envPath = path.join(projectDir, ".env");
|
|
44
|
+
// Load the .env file using dotenv (ESM import)
|
|
45
|
+
if (fs.existsSync(envPath))
|
|
46
|
+
dotenv.config({ path: envPath });
|
|
47
|
+
// 📌 Static override rules
|
|
48
|
+
const specialPackageOverrides = [
|
|
49
|
+
// SBG packages
|
|
50
|
+
{ pkg: "sbg-utility", branch: "sbg-utility", repo: "static-blog-generator", owner: "dimaslanjaka" },
|
|
51
|
+
{ pkg: "sbg-api", branch: "sbg-api", repo: "static-blog-generator", owner: "dimaslanjaka" },
|
|
52
|
+
{ pkg: "instant-indexing", branch: "instant-indexing", repo: "static-blog-generator", owner: "dimaslanjaka" },
|
|
53
|
+
{ pkg: "sbg-server", branch: "master", repo: "static-blog-generator", owner: "dimaslanjaka" },
|
|
54
|
+
{ pkg: "sbg-cli", branch: "master", repo: "static-blog-generator", owner: "dimaslanjaka" },
|
|
55
|
+
{ pkg: "static-blog-generator", branch: "master", repo: "static-blog-generator", owner: "dimaslanjaka" },
|
|
56
|
+
// Hexo family
|
|
57
|
+
{ pkg: "hexo", branch: "monorepo-v7", repo: "hexo", owner: "dimaslanjaka" },
|
|
58
|
+
{ pkg: "hexo-util", branch: "monorepo-v7", repo: "hexo", owner: "dimaslanjaka" },
|
|
59
|
+
{ pkg: "warehouse", branch: "monorepo-v7", repo: "hexo", owner: "dimaslanjaka" },
|
|
60
|
+
{ pkg: "hexo-server", branch: "monorepo-v7", repo: "hexo", owner: "dimaslanjaka" },
|
|
61
|
+
{ pkg: "hexo-log", branch: "monorepo-v7", repo: "hexo", owner: "dimaslanjaka" },
|
|
62
|
+
{ pkg: "hexo-front-matter", branch: "monorepo-v7", repo: "hexo", owner: "dimaslanjaka" },
|
|
63
|
+
{ pkg: "hexo-cli", branch: "monorepo-v7", repo: "hexo", owner: "dimaslanjaka" },
|
|
64
|
+
{ pkg: "hexo-asset-link", branch: "monorepo-v7", repo: "hexo", owner: "dimaslanjaka" },
|
|
65
|
+
{ pkg: "hexo-post-parser", branch: "pre-release", repo: "hexo-post-parser", owner: "dimaslanjaka" },
|
|
66
|
+
{ pkg: "hexo-seo", branch: "pre-release", repo: "hexo-seo", owner: "dimaslanjaka" },
|
|
67
|
+
{ pkg: "hexo-is", branch: "master", repo: "hexo-is", owner: "dimaslanjaka" },
|
|
68
|
+
{ pkg: "markdown-it", branch: "master", repo: "markdown-it", owner: "dimaslanjaka" },
|
|
69
|
+
{ pkg: "hexo-renderers", branch: "pre-release", repo: "hexo-renderers", owner: "dimaslanjaka" },
|
|
70
|
+
{ pkg: "hexo-shortcodes", branch: "pre-release", repo: "hexo-shortcodes", owner: "dimaslanjaka" },
|
|
71
|
+
{ pkg: "google-news-sitemap", branch: "master", repo: "google-news-sitemap", owner: "dimaslanjaka" },
|
|
72
|
+
{ pkg: "git-command-helper", branch: "pre-release", repo: "git-command-helper", owner: "dimaslanjaka" },
|
|
73
|
+
{
|
|
74
|
+
pkg: "nodejs-package-types",
|
|
75
|
+
branch: "main",
|
|
76
|
+
repo: "nodejs-package-types",
|
|
77
|
+
owner: "dimaslanjaka"
|
|
78
|
+
},
|
|
79
|
+
{ pkg: "cross-spawn", branch: "private", repo: "node-cross-spawn", owner: "dimaslanjaka" },
|
|
80
|
+
{ pkg: "hexo-generator-redirect", branch: "master", repo: "hexo-generator-redirect", owner: "dimaslanjaka" },
|
|
81
|
+
{ pkg: "binary-collections", branch: "master", repo: "bin", owner: "dimaslanjaka" },
|
|
82
|
+
{ pkg: "@types/hexo", branch: "monorepo-v7", repo: "hexo", owner: "dimaslanjaka" },
|
|
83
|
+
{ pkg: "@types/git-command-helper", branch: "pre-release", repo: "git-command-helper", owner: "dimaslanjaka" }
|
|
84
|
+
];
|
|
85
|
+
// --- Optimized: Load package.json once at the top ---
|
|
86
|
+
const pkgPath = path.join(process.cwd(), "package.json");
|
|
87
|
+
let pkg;
|
|
88
|
+
try {
|
|
89
|
+
pkg = JSON.parse(fs.readFileSync(pkgPath, "utf-8"));
|
|
90
|
+
}
|
|
91
|
+
catch (e) {
|
|
92
|
+
console.error(ansiColors.red(`Failed to read package.json: ${e.message}`));
|
|
93
|
+
process.exit(1);
|
|
94
|
+
}
|
|
95
|
+
// --- Use a random User-Agent for GitHub API requests ---
|
|
96
|
+
const GITHUB_USER_AGENTS = [
|
|
97
|
+
"octokit-rest.js/19.0.7",
|
|
98
|
+
"GitHub CLI/2.40.0",
|
|
99
|
+
"Mozilla/5.0 (compatible; GitHubCopilot/1.0)",
|
|
100
|
+
"PostmanRuntime/7.32.3",
|
|
101
|
+
"binary-collections-resolver/1.0 (+https://github.com/dimaslanjaka/bin)"
|
|
102
|
+
];
|
|
103
|
+
// --- User-Agent persistence in system temp folder ---
|
|
104
|
+
const userAgentDir = path.join(os.tmpdir(), "nodejs");
|
|
105
|
+
const userAgentFile = path.join(userAgentDir, "useragent.txt");
|
|
106
|
+
let selectedUserAgent;
|
|
107
|
+
try {
|
|
108
|
+
if (!fs.existsSync(userAgentDir))
|
|
109
|
+
fs.mkdirSync(userAgentDir, { recursive: true });
|
|
110
|
+
if (fs.existsSync(userAgentFile)) {
|
|
111
|
+
const fileAgent = fs.readFileSync(userAgentFile, "utf-8").trim();
|
|
112
|
+
if (GITHUB_USER_AGENTS.includes(fileAgent)) {
|
|
113
|
+
selectedUserAgent = fileAgent;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
if (!selectedUserAgent) {
|
|
117
|
+
selectedUserAgent = GITHUB_USER_AGENTS[Math.floor(Math.random() * GITHUB_USER_AGENTS.length)];
|
|
118
|
+
fs.writeFileSync(userAgentFile, selectedUserAgent, "utf-8");
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
catch (_e) {
|
|
122
|
+
// fallback to random if any error
|
|
123
|
+
selectedUserAgent = GITHUB_USER_AGENTS[Math.floor(Math.random() * GITHUB_USER_AGENTS.length)];
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Fetch JSON from a URL with GitHub headers.
|
|
127
|
+
* @param {string} url
|
|
128
|
+
* @returns {Promise<any>}
|
|
129
|
+
*/
|
|
130
|
+
function fetchJson(url) {
|
|
131
|
+
const headers = Object.assign({ "User-Agent": selectedUserAgent, Accept: "application/vnd.github.v3+json", "X-GitHub-Api-Version": "2022-11-28" }, (process.env.GITHUB_TOKEN ? { Authorization: `token ${process.env.GITHUB_TOKEN}` } : {}));
|
|
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(new Error(`GitHub API Error ${res.statusCode}: ${json.message || "Unknown error"}\nURL: ${url}`));
|
|
142
|
+
}
|
|
143
|
+
resolve(json);
|
|
144
|
+
}
|
|
145
|
+
catch (_a) {
|
|
146
|
+
reject(new Error(`Invalid JSON from: ${url}`));
|
|
147
|
+
}
|
|
148
|
+
});
|
|
149
|
+
})
|
|
150
|
+
.on("error", reject);
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Get latest commit SHA from a specific branch.
|
|
155
|
+
*/
|
|
156
|
+
function getLatestCommit(owner_1, repo_1) {
|
|
157
|
+
return __awaiter(this, arguments, void 0, function* (owner, repo, branch = "main") {
|
|
158
|
+
var _a, _b, _c, _d;
|
|
159
|
+
const url = `https://api.github.com/repos/${owner}/${repo}/commits/${branch}`;
|
|
160
|
+
const json = yield fetchJson(url);
|
|
161
|
+
const sha = json.sha;
|
|
162
|
+
const dateStr = ((_b = (_a = json.commit) === null || _a === void 0 ? void 0 : _a.committer) === null || _b === void 0 ? void 0 : _b.date) || ((_d = (_c = json.commit) === null || _c === void 0 ? void 0 : _c.author) === null || _d === void 0 ? void 0 : _d.date);
|
|
163
|
+
if (!sha || !dateStr) {
|
|
164
|
+
console.log(json);
|
|
165
|
+
throw new Error(`Missing SHA or date for ${owner}/${repo}@${branch}`);
|
|
166
|
+
}
|
|
167
|
+
return {
|
|
168
|
+
owner,
|
|
169
|
+
repo,
|
|
170
|
+
branch,
|
|
171
|
+
sha,
|
|
172
|
+
date: new Date(dateStr).toISOString()
|
|
173
|
+
};
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Get latest commit SHA from all branches and pick the latest.
|
|
178
|
+
*/
|
|
179
|
+
function getLatestCommitAcrossBranches(owner, repo) {
|
|
180
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
181
|
+
const branches = yield fetchJson(`https://api.github.com/repos/${owner}/${repo}/branches`);
|
|
182
|
+
const commits = yield Promise.all(branches.map((_a) => __awaiter(this, [_a], void 0, function* ({ name, commit }) {
|
|
183
|
+
var _b, _c, _d, _f;
|
|
184
|
+
const commitSha = commit === null || commit === void 0 ? void 0 : commit.sha;
|
|
185
|
+
if (!commitSha) {
|
|
186
|
+
console.warn(`No commit SHA for '${owner}/${repo}' branch: ${name}`);
|
|
187
|
+
return { branch: name, sha: "", date: new Date(0) };
|
|
188
|
+
}
|
|
189
|
+
try {
|
|
190
|
+
const commitData = yield fetchJson(`https://api.github.com/repos/${owner}/${repo}/commits/${commitSha}`);
|
|
191
|
+
const dateStr = ((_c = (_b = commitData.commit) === null || _b === void 0 ? void 0 : _b.committer) === null || _c === void 0 ? void 0 : _c.date) || ((_f = (_d = commitData.commit) === null || _d === void 0 ? void 0 : _d.author) === null || _f === void 0 ? void 0 : _f.date);
|
|
192
|
+
const date = dateStr ? new Date(dateStr) : new Date(0);
|
|
193
|
+
return { branch: name, sha: commitData.sha, date };
|
|
194
|
+
}
|
|
195
|
+
catch (e) {
|
|
196
|
+
console.warn(`Failed to fetch commit for ${name}: ${e.message}`);
|
|
197
|
+
return { branch: name, sha: commitSha, date: new Date(0) };
|
|
198
|
+
}
|
|
199
|
+
})));
|
|
200
|
+
const latest = commits.reduce((a, b) => (a.date > b.date ? a : b), { date: new Date(0) });
|
|
201
|
+
return {
|
|
202
|
+
owner,
|
|
203
|
+
repo,
|
|
204
|
+
branch: latest.branch,
|
|
205
|
+
sha: latest.sha,
|
|
206
|
+
date: latest.date.toISOString()
|
|
207
|
+
};
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* Replace the branch or commit in a GitHub raw URL with the latest hash.
|
|
212
|
+
*/
|
|
213
|
+
function replaceRawWithLatestHash(url, latestHash) {
|
|
214
|
+
const match = url.match(/^https:\/\/github\.com\/([^/]+)\/([^/]+)\/raw\/([^/]+)\/(.+)$/);
|
|
215
|
+
if (!match)
|
|
216
|
+
throw new Error("Invalid GitHub raw URL");
|
|
217
|
+
const [, owner, repo, _oldHash, path] = match;
|
|
218
|
+
return `https://github.com/${owner}/${repo}/raw/${latestHash}/${path}`;
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* Parse GitHub URLs and extract owner, repo, branch, and original URL.
|
|
222
|
+
*/
|
|
223
|
+
function parseGitHubUrl(url) {
|
|
224
|
+
const ghRepoRoot = /^https:\/\/github\.com\/([^/]+)\/([^/]+)\/?$/;
|
|
225
|
+
const ghTreeOrBlob = /^https:\/\/github\.com\/([^/]+)\/([^/]+)\/(tree|blob)\/([^/]+(?:\/[^/]+)*)/;
|
|
226
|
+
const ghRaw = /^https:\/\/raw\.githubusercontent\.com\/([^/]+)\/([^/]+)\/([^/]+)(\/.+)?$/;
|
|
227
|
+
const ghDotComRaw = /^https:\/\/github\.com\/([^/]+)\/([^/]+)\/raw\/([^/]+)\/.+/;
|
|
228
|
+
let match;
|
|
229
|
+
if ((match = url.match(ghRaw))) {
|
|
230
|
+
const [, owner, repo, branch] = match;
|
|
231
|
+
return { owner, repo, branch, url };
|
|
232
|
+
}
|
|
233
|
+
if ((match = url.match(ghDotComRaw))) {
|
|
234
|
+
const [, owner, repo, branch] = match;
|
|
235
|
+
return { owner, repo, branch, url };
|
|
236
|
+
}
|
|
237
|
+
if ((match = url.match(ghTreeOrBlob))) {
|
|
238
|
+
const [, owner, repo, , branchPath] = match;
|
|
239
|
+
return { owner, repo, branch: branchPath, url };
|
|
240
|
+
}
|
|
241
|
+
if ((match = url.match(ghRepoRoot))) {
|
|
242
|
+
const [, owner, repo] = match;
|
|
243
|
+
return { owner, repo, url };
|
|
244
|
+
}
|
|
245
|
+
throw new Error(`Unsupported GitHub URL: ${url}`);
|
|
246
|
+
}
|
|
247
|
+
// --- Main logic ---
|
|
248
|
+
(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
249
|
+
const entries = Object.entries(pkg.resolutions || {});
|
|
250
|
+
if (entries.length === 0) {
|
|
251
|
+
console.log(ansiColors.yellow("No resolutions found in package.json"));
|
|
252
|
+
return;
|
|
253
|
+
}
|
|
254
|
+
console.log(`Processing ${entries.length} resolution(s)...`);
|
|
255
|
+
const updates = [];
|
|
256
|
+
for (const [currentPkgName, url] of entries) {
|
|
257
|
+
// Validate if URL is a GitHub URL
|
|
258
|
+
let repo;
|
|
259
|
+
try {
|
|
260
|
+
repo = parseGitHubUrl(url);
|
|
261
|
+
console.log(`✅ Valid GitHub URL for ${ansiColors.cyan(currentPkgName)}: ${url}`);
|
|
262
|
+
}
|
|
263
|
+
catch (error) {
|
|
264
|
+
console.log(`⏭️ Skipping ${ansiColors.yellow(currentPkgName)}: ${error.message}`);
|
|
265
|
+
continue;
|
|
266
|
+
}
|
|
267
|
+
try {
|
|
268
|
+
const override = specialPackageOverrides.find((p) => p.pkg === currentPkgName);
|
|
269
|
+
const latest = override
|
|
270
|
+
? yield getLatestCommit(override.owner, override.repo, override.branch)
|
|
271
|
+
: yield getLatestCommitAcrossBranches(repo.owner, repo.repo);
|
|
272
|
+
const new_url = replaceRawWithLatestHash(url, latest.sha);
|
|
273
|
+
updates.push({
|
|
274
|
+
currentPkgName,
|
|
275
|
+
url,
|
|
276
|
+
new_url,
|
|
277
|
+
repo,
|
|
278
|
+
latest
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
catch (error) {
|
|
282
|
+
console.log(`❌ Failed to process ${ansiColors.red(currentPkgName)}: ${error.message}`);
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
if (updates.length === 0) {
|
|
286
|
+
console.log(ansiColors.yellow("No GitHub URLs were processed"));
|
|
287
|
+
return;
|
|
288
|
+
}
|
|
289
|
+
console.log(`\n📝 Applying updates to ${updates.length} GitHub URL(s)...`);
|
|
290
|
+
let changed = false;
|
|
291
|
+
for (const { currentPkgName, url, new_url, repo, latest } of updates) {
|
|
292
|
+
if (url !== new_url) {
|
|
293
|
+
console.log(`\n${ansiColors.cyan(currentPkgName)}:`);
|
|
294
|
+
console.log(" from:", url.replace(repo.branch, ansiColors.red(repo.branch)));
|
|
295
|
+
console.log(" to:", new_url.replace(latest.sha, ansiColors.green(latest.sha)));
|
|
296
|
+
pkg.resolutions[currentPkgName] = new_url;
|
|
297
|
+
changed = true;
|
|
298
|
+
}
|
|
299
|
+
else {
|
|
300
|
+
console.log(`\n${ansiColors.cyan(currentPkgName)}: ${ansiColors.gray("already up-to-date")}`);
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
if (changed) {
|
|
304
|
+
try {
|
|
305
|
+
fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + "\n");
|
|
306
|
+
console.log(`\n✅ package.json updated successfully`);
|
|
307
|
+
}
|
|
308
|
+
catch (e) {
|
|
309
|
+
console.error(ansiColors.red(`Failed to write package.json: ${e.message}`));
|
|
310
|
+
process.exit(1);
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
else {
|
|
314
|
+
console.log(ansiColors.green("No changes to package.json were necessary."));
|
|
315
|
+
}
|
|
316
|
+
}))();
|
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
const fs = require("fs").promises;
|
|
12
|
+
const path = require("path");
|
|
13
|
+
const crypto = require("crypto");
|
|
14
|
+
const { execSync } = require("child_process");
|
|
15
|
+
const glob = require("glob");
|
|
16
|
+
const { getArgs } = require("./utils.js");
|
|
17
|
+
const sbgUtil = require("sbg-utility");
|
|
18
|
+
const dotenv = require("dotenv");
|
|
19
|
+
const projectDir = process.cwd();
|
|
20
|
+
const envPath = path.join(projectDir, ".env");
|
|
21
|
+
// Load the .env file using dotenv (ESM import)
|
|
22
|
+
if (fs.existsSync(envPath))
|
|
23
|
+
dotenv.config({ path: envPath });
|
|
24
|
+
// Parse CLI arguments
|
|
25
|
+
const argv = getArgs();
|
|
26
|
+
// Main logic wrapped in an async function
|
|
27
|
+
function main() {
|
|
28
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
29
|
+
// Determine output file from CLI args
|
|
30
|
+
let relativeOutputFile = "tmp/directory-structure.txt";
|
|
31
|
+
if (argv.output || argv.o) {
|
|
32
|
+
relativeOutputFile = argv.output || argv.o;
|
|
33
|
+
}
|
|
34
|
+
// If not absolute, resolve relative to projectDir
|
|
35
|
+
const outputFile = path.isAbsolute(relativeOutputFile)
|
|
36
|
+
? relativeOutputFile
|
|
37
|
+
: path.join(projectDir, relativeOutputFile);
|
|
38
|
+
// Create or clear the hash file
|
|
39
|
+
sbgUtil.writefile(outputFile, "");
|
|
40
|
+
/**
|
|
41
|
+
* List of file extensions to include
|
|
42
|
+
* @type {string[]}
|
|
43
|
+
*/
|
|
44
|
+
let extensions = [];
|
|
45
|
+
if (argv.ext) {
|
|
46
|
+
extensions = argv.ext
|
|
47
|
+
.split(",")
|
|
48
|
+
.map((e) => e.trim().replace(/^\./, ""))
|
|
49
|
+
.filter(Boolean);
|
|
50
|
+
}
|
|
51
|
+
// Directories to exclude
|
|
52
|
+
let excludeDirs = [
|
|
53
|
+
"node_modules",
|
|
54
|
+
"vendor",
|
|
55
|
+
"venv",
|
|
56
|
+
".venv",
|
|
57
|
+
".git",
|
|
58
|
+
".hg",
|
|
59
|
+
".svn",
|
|
60
|
+
".idea",
|
|
61
|
+
".vscode",
|
|
62
|
+
"dist",
|
|
63
|
+
"build",
|
|
64
|
+
"out",
|
|
65
|
+
"coverage",
|
|
66
|
+
".DS_Store"
|
|
67
|
+
];
|
|
68
|
+
if (argv.exclude) {
|
|
69
|
+
const userExcludes = argv.exclude
|
|
70
|
+
.split(",")
|
|
71
|
+
.map((d) => d.trim())
|
|
72
|
+
.filter(Boolean);
|
|
73
|
+
if (argv["override-exclude"] || argv.we) {
|
|
74
|
+
// Override the default excludes with user-provided ones
|
|
75
|
+
excludeDirs = userExcludes;
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
// Append user-provided excludes to the default ones
|
|
79
|
+
excludeDirs = excludeDirs.concat(userExcludes);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
// Convert excludeDirs into glob ignore patterns
|
|
83
|
+
const ignorePatterns = excludeDirs.map((dir) => `**/${dir}/**`);
|
|
84
|
+
// Initialize an array to hold the formatted outputs
|
|
85
|
+
let hashArray = [];
|
|
86
|
+
// Helper function to hash and add file if not already processed
|
|
87
|
+
const processedFiles = new Set();
|
|
88
|
+
/**
|
|
89
|
+
* Hashes a file and pushes its relative path and hash to the hashArray.
|
|
90
|
+
* @param {string} file - The absolute path to the file.
|
|
91
|
+
* @returns {Promise<void>} Resolves when the file has been processed and added to hashArray.
|
|
92
|
+
*/
|
|
93
|
+
function hashAndPush(file) {
|
|
94
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
95
|
+
if (processedFiles.has(file))
|
|
96
|
+
return;
|
|
97
|
+
processedFiles.add(file);
|
|
98
|
+
let relativePath = path.relative(projectDir, file);
|
|
99
|
+
relativePath = relativePath.split(path.sep).join("/");
|
|
100
|
+
try {
|
|
101
|
+
const stats = yield fs.stat(file);
|
|
102
|
+
const pseudoHash = `${stats.size}-${stats.mtimeMs}`;
|
|
103
|
+
const hash = crypto.createHash("sha256").update(pseudoHash).digest("hex");
|
|
104
|
+
hashArray.push(`${relativePath} ${hash.slice(0, 8)}`);
|
|
105
|
+
}
|
|
106
|
+
catch (err) {
|
|
107
|
+
console.error(`Error processing file: ${file}`, err instanceof Error ? err.message : "<unknown error>");
|
|
108
|
+
if (err && err.code === "ENOENT") {
|
|
109
|
+
hashArray.push(`${relativePath} <file not found>`);
|
|
110
|
+
}
|
|
111
|
+
else {
|
|
112
|
+
hashArray.push(`${relativePath} <error: ${err && err.code ? `code ${err.code}` : "unknown"}>`);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
// Collect all files to process (extensions + special files)
|
|
118
|
+
const initialFiles = [
|
|
119
|
+
path.join(projectDir, "package.json"),
|
|
120
|
+
path.join(projectDir, "composer.json"),
|
|
121
|
+
path.join(projectDir, "requirements.txt")
|
|
122
|
+
];
|
|
123
|
+
let patterns = [];
|
|
124
|
+
if (argv.pattern) {
|
|
125
|
+
if (Array.isArray(argv.pattern)) {
|
|
126
|
+
patterns = argv.pattern.map((p) => p.trim()).filter(Boolean);
|
|
127
|
+
}
|
|
128
|
+
else {
|
|
129
|
+
patterns = [argv.pattern.trim()];
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
else if (extensions.length === 0) {
|
|
133
|
+
patterns = ["**/*.*"];
|
|
134
|
+
}
|
|
135
|
+
else {
|
|
136
|
+
patterns = extensions.map((ext) => `**/*.${ext}`);
|
|
137
|
+
}
|
|
138
|
+
const globFiles = glob.sync(patterns.length === 1 ? patterns[0] : `{${patterns.join(",")}}`, {
|
|
139
|
+
cwd: projectDir,
|
|
140
|
+
ignore: ignorePatterns,
|
|
141
|
+
absolute: true,
|
|
142
|
+
nodir: true
|
|
143
|
+
});
|
|
144
|
+
const allFiles = new Set([...initialFiles, ...globFiles]);
|
|
145
|
+
// Hash all unique files
|
|
146
|
+
yield Promise.all(Array.from(allFiles).map(hashAndPush));
|
|
147
|
+
// Sort the hashArray by file paths
|
|
148
|
+
hashArray.sort((a, b) => a.localeCompare(b));
|
|
149
|
+
/**
|
|
150
|
+
* Generates a directory/file tree string from a hash array of file paths and hashes.
|
|
151
|
+
*
|
|
152
|
+
* @param {string[]} hashArray - Array of strings in the format 'relative/path/to/file hash'.
|
|
153
|
+
* @returns {string} The directory/file tree as a string, with file hashes.
|
|
154
|
+
*/
|
|
155
|
+
function getFileTreeString(hashArray) {
|
|
156
|
+
const tree = {};
|
|
157
|
+
// Map file paths to hashes for quick lookup
|
|
158
|
+
const hashMap = {};
|
|
159
|
+
for (const entry of hashArray) {
|
|
160
|
+
const [filePath, hash] = entry.split(" ");
|
|
161
|
+
hashMap[filePath] = hash;
|
|
162
|
+
const parts = filePath.split("/");
|
|
163
|
+
let current = tree;
|
|
164
|
+
for (let i = 0; i < parts.length; i++) {
|
|
165
|
+
const part = parts[i];
|
|
166
|
+
if (i === parts.length - 1) {
|
|
167
|
+
current[part] = null; // file
|
|
168
|
+
}
|
|
169
|
+
else {
|
|
170
|
+
current[part] = current[part] || {};
|
|
171
|
+
current = current[part];
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Recursively builds the tree string for a given node.
|
|
177
|
+
*
|
|
178
|
+
* @param {Object} node - The current node in the tree.
|
|
179
|
+
* @param {string} prefix - The prefix for the current tree level.
|
|
180
|
+
* @param {string} parentPath - The path to the current node.
|
|
181
|
+
* @returns {string[]} Array of lines representing the tree structure.
|
|
182
|
+
*/
|
|
183
|
+
function printNode(node, prefix = "", parentPath = "") {
|
|
184
|
+
const keys = Object.keys(node).sort();
|
|
185
|
+
let lines = [];
|
|
186
|
+
keys.forEach((key, idx) => {
|
|
187
|
+
const isLast = idx === keys.length - 1;
|
|
188
|
+
const branch = isLast ? "└── " : "├── ";
|
|
189
|
+
const currentPath = parentPath ? parentPath + "/" + key : key;
|
|
190
|
+
if (node[key] === null) {
|
|
191
|
+
// file: show hash
|
|
192
|
+
lines.push(prefix + branch + key + " [" + (hashMap[currentPath] || "") + "]");
|
|
193
|
+
}
|
|
194
|
+
else {
|
|
195
|
+
lines.push(prefix + branch + key + "/");
|
|
196
|
+
lines = lines.concat(printNode(node[key], prefix + (isLast ? " " : "│ "), currentPath));
|
|
197
|
+
}
|
|
198
|
+
});
|
|
199
|
+
return lines;
|
|
200
|
+
}
|
|
201
|
+
return printNode(tree, "", "").join("\n");
|
|
202
|
+
}
|
|
203
|
+
// Write directory/file tree to the output file (hashes are included in the tree)
|
|
204
|
+
const fileTreeString = getFileTreeString(hashArray);
|
|
205
|
+
yield fs.writeFile(outputFile, fileTreeString + "\n", "utf-8");
|
|
206
|
+
// Add the hash file to the commit if --git-add is present
|
|
207
|
+
if (argv["git-add"]) {
|
|
208
|
+
execSync(`git add ${relativeOutputFile}`);
|
|
209
|
+
console.log(`Directory tree written to ${relativeOutputFile} and staged for git.`);
|
|
210
|
+
}
|
|
211
|
+
else {
|
|
212
|
+
console.log(`Directory tree written to ${relativeOutputFile}.`);
|
|
213
|
+
}
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
if (argv.help || argv.h) {
|
|
217
|
+
console.log(`
|
|
218
|
+
Usage: node print-directory-tree.cjs [options]
|
|
219
|
+
|
|
220
|
+
Options:
|
|
221
|
+
--output, -o <file> Output file path (default: tmp/directory-structure.txt)
|
|
222
|
+
--ext <exts> Comma-separated list of file extensions (no dot, e.g. js,ts)
|
|
223
|
+
--pattern <glob> Glob pattern(s) for files (can be repeated)
|
|
224
|
+
--exclude <dirs> Comma-separated list of directories to exclude (appends to default)
|
|
225
|
+
--override-exclude, -we Override default exclude directories with --exclude
|
|
226
|
+
--git-add Add output file to git after writing
|
|
227
|
+
--help, -h Show this help message
|
|
228
|
+
|
|
229
|
+
Examples:
|
|
230
|
+
node print-directory-tree.cjs --ext=js,ts
|
|
231
|
+
node print-directory-tree.cjs --pattern=src/**/*.js --pattern=test/**/*.js
|
|
232
|
+
node print-directory-tree.cjs --exclude=dist,build
|
|
233
|
+
node print-directory-tree.cjs --output=tmp/tree.txt
|
|
234
|
+
`);
|
|
235
|
+
process.exit(0);
|
|
236
|
+
}
|
|
237
|
+
// Execute the main function
|
|
238
|
+
main().catch((err) => {
|
|
239
|
+
console.error(err);
|
|
240
|
+
process.exit(1);
|
|
241
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|