Skip to content

Instantly share code, notes, and snippets.

@myobie
Created January 4, 2021 09:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save myobie/a3560f26a021e68c3e5f05df368d9b8e to your computer and use it in GitHub Desktop.
Save myobie/a3560f26a021e68c3e5f05df368d9b8e to your computer and use it in GitHub Desktop.
Alternative to estrella's tslint; instead use tsc --build so we can choose to produce declaration files (.d.ts)
/* eslint-disable @typescript-eslint/no-var-requires */
const perf = require('perf_hooks')
const { spawn } = require('child_process')
const { cliopts, fmtDuration, findInPATH, log, stdoutStyle } = require('estrella')
module.exports = {
clock,
tscBuild
}
function clock () {
return perf.performance.now()
}
async function tscBuild (cwd) {
const time = clock()
return new Promise((resolve, reject) => {
let resolved = false
const tsc = findInPATH('tsc')
if (!tsc) {
reject(new Error('** cannot find tsc'))
return
}
const args = [
'--build',
cwd
]
if (cliopts.colors) {
args.push('--pretty')
}
if (cliopts.watch) {
args.push('--watch')
args.push('--preserveWatchOutput')
}
const proc = spawn(
tsc,
args,
{
cwd,
stdio: 'inherit'
}
)
const onProcessExitHandler = () => {
try { proc.kill() } catch (_) {}
}
process.on('exit', onProcessExitHandler)
proc.on('error', e => {
if (!resolved) {
resolved = true
reject(e)
}
})
proc.on('exit', code => {
if (!resolved) {
resolved = true
if (code !== 0) {
reject(new Error(`tsc --build failed; exited with code ${code}`))
} else {
resolve(true)
}
}
})
})
.then(() => {
log.info(stdoutStyle.green(`tsc --build complete (${fmtDuration(clock() - time)})`))
})
.catch(e => {
log.error(stdoutStyle.red('** problem running tsc --build'))
log.error(e)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment