|
| 1 | +import pathUtil from 'node:path'; |
| 2 | +import ts from 'typescript'; |
| 3 | + |
| 4 | +/** |
| 5 | + * @fileoverview |
| 6 | + * tsc command output will be parsed by GitHub Actions as errors. |
| 7 | + * We want them to be just warnings. |
| 8 | + */ |
| 9 | + |
| 10 | +const check = () => { |
| 11 | + const rootDir = pathUtil.join(import.meta.dirname, '..'); |
| 12 | + |
| 13 | + const tsconfigPath = pathUtil.join(rootDir, 'tsconfig.json'); |
| 14 | + const commandLine = ts.getParsedCommandLineOfConfigFile(tsconfigPath, {}, ts.sys); |
| 15 | + |
| 16 | + const program = ts.createProgram({ |
| 17 | + rootNames: commandLine.fileNames, |
| 18 | + options: commandLine.options |
| 19 | + }); |
| 20 | + const emitted = program.emit(); |
| 21 | + |
| 22 | + const diagnostics = [ |
| 23 | + ...ts.getPreEmitDiagnostics(program), |
| 24 | + ...emitted.diagnostics |
| 25 | + ]; |
| 26 | + |
| 27 | + let numWarnings = 0; |
| 28 | + |
| 29 | + for (const diagnostic of diagnostics) { |
| 30 | + // https://docs.github.com/en/actions/reference/workflows-and-actions/workflow-commands |
| 31 | + const relativeFileName = pathUtil.relative(rootDir, diagnostic.file.fileName); |
| 32 | + let prefix = `::warning file=${relativeFileName}`; |
| 33 | + |
| 34 | + const startPosition = ts.getLineAndCharacterOfPosition(diagnostic.file, diagnostic.start); |
| 35 | + const endPosition = ts.getLineAndCharacterOfPosition(diagnostic.file, diagnostic.start); |
| 36 | + prefix += `,line=${startPosition.line + 1},col=${startPosition.character + 1}`; |
| 37 | + prefix += `,endLine=${endPosition.line + 1},endCol=${endPosition.character + 1}`; |
| 38 | + |
| 39 | + prefix += ",title=Type warning - may indicate a bug. No action required if no bug.::"; |
| 40 | + |
| 41 | + let messageText = diagnostic.messageText; |
| 42 | + while (messageText) { |
| 43 | + numWarnings++; |
| 44 | + |
| 45 | + if (typeof messageText === 'string') { |
| 46 | + console.log(prefix + messageText); |
| 47 | + messageText = null; |
| 48 | + } else { |
| 49 | + console.log(prefix + messageText.messageText); |
| 50 | + messageText = messageText.next; |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + console.log(`${numWarnings} warnings.`); |
| 56 | +}; |
| 57 | + |
| 58 | +check(); |
0 commit comments