@andy2639/jest-context 1.0.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/CHANGELOG.md +16 -0
- package/LICENSE +21 -0
- package/README.md +35 -0
- package/dist/bin/console-context.cjs +685 -0
- package/dist/bin/console-context.cjs.map +1 -0
- package/dist/bin/console-context.d.mts +1 -0
- package/dist/bin/console-context.d.ts +1 -0
- package/dist/bin/console-context.mjs +12 -0
- package/dist/bin/console-context.mjs.map +1 -0
- package/dist/bin/coverage-context.cjs +496 -0
- package/dist/bin/coverage-context.cjs.map +1 -0
- package/dist/bin/coverage-context.d.mts +1 -0
- package/dist/bin/coverage-context.d.ts +1 -0
- package/dist/bin/coverage-context.mjs +12 -0
- package/dist/bin/coverage-context.mjs.map +1 -0
- package/dist/bin/test-context.cjs +585 -0
- package/dist/bin/test-context.cjs.map +1 -0
- package/dist/bin/test-context.d.mts +1 -0
- package/dist/bin/test-context.d.ts +1 -0
- package/dist/bin/test-context.mjs +12 -0
- package/dist/bin/test-context.mjs.map +1 -0
- package/dist/chunk-DEJBEL4M.mjs +168 -0
- package/dist/chunk-DEJBEL4M.mjs.map +1 -0
- package/dist/chunk-DUQBPBV4.mjs +252 -0
- package/dist/chunk-DUQBPBV4.mjs.map +1 -0
- package/dist/chunk-WPFTKCAT.mjs +169 -0
- package/dist/chunk-WPFTKCAT.mjs.map +1 -0
- package/dist/chunk-YTFA3KPD.mjs +513 -0
- package/dist/chunk-YTFA3KPD.mjs.map +1 -0
- package/dist/index.cjs +1112 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.mts +141 -0
- package/dist/index.d.ts +141 -0
- package/dist/index.mjs +99 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +57 -0
|
@@ -0,0 +1,685 @@
|
|
|
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 __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
18
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
19
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
20
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
21
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
22
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
23
|
+
mod
|
|
24
|
+
));
|
|
25
|
+
|
|
26
|
+
// src/commands/console-context.ts
|
|
27
|
+
var import_node_path2 = __toESM(require("path"));
|
|
28
|
+
|
|
29
|
+
// src/core/constants.ts
|
|
30
|
+
var VALID_MODES = ["--all", "--related", "--tests"];
|
|
31
|
+
var VALID_EXPORT_FORMATS = ["txt", "md"];
|
|
32
|
+
var VALID_TEST_EXTENSIONS = /\.(test|spec)\.(ts|tsx|js|jsx)$/;
|
|
33
|
+
var CONSOLE_LOG_TYPES = {
|
|
34
|
+
ERROR: "console.error",
|
|
35
|
+
WARN: "console.warn",
|
|
36
|
+
LOG: "console.log",
|
|
37
|
+
DEBUG: "console.debug"
|
|
38
|
+
};
|
|
39
|
+
var JEST_PATTERNS = {
|
|
40
|
+
FAIL_LINE: /^FAIL\s+/,
|
|
41
|
+
PASS_LINE: /^PASS\s+/,
|
|
42
|
+
TEST_FILE: /^(PASS|FAIL)\s+(.+\.(test|spec)\.tsx?)/,
|
|
43
|
+
COVERAGE_LINE: /^(.+?)\s+\|\s+([\d.]+)\s+\|\s+([\d.]+)\s+\|\s+([\d.]+)\s+\|\s+([\d.]+)(?:\s+\|\s+(.+))?$/,
|
|
44
|
+
BULLET_POINT: /^●\s+/,
|
|
45
|
+
CODE_LINE: /^>?\s*\d*\s*\|/,
|
|
46
|
+
STACK_TRACE: /^\s+at\s+/
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
// src/core/platform.ts
|
|
50
|
+
function isWindows() {
|
|
51
|
+
return process.platform === "win32";
|
|
52
|
+
}
|
|
53
|
+
function resolveCommand(cmd) {
|
|
54
|
+
if (!isWindows()) return cmd;
|
|
55
|
+
const windowsCommands = {
|
|
56
|
+
git: "git.exe",
|
|
57
|
+
node: "node.exe",
|
|
58
|
+
npm: "npm.cmd",
|
|
59
|
+
npx: "npx.cmd",
|
|
60
|
+
yarn: "yarn.cmd",
|
|
61
|
+
pnpm: "pnpm.cmd"
|
|
62
|
+
};
|
|
63
|
+
return windowsCommands[cmd] ?? cmd;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// src/core/package-manager.ts
|
|
67
|
+
function detectPackageManager() {
|
|
68
|
+
if (process.env.npm_execpath?.includes("yarn")) return "yarn";
|
|
69
|
+
if (process.env.npm_execpath?.includes("pnpm")) return "pnpm";
|
|
70
|
+
return "npm";
|
|
71
|
+
}
|
|
72
|
+
function getJestCommand(packageManager = null) {
|
|
73
|
+
const pm = packageManager ?? detectPackageManager();
|
|
74
|
+
switch (pm) {
|
|
75
|
+
case "yarn":
|
|
76
|
+
return ["yarn", "jest"];
|
|
77
|
+
case "pnpm":
|
|
78
|
+
return ["pnpm", "jest"];
|
|
79
|
+
default:
|
|
80
|
+
return ["npx", "jest"];
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// src/core/exec.ts
|
|
85
|
+
var import_node_child_process = require("child_process");
|
|
86
|
+
async function execCommand(cmd, args = [], options = {}) {
|
|
87
|
+
return new Promise((resolve, reject) => {
|
|
88
|
+
const resolvedCmd = resolveCommand(cmd);
|
|
89
|
+
const child = (0, import_node_child_process.spawn)(resolvedCmd, args, {
|
|
90
|
+
shell: options.shell ?? process.platform === "win32"
|
|
91
|
+
});
|
|
92
|
+
let stdout = "";
|
|
93
|
+
let stderr = "";
|
|
94
|
+
child.stdout?.on("data", (d) => {
|
|
95
|
+
stdout += d.toString();
|
|
96
|
+
});
|
|
97
|
+
child.stderr?.on("data", (d) => {
|
|
98
|
+
stderr += d.toString();
|
|
99
|
+
});
|
|
100
|
+
child.on("close", (code) => {
|
|
101
|
+
const result = {
|
|
102
|
+
stdout,
|
|
103
|
+
stderr,
|
|
104
|
+
output: `${stdout}
|
|
105
|
+
${stderr}`,
|
|
106
|
+
code
|
|
107
|
+
};
|
|
108
|
+
if (code !== 0 && !options.ignoreErrors) {
|
|
109
|
+
reject(new Error(`Command failed with code ${code}: ${cmd} ${args.join(" ")}`));
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
resolve(result);
|
|
113
|
+
});
|
|
114
|
+
child.on("error", (error) => {
|
|
115
|
+
reject(new Error(`Failed to execute command: ${error.message}`));
|
|
116
|
+
});
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// src/core/git.ts
|
|
121
|
+
var cachedGitAvailability = null;
|
|
122
|
+
var cachedStagedFiles = null;
|
|
123
|
+
var cachedStagedFilesAt = 0;
|
|
124
|
+
var STAGED_FILES_CACHE_TTL_MS = 1e3;
|
|
125
|
+
async function hasGit() {
|
|
126
|
+
if (cachedGitAvailability !== null) {
|
|
127
|
+
return cachedGitAvailability;
|
|
128
|
+
}
|
|
129
|
+
try {
|
|
130
|
+
const result = await execCommand("git", ["--version"], { ignoreErrors: true });
|
|
131
|
+
cachedGitAvailability = result.code === 0;
|
|
132
|
+
return cachedGitAvailability;
|
|
133
|
+
} catch {
|
|
134
|
+
cachedGitAvailability = false;
|
|
135
|
+
return false;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
async function getStagedFiles() {
|
|
139
|
+
const now = Date.now();
|
|
140
|
+
if (cachedStagedFiles && now - cachedStagedFilesAt <= STAGED_FILES_CACHE_TTL_MS) {
|
|
141
|
+
return cachedStagedFiles;
|
|
142
|
+
}
|
|
143
|
+
const gitAvailable = await hasGit();
|
|
144
|
+
if (!gitAvailable) {
|
|
145
|
+
throw new Error("Git is not installed or not available in PATH");
|
|
146
|
+
}
|
|
147
|
+
const result = await execCommand("git", ["diff", "--name-only", "--cached"], {
|
|
148
|
+
ignoreErrors: true
|
|
149
|
+
});
|
|
150
|
+
const files = result.stdout.split("\n").map((f) => f.trim()).filter(Boolean);
|
|
151
|
+
cachedStagedFiles = files;
|
|
152
|
+
cachedStagedFilesAt = now;
|
|
153
|
+
return files;
|
|
154
|
+
}
|
|
155
|
+
function filterTestFiles(files) {
|
|
156
|
+
return files.filter((file) => VALID_TEST_EXTENSIONS.test(file));
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// src/core/jest.ts
|
|
160
|
+
var import_node_child_process2 = require("child_process");
|
|
161
|
+
async function runJest(args = [], options = {}) {
|
|
162
|
+
const {
|
|
163
|
+
verbose = false,
|
|
164
|
+
coverage = false,
|
|
165
|
+
packageManager = null,
|
|
166
|
+
ignoreErrors = true,
|
|
167
|
+
disableVerbose = false
|
|
168
|
+
} = options;
|
|
169
|
+
const jestCmd = getJestCommand(packageManager);
|
|
170
|
+
const jestArgs = [...jestCmd.slice(1)];
|
|
171
|
+
if (verbose && !disableVerbose) {
|
|
172
|
+
jestArgs.push("--verbose");
|
|
173
|
+
}
|
|
174
|
+
if (coverage) {
|
|
175
|
+
jestArgs.push("--coverage");
|
|
176
|
+
if (options.coverageReporters) {
|
|
177
|
+
for (const reporter of options.coverageReporters) {
|
|
178
|
+
jestArgs.push(`--coverageReporters=${reporter}`);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
if (isWindows()) {
|
|
183
|
+
jestArgs.push("--no-watchman");
|
|
184
|
+
}
|
|
185
|
+
jestArgs.push(...args);
|
|
186
|
+
return new Promise((resolve, reject) => {
|
|
187
|
+
const child = (0, import_node_child_process2.spawn)(jestCmd[0], jestArgs, {
|
|
188
|
+
shell: isWindows()
|
|
189
|
+
});
|
|
190
|
+
let stdout = "";
|
|
191
|
+
let stderr = "";
|
|
192
|
+
child.stdout.on("data", (d) => {
|
|
193
|
+
stdout += d.toString();
|
|
194
|
+
});
|
|
195
|
+
child.stderr.on("data", (d) => {
|
|
196
|
+
stderr += d.toString();
|
|
197
|
+
});
|
|
198
|
+
child.on("close", (code) => {
|
|
199
|
+
const result = {
|
|
200
|
+
stdout,
|
|
201
|
+
stderr,
|
|
202
|
+
output: `${stdout}
|
|
203
|
+
${stderr}`,
|
|
204
|
+
code
|
|
205
|
+
};
|
|
206
|
+
if (code !== 0 && !ignoreErrors) {
|
|
207
|
+
reject(new Error(`Jest failed with code ${code}`));
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
resolve(result);
|
|
211
|
+
});
|
|
212
|
+
child.on("error", reject);
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
async function buildJestArgsForMode(mode) {
|
|
216
|
+
if (mode === "all") {
|
|
217
|
+
return [];
|
|
218
|
+
}
|
|
219
|
+
const staged = await getStagedFiles();
|
|
220
|
+
if (staged.length === 0) {
|
|
221
|
+
throw new Error(`No staged files found for mode: ${mode}`);
|
|
222
|
+
}
|
|
223
|
+
if (mode === "tests") {
|
|
224
|
+
const testFiles = filterTestFiles(staged);
|
|
225
|
+
if (testFiles.length === 0) {
|
|
226
|
+
throw new Error("No test files found in staged files");
|
|
227
|
+
}
|
|
228
|
+
return testFiles;
|
|
229
|
+
}
|
|
230
|
+
return ["--findRelatedTests", ...staged];
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
// src/core/parsing.ts
|
|
234
|
+
function stripAnsi(text) {
|
|
235
|
+
return text.replace(/\x1b\[[0-9;]*m/g, "");
|
|
236
|
+
}
|
|
237
|
+
function isCodeSnippetLine(line) {
|
|
238
|
+
return JEST_PATTERNS.CODE_LINE.test(stripAnsi(line).trim());
|
|
239
|
+
}
|
|
240
|
+
function isStackTraceLine(line) {
|
|
241
|
+
return JEST_PATTERNS.STACK_TRACE.test(stripAnsi(line));
|
|
242
|
+
}
|
|
243
|
+
function isConsoleTypeLine(line) {
|
|
244
|
+
const clean = stripAnsi(line).trim();
|
|
245
|
+
return Object.values(CONSOLE_LOG_TYPES).includes(clean);
|
|
246
|
+
}
|
|
247
|
+
function extractConsoleType(line) {
|
|
248
|
+
const clean = stripAnsi(line).trim();
|
|
249
|
+
return Object.values(CONSOLE_LOG_TYPES).find((type) => type === clean) ?? null;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
// src/core/export.ts
|
|
253
|
+
var import_node_fs = require("fs");
|
|
254
|
+
var import_node_path = __toESM(require("path"));
|
|
255
|
+
function getDisplayTimestamp(locale = "en-US") {
|
|
256
|
+
return (/* @__PURE__ */ new Date()).toLocaleString(locale, {
|
|
257
|
+
year: "numeric",
|
|
258
|
+
month: "2-digit",
|
|
259
|
+
day: "2-digit",
|
|
260
|
+
hour: "2-digit",
|
|
261
|
+
minute: "2-digit",
|
|
262
|
+
second: "2-digit",
|
|
263
|
+
hour12: false
|
|
264
|
+
});
|
|
265
|
+
}
|
|
266
|
+
function getFilenameTimestamp() {
|
|
267
|
+
const now = /* @__PURE__ */ new Date();
|
|
268
|
+
const pad = (value) => String(value).padStart(2, "0");
|
|
269
|
+
return [now.getFullYear(), pad(now.getMonth() + 1), pad(now.getDate())].join("-") + "_" + [pad(now.getHours()), pad(now.getMinutes()), pad(now.getSeconds())].join("-");
|
|
270
|
+
}
|
|
271
|
+
function buildTimestampedPath(prefix, extension, dir = process.cwd()) {
|
|
272
|
+
const timestamp = getFilenameTimestamp();
|
|
273
|
+
const filename = `${prefix}-${timestamp}.${extension}`;
|
|
274
|
+
return import_node_path.default.resolve(dir, filename);
|
|
275
|
+
}
|
|
276
|
+
function formatAsMarkdown(content, title, timestamp = null) {
|
|
277
|
+
const lines = [`# ${title}`, ""];
|
|
278
|
+
if (timestamp) {
|
|
279
|
+
lines.push(`Generated: ${timestamp}`, "");
|
|
280
|
+
}
|
|
281
|
+
lines.push("```text", content.trimEnd(), "```", "");
|
|
282
|
+
return lines.join("\n");
|
|
283
|
+
}
|
|
284
|
+
function exportToFile(content, options) {
|
|
285
|
+
const { prefix, format, title, dir = process.cwd() } = options;
|
|
286
|
+
const filePath = buildTimestampedPath(prefix, format, dir);
|
|
287
|
+
let finalContent = content;
|
|
288
|
+
if (format === "md" && title) {
|
|
289
|
+
finalContent = formatAsMarkdown(content, title, getDisplayTimestamp());
|
|
290
|
+
}
|
|
291
|
+
(0, import_node_fs.writeFileSync)(filePath, finalContent, "utf-8");
|
|
292
|
+
return filePath;
|
|
293
|
+
}
|
|
294
|
+
function parseExportCliArgs(args) {
|
|
295
|
+
let exportFormat = null;
|
|
296
|
+
const exportArgs = [];
|
|
297
|
+
for (const arg of args) {
|
|
298
|
+
if (arg === "--export") {
|
|
299
|
+
exportFormat = "txt";
|
|
300
|
+
exportArgs.push(arg);
|
|
301
|
+
continue;
|
|
302
|
+
}
|
|
303
|
+
if (!arg.startsWith("--export=")) {
|
|
304
|
+
continue;
|
|
305
|
+
}
|
|
306
|
+
const value = arg.slice("--export=".length).trim().toLowerCase();
|
|
307
|
+
if (!value || !VALID_EXPORT_FORMATS.includes(value)) {
|
|
308
|
+
throw new Error(`Invalid value for --export: ${value || "(empty)"}. Valid values: txt, md`);
|
|
309
|
+
}
|
|
310
|
+
exportFormat = value;
|
|
311
|
+
exportArgs.push(arg);
|
|
312
|
+
}
|
|
313
|
+
return { exportFormat, exportArgs };
|
|
314
|
+
}
|
|
315
|
+
function handleExportOrDisplay(content, config) {
|
|
316
|
+
const { exportFormat, prefix, title } = config;
|
|
317
|
+
if (!exportFormat) {
|
|
318
|
+
console.log(content);
|
|
319
|
+
return null;
|
|
320
|
+
}
|
|
321
|
+
const outputPath = exportToFile(content, {
|
|
322
|
+
prefix,
|
|
323
|
+
format: exportFormat,
|
|
324
|
+
title
|
|
325
|
+
});
|
|
326
|
+
console.log(`Output exported to: ${outputPath}`);
|
|
327
|
+
return outputPath;
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
// src/core/cli.ts
|
|
331
|
+
function validateCliArgs(args, schema) {
|
|
332
|
+
const parsed = {};
|
|
333
|
+
const unknownArgs = [];
|
|
334
|
+
for (const [key, config] of Object.entries(schema)) {
|
|
335
|
+
if (config.default !== void 0) {
|
|
336
|
+
parsed[key] = config.default;
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
for (const arg of args) {
|
|
340
|
+
let matched = false;
|
|
341
|
+
for (const [key, config] of Object.entries(schema)) {
|
|
342
|
+
if (config.values?.includes(arg)) {
|
|
343
|
+
if (config.exclusive && parsed[key] !== void 0 && parsed[key] !== config.default) {
|
|
344
|
+
throw new Error(`Multiple values provided for ${key}: ${parsed[key]} and ${arg}`);
|
|
345
|
+
}
|
|
346
|
+
parsed[key] = arg;
|
|
347
|
+
matched = true;
|
|
348
|
+
break;
|
|
349
|
+
}
|
|
350
|
+
if (config.prefix && arg.startsWith(config.prefix)) {
|
|
351
|
+
const value = arg.slice(config.prefix.length).trim().toLowerCase();
|
|
352
|
+
if (!value) {
|
|
353
|
+
parsed[key] = "true";
|
|
354
|
+
matched = true;
|
|
355
|
+
break;
|
|
356
|
+
}
|
|
357
|
+
if (config.allowedValues && !config.allowedValues.includes(value)) {
|
|
358
|
+
throw new Error(
|
|
359
|
+
`Invalid value for ${config.prefix}: ${value}. Valid values: ${config.allowedValues.join(", ")}`
|
|
360
|
+
);
|
|
361
|
+
}
|
|
362
|
+
parsed[key] = value;
|
|
363
|
+
matched = true;
|
|
364
|
+
break;
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
if (!matched && arg.startsWith("--")) {
|
|
368
|
+
unknownArgs.push(arg);
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
return { parsed, unknownArgs };
|
|
372
|
+
}
|
|
373
|
+
function displayHelp(title, content) {
|
|
374
|
+
console.log(`
|
|
375
|
+
${title}
|
|
376
|
+
`);
|
|
377
|
+
if (content.description) {
|
|
378
|
+
console.log(`${content.description}
|
|
379
|
+
`);
|
|
380
|
+
}
|
|
381
|
+
if (content.usage?.length) {
|
|
382
|
+
console.log("Usage:");
|
|
383
|
+
content.usage.forEach((line) => console.log(` ${line}`));
|
|
384
|
+
console.log("");
|
|
385
|
+
}
|
|
386
|
+
if (content.options?.length) {
|
|
387
|
+
console.log("Options:");
|
|
388
|
+
content.options.forEach((line) => console.log(` ${line}`));
|
|
389
|
+
console.log("");
|
|
390
|
+
}
|
|
391
|
+
if (content.examples?.length) {
|
|
392
|
+
console.log("Examples:");
|
|
393
|
+
content.examples.forEach((line) => console.log(` ${line}`));
|
|
394
|
+
console.log("");
|
|
395
|
+
}
|
|
396
|
+
process.exit(0);
|
|
397
|
+
}
|
|
398
|
+
function exitWithError(message, code = 1) {
|
|
399
|
+
console.error(`\u274C ${message}`);
|
|
400
|
+
process.exit(code);
|
|
401
|
+
}
|
|
402
|
+
function logWarning(message) {
|
|
403
|
+
console.warn(`\u26A0\uFE0F ${message}`);
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
// src/core/ui.ts
|
|
407
|
+
function shouldShowUI(exportMode, noBanner = false) {
|
|
408
|
+
return !exportMode && !noBanner && Boolean(process.stdout.isTTY);
|
|
409
|
+
}
|
|
410
|
+
function displayBanner(config) {
|
|
411
|
+
const { text, subtitle, info = {}, colors = ["cyan"], font = "block" } = config;
|
|
412
|
+
const bannerText = [text, subtitle].filter(Boolean).join(" ").trim();
|
|
413
|
+
const cfonts = require("cfonts");
|
|
414
|
+
const boxenModule = require("boxen");
|
|
415
|
+
const boxen = typeof boxenModule === "function" ? boxenModule : boxenModule.default;
|
|
416
|
+
const renderedBanner = cfonts.render(bannerText, {
|
|
417
|
+
font,
|
|
418
|
+
colors,
|
|
419
|
+
align: "left",
|
|
420
|
+
background: "transparent",
|
|
421
|
+
letterSpacing: 1,
|
|
422
|
+
lineHeight: 1,
|
|
423
|
+
space: false,
|
|
424
|
+
maxLength: "0",
|
|
425
|
+
env: "node"
|
|
426
|
+
});
|
|
427
|
+
const infoLine = Object.entries(info).map(([key, value]) => `${key}: ${value}`).join(" | ");
|
|
428
|
+
const contentLines = [renderedBanner.string.trimEnd()];
|
|
429
|
+
if (infoLine) {
|
|
430
|
+
contentLines.push("", ` ${infoLine}`);
|
|
431
|
+
}
|
|
432
|
+
contentLines.push(` Date: ${getDisplayTimestamp()}`);
|
|
433
|
+
const boxed = boxen(contentLines.join("\n"), {
|
|
434
|
+
padding: {
|
|
435
|
+
top: 0,
|
|
436
|
+
right: 1,
|
|
437
|
+
bottom: 0,
|
|
438
|
+
left: 1
|
|
439
|
+
},
|
|
440
|
+
margin: {
|
|
441
|
+
top: 0,
|
|
442
|
+
right: 0,
|
|
443
|
+
bottom: 1,
|
|
444
|
+
left: 0
|
|
445
|
+
},
|
|
446
|
+
borderStyle: "round",
|
|
447
|
+
borderColor: colors[0] ?? "cyan"
|
|
448
|
+
});
|
|
449
|
+
console.log(boxed);
|
|
450
|
+
}
|
|
451
|
+
function createSpinner(text, color = "cyan") {
|
|
452
|
+
const ora = require("ora");
|
|
453
|
+
return ora({ text, color, spinner: "dots" });
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
// src/commands/console-context.ts
|
|
457
|
+
function parseConsoleWarnings(rawOutput) {
|
|
458
|
+
const lines = rawOutput.split("\n");
|
|
459
|
+
const warnings = [];
|
|
460
|
+
const state = {
|
|
461
|
+
currentTestFile: null,
|
|
462
|
+
currentWarning: null,
|
|
463
|
+
pendingWarnings: []
|
|
464
|
+
};
|
|
465
|
+
const flushCurrentWarning = () => {
|
|
466
|
+
if (!state.currentWarning) return;
|
|
467
|
+
if (state.currentWarning.testFile === "unknown" && state.currentTestFile) {
|
|
468
|
+
state.currentWarning.testFile = state.currentTestFile;
|
|
469
|
+
}
|
|
470
|
+
warnings.push(state.currentWarning);
|
|
471
|
+
state.pendingWarnings.push(state.currentWarning);
|
|
472
|
+
state.currentWarning = null;
|
|
473
|
+
};
|
|
474
|
+
for (const line of lines) {
|
|
475
|
+
const trimmed = line.trim();
|
|
476
|
+
if (JEST_PATTERNS.TEST_FILE.test(trimmed)) {
|
|
477
|
+
const match = trimmed.match(JEST_PATTERNS.TEST_FILE);
|
|
478
|
+
if (match) {
|
|
479
|
+
state.currentTestFile = import_node_path2.default.basename(match[2]);
|
|
480
|
+
for (const warning of state.pendingWarnings) {
|
|
481
|
+
if (warning.testFile === "unknown") warning.testFile = state.currentTestFile;
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
flushCurrentWarning();
|
|
485
|
+
continue;
|
|
486
|
+
}
|
|
487
|
+
if (trimmed === "\u25CF Console") {
|
|
488
|
+
continue;
|
|
489
|
+
}
|
|
490
|
+
if (isConsoleTypeLine(line)) {
|
|
491
|
+
flushCurrentWarning();
|
|
492
|
+
state.currentWarning = {
|
|
493
|
+
type: extractConsoleType(line) ?? "console.warn",
|
|
494
|
+
testFile: state.currentTestFile ?? "unknown",
|
|
495
|
+
messages: []
|
|
496
|
+
};
|
|
497
|
+
continue;
|
|
498
|
+
}
|
|
499
|
+
if (!state.currentWarning) continue;
|
|
500
|
+
if (isCodeSnippetLine(line)) continue;
|
|
501
|
+
if (isStackTraceLine(line)) continue;
|
|
502
|
+
const cleanMessage = stripAnsi(line).trim();
|
|
503
|
+
if (cleanMessage.length > 0) {
|
|
504
|
+
state.currentWarning.messages.push(cleanMessage);
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
flushCurrentWarning();
|
|
508
|
+
return warnings;
|
|
509
|
+
}
|
|
510
|
+
function filterWarningsByType(warnings, filter) {
|
|
511
|
+
if (filter === "errors") {
|
|
512
|
+
return warnings.filter((w) => w.type === CONSOLE_LOG_TYPES.ERROR);
|
|
513
|
+
}
|
|
514
|
+
if (filter === "warns") {
|
|
515
|
+
return warnings.filter((w) => w.type === CONSOLE_LOG_TYPES.WARN);
|
|
516
|
+
}
|
|
517
|
+
return warnings;
|
|
518
|
+
}
|
|
519
|
+
function groupByTestFile(warnings) {
|
|
520
|
+
const grouped = {};
|
|
521
|
+
for (const warning of warnings) {
|
|
522
|
+
const file = warning.testFile;
|
|
523
|
+
if (!grouped[file]) grouped[file] = [];
|
|
524
|
+
grouped[file].push(warning);
|
|
525
|
+
}
|
|
526
|
+
return grouped;
|
|
527
|
+
}
|
|
528
|
+
function sortFilesByWarningCount(grouped) {
|
|
529
|
+
const entries = Object.entries(grouped);
|
|
530
|
+
entries.sort((a, b) => b[1].length - a[1].length);
|
|
531
|
+
return entries;
|
|
532
|
+
}
|
|
533
|
+
function formatWarningsOutput(grouped, timestamp, stats) {
|
|
534
|
+
const lines = [];
|
|
535
|
+
lines.push("=== CONSOLE WARNINGS CONTEXT BEGIN ===", "", `\u{1F4C5} Run Date: ${timestamp}`, "");
|
|
536
|
+
const sortedEntries = sortFilesByWarningCount(grouped);
|
|
537
|
+
const totalFiles = sortedEntries.length;
|
|
538
|
+
if (totalFiles === 0) {
|
|
539
|
+
lines.push("\u2705 No console warnings found!");
|
|
540
|
+
} else {
|
|
541
|
+
lines.push(
|
|
542
|
+
`\u26A0\uFE0F Found ${stats.total} warning(s) in ${totalFiles} test file(s)`,
|
|
543
|
+
"",
|
|
544
|
+
"\u{1F4CA} Summary by Type:",
|
|
545
|
+
` - console.error: ${stats.byType[CONSOLE_LOG_TYPES.ERROR] ?? 0} occurrence(s)`,
|
|
546
|
+
` - console.warn: ${stats.byType[CONSOLE_LOG_TYPES.WARN] ?? 0} occurrence(s)`,
|
|
547
|
+
"",
|
|
548
|
+
"=".repeat(60),
|
|
549
|
+
""
|
|
550
|
+
);
|
|
551
|
+
for (const [file, warnings] of sortedEntries) {
|
|
552
|
+
lines.push(`\u{1F4C4} File: ${file}`, ` Total Warnings: ${warnings.length}`, "");
|
|
553
|
+
warnings.forEach((warning, idx) => {
|
|
554
|
+
lines.push(` ${idx + 1}. ${warning.type}`, "");
|
|
555
|
+
if (warning.messages.length > 0) {
|
|
556
|
+
lines.push(" \u{1F4DD} Message:");
|
|
557
|
+
warning.messages.forEach((msg) => lines.push(` ${msg}`));
|
|
558
|
+
lines.push("");
|
|
559
|
+
}
|
|
560
|
+
if (idx < warnings.length - 1) {
|
|
561
|
+
lines.push(" " + "-".repeat(50), "");
|
|
562
|
+
}
|
|
563
|
+
});
|
|
564
|
+
lines.push("=".repeat(60), "");
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
lines.push("=== CONSOLE WARNINGS CONTEXT END ===");
|
|
568
|
+
return `${lines.join("\n")}
|
|
569
|
+
`;
|
|
570
|
+
}
|
|
571
|
+
function calculateStatistics(warnings) {
|
|
572
|
+
const stats = {
|
|
573
|
+
total: warnings.length,
|
|
574
|
+
byType: {
|
|
575
|
+
[CONSOLE_LOG_TYPES.ERROR]: 0,
|
|
576
|
+
[CONSOLE_LOG_TYPES.WARN]: 0
|
|
577
|
+
}
|
|
578
|
+
};
|
|
579
|
+
for (const warning of warnings) {
|
|
580
|
+
if (warning.type === CONSOLE_LOG_TYPES.ERROR) {
|
|
581
|
+
stats.byType[CONSOLE_LOG_TYPES.ERROR]++;
|
|
582
|
+
} else if (warning.type === CONSOLE_LOG_TYPES.WARN) {
|
|
583
|
+
stats.byType[CONSOLE_LOG_TYPES.WARN]++;
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
return stats;
|
|
587
|
+
}
|
|
588
|
+
async function runConsoleContext(argv = process.argv.slice(2)) {
|
|
589
|
+
if (argv.includes("-h") || argv.includes("--help")) {
|
|
590
|
+
displayHelp("Console Context Script", {
|
|
591
|
+
description: "Capture and format console warnings/errors from Jest output.",
|
|
592
|
+
usage: [
|
|
593
|
+
"console-context -- --all",
|
|
594
|
+
"console-context -- --related",
|
|
595
|
+
"console-context -- --tests"
|
|
596
|
+
],
|
|
597
|
+
options: [
|
|
598
|
+
"--all Run all tests (default)",
|
|
599
|
+
"--related Run tests related to staged files",
|
|
600
|
+
"--tests Run only staged test files",
|
|
601
|
+
"--only-errors Show only console.error warnings",
|
|
602
|
+
"--only-warns Show only console.warn warnings",
|
|
603
|
+
"--export Export output as txt",
|
|
604
|
+
"--export=txt|md Export output in selected format",
|
|
605
|
+
"--no-banner Disable fancy terminal UI",
|
|
606
|
+
"-h, --help Show this help"
|
|
607
|
+
],
|
|
608
|
+
examples: [
|
|
609
|
+
"console-context -- --related --only-errors",
|
|
610
|
+
"console-context -- --all --export=md"
|
|
611
|
+
]
|
|
612
|
+
});
|
|
613
|
+
}
|
|
614
|
+
const noBanner = argv.includes("--no-banner");
|
|
615
|
+
const args = argv.filter((arg) => arg !== "--no-banner");
|
|
616
|
+
const { exportFormat, exportArgs } = parseExportCliArgs(args);
|
|
617
|
+
const { parsed, unknownArgs } = validateCliArgs(args, {
|
|
618
|
+
mode: {
|
|
619
|
+
values: [...VALID_MODES],
|
|
620
|
+
default: "--all",
|
|
621
|
+
exclusive: true
|
|
622
|
+
},
|
|
623
|
+
filter: {
|
|
624
|
+
values: ["--only-errors", "--only-warns"],
|
|
625
|
+
exclusive: true
|
|
626
|
+
}
|
|
627
|
+
});
|
|
628
|
+
const exportArgSet = new Set(exportArgs);
|
|
629
|
+
const filteredUnknownArgs = unknownArgs.filter((arg) => !exportArgSet.has(arg));
|
|
630
|
+
if (filteredUnknownArgs.length > 0) {
|
|
631
|
+
logWarning(`Unknown arguments ignored: ${filteredUnknownArgs.join(", ")}`);
|
|
632
|
+
}
|
|
633
|
+
const mode = parsed.mode ?? "--all";
|
|
634
|
+
const filter = parsed.filter === "--only-errors" ? "errors" : parsed.filter === "--only-warns" ? "warns" : null;
|
|
635
|
+
const showUI = shouldShowUI(exportFormat, noBanner);
|
|
636
|
+
if (showUI) {
|
|
637
|
+
displayBanner({
|
|
638
|
+
text: "CONSOLE",
|
|
639
|
+
subtitle: "WARNINGS",
|
|
640
|
+
info: {
|
|
641
|
+
Mode: mode,
|
|
642
|
+
Filter: filter ?? "none"
|
|
643
|
+
},
|
|
644
|
+
colors: ["yellow", "red"]
|
|
645
|
+
});
|
|
646
|
+
}
|
|
647
|
+
const modeKey = mode.replace("--", "");
|
|
648
|
+
let jestArgs = [];
|
|
649
|
+
try {
|
|
650
|
+
jestArgs = await buildJestArgsForMode(modeKey);
|
|
651
|
+
} catch (error) {
|
|
652
|
+
exitWithError(error.message);
|
|
653
|
+
}
|
|
654
|
+
const spinner = showUI ? createSpinner("Capturing console warnings...", "yellow") : null;
|
|
655
|
+
spinner?.start();
|
|
656
|
+
const result = await runJest(jestArgs, {
|
|
657
|
+
disableVerbose: true,
|
|
658
|
+
ignoreErrors: true
|
|
659
|
+
});
|
|
660
|
+
let warnings = parseConsoleWarnings(result.output);
|
|
661
|
+
warnings = filterWarningsByType(warnings, filter);
|
|
662
|
+
if (spinner) {
|
|
663
|
+
if (warnings.length > 0) {
|
|
664
|
+
spinner.warn(`Found ${warnings.length} warning(s)`);
|
|
665
|
+
} else {
|
|
666
|
+
spinner.succeed("No console warnings found");
|
|
667
|
+
}
|
|
668
|
+
}
|
|
669
|
+
const grouped = groupByTestFile(warnings);
|
|
670
|
+
const stats = calculateStatistics(warnings);
|
|
671
|
+
const timestamp = getDisplayTimestamp();
|
|
672
|
+
const formatted = formatWarningsOutput(grouped, timestamp, stats);
|
|
673
|
+
handleExportOrDisplay(formatted, {
|
|
674
|
+
exportFormat,
|
|
675
|
+
prefix: "export-console-context",
|
|
676
|
+
title: "Console Warnings Context"
|
|
677
|
+
});
|
|
678
|
+
}
|
|
679
|
+
|
|
680
|
+
// src/bin/console-context.ts
|
|
681
|
+
runConsoleContext().catch((error) => {
|
|
682
|
+
console.error(`\u274C ${error.message}`);
|
|
683
|
+
process.exit(1);
|
|
684
|
+
});
|
|
685
|
+
//# sourceMappingURL=console-context.cjs.map
|