Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: TypeStrong/ts-node
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v3.1.0
Choose a base ref
...
head repository: TypeStrong/ts-node
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v3.2.0
Choose a head ref
  • 7 commits
  • 5 files changed
  • 2 contributors

Commits on Jun 23, 2017

  1. Copy the full SHA
    9bc3dd8 View commit details

Commits on Jun 28, 2017

  1. Copy the full SHA
    82effb2 View commit details

Commits on Jul 4, 2017

  1. Copy the full SHA
    394ddb8 View commit details

Commits on Jul 5, 2017

  1. Copy the full SHA
    3f0d975 View commit details
  2. Copy the full SHA
    c60d3a7 View commit details
  3. Copy the full SHA
    2e03f54 View commit details
  4. 3.2.0

    blakeembrey committed Jul 5, 2017
    Copy the full SHA
    d90ffba View commit details
Showing with 80 additions and 50 deletions.
  1. +3 −4 package.json
  2. +6 −13 src/_bin.ts
  3. +19 −0 src/index.spec.ts
  4. +47 −33 src/index.ts
  5. +5 −0 tests/compiler-error.ts
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"name": "ts-node",
"version": "3.1.0",
"preferGlobal": true,
"version": "3.2.0",
"description": "TypeScript execution environment and REPL for node",
"main": "dist/index.js",
"bin": {
@@ -59,12 +58,12 @@
"semver": "^5.1.0",
"tslint": "^5.0.0",
"tslint-config-standard": "^6.0.1",
"typescript": "^2.1.4",
"typescript": "^2.4.1",
"typings": "^2.0.0"
},
"dependencies": {
"arrify": "^1.0.0",
"chalk": "^1.1.1",
"chalk": "^2.0.0",
"diff": "^3.1.0",
"make-error": "^1.1.1",
"minimist": "^1.2.0",
19 changes: 6 additions & 13 deletions src/_bin.ts
Original file line number Diff line number Diff line change
@@ -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
@@ -101,7 +101,9 @@ const argv = minimist<Argv>(process.argv.slice(2, stop), {
boolean: booleans,
alias: aliases,
default: {
cache: true
cache: null,
fast: null,
disableWarnings: null
}
})

@@ -210,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)
}

@@ -224,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.
*/
@@ -332,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
19 changes: 19 additions & 0 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
@@ -100,6 +100,25 @@ describe('ts-node', function () {
)
})

it('should be able to disable warnings from environment', function (done) {
exec(
`${BIN_EXEC} tests/compiler-error`,
{
env: {
PATH: process.env.PATH,
TS_NODE_DISABLE_WARNINGS: true
}
},
function (err) {
expect(err.message).to.match(
/TypeError: (?:(?:undefined|str\.toUpperCase) is not a function|.*has no method \'toUpperCase\')/
)

return done()
}
)
})

it('should work with source maps', function (done) {
exec(`${BIN_EXEC} tests/throw`, function (err) {
expect(err.message).to.contain([
80 changes: 47 additions & 33 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { relative, basename, extname, resolve, dirname, join } from 'path'
import { readdirSync, writeFileSync, readFileSync, statSync } from 'fs'
import { 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')
@@ -11,6 +12,16 @@ import * as TS from 'typescript'
import { loadSync } from 'tsconfig'

const pkg = require('../package.json')
const shouldDebug = yn(process.env.TS_NODE_DEBUG)
const debug = shouldDebug ? console.log.bind(console, 'ts-node') : () => undefined
const debugFn = shouldDebug ?
<T, U> (key: string, fn: (arg: T) => U) => {
return (x: T) => {
debug(key, x)
return fn(x)
}
} :
<T, U> (_: string, fn: (arg: T) => U) => fn

/**
* Common TypeScript interfaces between versions.
@@ -52,18 +63,18 @@ export interface Options {
ignore?: boolean | string | string[]
ignoreWarnings?: number | string | Array<number | string>
disableWarnings?: boolean | null
getFile?: (fileName: string) => string
fileExists?: (fileName: string) => boolean
getFile?: (path: string) => string
fileExists?: (path: string) => boolean
compilerOptions?: any
}

/**
* Track the project information.
*/
interface Cache {
contents: { [fileName: string]: string }
versions: { [fileName: string]: number }
outputs: { [fileName: string]: string }
contents: { [path: string]: string }
versions: { [path: string]: number }
outputs: { [path: string]: string }
}

/**
@@ -269,11 +280,11 @@ export function register (options: Options = {}): Register {

return ts.ScriptSnapshot.fromString(cache.contents[fileName])
},
getDirectories: getDirectories,
directoryExists: directoryExists,
fileExists: fileExists,
readFile: getFile,
readDirectory: ts.sys.readDirectory,
fileExists: debugFn('fileExists', fileExists),
readFile: debugFn('getFile', getFile),
readDirectory: debugFn('readDirectory', ts.sys.readDirectory),
getDirectories: debugFn('getDirectories', ts.sys.getDirectories),
directoryExists: debugFn('directoryExists', ts.sys.directoryExists),
getNewLine: () => EOL,
getCurrentDirectory: () => cwd,
getCompilationSettings: () => config.options,
@@ -376,6 +387,8 @@ function registerExtension (
const _compile = m._compile

m._compile = function (code, fileName) {
debug('module._compile', fileName)

return _compile.call(this, register.compile(code, fileName), fileName)
}

@@ -453,6 +466,8 @@ function readThrough (
) {
if (shouldCache === false) {
return function (code: string, fileName: string, lineOffset?: number) {
debug('readThrough', fileName)

const [value, sourceMap] = compile(code, fileName, lineOffset)
const output = updateOutput(value, fileName, sourceMap)

@@ -466,6 +481,8 @@ function readThrough (
mkdirp.sync(cachedir)

return function (code: string, fileName: string, lineOffset?: number) {
debug('readThrough', fileName)

const cachePath = join(cachedir, getCacheName(code, fileName))
const extension = getExtension(fileName)
const outputPath = `${cachePath}${extension}`
@@ -538,24 +555,6 @@ export function fileExists (fileName: string): boolean {
}
}

/**
* Get directories within a directory.
*/
export function getDirectories (path: string): string[] {
return readdirSync(path).filter(name => directoryExists(join(path, name)))
}

/**
* Check if a directory exists.
*/
export function directoryExists (path: string): boolean {
try {
return statSync(path).isDirectory()
} catch (err) {
return false
}
}

/**
* Get the file from the file system.
*/
@@ -599,16 +598,22 @@ export function formatDiagnostic (
lineOffset: number
): TSDiagnostic {
const messageText = ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n')
const { code } = diagnostic

if (diagnostic.file) {
const path = relative(cwd, diagnostic.file.fileName)
const { line, character } = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start)
const message = `${path} (${line + 1 + lineOffset},${character + 1}): ${messageText} (${diagnostic.code})`

return { message, code: diagnostic.code }
if (diagnostic.start) {
const { line, character } = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start)
const message = `${path} (${line + 1 + lineOffset},${character + 1}): ${messageText} (${code})`

return { message, code }
}

return { message: `${path}: ${messageText} (${code})`, code }
}

return { message: `${messageText} (${diagnostic.code})`, code: diagnostic.code }
return { message: `${messageText} (${code})`, code }
}

/**
@@ -625,3 +630,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')}`
}
5 changes: 5 additions & 0 deletions tests/compiler-error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function upper (str: string) {
return str.toUpperCase()
}

upper(10)