Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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`.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason for this requirement to add noEmit to the config? Could we not use the flag in the command instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, no.
When you build a typescript project, you have two options:

  1. Use a tsconfig.json file (that's what we do here)
  2. Provide all compiler options as command line options

And you can't mix those two.
So running something like tsc --noEmit --build tsconfig.json will not work (error TS6369: Option '--build' must be the first command line argument.) and switching noEmit and build will also not work (error TS5072: Unknown build option '--noEmit'.).

I thought about creating a custom tsconfig file just before building the ts project. It could use the extends field to include all the configuration of the project's tsconfig. But I'm not sure if this is that reliable. For example it needs to be removed again after building the project. But what happens if the user interrupts the script during the ts build? These cases would need to be handled. Handling that would require quite some code. And I'm not sure if adding this code is really worth it. Maybe having this hint (i.e. "please add --noEmit to your tsconfig file") is enough. After all, you will want to have this option set anyways if you use babel as a typescript compiler.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think eventually we'll have a tsconfig in kcd-scripts that my projects will extend. But for now this is acceptable. Thanks!


## 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)