@yabasha/gex 1.0.1 → 1.3.0
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 +16 -4
- package/dist/cli-bun.mjs +916 -0
- package/dist/cli-bun.mjs.map +1 -0
- package/dist/cli-node.cjs +766 -0
- package/dist/cli-node.cjs.map +1 -0
- package/dist/cli-node.mjs +734 -0
- package/dist/cli-node.mjs.map +1 -0
- package/dist/cli.cjs +359 -302
- package/dist/cli.cjs.map +1 -1
- package/dist/cli.mjs +356 -300
- package/dist/cli.mjs.map +1 -1
- package/dist/index.cjs +18 -9
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +18 -9
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -2
|
@@ -0,0 +1,734 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
3
|
+
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
4
|
+
}) : x)(function(x) {
|
|
5
|
+
if (typeof require !== "undefined") return require.apply(this, arguments);
|
|
6
|
+
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
// src/runtimes/node/commands.ts
|
|
10
|
+
import path6 from "path";
|
|
11
|
+
import { Command } from "commander";
|
|
12
|
+
|
|
13
|
+
// src/shared/cli/install.ts
|
|
14
|
+
var INSTALL_COMMANDS = {
|
|
15
|
+
npm: {
|
|
16
|
+
global: ["i", "-g"],
|
|
17
|
+
local: ["i"],
|
|
18
|
+
dev: ["i", "-D"]
|
|
19
|
+
},
|
|
20
|
+
bun: {
|
|
21
|
+
global: ["add", "-g"],
|
|
22
|
+
local: ["add"],
|
|
23
|
+
dev: ["add", "-d"]
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
var MAX_BUFFER = 10 * 1024 * 1024;
|
|
27
|
+
function formatSpec(pkg) {
|
|
28
|
+
return pkg.version ? `${pkg.name}@${pkg.version}` : pkg.name;
|
|
29
|
+
}
|
|
30
|
+
async function getExecFileAsync() {
|
|
31
|
+
const { execFile } = await import("child_process");
|
|
32
|
+
const { promisify: promisify2 } = await import("util");
|
|
33
|
+
return promisify2(execFile);
|
|
34
|
+
}
|
|
35
|
+
async function installFromReport(report, options) {
|
|
36
|
+
const opts = typeof options === "string" ? { cwd: options } : options;
|
|
37
|
+
const { cwd, packageManager = "npm" } = opts;
|
|
38
|
+
const globalPkgs = report.global_packages.map(formatSpec).filter(Boolean);
|
|
39
|
+
const localPkgs = report.local_dependencies.map(formatSpec).filter(Boolean);
|
|
40
|
+
const devPkgs = report.local_dev_dependencies.map(formatSpec).filter(Boolean);
|
|
41
|
+
if (globalPkgs.length === 0 && localPkgs.length === 0 && devPkgs.length === 0) {
|
|
42
|
+
console.log("No packages to install from report.");
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
const execFileAsync = await getExecFileAsync();
|
|
46
|
+
const cmd = INSTALL_COMMANDS[packageManager];
|
|
47
|
+
const binary = packageManager === "bun" ? "bun" : "npm";
|
|
48
|
+
if (globalPkgs.length > 0) {
|
|
49
|
+
console.log(`Installing global: ${globalPkgs.join(" ")}`);
|
|
50
|
+
await execFileAsync(binary, [...cmd.global, ...globalPkgs], { cwd, maxBuffer: MAX_BUFFER });
|
|
51
|
+
}
|
|
52
|
+
if (localPkgs.length > 0) {
|
|
53
|
+
console.log(`Installing local deps: ${localPkgs.join(" ")}`);
|
|
54
|
+
await execFileAsync(binary, [...cmd.local, ...localPkgs], { cwd, maxBuffer: MAX_BUFFER });
|
|
55
|
+
}
|
|
56
|
+
if (devPkgs.length > 0) {
|
|
57
|
+
console.log(`Installing local devDeps: ${devPkgs.join(" ")}`);
|
|
58
|
+
await execFileAsync(binary, [...cmd.dev, ...devPkgs], { cwd, maxBuffer: MAX_BUFFER });
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
function printFromReport(report) {
|
|
62
|
+
const lines = [];
|
|
63
|
+
if (report.global_packages.length > 0) {
|
|
64
|
+
lines.push("Global Packages:");
|
|
65
|
+
for (const p of report.global_packages) {
|
|
66
|
+
lines.push(`- ${p.name}@${p.version}`);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
if (report.local_dependencies.length > 0) {
|
|
70
|
+
if (lines.length) lines.push("");
|
|
71
|
+
lines.push("Local Dependencies:");
|
|
72
|
+
for (const p of report.local_dependencies) {
|
|
73
|
+
lines.push(`- ${p.name}@${p.version}`);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
if (report.local_dev_dependencies.length > 0) {
|
|
77
|
+
if (lines.length) lines.push("");
|
|
78
|
+
lines.push("Local Dev Dependencies:");
|
|
79
|
+
for (const p of report.local_dev_dependencies) {
|
|
80
|
+
lines.push(`- ${p.name}@${p.version}`);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
if (lines.length === 0) {
|
|
84
|
+
lines.push("(no packages found in report)");
|
|
85
|
+
}
|
|
86
|
+
console.log(lines.join("\n"));
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// src/shared/cli/output.ts
|
|
90
|
+
import path from "path";
|
|
91
|
+
|
|
92
|
+
// src/shared/report/json.ts
|
|
93
|
+
function renderJson(report) {
|
|
94
|
+
const r = {
|
|
95
|
+
...report,
|
|
96
|
+
global_packages: [...report.global_packages].sort((a, b) => a.name.localeCompare(b.name)),
|
|
97
|
+
local_dependencies: [...report.local_dependencies].sort((a, b) => a.name.localeCompare(b.name)),
|
|
98
|
+
local_dev_dependencies: [...report.local_dev_dependencies].sort(
|
|
99
|
+
(a, b) => a.name.localeCompare(b.name)
|
|
100
|
+
)
|
|
101
|
+
};
|
|
102
|
+
return JSON.stringify(r, null, 2);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// src/shared/report/md.ts
|
|
106
|
+
function table(headers, rows) {
|
|
107
|
+
const header = `| ${headers.join(" | ")} |`;
|
|
108
|
+
const sep = `| ${headers.map(() => "---").join(" | ")} |`;
|
|
109
|
+
const body = rows.map((r) => `| ${r.join(" | ")} |`).join("\n");
|
|
110
|
+
return [header, sep, body].filter(Boolean).join("\n");
|
|
111
|
+
}
|
|
112
|
+
function renderMarkdown(report) {
|
|
113
|
+
const lines = [];
|
|
114
|
+
lines.push("# GEX Report");
|
|
115
|
+
lines.push("");
|
|
116
|
+
if (report.project_name || report.project_version || report.project_description || report.project_homepage || report.project_bugs) {
|
|
117
|
+
lines.push("## Project Metadata");
|
|
118
|
+
if (report.project_name) lines.push(`- Name: ${report.project_name}`);
|
|
119
|
+
if (report.project_version) lines.push(`- Version: ${report.project_version}`);
|
|
120
|
+
if (report.project_description)
|
|
121
|
+
lines.push(`- Description: ${report.project_description}`);
|
|
122
|
+
if (report.project_homepage)
|
|
123
|
+
lines.push(`- Homepage: ${report.project_homepage}`);
|
|
124
|
+
if (report.project_bugs) lines.push(`- Bugs: ${report.project_bugs}`);
|
|
125
|
+
lines.push("");
|
|
126
|
+
}
|
|
127
|
+
if (report.global_packages.length > 0) {
|
|
128
|
+
lines.push("## Global Packages");
|
|
129
|
+
const rows = report.global_packages.map((p) => [p.name, p.version || "", p.resolved_path || ""]);
|
|
130
|
+
lines.push(table(["Name", "Version", "Path"], rows));
|
|
131
|
+
lines.push("");
|
|
132
|
+
}
|
|
133
|
+
if (report.local_dependencies.length > 0) {
|
|
134
|
+
lines.push("## Local Dependencies");
|
|
135
|
+
const rows = report.local_dependencies.map((p) => [
|
|
136
|
+
p.name,
|
|
137
|
+
p.version || "",
|
|
138
|
+
p.resolved_path || ""
|
|
139
|
+
]);
|
|
140
|
+
lines.push(table(["Name", "Version", "Path"], rows));
|
|
141
|
+
lines.push("");
|
|
142
|
+
}
|
|
143
|
+
if (report.local_dev_dependencies.length > 0) {
|
|
144
|
+
lines.push("## Local Dev Dependencies");
|
|
145
|
+
const rows = report.local_dev_dependencies.map((p) => [
|
|
146
|
+
p.name,
|
|
147
|
+
p.version || "",
|
|
148
|
+
p.resolved_path || ""
|
|
149
|
+
]);
|
|
150
|
+
lines.push(table(["Name", "Version", "Path"], rows));
|
|
151
|
+
lines.push("");
|
|
152
|
+
}
|
|
153
|
+
lines.push("---");
|
|
154
|
+
lines.push("_Generated by GEX_");
|
|
155
|
+
return lines.join("\n");
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// src/shared/cli/output.ts
|
|
159
|
+
async function outputReport(report, format, outFile, markdownExtras) {
|
|
160
|
+
const content = format === "json" ? renderJson(report) : renderMarkdown({ ...report, ...markdownExtras || {} });
|
|
161
|
+
if (outFile) {
|
|
162
|
+
const outDir = path.dirname(outFile);
|
|
163
|
+
const { mkdir, writeFile } = await import("fs/promises");
|
|
164
|
+
await mkdir(outDir, { recursive: true });
|
|
165
|
+
await writeFile(outFile, content, "utf8");
|
|
166
|
+
console.log(`Wrote report to ${outFile}`);
|
|
167
|
+
} else {
|
|
168
|
+
console.log(content);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// src/shared/cli/parser.ts
|
|
173
|
+
import { readFile } from "fs/promises";
|
|
174
|
+
import path2 from "path";
|
|
175
|
+
function isMarkdownReportFile(filePath) {
|
|
176
|
+
const ext = path2.extname(filePath).toLowerCase();
|
|
177
|
+
return ext === ".md" || ext === ".markdown";
|
|
178
|
+
}
|
|
179
|
+
function parseMarkdownPackagesTable(lines, startIndex) {
|
|
180
|
+
const rows = [];
|
|
181
|
+
if (!lines[startIndex] || !lines[startIndex].trim().startsWith("|")) return rows;
|
|
182
|
+
let i = startIndex + 2;
|
|
183
|
+
while (i < lines.length && lines[i].trim().startsWith("|")) {
|
|
184
|
+
const cols = lines[i].split("|").map((c) => c.trim()).filter((_, idx, arr) => !(idx === 0 || idx === arr.length - 1));
|
|
185
|
+
const [name = "", version = "", resolved_path = ""] = cols;
|
|
186
|
+
if (name) rows.push({ name, version, resolved_path });
|
|
187
|
+
i++;
|
|
188
|
+
}
|
|
189
|
+
return rows;
|
|
190
|
+
}
|
|
191
|
+
function parseMarkdownReport(md) {
|
|
192
|
+
const lines = md.split(/\r?\n/);
|
|
193
|
+
const findSection = (title) => lines.findIndex((l) => l.trim().toLowerCase() === `## ${title}`.toLowerCase());
|
|
194
|
+
const parseSection = (idx) => {
|
|
195
|
+
if (idx < 0) return [];
|
|
196
|
+
let i = idx + 1;
|
|
197
|
+
while (i < lines.length && !lines[i].trim().startsWith("|")) i++;
|
|
198
|
+
return parseMarkdownPackagesTable(lines, i);
|
|
199
|
+
};
|
|
200
|
+
const global_packages = parseSection(findSection("Global Packages"));
|
|
201
|
+
const local_dependencies = parseSection(findSection("Local Dependencies"));
|
|
202
|
+
const local_dev_dependencies = parseSection(findSection("Local Dev Dependencies"));
|
|
203
|
+
const report = {
|
|
204
|
+
report_version: "1.0",
|
|
205
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
206
|
+
tool_version: "unknown",
|
|
207
|
+
global_packages,
|
|
208
|
+
local_dependencies,
|
|
209
|
+
local_dev_dependencies
|
|
210
|
+
};
|
|
211
|
+
return report;
|
|
212
|
+
}
|
|
213
|
+
async function loadReportFromFile(reportPath) {
|
|
214
|
+
const raw = await readFile(reportPath, "utf8");
|
|
215
|
+
if (isMarkdownReportFile(reportPath) || raw.startsWith("# GEX Report")) {
|
|
216
|
+
return parseMarkdownReport(raw);
|
|
217
|
+
}
|
|
218
|
+
return JSON.parse(raw);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
// src/shared/npm-cli.ts
|
|
222
|
+
import { promisify } from "util";
|
|
223
|
+
async function getExecFileAsync2() {
|
|
224
|
+
const { execFile } = await import("child_process");
|
|
225
|
+
return promisify(execFile);
|
|
226
|
+
}
|
|
227
|
+
async function npmOutdated(options = {}) {
|
|
228
|
+
const args = ["outdated", "--json"];
|
|
229
|
+
if (options.global) args.push("--global");
|
|
230
|
+
try {
|
|
231
|
+
const execFileAsync = await getExecFileAsync2();
|
|
232
|
+
const { stdout } = await execFileAsync("npm", args, {
|
|
233
|
+
cwd: options.cwd,
|
|
234
|
+
maxBuffer: 10 * 1024 * 1024
|
|
235
|
+
});
|
|
236
|
+
return normalizeOutdated(stdout);
|
|
237
|
+
} catch (error) {
|
|
238
|
+
const stdout = typeof error?.stdout === "string" ? error.stdout : "";
|
|
239
|
+
if (stdout.trim()) {
|
|
240
|
+
return normalizeOutdated(stdout);
|
|
241
|
+
}
|
|
242
|
+
throw formatNpmError(error, "npm outdated");
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
async function npmUpdate(options) {
|
|
246
|
+
const args = ["update"];
|
|
247
|
+
if (options.global) args.push("-g");
|
|
248
|
+
if (options.packages && options.packages.length > 0) args.push(...options.packages);
|
|
249
|
+
try {
|
|
250
|
+
const execFileAsync = await getExecFileAsync2();
|
|
251
|
+
await execFileAsync("npm", args, {
|
|
252
|
+
cwd: options.cwd,
|
|
253
|
+
maxBuffer: 10 * 1024 * 1024
|
|
254
|
+
});
|
|
255
|
+
} catch (error) {
|
|
256
|
+
throw formatNpmError(error, "npm update");
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
function normalizeOutdated(stdout) {
|
|
260
|
+
if (!stdout.trim()) return [];
|
|
261
|
+
let data;
|
|
262
|
+
try {
|
|
263
|
+
data = JSON.parse(stdout);
|
|
264
|
+
} catch {
|
|
265
|
+
return [];
|
|
266
|
+
}
|
|
267
|
+
if (!data) return [];
|
|
268
|
+
return Object.entries(data).map(([name, info]) => ({
|
|
269
|
+
name,
|
|
270
|
+
current: info?.current ? String(info.current) : "",
|
|
271
|
+
wanted: info?.wanted ? String(info.wanted) : "",
|
|
272
|
+
latest: info?.latest ? String(info.latest) : "",
|
|
273
|
+
type: info?.type ? String(info.type) : void 0
|
|
274
|
+
}));
|
|
275
|
+
}
|
|
276
|
+
function formatNpmError(error, commandLabel) {
|
|
277
|
+
const stderr = typeof error?.stderr === "string" ? error.stderr.trim() : "";
|
|
278
|
+
const message = stderr || error?.message || `${commandLabel} failed`;
|
|
279
|
+
return new Error(`${commandLabel} failed: ${message}`);
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
// src/shared/cli/loader.ts
|
|
283
|
+
var frames = ["-", "\\", "|", "/"];
|
|
284
|
+
function createLoader(message) {
|
|
285
|
+
if (!process.stdout.isTTY) {
|
|
286
|
+
console.log(`${message}...`);
|
|
287
|
+
return {
|
|
288
|
+
stop(finalMessage) {
|
|
289
|
+
if (finalMessage) console.log(finalMessage);
|
|
290
|
+
}
|
|
291
|
+
};
|
|
292
|
+
}
|
|
293
|
+
let index = 0;
|
|
294
|
+
const interval = setInterval(() => {
|
|
295
|
+
const frame = frames[index % frames.length];
|
|
296
|
+
index += 1;
|
|
297
|
+
process.stdout.write(`\r${message} ${frame}`);
|
|
298
|
+
}, 80);
|
|
299
|
+
return {
|
|
300
|
+
stop(finalMessage) {
|
|
301
|
+
clearInterval(interval);
|
|
302
|
+
process.stdout.write("\r");
|
|
303
|
+
if (finalMessage) {
|
|
304
|
+
console.log(finalMessage);
|
|
305
|
+
} else {
|
|
306
|
+
process.stdout.write("\x1B[2K");
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
};
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
// src/shared/cli/outdated.ts
|
|
313
|
+
function normalizeUpdateSelection(value) {
|
|
314
|
+
if (value === void 0) {
|
|
315
|
+
return { shouldUpdate: false, updateAll: false, packages: [] };
|
|
316
|
+
}
|
|
317
|
+
if (value === true) {
|
|
318
|
+
return { shouldUpdate: true, updateAll: true, packages: [] };
|
|
319
|
+
}
|
|
320
|
+
const packages = Array.isArray(value) ? value : typeof value === "string" ? [value] : [];
|
|
321
|
+
const normalized = packages.flatMap((entry) => String(entry).split(",").map((part) => part.trim())).filter(Boolean);
|
|
322
|
+
return {
|
|
323
|
+
shouldUpdate: true,
|
|
324
|
+
updateAll: false,
|
|
325
|
+
packages: normalized
|
|
326
|
+
};
|
|
327
|
+
}
|
|
328
|
+
function formatOutdatedTable(entries) {
|
|
329
|
+
const headers = ["Name", "Current", "Wanted", "Latest", "Type"];
|
|
330
|
+
const rows = entries.map((entry) => [
|
|
331
|
+
entry.name,
|
|
332
|
+
entry.current || "-",
|
|
333
|
+
entry.wanted || "-",
|
|
334
|
+
entry.latest || "-",
|
|
335
|
+
entry.type || "-"
|
|
336
|
+
]);
|
|
337
|
+
const widths = headers.map(
|
|
338
|
+
(header, index) => Math.max(header.length, ...rows.map((row) => row[index].length))
|
|
339
|
+
);
|
|
340
|
+
const formatRow = (columns) => columns.map((col, idx) => col.padEnd(widths[idx], " ")).join(" ");
|
|
341
|
+
const lines = [formatRow(headers), formatRow(widths.map((w) => "-".repeat(w)))];
|
|
342
|
+
for (const row of rows) {
|
|
343
|
+
lines.push(formatRow(row));
|
|
344
|
+
}
|
|
345
|
+
return lines.join("\n");
|
|
346
|
+
}
|
|
347
|
+
async function handleOutdatedWorkflow(opts) {
|
|
348
|
+
if (!opts.checkOutdated && !opts.selection.shouldUpdate) {
|
|
349
|
+
return true;
|
|
350
|
+
}
|
|
351
|
+
let fetchLoader;
|
|
352
|
+
if (opts.checkOutdated || opts.selection.shouldUpdate) {
|
|
353
|
+
fetchLoader = createLoader("Checking for outdated packages");
|
|
354
|
+
}
|
|
355
|
+
const outdated = await opts.fetchOutdated();
|
|
356
|
+
fetchLoader?.stop("Finished checking outdated packages.");
|
|
357
|
+
if (opts.checkOutdated) {
|
|
358
|
+
if (outdated.length === 0) {
|
|
359
|
+
console.log(`All ${opts.contextLabel} packages are up to date.`);
|
|
360
|
+
} else {
|
|
361
|
+
console.log(formatOutdatedTable(outdated));
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
if (opts.selection.shouldUpdate && opts.updateRunner) {
|
|
365
|
+
const packagesToUpdate = opts.selection.updateAll ? outdated.map((entry) => entry.name) : opts.selection.packages;
|
|
366
|
+
if (!packagesToUpdate || packagesToUpdate.length === 0) {
|
|
367
|
+
if (opts.selection.updateAll) {
|
|
368
|
+
console.log("No outdated packages to update.");
|
|
369
|
+
} else {
|
|
370
|
+
console.log("No packages were specified for updating.");
|
|
371
|
+
}
|
|
372
|
+
} else {
|
|
373
|
+
const updateLoader = createLoader("Updating packages");
|
|
374
|
+
await opts.updateRunner(packagesToUpdate);
|
|
375
|
+
updateLoader.stop("Finished updating packages.");
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
if (opts.checkOutdated || opts.selection.shouldUpdate) {
|
|
379
|
+
if (!opts.outFile) {
|
|
380
|
+
return false;
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
return true;
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
// src/shared/cli/utils.ts
|
|
387
|
+
import { existsSync } from "fs";
|
|
388
|
+
import { readFile as readFile2 } from "fs/promises";
|
|
389
|
+
import path3 from "path";
|
|
390
|
+
import { fileURLToPath } from "url";
|
|
391
|
+
function getPkgJsonPath() {
|
|
392
|
+
let startDir;
|
|
393
|
+
try {
|
|
394
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
395
|
+
startDir = path3.dirname(__filename);
|
|
396
|
+
} catch {
|
|
397
|
+
startDir = typeof __dirname !== "undefined" ? __dirname : process.cwd();
|
|
398
|
+
}
|
|
399
|
+
return findPackageJson(startDir);
|
|
400
|
+
}
|
|
401
|
+
function findPackageJson(startDir) {
|
|
402
|
+
let current = startDir;
|
|
403
|
+
const maxDepth = 6;
|
|
404
|
+
for (let i = 0; i < maxDepth; i++) {
|
|
405
|
+
const candidate = path3.resolve(current, "package.json");
|
|
406
|
+
if (existsSync(candidate)) {
|
|
407
|
+
return candidate;
|
|
408
|
+
}
|
|
409
|
+
const parent = path3.dirname(current);
|
|
410
|
+
if (parent === current) break;
|
|
411
|
+
current = parent;
|
|
412
|
+
}
|
|
413
|
+
return path3.resolve(process.cwd(), "package.json");
|
|
414
|
+
}
|
|
415
|
+
async function getToolVersion() {
|
|
416
|
+
try {
|
|
417
|
+
const pkgPath = getPkgJsonPath();
|
|
418
|
+
const raw = await readFile2(pkgPath, "utf8");
|
|
419
|
+
const pkg = JSON.parse(raw);
|
|
420
|
+
return pkg.version || "0.0.0";
|
|
421
|
+
} catch {
|
|
422
|
+
return "0.0.0";
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
var ASCII_BANNER = String.raw`
|
|
426
|
+
________ __
|
|
427
|
+
/ _____/ ____ _____/ |_ ____ ____
|
|
428
|
+
/ \ ___ / _ \ / _ \ __\/ __ \ / \
|
|
429
|
+
\ \_\ ( <_> | <_> ) | \ ___/| | \
|
|
430
|
+
\______ /\____/ \____/|__| \___ >___| /
|
|
431
|
+
\/ \/ \/
|
|
432
|
+
GEX
|
|
433
|
+
`;
|
|
434
|
+
|
|
435
|
+
// src/runtimes/node/report.ts
|
|
436
|
+
import { readFile as readFile4 } from "fs/promises";
|
|
437
|
+
import path5 from "path";
|
|
438
|
+
|
|
439
|
+
// src/shared/transform.ts
|
|
440
|
+
import path4 from "path";
|
|
441
|
+
import { readFile as readFile3 } from "fs/promises";
|
|
442
|
+
function toPkgArray(obj) {
|
|
443
|
+
if (!obj) return [];
|
|
444
|
+
return Object.keys(obj).map((name) => ({ name, node: obj[name] })).filter((p) => p && p.node);
|
|
445
|
+
}
|
|
446
|
+
async function buildReportFromNpmTree(tree, opts) {
|
|
447
|
+
const timestamp = (/* @__PURE__ */ new Date()).toISOString();
|
|
448
|
+
const report = {
|
|
449
|
+
report_version: "1.0",
|
|
450
|
+
timestamp,
|
|
451
|
+
tool_version: opts.toolVersion,
|
|
452
|
+
global_packages: [],
|
|
453
|
+
local_dependencies: [],
|
|
454
|
+
local_dev_dependencies: []
|
|
455
|
+
};
|
|
456
|
+
if (opts.context === "local") {
|
|
457
|
+
let pkgMeta = null;
|
|
458
|
+
try {
|
|
459
|
+
const pkgJsonPath = path4.join(opts.cwd || process.cwd(), "package.json");
|
|
460
|
+
const raw = await readFile3(pkgJsonPath, "utf8");
|
|
461
|
+
pkgMeta = JSON.parse(raw);
|
|
462
|
+
} catch {
|
|
463
|
+
}
|
|
464
|
+
if (pkgMeta?.name) report.project_name = pkgMeta.name;
|
|
465
|
+
if (pkgMeta?.version) report.project_version = pkgMeta.version;
|
|
466
|
+
const depsObj = tree?.dependencies;
|
|
467
|
+
const devDepsObj = tree?.devDependencies;
|
|
468
|
+
const prodItems = toPkgArray(depsObj);
|
|
469
|
+
const treeDevItems = toPkgArray(devDepsObj);
|
|
470
|
+
if (treeDevItems.length > 0) {
|
|
471
|
+
for (const { name, node } of treeDevItems) {
|
|
472
|
+
const version = node && node.version || "";
|
|
473
|
+
const resolvedPath = node && node.path || path4.join(opts.cwd || process.cwd(), "node_modules", name);
|
|
474
|
+
report.local_dev_dependencies.push({ name, version, resolved_path: resolvedPath });
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
const devKeys = treeDevItems.length > 0 ? new Set(treeDevItems.map((entry) => entry.name)) : new Set(Object.keys(pkgMeta?.devDependencies || {}));
|
|
478
|
+
for (const { name, node } of prodItems) {
|
|
479
|
+
const version = node && node.version || "";
|
|
480
|
+
const resolvedPath = node && node.path || path4.join(opts.cwd || process.cwd(), "node_modules", name);
|
|
481
|
+
const pkg = { name, version, resolved_path: resolvedPath };
|
|
482
|
+
if (!treeDevItems.length && devKeys.has(name)) {
|
|
483
|
+
report.local_dev_dependencies.push(pkg);
|
|
484
|
+
} else {
|
|
485
|
+
report.local_dependencies.push(pkg);
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
report.local_dependencies.sort((a, b) => a.name.localeCompare(b.name));
|
|
489
|
+
report.local_dev_dependencies.sort((a, b) => a.name.localeCompare(b.name));
|
|
490
|
+
} else if (opts.context === "global") {
|
|
491
|
+
const depsObj = tree?.dependencies;
|
|
492
|
+
const items = toPkgArray(depsObj);
|
|
493
|
+
for (const { name, node } of items) {
|
|
494
|
+
const version = node && node.version || "";
|
|
495
|
+
const resolvedPath = node && node.path || path4.join(opts.globalRoot || "", name);
|
|
496
|
+
const pkg = { name, version, resolved_path: resolvedPath };
|
|
497
|
+
report.global_packages.push(pkg);
|
|
498
|
+
}
|
|
499
|
+
report.global_packages.sort((a, b) => a.name.localeCompare(b.name));
|
|
500
|
+
}
|
|
501
|
+
if (opts.includeTree) {
|
|
502
|
+
report.tree = tree;
|
|
503
|
+
}
|
|
504
|
+
return report;
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
// src/runtimes/node/package-manager.ts
|
|
508
|
+
async function getExecFileAsync3() {
|
|
509
|
+
const { execFile } = await import("child_process");
|
|
510
|
+
const { promisify: promisify2 } = await import("util");
|
|
511
|
+
return promisify2(execFile);
|
|
512
|
+
}
|
|
513
|
+
async function npmLs(options = {}) {
|
|
514
|
+
const args = ["ls", "--json"];
|
|
515
|
+
if (options.global) args.push("--global");
|
|
516
|
+
if (options.omitDev) args.push("--omit=dev");
|
|
517
|
+
if (options.depth0) args.push("--depth=0");
|
|
518
|
+
try {
|
|
519
|
+
const execFileAsync = await getExecFileAsync3();
|
|
520
|
+
const { stdout } = await execFileAsync("npm", args, {
|
|
521
|
+
cwd: options.cwd,
|
|
522
|
+
maxBuffer: 10 * 1024 * 1024
|
|
523
|
+
});
|
|
524
|
+
if (stdout && stdout.trim()) return JSON.parse(stdout);
|
|
525
|
+
return {};
|
|
526
|
+
} catch (err) {
|
|
527
|
+
const stdout = err?.stdout;
|
|
528
|
+
if (typeof stdout === "string" && stdout.trim()) {
|
|
529
|
+
try {
|
|
530
|
+
return JSON.parse(stdout);
|
|
531
|
+
} catch (parseErr) {
|
|
532
|
+
if (process.env.DEBUG?.includes("gex")) {
|
|
533
|
+
console.warn("npm ls stdout parse failed:", parseErr);
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
const stderr = err?.stderr;
|
|
538
|
+
const msg = typeof stderr === "string" && stderr.trim() || err?.message || "npm ls failed";
|
|
539
|
+
throw new Error(`npm ls failed: ${msg}`);
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
async function npmRootGlobal() {
|
|
543
|
+
try {
|
|
544
|
+
const execFileAsync = await getExecFileAsync3();
|
|
545
|
+
const { stdout } = await execFileAsync("npm", ["root", "-g"]);
|
|
546
|
+
return stdout.trim();
|
|
547
|
+
} catch (err) {
|
|
548
|
+
const stderr = err?.stderr;
|
|
549
|
+
const msg = typeof stderr === "string" && stderr.trim() || err?.message || "npm root -g failed";
|
|
550
|
+
throw new Error(`npm root -g failed: ${msg}`);
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
// src/runtimes/node/report.ts
|
|
555
|
+
async function produceReport(ctx, options) {
|
|
556
|
+
const toolVersion = await getToolVersion();
|
|
557
|
+
const depth0 = !options.fullTree;
|
|
558
|
+
const cwd = options.cwd || process.cwd();
|
|
559
|
+
const tree = await npmLs({
|
|
560
|
+
global: ctx === "global",
|
|
561
|
+
omitDev: ctx === "local" ? Boolean(options.omitDev) : false,
|
|
562
|
+
depth0,
|
|
563
|
+
cwd
|
|
564
|
+
});
|
|
565
|
+
let project_description;
|
|
566
|
+
let project_homepage;
|
|
567
|
+
let project_bugs;
|
|
568
|
+
if (ctx === "local") {
|
|
569
|
+
try {
|
|
570
|
+
const pkgRaw = await readFile4(path5.join(cwd, "package.json"), "utf8");
|
|
571
|
+
const pkg = JSON.parse(pkgRaw);
|
|
572
|
+
project_description = pkg.description;
|
|
573
|
+
project_homepage = pkg.homepage;
|
|
574
|
+
if (typeof pkg.bugs === "string") project_bugs = pkg.bugs;
|
|
575
|
+
else if (pkg.bugs && typeof pkg.bugs.url === "string") project_bugs = pkg.bugs.url;
|
|
576
|
+
} catch {
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
const globalRoot = ctx === "global" ? await npmRootGlobal().catch(() => void 0) : void 0;
|
|
580
|
+
const report = await buildReportFromNpmTree(tree, {
|
|
581
|
+
context: ctx,
|
|
582
|
+
includeTree: Boolean(options.fullTree),
|
|
583
|
+
omitDev: Boolean(options.omitDev),
|
|
584
|
+
cwd,
|
|
585
|
+
toolVersion,
|
|
586
|
+
globalRoot
|
|
587
|
+
});
|
|
588
|
+
const markdownExtras = { project_description, project_homepage, project_bugs };
|
|
589
|
+
return { report, markdownExtras };
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
// src/runtimes/node/commands.ts
|
|
593
|
+
function addCommonOptions(cmd, { allowOmitDev }) {
|
|
594
|
+
cmd.option(
|
|
595
|
+
"-f, --output-format <format>",
|
|
596
|
+
"Output format: md or json",
|
|
597
|
+
(val) => val === "md" ? "md" : "json",
|
|
598
|
+
"json"
|
|
599
|
+
).option("-o, --out-file <path>", "Write report to file").option("--full-tree", "Include full npm ls tree (omit depth=0 default)", false).option("-c, --check-outdated", "List outdated packages instead of printing the report", false).option(
|
|
600
|
+
"-u, --update-outdated [packages...]",
|
|
601
|
+
"Update outdated packages (omit package names to update every package)"
|
|
602
|
+
);
|
|
603
|
+
if (allowOmitDev) {
|
|
604
|
+
cmd.option("--omit-dev", "Exclude devDependencies (local only)", false);
|
|
605
|
+
}
|
|
606
|
+
return cmd;
|
|
607
|
+
}
|
|
608
|
+
function createLocalCommand(program) {
|
|
609
|
+
const localCmd = program.command("local", { isDefault: true }).description("Generate a report for the current project's dependencies");
|
|
610
|
+
addCommonOptions(localCmd, { allowOmitDev: true });
|
|
611
|
+
localCmd.action(async (opts) => {
|
|
612
|
+
const outputFormat = opts.outputFormat ?? "json";
|
|
613
|
+
const outFile = opts.outFile;
|
|
614
|
+
const fullTree = Boolean(opts.fullTree);
|
|
615
|
+
const omitDev = Boolean(opts.omitDev);
|
|
616
|
+
const cwd = process.cwd();
|
|
617
|
+
const selection = normalizeUpdateSelection(opts.updateOutdated);
|
|
618
|
+
const proceed = await handleOutdatedWorkflow({
|
|
619
|
+
checkOutdated: Boolean(opts.checkOutdated),
|
|
620
|
+
selection,
|
|
621
|
+
contextLabel: "local",
|
|
622
|
+
outFile,
|
|
623
|
+
fetchOutdated: () => npmOutdated({ cwd }),
|
|
624
|
+
updateRunner: selection.shouldUpdate ? async (packages) => {
|
|
625
|
+
await npmUpdate({ cwd, packages });
|
|
626
|
+
} : void 0
|
|
627
|
+
});
|
|
628
|
+
if (!proceed) return;
|
|
629
|
+
const finalOutFile = outFile;
|
|
630
|
+
const { report, markdownExtras } = await produceReport("local", {
|
|
631
|
+
outputFormat,
|
|
632
|
+
outFile: finalOutFile,
|
|
633
|
+
fullTree,
|
|
634
|
+
omitDev
|
|
635
|
+
});
|
|
636
|
+
await outputReport(report, outputFormat, finalOutFile, markdownExtras);
|
|
637
|
+
});
|
|
638
|
+
return localCmd;
|
|
639
|
+
}
|
|
640
|
+
function createGlobalCommand(program) {
|
|
641
|
+
const globalCmd = program.command("global").description("Generate a report of globally installed packages");
|
|
642
|
+
addCommonOptions(globalCmd, { allowOmitDev: false });
|
|
643
|
+
globalCmd.action(async (opts) => {
|
|
644
|
+
const outputFormat = opts.outputFormat ?? "json";
|
|
645
|
+
const outFile = opts.outFile;
|
|
646
|
+
const fullTree = Boolean(opts.fullTree);
|
|
647
|
+
const cwd = process.cwd();
|
|
648
|
+
const selection = normalizeUpdateSelection(opts.updateOutdated);
|
|
649
|
+
const proceed = await handleOutdatedWorkflow({
|
|
650
|
+
checkOutdated: Boolean(opts.checkOutdated),
|
|
651
|
+
selection,
|
|
652
|
+
contextLabel: "global",
|
|
653
|
+
outFile,
|
|
654
|
+
fetchOutdated: () => npmOutdated({ cwd, global: true }),
|
|
655
|
+
updateRunner: selection.shouldUpdate ? async (packages) => {
|
|
656
|
+
await npmUpdate({ cwd, global: true, packages });
|
|
657
|
+
} : void 0
|
|
658
|
+
});
|
|
659
|
+
if (!proceed) return;
|
|
660
|
+
const finalOutFile = outFile;
|
|
661
|
+
const { report, markdownExtras } = await produceReport("global", {
|
|
662
|
+
outputFormat,
|
|
663
|
+
outFile: finalOutFile,
|
|
664
|
+
fullTree
|
|
665
|
+
});
|
|
666
|
+
await outputReport(report, outputFormat, finalOutFile, markdownExtras);
|
|
667
|
+
});
|
|
668
|
+
return globalCmd;
|
|
669
|
+
}
|
|
670
|
+
function createReadCommand(program) {
|
|
671
|
+
const readCmd = program.command("read").description(
|
|
672
|
+
"Read a previously generated report (JSON or Markdown) and either print package names or install them"
|
|
673
|
+
).argument("[report]", "Path to report file (JSON or Markdown)", "gex-report.json").option("-r, --report <path>", "Path to report file (JSON or Markdown)").option("-p, --print", "Print package names/versions from the report (default)", false).option("-i, --install", "Install packages from the report", false);
|
|
674
|
+
readCmd.action(async (reportArg, opts) => {
|
|
675
|
+
const chosen = opts.report || reportArg || "gex-report.json";
|
|
676
|
+
const reportPath = path6.resolve(process.cwd(), chosen);
|
|
677
|
+
try {
|
|
678
|
+
const parsed = await loadReportFromFile(reportPath);
|
|
679
|
+
const doInstall = Boolean(opts.install);
|
|
680
|
+
const doPrint = Boolean(opts.print) || !doInstall;
|
|
681
|
+
if (doPrint) {
|
|
682
|
+
printFromReport(parsed);
|
|
683
|
+
}
|
|
684
|
+
if (doInstall) {
|
|
685
|
+
await installFromReport(parsed, { cwd: process.cwd(), packageManager: "npm" });
|
|
686
|
+
}
|
|
687
|
+
} catch (err) {
|
|
688
|
+
const isMd = isMarkdownReportFile(reportPath);
|
|
689
|
+
const hint = isMd ? "Try generating a JSON report with: gex global -f json -o global.json, then: gex read global.json" : "Specify a report path with: gex read <path-to-report.json>";
|
|
690
|
+
console.error(`Failed to read report at ${reportPath}: ${err?.message || err}`);
|
|
691
|
+
console.error(hint);
|
|
692
|
+
process.exitCode = 1;
|
|
693
|
+
}
|
|
694
|
+
});
|
|
695
|
+
return readCmd;
|
|
696
|
+
}
|
|
697
|
+
async function createProgram() {
|
|
698
|
+
const program = new Command().name("gex").description("GEX: Dependency auditing and documentation for Node.js (local and global).").version(await getToolVersion());
|
|
699
|
+
program.addHelpText("beforeAll", `
|
|
700
|
+
${ASCII_BANNER}`);
|
|
701
|
+
createLocalCommand(program);
|
|
702
|
+
createGlobalCommand(program);
|
|
703
|
+
createReadCommand(program);
|
|
704
|
+
return program;
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
// src/runtimes/node/cli.ts
|
|
708
|
+
async function run(argv = process.argv) {
|
|
709
|
+
const program = await createProgram();
|
|
710
|
+
await program.parseAsync(argv);
|
|
711
|
+
}
|
|
712
|
+
var isMainModule = (() => {
|
|
713
|
+
try {
|
|
714
|
+
if (typeof __require !== "undefined" && typeof module !== "undefined") {
|
|
715
|
+
return __require.main === module;
|
|
716
|
+
}
|
|
717
|
+
if (typeof import.meta !== "undefined") {
|
|
718
|
+
return import.meta.url === `file://${process.argv[1]}`;
|
|
719
|
+
}
|
|
720
|
+
return false;
|
|
721
|
+
} catch {
|
|
722
|
+
return false;
|
|
723
|
+
}
|
|
724
|
+
})();
|
|
725
|
+
if (isMainModule) {
|
|
726
|
+
run().catch((error) => {
|
|
727
|
+
console.error("CLI error:", error);
|
|
728
|
+
process.exitCode = 1;
|
|
729
|
+
});
|
|
730
|
+
}
|
|
731
|
+
export {
|
|
732
|
+
run
|
|
733
|
+
};
|
|
734
|
+
//# sourceMappingURL=cli-node.mjs.map
|