From c640c9cc25a390168151e0565a5ef987150c2875 Mon Sep 17 00:00:00 2001 From: Lukas Date: Mon, 18 May 2020 15:02:18 +0200 Subject: [PATCH] fix: Only build typescript files in pre-commit for typescript projects (#142) * fix: Only build typescript files in pre-commit for typescript projects * docs: Update documentation on typescript support --- README.md | 4 +++- src/config/lintstagedrc.js | 3 +-- src/scripts/pre-commit.js | 28 ++++++++++++++++++++-------- 3 files changed, 24 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index a9f3e570..5c499b9e 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,7 @@ for linting, testing, building, and more. + - [Installation](#installation) - [Usage](#usage) - [Overriding Config](#overriding-config) @@ -134,7 +135,8 @@ If you customised your `.babelrc`-file you might need to manually add `kcd-scripts` will automatically load any `.ts` and `.tsx` files, including the default entry point, so you don't have to worry about any rollup configuration. -`tsc --noemit` will run during lint-staged to verify that files will compile. +`tsc --build tsconfig.json` will run during before committing to verify that files will compile. +So make sure to add the `noEmit` flag to the `tsconfig.json`'s `compilerOptions`. ## Inspiration diff --git a/src/config/lintstagedrc.js b/src/config/lintstagedrc.js index f447b8cf..b367b89d 100644 --- a/src/config/lintstagedrc.js +++ b/src/config/lintstagedrc.js @@ -1,4 +1,4 @@ -const {resolveKcdScripts, resolveBin, ifTypescript} = require('../utils') +const {resolveKcdScripts, resolveBin} = require('../utils') const kcdScripts = resolveKcdScripts() const doctoc = resolveBin('doctoc') @@ -10,5 +10,4 @@ module.exports = { `${kcdScripts} lint`, `${kcdScripts} test --findRelatedTests`, ], - '*.+(ts|tsx)': ifTypescript ? [`tsc --noEmit`] : undefined, } diff --git a/src/scripts/pre-commit.js b/src/scripts/pre-commit.js index 17733caa..e75ae67a 100644 --- a/src/scripts/pre-commit.js +++ b/src/scripts/pre-commit.js @@ -1,6 +1,6 @@ const path = require('path') const spawn = require('cross-spawn') -const {hasPkgProp, hasFile, resolveBin} = require('../utils') +const {hasPkgProp, hasFile, resolveBin, hasTypescript} = require('../utils') const here = p => path.join(__dirname, p) const hereRelative = p => here(p).replace(process.cwd(), '.') @@ -23,12 +23,24 @@ const lintStagedResult = spawn.sync( {stdio: 'inherit'}, ) -if (lintStagedResult.status === 0) { - const validateResult = spawn.sync('npm', ['run', 'validate'], { - stdio: 'inherit', - }) - - process.exit(validateResult.status) -} else { +if (lintStagedResult.status !== 0) { process.exit(lintStagedResult.status) } + +if (hasTypescript) { + const tscResult = spawn.sync( + resolveBin('typescript', {executable: 'tsc'}), + ['--build', 'tsconfig.json'], + {stdio: 'inherit'}, + ) + + if (tscResult.status !== 0) { + process.exit(tscResult.status) + } +} + +const validateResult = spawn.sync('npm', ['run', 'validate'], { + stdio: 'inherit', +}) + +process.exit(validateResult.status)