|
| 1 | +//import node modules for running shell commands and handling files, define file names |
| 2 | +import { execSync } from "child_process"; |
| 3 | +import fs from "fs"; |
| 4 | +const reportFile = "lint-report.json"; |
| 5 | +const countFile = "lint-files.count.txt"; |
| 6 | + |
| 7 | +// Generate report |
| 8 | +const generateReports = (results, exitCode) => { |
| 9 | + const countContent = `After running the lint found total number of the files has linting problems: ${results.length}`; |
| 10 | + fs.writeFileSync(countFile, countContent, "utf-8"); |
| 11 | + console.log( |
| 12 | + `Found ${results.length} files with linting errors. Total written to ${countFile}`, |
| 13 | + ); |
| 14 | + |
| 15 | + const modifiedResults = results.map((result) => { |
| 16 | + const vrmsIndex = result.filePath.indexOf("VRMS"); |
| 17 | + const relativePath = result.filePath.substring(vrmsIndex + 4); |
| 18 | + return { ...result, filePath: relativePath }; |
| 19 | + }); |
| 20 | + |
| 21 | + const formattedJson = JSON.stringify(modifiedResults, null, 2); |
| 22 | + fs.writeFileSync(reportFile, formattedJson, "utf-8"); |
| 23 | + |
| 24 | + console.log(`Lint report created: ${reportFile}`); |
| 25 | + process.exit(exitCode); |
| 26 | +}; |
| 27 | + |
| 28 | +try { |
| 29 | + const stdout = execSync("npx eslint . --format json").toString(); |
| 30 | + const results = JSON.parse(stdout); |
| 31 | + generateReports(results, 0); |
| 32 | +} catch (error) { |
| 33 | + let results; |
| 34 | + try { |
| 35 | + const stdout = error.stdout.toString(); |
| 36 | + results = JSON.parse(stdout); |
| 37 | + } catch (parseError) { |
| 38 | + console.error( |
| 39 | + "Failed to parse ESLint output. The output was not valid JSON.", |
| 40 | + ); |
| 41 | + console.error("Error details:", parseError.message); |
| 42 | + console.error("Raw output:", error.stdout.toString()); |
| 43 | + process.exit(1); |
| 44 | + } |
| 45 | + generateReports(results, 1); |
| 46 | +} |
0 commit comments