|
| 1 | +import { writeFile } from 'node:fs/promises'; |
| 2 | +import { transformTSErrorCodeToAuditSlug } from '../../src/lib/runner/utils'; |
| 3 | + |
| 4 | +/* |
| 5 | +transform strictNullChecks to Strict null checks |
| 6 | + */ |
| 7 | +function formatTitle(description: string = '') { |
| 8 | + return description |
| 9 | + .replace(/-/g, ' ') |
| 10 | + .replace(/\b\w/g, letter => letter.toUpperCase()); |
| 11 | +} |
| 12 | + |
| 13 | +async function fetchJsonFromGitHub(url: string): Promise<any> { |
| 14 | + try { |
| 15 | + const response = await fetch(url, { |
| 16 | + headers: new Headers({ 'Content-Type': 'application/json' }), |
| 17 | + }); |
| 18 | + |
| 19 | + if (!response.ok) { |
| 20 | + throw new Error(`Failed to fetch JSON. Status: ${response.status}`); |
| 21 | + } |
| 22 | + return await response.json(); |
| 23 | + } catch (error) { |
| 24 | + throw new Error(`Error fetching JSON: ${(error as Error).message}`); |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +export async function generateAuditsFromGithub() { |
| 29 | + const githubResult = (await fetchJsonFromGitHub( |
| 30 | + 'https://raw.githubusercontent.com/microsoft/TypeScript/main/src/compiler/diagnosticMessages.json', |
| 31 | + )) as Record< |
| 32 | + string, |
| 33 | + { |
| 34 | + category: 'Error' | 'Warning' | 'Message'; |
| 35 | + code: number; |
| 36 | + } |
| 37 | + >; |
| 38 | + |
| 39 | + const audits = Object.entries(githubResult) |
| 40 | + .filter(([_, { category }]) => { |
| 41 | + return category === 'Error' || category === 'Warning'; |
| 42 | + }) |
| 43 | + .map(([description, { code, category }]) => { |
| 44 | + return errorToAudit(code, description); |
| 45 | + }); |
| 46 | + |
| 47 | + console.info( |
| 48 | + `Generated ${audits.length} audits in packages/plugin-typescript/src/lib/audits.ts`, |
| 49 | + ); |
| 50 | + |
| 51 | + await writeFile( |
| 52 | + 'packages/plugin-typescript/src/lib/audits.ts', |
| 53 | + ` |
| 54 | + import type {Audit} from "@code-pushup/models"; |
| 55 | + export const AUDITS: Audit[] = ${JSON.stringify(audits)}; |
| 56 | + `, |
| 57 | + ); |
| 58 | +} |
| 59 | + |
| 60 | +function errorToAudit(tscode: number, description: string) { |
| 61 | + const slug = transformTSErrorCodeToAuditSlug(tscode); |
| 62 | + return { |
| 63 | + slug, |
| 64 | + title: formatTitle(slug), |
| 65 | + description, |
| 66 | + }; |
| 67 | +} |
0 commit comments