Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export printError utility #376

Merged
merged 1 commit into from
Jul 5, 2017
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
15 changes: 3 additions & 12 deletions src/_bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import minimist = require('minimist')
import chalk = require('chalk')
import { diffLines } from 'diff'
import { Script } from 'vm'
import { register, VERSION, getFile, fileExists, TSError, parse } from './index'
import { register, VERSION, getFile, fileExists, TSError, parse, printError } from './index'

interface Argv {
eval?: string
Expand Down Expand Up @@ -212,7 +212,7 @@ function evalAndExit (code: string, isPrinted: boolean) {
result = _eval(code, global)
} catch (error) {
if (error instanceof TSError) {
console.error(print(error))
console.error(printError(error))
process.exit(1)
}

Expand All @@ -226,15 +226,6 @@ function evalAndExit (code: string, isPrinted: boolean) {
process.exit(0)
}

/**
* Stringify the `TSError` instance.
*/
function print (error: TSError) {
const title = `${chalk.red('⨯')} Unable to compile TypeScript`

return `${chalk.bold(title)}\n${error.diagnostics.map(x => x.message).join('\n')}`
}

/**
* Evaluate the code snippet.
*/
Expand Down Expand Up @@ -334,7 +325,7 @@ function replEval (code: string, context: any, _filename: string, callback: (err
if (typeof Recoverable === 'function' && isRecoverable(error)) {
err = new Recoverable(error)
} else {
err = print(error)
err = printError(error)
}
} else {
err = error
Expand Down
10 changes: 10 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { relative, basename, extname, resolve, dirname, join } from 'path'
import { readdirSync, writeFileSync, readFileSync, statSync } from 'fs'
import { EOL, tmpdir, homedir } from 'os'
import sourceMapSupport = require('source-map-support')
import chalk = require('chalk')
import mkdirp = require('mkdirp')
import crypto = require('crypto')
import yn = require('yn')
Expand Down Expand Up @@ -631,3 +632,12 @@ export class TSError extends BaseError {
}

}

/**
* Stringify the `TSError` instance.
*/
export function printError (error: TSError) {
const title = `${chalk.red('⨯')} Unable to compile TypeScript`

return `${chalk.bold(title)}\n${error.diagnostics.map(x => x.message).join('\n')}`
}