@yabasha/gex 1.0.0 → 1.1.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/dist/cli-bun.mjs +660 -0
- package/dist/cli-bun.mjs.map +1 -0
- package/dist/cli-node.cjs +572 -0
- package/dist/cli-node.cjs.map +1 -0
- package/dist/cli-node.mjs +540 -0
- package/dist/cli-node.mjs.map +1 -0
- package/dist/cli.cjs +170 -101
- package/dist/cli.cjs.map +1 -1
- package/dist/cli.mjs +168 -100
- package/dist/cli.mjs.map +1 -1
- package/dist/index.cjs +18 -9
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.mjs +18 -9
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -2
|
@@ -0,0 +1,572 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
var __create = Object.create;
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
8
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
+
var __export = (target, all) => {
|
|
10
|
+
for (var name in all)
|
|
11
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
12
|
+
};
|
|
13
|
+
var __copyProps = (to, from, except, desc) => {
|
|
14
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
15
|
+
for (let key of __getOwnPropNames(from))
|
|
16
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
17
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
18
|
+
}
|
|
19
|
+
return to;
|
|
20
|
+
};
|
|
21
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
22
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
23
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
24
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
25
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
26
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
27
|
+
mod
|
|
28
|
+
));
|
|
29
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
30
|
+
|
|
31
|
+
// src/runtimes/node/cli.ts
|
|
32
|
+
var cli_exports = {};
|
|
33
|
+
__export(cli_exports, {
|
|
34
|
+
run: () => run
|
|
35
|
+
});
|
|
36
|
+
module.exports = __toCommonJS(cli_exports);
|
|
37
|
+
|
|
38
|
+
// src/runtimes/node/commands.ts
|
|
39
|
+
var import_node_path6 = __toESM(require("path"), 1);
|
|
40
|
+
var import_commander = require("commander");
|
|
41
|
+
|
|
42
|
+
// src/shared/cli/install.ts
|
|
43
|
+
var INSTALL_COMMANDS = {
|
|
44
|
+
npm: {
|
|
45
|
+
global: ["i", "-g"],
|
|
46
|
+
local: ["i"],
|
|
47
|
+
dev: ["i", "-D"]
|
|
48
|
+
},
|
|
49
|
+
bun: {
|
|
50
|
+
global: ["add", "-g"],
|
|
51
|
+
local: ["add"],
|
|
52
|
+
dev: ["add", "-d"]
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
var MAX_BUFFER = 10 * 1024 * 1024;
|
|
56
|
+
function formatSpec(pkg) {
|
|
57
|
+
return pkg.version ? `${pkg.name}@${pkg.version}` : pkg.name;
|
|
58
|
+
}
|
|
59
|
+
async function getExecFileAsync() {
|
|
60
|
+
const { execFile } = await import("child_process");
|
|
61
|
+
const { promisify } = await import("util");
|
|
62
|
+
return promisify(execFile);
|
|
63
|
+
}
|
|
64
|
+
async function installFromReport(report, options) {
|
|
65
|
+
const opts = typeof options === "string" ? { cwd: options } : options;
|
|
66
|
+
const { cwd, packageManager = "npm" } = opts;
|
|
67
|
+
const globalPkgs = report.global_packages.map(formatSpec).filter(Boolean);
|
|
68
|
+
const localPkgs = report.local_dependencies.map(formatSpec).filter(Boolean);
|
|
69
|
+
const devPkgs = report.local_dev_dependencies.map(formatSpec).filter(Boolean);
|
|
70
|
+
if (globalPkgs.length === 0 && localPkgs.length === 0 && devPkgs.length === 0) {
|
|
71
|
+
console.log("No packages to install from report.");
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
const execFileAsync = await getExecFileAsync();
|
|
75
|
+
const cmd = INSTALL_COMMANDS[packageManager];
|
|
76
|
+
const binary = packageManager === "bun" ? "bun" : "npm";
|
|
77
|
+
if (globalPkgs.length > 0) {
|
|
78
|
+
console.log(`Installing global: ${globalPkgs.join(" ")}`);
|
|
79
|
+
await execFileAsync(binary, [...cmd.global, ...globalPkgs], { cwd, maxBuffer: MAX_BUFFER });
|
|
80
|
+
}
|
|
81
|
+
if (localPkgs.length > 0) {
|
|
82
|
+
console.log(`Installing local deps: ${localPkgs.join(" ")}`);
|
|
83
|
+
await execFileAsync(binary, [...cmd.local, ...localPkgs], { cwd, maxBuffer: MAX_BUFFER });
|
|
84
|
+
}
|
|
85
|
+
if (devPkgs.length > 0) {
|
|
86
|
+
console.log(`Installing local devDeps: ${devPkgs.join(" ")}`);
|
|
87
|
+
await execFileAsync(binary, [...cmd.dev, ...devPkgs], { cwd, maxBuffer: MAX_BUFFER });
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
function printFromReport(report) {
|
|
91
|
+
const lines = [];
|
|
92
|
+
if (report.global_packages.length > 0) {
|
|
93
|
+
lines.push("Global Packages:");
|
|
94
|
+
for (const p of report.global_packages) {
|
|
95
|
+
lines.push(`- ${p.name}@${p.version}`);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
if (report.local_dependencies.length > 0) {
|
|
99
|
+
if (lines.length) lines.push("");
|
|
100
|
+
lines.push("Local Dependencies:");
|
|
101
|
+
for (const p of report.local_dependencies) {
|
|
102
|
+
lines.push(`- ${p.name}@${p.version}`);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
if (report.local_dev_dependencies.length > 0) {
|
|
106
|
+
if (lines.length) lines.push("");
|
|
107
|
+
lines.push("Local Dev Dependencies:");
|
|
108
|
+
for (const p of report.local_dev_dependencies) {
|
|
109
|
+
lines.push(`- ${p.name}@${p.version}`);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
if (lines.length === 0) {
|
|
113
|
+
lines.push("(no packages found in report)");
|
|
114
|
+
}
|
|
115
|
+
console.log(lines.join("\n"));
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// src/shared/cli/output.ts
|
|
119
|
+
var import_node_path = __toESM(require("path"), 1);
|
|
120
|
+
|
|
121
|
+
// src/shared/report/json.ts
|
|
122
|
+
function renderJson(report) {
|
|
123
|
+
const r = {
|
|
124
|
+
...report,
|
|
125
|
+
global_packages: [...report.global_packages].sort((a, b) => a.name.localeCompare(b.name)),
|
|
126
|
+
local_dependencies: [...report.local_dependencies].sort((a, b) => a.name.localeCompare(b.name)),
|
|
127
|
+
local_dev_dependencies: [...report.local_dev_dependencies].sort(
|
|
128
|
+
(a, b) => a.name.localeCompare(b.name)
|
|
129
|
+
)
|
|
130
|
+
};
|
|
131
|
+
return JSON.stringify(r, null, 2);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// src/shared/report/md.ts
|
|
135
|
+
function table(headers, rows) {
|
|
136
|
+
const header = `| ${headers.join(" | ")} |`;
|
|
137
|
+
const sep = `| ${headers.map(() => "---").join(" | ")} |`;
|
|
138
|
+
const body = rows.map((r) => `| ${r.join(" | ")} |`).join("\n");
|
|
139
|
+
return [header, sep, body].filter(Boolean).join("\n");
|
|
140
|
+
}
|
|
141
|
+
function renderMarkdown(report) {
|
|
142
|
+
const lines = [];
|
|
143
|
+
lines.push("# GEX Report");
|
|
144
|
+
lines.push("");
|
|
145
|
+
if (report.project_name || report.project_version || report.project_description || report.project_homepage || report.project_bugs) {
|
|
146
|
+
lines.push("## Project Metadata");
|
|
147
|
+
if (report.project_name) lines.push(`- Name: ${report.project_name}`);
|
|
148
|
+
if (report.project_version) lines.push(`- Version: ${report.project_version}`);
|
|
149
|
+
if (report.project_description)
|
|
150
|
+
lines.push(`- Description: ${report.project_description}`);
|
|
151
|
+
if (report.project_homepage)
|
|
152
|
+
lines.push(`- Homepage: ${report.project_homepage}`);
|
|
153
|
+
if (report.project_bugs) lines.push(`- Bugs: ${report.project_bugs}`);
|
|
154
|
+
lines.push("");
|
|
155
|
+
}
|
|
156
|
+
if (report.global_packages.length > 0) {
|
|
157
|
+
lines.push("## Global Packages");
|
|
158
|
+
const rows = report.global_packages.map((p) => [p.name, p.version || "", p.resolved_path || ""]);
|
|
159
|
+
lines.push(table(["Name", "Version", "Path"], rows));
|
|
160
|
+
lines.push("");
|
|
161
|
+
}
|
|
162
|
+
if (report.local_dependencies.length > 0) {
|
|
163
|
+
lines.push("## Local Dependencies");
|
|
164
|
+
const rows = report.local_dependencies.map((p) => [
|
|
165
|
+
p.name,
|
|
166
|
+
p.version || "",
|
|
167
|
+
p.resolved_path || ""
|
|
168
|
+
]);
|
|
169
|
+
lines.push(table(["Name", "Version", "Path"], rows));
|
|
170
|
+
lines.push("");
|
|
171
|
+
}
|
|
172
|
+
if (report.local_dev_dependencies.length > 0) {
|
|
173
|
+
lines.push("## Local Dev Dependencies");
|
|
174
|
+
const rows = report.local_dev_dependencies.map((p) => [
|
|
175
|
+
p.name,
|
|
176
|
+
p.version || "",
|
|
177
|
+
p.resolved_path || ""
|
|
178
|
+
]);
|
|
179
|
+
lines.push(table(["Name", "Version", "Path"], rows));
|
|
180
|
+
lines.push("");
|
|
181
|
+
}
|
|
182
|
+
lines.push("---");
|
|
183
|
+
lines.push("_Generated by GEX_");
|
|
184
|
+
return lines.join("\n");
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// src/shared/cli/output.ts
|
|
188
|
+
async function outputReport(report, format, outFile, markdownExtras) {
|
|
189
|
+
const content = format === "json" ? renderJson(report) : renderMarkdown({ ...report, ...markdownExtras || {} });
|
|
190
|
+
if (outFile) {
|
|
191
|
+
const outDir = import_node_path.default.dirname(outFile);
|
|
192
|
+
const { mkdir, writeFile } = await import("fs/promises");
|
|
193
|
+
await mkdir(outDir, { recursive: true });
|
|
194
|
+
await writeFile(outFile, content, "utf8");
|
|
195
|
+
console.log(`Wrote report to ${outFile}`);
|
|
196
|
+
} else {
|
|
197
|
+
console.log(content);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// src/shared/cli/parser.ts
|
|
202
|
+
var import_promises = require("fs/promises");
|
|
203
|
+
var import_node_path2 = __toESM(require("path"), 1);
|
|
204
|
+
function isMarkdownReportFile(filePath) {
|
|
205
|
+
const ext = import_node_path2.default.extname(filePath).toLowerCase();
|
|
206
|
+
return ext === ".md" || ext === ".markdown";
|
|
207
|
+
}
|
|
208
|
+
function parseMarkdownPackagesTable(lines, startIndex) {
|
|
209
|
+
const rows = [];
|
|
210
|
+
if (!lines[startIndex] || !lines[startIndex].trim().startsWith("|")) return rows;
|
|
211
|
+
let i = startIndex + 2;
|
|
212
|
+
while (i < lines.length && lines[i].trim().startsWith("|")) {
|
|
213
|
+
const cols = lines[i].split("|").map((c) => c.trim()).filter((_, idx, arr) => !(idx === 0 || idx === arr.length - 1));
|
|
214
|
+
const [name = "", version = "", resolved_path = ""] = cols;
|
|
215
|
+
if (name) rows.push({ name, version, resolved_path });
|
|
216
|
+
i++;
|
|
217
|
+
}
|
|
218
|
+
return rows;
|
|
219
|
+
}
|
|
220
|
+
function parseMarkdownReport(md) {
|
|
221
|
+
const lines = md.split(/\r?\n/);
|
|
222
|
+
const findSection = (title) => lines.findIndex((l) => l.trim().toLowerCase() === `## ${title}`.toLowerCase());
|
|
223
|
+
const parseSection = (idx) => {
|
|
224
|
+
if (idx < 0) return [];
|
|
225
|
+
let i = idx + 1;
|
|
226
|
+
while (i < lines.length && !lines[i].trim().startsWith("|")) i++;
|
|
227
|
+
return parseMarkdownPackagesTable(lines, i);
|
|
228
|
+
};
|
|
229
|
+
const global_packages = parseSection(findSection("Global Packages"));
|
|
230
|
+
const local_dependencies = parseSection(findSection("Local Dependencies"));
|
|
231
|
+
const local_dev_dependencies = parseSection(findSection("Local Dev Dependencies"));
|
|
232
|
+
const report = {
|
|
233
|
+
report_version: "1.0",
|
|
234
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
235
|
+
tool_version: "unknown",
|
|
236
|
+
global_packages,
|
|
237
|
+
local_dependencies,
|
|
238
|
+
local_dev_dependencies
|
|
239
|
+
};
|
|
240
|
+
return report;
|
|
241
|
+
}
|
|
242
|
+
async function loadReportFromFile(reportPath) {
|
|
243
|
+
const raw = await (0, import_promises.readFile)(reportPath, "utf8");
|
|
244
|
+
if (isMarkdownReportFile(reportPath) || raw.startsWith("# GEX Report")) {
|
|
245
|
+
return parseMarkdownReport(raw);
|
|
246
|
+
}
|
|
247
|
+
return JSON.parse(raw);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
// src/shared/cli/utils.ts
|
|
251
|
+
var import_node_fs = require("fs");
|
|
252
|
+
var import_promises2 = require("fs/promises");
|
|
253
|
+
var import_node_path3 = __toESM(require("path"), 1);
|
|
254
|
+
var import_node_url = require("url");
|
|
255
|
+
var import_meta = {};
|
|
256
|
+
function getPkgJsonPath() {
|
|
257
|
+
let startDir;
|
|
258
|
+
try {
|
|
259
|
+
const __filename = (0, import_node_url.fileURLToPath)(import_meta.url);
|
|
260
|
+
startDir = import_node_path3.default.dirname(__filename);
|
|
261
|
+
} catch {
|
|
262
|
+
startDir = typeof __dirname !== "undefined" ? __dirname : process.cwd();
|
|
263
|
+
}
|
|
264
|
+
return findPackageJson(startDir);
|
|
265
|
+
}
|
|
266
|
+
function findPackageJson(startDir) {
|
|
267
|
+
let current = startDir;
|
|
268
|
+
const maxDepth = 6;
|
|
269
|
+
for (let i = 0; i < maxDepth; i++) {
|
|
270
|
+
const candidate = import_node_path3.default.resolve(current, "package.json");
|
|
271
|
+
if ((0, import_node_fs.existsSync)(candidate)) {
|
|
272
|
+
return candidate;
|
|
273
|
+
}
|
|
274
|
+
const parent = import_node_path3.default.dirname(current);
|
|
275
|
+
if (parent === current) break;
|
|
276
|
+
current = parent;
|
|
277
|
+
}
|
|
278
|
+
return import_node_path3.default.resolve(process.cwd(), "package.json");
|
|
279
|
+
}
|
|
280
|
+
async function getToolVersion() {
|
|
281
|
+
try {
|
|
282
|
+
const pkgPath = getPkgJsonPath();
|
|
283
|
+
const raw = await (0, import_promises2.readFile)(pkgPath, "utf8");
|
|
284
|
+
const pkg = JSON.parse(raw);
|
|
285
|
+
return pkg.version || "0.0.0";
|
|
286
|
+
} catch {
|
|
287
|
+
return "0.0.0";
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
var ASCII_BANNER = String.raw`
|
|
291
|
+
________ __
|
|
292
|
+
/ _____/ ____ _____/ |_ ____ ____
|
|
293
|
+
/ \ ___ / _ \ / _ \ __\/ __ \ / \
|
|
294
|
+
\ \_\ ( <_> | <_> ) | \ ___/| | \
|
|
295
|
+
\______ /\____/ \____/|__| \___ >___| /
|
|
296
|
+
\/ \/ \/
|
|
297
|
+
GEX
|
|
298
|
+
`;
|
|
299
|
+
|
|
300
|
+
// src/runtimes/node/report.ts
|
|
301
|
+
var import_promises4 = require("fs/promises");
|
|
302
|
+
var import_node_path5 = __toESM(require("path"), 1);
|
|
303
|
+
|
|
304
|
+
// src/shared/transform.ts
|
|
305
|
+
var import_node_path4 = __toESM(require("path"), 1);
|
|
306
|
+
var import_promises3 = require("fs/promises");
|
|
307
|
+
function toPkgArray(obj) {
|
|
308
|
+
if (!obj) return [];
|
|
309
|
+
return Object.keys(obj).map((name) => ({ name, node: obj[name] })).filter((p) => p && p.node);
|
|
310
|
+
}
|
|
311
|
+
async function buildReportFromNpmTree(tree, opts) {
|
|
312
|
+
const timestamp = (/* @__PURE__ */ new Date()).toISOString();
|
|
313
|
+
const report = {
|
|
314
|
+
report_version: "1.0",
|
|
315
|
+
timestamp,
|
|
316
|
+
tool_version: opts.toolVersion,
|
|
317
|
+
global_packages: [],
|
|
318
|
+
local_dependencies: [],
|
|
319
|
+
local_dev_dependencies: []
|
|
320
|
+
};
|
|
321
|
+
if (opts.context === "local") {
|
|
322
|
+
let pkgMeta = null;
|
|
323
|
+
try {
|
|
324
|
+
const pkgJsonPath = import_node_path4.default.join(opts.cwd || process.cwd(), "package.json");
|
|
325
|
+
const raw = await (0, import_promises3.readFile)(pkgJsonPath, "utf8");
|
|
326
|
+
pkgMeta = JSON.parse(raw);
|
|
327
|
+
} catch {
|
|
328
|
+
}
|
|
329
|
+
if (pkgMeta?.name) report.project_name = pkgMeta.name;
|
|
330
|
+
if (pkgMeta?.version) report.project_version = pkgMeta.version;
|
|
331
|
+
const depsObj = tree?.dependencies;
|
|
332
|
+
const devDepsObj = tree?.devDependencies;
|
|
333
|
+
const prodItems = toPkgArray(depsObj);
|
|
334
|
+
const treeDevItems = toPkgArray(devDepsObj);
|
|
335
|
+
if (treeDevItems.length > 0) {
|
|
336
|
+
for (const { name, node } of treeDevItems) {
|
|
337
|
+
const version = node && node.version || "";
|
|
338
|
+
const resolvedPath = node && node.path || import_node_path4.default.join(opts.cwd || process.cwd(), "node_modules", name);
|
|
339
|
+
report.local_dev_dependencies.push({ name, version, resolved_path: resolvedPath });
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
const devKeys = treeDevItems.length > 0 ? new Set(treeDevItems.map((entry) => entry.name)) : new Set(Object.keys(pkgMeta?.devDependencies || {}));
|
|
343
|
+
for (const { name, node } of prodItems) {
|
|
344
|
+
const version = node && node.version || "";
|
|
345
|
+
const resolvedPath = node && node.path || import_node_path4.default.join(opts.cwd || process.cwd(), "node_modules", name);
|
|
346
|
+
const pkg = { name, version, resolved_path: resolvedPath };
|
|
347
|
+
if (!treeDevItems.length && devKeys.has(name)) {
|
|
348
|
+
report.local_dev_dependencies.push(pkg);
|
|
349
|
+
} else {
|
|
350
|
+
report.local_dependencies.push(pkg);
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
report.local_dependencies.sort((a, b) => a.name.localeCompare(b.name));
|
|
354
|
+
report.local_dev_dependencies.sort((a, b) => a.name.localeCompare(b.name));
|
|
355
|
+
} else if (opts.context === "global") {
|
|
356
|
+
const depsObj = tree?.dependencies;
|
|
357
|
+
const items = toPkgArray(depsObj);
|
|
358
|
+
for (const { name, node } of items) {
|
|
359
|
+
const version = node && node.version || "";
|
|
360
|
+
const resolvedPath = node && node.path || import_node_path4.default.join(opts.globalRoot || "", name);
|
|
361
|
+
const pkg = { name, version, resolved_path: resolvedPath };
|
|
362
|
+
report.global_packages.push(pkg);
|
|
363
|
+
}
|
|
364
|
+
report.global_packages.sort((a, b) => a.name.localeCompare(b.name));
|
|
365
|
+
}
|
|
366
|
+
if (opts.includeTree) {
|
|
367
|
+
report.tree = tree;
|
|
368
|
+
}
|
|
369
|
+
return report;
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
// src/runtimes/node/package-manager.ts
|
|
373
|
+
async function getExecFileAsync2() {
|
|
374
|
+
const { execFile } = await import("child_process");
|
|
375
|
+
const { promisify } = await import("util");
|
|
376
|
+
return promisify(execFile);
|
|
377
|
+
}
|
|
378
|
+
async function npmLs(options = {}) {
|
|
379
|
+
const args = ["ls", "--json"];
|
|
380
|
+
if (options.global) args.push("--global");
|
|
381
|
+
if (options.omitDev) args.push("--omit=dev");
|
|
382
|
+
if (options.depth0) args.push("--depth=0");
|
|
383
|
+
try {
|
|
384
|
+
const execFileAsync = await getExecFileAsync2();
|
|
385
|
+
const { stdout } = await execFileAsync("npm", args, {
|
|
386
|
+
cwd: options.cwd,
|
|
387
|
+
maxBuffer: 10 * 1024 * 1024
|
|
388
|
+
});
|
|
389
|
+
if (stdout && stdout.trim()) return JSON.parse(stdout);
|
|
390
|
+
return {};
|
|
391
|
+
} catch (err) {
|
|
392
|
+
const stdout = err?.stdout;
|
|
393
|
+
if (typeof stdout === "string" && stdout.trim()) {
|
|
394
|
+
try {
|
|
395
|
+
return JSON.parse(stdout);
|
|
396
|
+
} catch (parseErr) {
|
|
397
|
+
if (process.env.DEBUG?.includes("gex")) {
|
|
398
|
+
console.warn("npm ls stdout parse failed:", parseErr);
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
const stderr = err?.stderr;
|
|
403
|
+
const msg = typeof stderr === "string" && stderr.trim() || err?.message || "npm ls failed";
|
|
404
|
+
throw new Error(`npm ls failed: ${msg}`);
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
async function npmRootGlobal() {
|
|
408
|
+
try {
|
|
409
|
+
const execFileAsync = await getExecFileAsync2();
|
|
410
|
+
const { stdout } = await execFileAsync("npm", ["root", "-g"]);
|
|
411
|
+
return stdout.trim();
|
|
412
|
+
} catch (err) {
|
|
413
|
+
const stderr = err?.stderr;
|
|
414
|
+
const msg = typeof stderr === "string" && stderr.trim() || err?.message || "npm root -g failed";
|
|
415
|
+
throw new Error(`npm root -g failed: ${msg}`);
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
// src/runtimes/node/report.ts
|
|
420
|
+
async function produceReport(ctx, options) {
|
|
421
|
+
const toolVersion = await getToolVersion();
|
|
422
|
+
const depth0 = !options.fullTree;
|
|
423
|
+
const cwd = options.cwd || process.cwd();
|
|
424
|
+
const tree = await npmLs({
|
|
425
|
+
global: ctx === "global",
|
|
426
|
+
omitDev: ctx === "local" ? Boolean(options.omitDev) : false,
|
|
427
|
+
depth0,
|
|
428
|
+
cwd
|
|
429
|
+
});
|
|
430
|
+
let project_description;
|
|
431
|
+
let project_homepage;
|
|
432
|
+
let project_bugs;
|
|
433
|
+
if (ctx === "local") {
|
|
434
|
+
try {
|
|
435
|
+
const pkgRaw = await (0, import_promises4.readFile)(import_node_path5.default.join(cwd, "package.json"), "utf8");
|
|
436
|
+
const pkg = JSON.parse(pkgRaw);
|
|
437
|
+
project_description = pkg.description;
|
|
438
|
+
project_homepage = pkg.homepage;
|
|
439
|
+
if (typeof pkg.bugs === "string") project_bugs = pkg.bugs;
|
|
440
|
+
else if (pkg.bugs && typeof pkg.bugs.url === "string") project_bugs = pkg.bugs.url;
|
|
441
|
+
} catch {
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
const globalRoot = ctx === "global" ? await npmRootGlobal().catch(() => void 0) : void 0;
|
|
445
|
+
const report = await buildReportFromNpmTree(tree, {
|
|
446
|
+
context: ctx,
|
|
447
|
+
includeTree: Boolean(options.fullTree),
|
|
448
|
+
omitDev: Boolean(options.omitDev),
|
|
449
|
+
cwd,
|
|
450
|
+
toolVersion,
|
|
451
|
+
globalRoot
|
|
452
|
+
});
|
|
453
|
+
const markdownExtras = { project_description, project_homepage, project_bugs };
|
|
454
|
+
return { report, markdownExtras };
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
// src/runtimes/node/commands.ts
|
|
458
|
+
function addCommonOptions(cmd, { allowOmitDev }) {
|
|
459
|
+
cmd.option(
|
|
460
|
+
"-f, --output-format <format>",
|
|
461
|
+
"Output format: md or json",
|
|
462
|
+
(val) => val === "md" ? "md" : "json",
|
|
463
|
+
"json"
|
|
464
|
+
).option("-o, --out-file <path>", "Write report to file").option("--full-tree", "Include full npm ls tree (omit depth=0 default)", false);
|
|
465
|
+
if (allowOmitDev) {
|
|
466
|
+
cmd.option("--omit-dev", "Exclude devDependencies (local only)", false);
|
|
467
|
+
}
|
|
468
|
+
return cmd;
|
|
469
|
+
}
|
|
470
|
+
function createLocalCommand(program) {
|
|
471
|
+
const localCmd = program.command("local", { isDefault: true }).description("Generate a report for the current project's dependencies");
|
|
472
|
+
addCommonOptions(localCmd, { allowOmitDev: true });
|
|
473
|
+
localCmd.action(async (opts) => {
|
|
474
|
+
const outputFormat = opts.outputFormat ?? "json";
|
|
475
|
+
const outFile = opts.outFile;
|
|
476
|
+
const fullTree = Boolean(opts.fullTree);
|
|
477
|
+
const omitDev = Boolean(opts.omitDev);
|
|
478
|
+
const finalOutFile = outFile;
|
|
479
|
+
const { report, markdownExtras } = await produceReport("local", {
|
|
480
|
+
outputFormat,
|
|
481
|
+
outFile: finalOutFile,
|
|
482
|
+
fullTree,
|
|
483
|
+
omitDev
|
|
484
|
+
});
|
|
485
|
+
await outputReport(report, outputFormat, finalOutFile, markdownExtras);
|
|
486
|
+
});
|
|
487
|
+
return localCmd;
|
|
488
|
+
}
|
|
489
|
+
function createGlobalCommand(program) {
|
|
490
|
+
const globalCmd = program.command("global").description("Generate a report of globally installed packages");
|
|
491
|
+
addCommonOptions(globalCmd, { allowOmitDev: false });
|
|
492
|
+
globalCmd.action(async (opts) => {
|
|
493
|
+
const outputFormat = opts.outputFormat ?? "json";
|
|
494
|
+
const outFile = opts.outFile;
|
|
495
|
+
const fullTree = Boolean(opts.fullTree);
|
|
496
|
+
const finalOutFile = outFile;
|
|
497
|
+
const { report, markdownExtras } = await produceReport("global", {
|
|
498
|
+
outputFormat,
|
|
499
|
+
outFile: finalOutFile,
|
|
500
|
+
fullTree
|
|
501
|
+
});
|
|
502
|
+
await outputReport(report, outputFormat, finalOutFile, markdownExtras);
|
|
503
|
+
});
|
|
504
|
+
return globalCmd;
|
|
505
|
+
}
|
|
506
|
+
function createReadCommand(program) {
|
|
507
|
+
const readCmd = program.command("read").description(
|
|
508
|
+
"Read a previously generated report (JSON or Markdown) and either print package names or install them"
|
|
509
|
+
).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);
|
|
510
|
+
readCmd.action(async (reportArg, opts) => {
|
|
511
|
+
const chosen = opts.report || reportArg || "gex-report.json";
|
|
512
|
+
const reportPath = import_node_path6.default.resolve(process.cwd(), chosen);
|
|
513
|
+
try {
|
|
514
|
+
const parsed = await loadReportFromFile(reportPath);
|
|
515
|
+
const doInstall = Boolean(opts.install);
|
|
516
|
+
const doPrint = Boolean(opts.print) || !doInstall;
|
|
517
|
+
if (doPrint) {
|
|
518
|
+
printFromReport(parsed);
|
|
519
|
+
}
|
|
520
|
+
if (doInstall) {
|
|
521
|
+
await installFromReport(parsed, { cwd: process.cwd(), packageManager: "npm" });
|
|
522
|
+
}
|
|
523
|
+
} catch (err) {
|
|
524
|
+
const isMd = isMarkdownReportFile(reportPath);
|
|
525
|
+
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>";
|
|
526
|
+
console.error(`Failed to read report at ${reportPath}: ${err?.message || err}`);
|
|
527
|
+
console.error(hint);
|
|
528
|
+
process.exitCode = 1;
|
|
529
|
+
}
|
|
530
|
+
});
|
|
531
|
+
return readCmd;
|
|
532
|
+
}
|
|
533
|
+
async function createProgram() {
|
|
534
|
+
const program = new import_commander.Command().name("gex").description("GEX: Dependency auditing and documentation for Node.js (local and global).").version(await getToolVersion());
|
|
535
|
+
program.addHelpText("beforeAll", `
|
|
536
|
+
${ASCII_BANNER}`);
|
|
537
|
+
createLocalCommand(program);
|
|
538
|
+
createGlobalCommand(program);
|
|
539
|
+
createReadCommand(program);
|
|
540
|
+
return program;
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
// src/runtimes/node/cli.ts
|
|
544
|
+
var import_meta2 = {};
|
|
545
|
+
async function run(argv = process.argv) {
|
|
546
|
+
const program = await createProgram();
|
|
547
|
+
await program.parseAsync(argv);
|
|
548
|
+
}
|
|
549
|
+
var isMainModule = (() => {
|
|
550
|
+
try {
|
|
551
|
+
if (typeof require !== "undefined" && typeof module !== "undefined") {
|
|
552
|
+
return require.main === module;
|
|
553
|
+
}
|
|
554
|
+
if (typeof import_meta2 !== "undefined") {
|
|
555
|
+
return import_meta2.url === `file://${process.argv[1]}`;
|
|
556
|
+
}
|
|
557
|
+
return false;
|
|
558
|
+
} catch {
|
|
559
|
+
return false;
|
|
560
|
+
}
|
|
561
|
+
})();
|
|
562
|
+
if (isMainModule) {
|
|
563
|
+
run().catch((error) => {
|
|
564
|
+
console.error("CLI error:", error);
|
|
565
|
+
process.exitCode = 1;
|
|
566
|
+
});
|
|
567
|
+
}
|
|
568
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
569
|
+
0 && (module.exports = {
|
|
570
|
+
run
|
|
571
|
+
});
|
|
572
|
+
//# sourceMappingURL=cli-node.cjs.map
|