Skip to content

Commit

Permalink
fix: Only build typescript files in pre-commit for typescript projects (
Browse files Browse the repository at this point in the history
#142)

* fix: Only build typescript files in pre-commit for typescript projects

* docs: Update documentation on typescript support
  • Loading branch information
Lukas-Kullmann authored May 18, 2020
1 parent 8899fe9 commit c640c9c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ for linting, testing, building, and more.
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->


- [Installation](#installation)
- [Usage](#usage)
- [Overriding Config](#overriding-config)
Expand Down Expand Up @@ -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

Expand Down
3 changes: 1 addition & 2 deletions src/config/lintstagedrc.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const {resolveKcdScripts, resolveBin, ifTypescript} = require('../utils')
const {resolveKcdScripts, resolveBin} = require('../utils')

const kcdScripts = resolveKcdScripts()
const doctoc = resolveBin('doctoc')
Expand All @@ -10,5 +10,4 @@ module.exports = {
`${kcdScripts} lint`,
`${kcdScripts} test --findRelatedTests`,
],
'*.+(ts|tsx)': ifTypescript ? [`tsc --noEmit`] : undefined,
}
28 changes: 20 additions & 8 deletions src/scripts/pre-commit.js
Original file line number Diff line number Diff line change
@@ -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(), '.')
Expand All @@ -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)

0 comments on commit c640c9c

Please sign in to comment.