forked from danny-avila/agents
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathesm-error-checker.mjs
More file actions
95 lines (78 loc) · 2.88 KB
/
esm-error-checker.mjs
File metadata and controls
95 lines (78 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import * as ts from 'typescript';
import * as fs from 'fs';
import * as path from 'path';
import { fileURLToPath } from 'url';
import { ESLint } from 'eslint';
const fileName = process.argv[2];
if (!fileName) {
console.error('Please provide a file name');
process.exit(1);
}
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// Read tsconfig.json
const tsconfigPath = path.resolve(__dirname, 'tsconfig.json');
const tsconfig = JSON.parse(fs.readFileSync(tsconfigPath, 'utf8'));
// Create compiler options
const compilerOptions = ts.convertCompilerOptionsFromJson(tsconfig.compilerOptions, './').options;
// Resolve path aliases
compilerOptions.baseUrl = path.resolve(__dirname, compilerOptions.baseUrl || '.');
if (compilerOptions.paths) {
compilerOptions.paths = Object.fromEntries(
Object.entries(compilerOptions.paths).map(([key, value]) => [
key,
value.map(p => path.resolve(compilerOptions.baseUrl, p))
])
);
}
// Get all TypeScript files in the project
const getAllTypeScriptFiles = (dir) => {
const files = fs.readdirSync(dir, { withFileTypes: true });
return files.flatMap(file => {
const filePath = path.join(dir, file.name);
if (file.isDirectory()) {
return getAllTypeScriptFiles(filePath);
} else if (file.name.endsWith('.ts')) {
return filePath;
}
return [];
});
};
const projectFiles = getAllTypeScriptFiles(path.resolve(__dirname, 'src'));
const fullPath = path.resolve(__dirname, fileName);
const program = ts.createProgram([fullPath, ...projectFiles], compilerOptions);
const sourceFile = program.getSourceFile(fullPath);
const diagnostics = ts.getPreEmitDiagnostics(program);
let output = '';
if (sourceFile) {
output += '```ts\n' + sourceFile.getFullText() + '\n```\n\n// TypeScript Errors:\n';
if (diagnostics.length === 0) {
output += 'No TypeScript errors found.\n';
} else {
diagnostics.forEach((diagnostic) => {
if (diagnostic.file === sourceFile) {
const { line, character } = ts.getLineAndCharacterOfPosition(diagnostic.file, diagnostic.start);
const message = ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n');
const lineContent = sourceFile.text.split('\n')[line];
output += `Line ${line + 1}, Column ${character + 1}: ${message}\n`;
output += '```ts\n' + lineContent + '\n```\n\n';
}
});
}
}
// Run ESLint
const runESLint = async () => {
const eslint = new ESLint();
const results = await eslint.lintFiles([fullPath]);
const formatter = await eslint.loadFormatter('stylish');
return formatter.format(results);
};
runESLint().then(eslintOutput => {
output += '\n// ESLint Results:\n' + eslintOutput;
console.log(output);
// Write the output file
const outputFile = 'lint_output.txt';
fs.writeFileSync(outputFile, output);
}).catch(error => {
console.error('Error running ESLint:', error);
});