Last active
April 19, 2024 08:58
-
-
Save dsernst/b1d2df3bb5be777e1dcb27e5c0d2474d to your computer and use it in GitHub Desktop.
Run typescript compiler in pre-commit hook
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Run this script to check our files for type errors | |
// using our existing tsconfig.json settings | |
// | |
// Usage: node typecheck.js | |
// | |
const fs = require('fs') | |
const stripJsonComments = require('strip-json-comments') | |
const tsConfig = JSON.parse(stripJsonComments(fs.readFileSync('./tsconfig.json', 'utf8'))) | |
const { exec } = require('child_process') | |
const chalk = require('chalk') | |
const filesToCheck = 'src/*.ts{,x}' // <--- YOU PROBABLY WANT TO CHANGE | |
const options = { | |
...tsConfig.compilerOptions, | |
// Overrides: | |
incremental: false, | |
} | |
let optionsString = '' | |
Object.keys(options).forEach(key => { | |
const value = options[key] | |
const option = '--' + key | |
const type = typeof value | |
if (type === 'boolean') { | |
if (value) { | |
optionsString += option + ' ' | |
} | |
} else if (type === 'string') { | |
optionsString += option + ' ' + value + ' ' | |
} else if (type === 'object') { | |
if (Array.isArray(value)) { | |
optionsString += option + ' ' + value.join(',') + ' ' | |
} | |
} else { | |
console.log('\nMissing support for compilerOption:') | |
console.log({ [key]: { type, value } }) | |
} | |
}) | |
console.log('π Typechecking files...') | |
exec(`tsc ${filesToCheck} ${optionsString}`, (err, stdout) => { | |
const results = stdout | |
.split('\n') | |
// Filter out excludes | |
.filter(line => !tsConfig.exclude.some(exclude => line.startsWith(exclude))) | |
// Highlight filenames | |
.map(line => line.replace(/^(\w|-|\/|\.)+tsx?/, filename => chalk.bold.cyan(filename))) | |
// Remove empty lines | |
.filter(line => line) | |
if (err && results.length) { | |
console.log(results.join('\n')) | |
} else { | |
console.log('β Typecheck passed without errors.') | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See microsoft/TypeScript#27379 (comment) for context: