Skip to content
Permalink
Browse files

Add `--log-error` option (#818)

  • Loading branch information...
tenorok authored and blakeembrey committed Apr 15, 2019
1 parent 8706c31 commit 750eccae4a594b429639d24ce6215afdd3a100aa
Showing with 23 additions and 6 deletions.
  1. +1 −0 README.md
  2. +4 −1 src/bin.ts
  3. +18 −5 src/index.ts
@@ -135,6 +135,7 @@ _Environment variable denoted in parentheses._
* `--pretty` Use pretty diagnostic formatter (`TS_NODE_PRETTY`, default: `false`)
* `--skip-project` Skip project config resolution and loading (`TS_NODE_SKIP_PROJECT`, default: `false`)
* `--skip-ignore` Skip ignore checks (`TS_NODE_SKIP_IGNORE`, default: `false`)
* `--log-error` Logs errors of types instead of exit the process (`TS_NODE_LOG_ERROR`, default: `false`)

### Programmatic Only Options

@@ -32,6 +32,7 @@ const args = arg({
'--pretty': Boolean,
'--skip-project': Boolean,
'--skip-ignore': Boolean,
'--log-error': Boolean,

// Aliases.
'-e': '--eval',
@@ -62,7 +63,8 @@ const {
'--type-check': typeCheck = DEFAULTS.typeCheck,
'--pretty': pretty = DEFAULTS.pretty,
'--skip-project': skipProject = DEFAULTS.skipProject,
'--skip-ignore': skipIgnore = DEFAULTS.skipIgnore
'--skip-ignore': skipIgnore = DEFAULTS.skipIgnore,
'--log-error': logError = DEFAULTS.logError
} = args

if (help) {
@@ -113,6 +115,7 @@ const service = register({
ignore,
project,
skipIgnore,
logError,
skipProject,
compiler,
ignoreDiagnostics,
@@ -59,6 +59,7 @@ export interface Options {
pretty?: boolean | null
typeCheck?: boolean | null
transpileOnly?: boolean | null
logError?: boolean | null
files?: boolean | null
compiler?: string
ignore?: string[]
@@ -106,7 +107,8 @@ export const DEFAULTS: Options = {
skipProject: yn(process.env['TS_NODE_SKIP_PROJECT']),
ignoreDiagnostics: split(process.env['TS_NODE_IGNORE_DIAGNOSTICS']),
typeCheck: yn(process.env['TS_NODE_TYPE_CHECK']),
transpileOnly: yn(process.env['TS_NODE_TRANSPILE_ONLY'])
transpileOnly: yn(process.env['TS_NODE_TRANSPILE_ONLY']),
logError: yn(process.env['TS_NODE_LOG_ERROR'])
}

/**
@@ -226,8 +228,19 @@ export function register (opts: Options = {}): Register {
return new TSError(diagnosticText, diagnosticCodes)
}

// Render the configuration errors and exit the script.
if (configDiagnosticList.length) throw createTSError(configDiagnosticList)
function reportTSError (configDiagnosticList: _ts.Diagnostic[]) {
const error = createTSError(configDiagnosticList)
if (options.logError) {
// Print error in red color and continue execution.
console.error('\x1b[31m%s\x1b[0m', error)
} else {
// Throw error and exit the script.
throw error
}
}

// Render the configuration errors.
if (configDiagnosticList.length) reportTSError(configDiagnosticList)

// Enable additional extensions when JSX or `allowJs` is enabled.
if (config.options.jsx) extensions.push('.tsx')
@@ -256,7 +269,7 @@ export function register (opts: Options = {}): Register {
filterDiagnostics(result.diagnostics, ignoreDiagnostics) :
[]

if (diagnosticList.length) throw createTSError(diagnosticList)
if (diagnosticList.length) reportTSError(configDiagnosticList)

return [result.outputText, result.sourceMapText as string]
}
@@ -332,7 +345,7 @@ export function register (opts: Options = {}): Register {

const diagnosticList = filterDiagnostics(diagnostics, ignoreDiagnostics)

if (diagnosticList.length) throw createTSError(diagnosticList)
if (diagnosticList.length) reportTSError(diagnosticList)

if (output.emitSkipped) {
throw new TypeError(`${relative(cwd, fileName)}: Emit skipped`)

0 comments on commit 750ecca

Please sign in to comment.
You can’t perform that action at this time.