Skip to content

Commit

Permalink
Add compiler option back and use require.resolve (#758)
Browse files Browse the repository at this point in the history
  • Loading branch information
blakeembrey committed Jan 21, 2019
1 parent abcb13b commit f148008
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 11 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ Create a new node.js configuration, add `-r ts-node/register` to node args and m
You can set options by passing them before the script path, via programmatic usage or via environment variables.

```sh
ts-node --project src/tsconfig.json hello-world.ts
ts-node --compiler ntypescript --project src/tsconfig.json hello-world.ts
```

### CLI Options
Expand All @@ -127,6 +127,7 @@ _Environment variable denoted in parentheses._
* `--cacheDirectory` Configure the output file cache directory (`TS_NODE_CACHE_DIRECTORY`)
* `-I, --ignore [pattern]` Override the path patterns to skip compilation (`TS_NODE_IGNORE`, default: `/node_modules/`)
* `-P, --project [path]` Path to TypeScript JSON project file (`TS_NODE_PROJECT`)
* `-C, --compiler [name]` Specify a custom TypeScript compiler (`TS_NODE_COMPILER`, default: `typescript`)
* `-D, --ignoreDiagnostics [code]` Ignore TypeScript warnings by diagnostic code (`TS_NODE_IGNORE_DIAGNOSTICS`)
* `-O, --compilerOptions [opts]` JSON object to merge with compiler options (`TS_NODE_COMPILER_OPTIONS`)
* `--files` Load files from `tsconfig.json` on startup (`TS_NODE_FILES`, default: `false`)
Expand Down
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"chai": "^4.0.1",
"istanbul": "^0.4.0",
"mocha": "^5.0.1",
"ntypescript": "^1.201507091536.1",
"proxyquire": "^2.0.0",
"react": "^16.0.0",
"rimraf": "^2.5.4",
Expand Down
45 changes: 35 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import yn = require('yn')
import arrify = require('arrify')
import { BaseError } from 'make-error'
import * as util from 'util'
import * as ts from 'typescript'
import * as _ts from 'typescript'

/**
* @internal
Expand All @@ -26,6 +26,28 @@ const debugFn = shouldDebug ?
} :
<T, U> (_: string, fn: (arg: T) => U) => fn

/**
* Common TypeScript interfaces between versions.
*/
export interface TSCommon {
version: typeof _ts.version
sys: typeof _ts.sys
ScriptSnapshot: typeof _ts.ScriptSnapshot
displayPartsToString: typeof _ts.displayPartsToString
createLanguageService: typeof _ts.createLanguageService
getDefaultLibFilePath: typeof _ts.getDefaultLibFilePath
getPreEmitDiagnostics: typeof _ts.getPreEmitDiagnostics
flattenDiagnosticMessageText: typeof _ts.flattenDiagnosticMessageText
transpileModule: typeof _ts.transpileModule
ModuleKind: typeof _ts.ModuleKind
ScriptTarget: typeof _ts.ScriptTarget
findConfigFile: typeof _ts.findConfigFile
readConfigFile: typeof _ts.readConfigFile
parseJsonConfigFileContent: typeof _ts.parseJsonConfigFileContent
formatDiagnostics: typeof _ts.formatDiagnostics
formatDiagnosticsWithColorAndContext: typeof _ts.formatDiagnosticsWithColorAndContext
}

/**
* Export the current version.
*/
Expand All @@ -48,7 +70,7 @@ export interface Options {
ignoreDiagnostics?: number | string | Array<number | string>
readFile?: (path: string) => string | undefined
fileExists?: (path: string) => boolean
transformers?: ts.CustomTransformers
transformers?: _ts.CustomTransformers
}

/**
Expand Down Expand Up @@ -142,7 +164,7 @@ export class TSError extends BaseError {
export interface Register {
cwd: string
extensions: string[]
ts: typeof ts
ts: TSCommon
compile (code: string, fileName: string, lineOffset?: number): string
getTypeInfo (code: string, fileName: string, position: number): TypeInfo
}
Expand Down Expand Up @@ -182,15 +204,17 @@ export function register (opts: Options = {}): Register {
const cwd = process.cwd()
const { compilerOptions, project, skipProject } = options
const typeCheck = options.typeCheck === true || options.transpileOnly !== true
const compiler = require.resolve(options.compiler || 'typescript', { paths: [cwd] })
const ts: typeof _ts = require(compiler)
const transformers = options.transformers || undefined
const readFile = options.readFile || ts.sys.readFile
const fileExists = options.fileExists || ts.sys.fileExists
const config = readConfig(cwd, fileExists, readFile, compilerOptions, project, skipProject)
const config = readConfig(cwd, ts, fileExists, readFile, compilerOptions, project, skipProject)
const configDiagnosticList = filterDiagnostics(config.errors, ignoreDiagnostics)
const extensions = ['.ts', '.tsx']
const fileNames = options.files ? config.fileNames : []

const diagnosticHost: ts.FormatDiagnosticsHost = {
const diagnosticHost: _ts.FormatDiagnosticsHost = {
getNewLine: () => EOL,
getCurrentDirectory: () => cwd,
getCanonicalFileName: (path) => path
Expand All @@ -200,7 +224,7 @@ export function register (opts: Options = {}): Register {
? ts.formatDiagnosticsWithColorAndContext
: ts.formatDiagnostics

function createTSError (diagnostics: ReadonlyArray<ts.Diagnostic>) {
function createTSError (diagnostics: ReadonlyArray<_ts.Diagnostic>) {
const diagnosticText = formatDiagnostics(diagnostics, diagnosticHost)
const diagnosticCodes = diagnostics.map(x => x.code)
return new TSError(diagnosticText, diagnosticCodes)
Expand Down Expand Up @@ -398,7 +422,7 @@ function registerExtension (
/**
* Do post-processing on config options to support `ts-node`.
*/
function fixConfig (config: ts.ParsedCommandLine) {
function fixConfig (ts: TSCommon, config: _ts.ParsedCommandLine) {
// Delete options that *should not* be passed through.
delete config.options.out
delete config.options.outFile
Expand All @@ -425,12 +449,13 @@ function fixConfig (config: ts.ParsedCommandLine) {
*/
function readConfig (
cwd: string,
ts: TSCommon,
fileExists: (path: string) => boolean,
readFile: (path: string) => string | undefined,
compilerOptions?: object,
project?: string | null,
noProject?: boolean | null
): ts.ParsedCommandLine {
): _ts.ParsedCommandLine {
let config: any = { compilerOptions: {} }
let basePath = normalizeSlashes(cwd)
let configFileName: string | undefined = undefined
Expand Down Expand Up @@ -461,7 +486,7 @@ function readConfig (
// Override default configuration options `ts-node` requires.
config.compilerOptions = Object.assign({}, config.compilerOptions, compilerOptions, TS_NODE_COMPILER_OPTIONS)

return fixConfig(ts.parseJsonConfigFileContent(config, ts.sys, basePath, undefined, configFileName))
return fixConfig(ts, ts.parseJsonConfigFileContent(config, ts.sys, basePath, undefined, configFileName))
}

/**
Expand Down Expand Up @@ -494,6 +519,6 @@ function updateSourceMap (sourceMapText: string, fileName: string) {
/**
* Filter diagnostics.
*/
function filterDiagnostics (diagnostics: ts.Diagnostic[], ignore: number[]) {
function filterDiagnostics (diagnostics: _ts.Diagnostic[], ignore: number[]) {
return diagnostics.filter(x => ignore.indexOf(x.code) === -1)
}

0 comments on commit f148008

Please sign in to comment.