Created
January 4, 2021 09:18
-
-
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)
This file contains 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
/* 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