Skip to content

Commit

Permalink
Add compilerOptions flag, tweak file structure
Browse files Browse the repository at this point in the history
  • Loading branch information
blakeembrey committed Jun 11, 2016
1 parent 5ce9c10 commit 17fd10d
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 25 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ ts-node --compiler ntypescript --project src --ignoreWarnings 2304 hello-world.t
* **--compiler, -c** Use a custom, require-able TypeScript compiler compatible with `typescript@>=1.5.0-alpha` (also `process.env.TS_NODE_COMPILER`)
* **--ignoreWarnings, -i** Set an array of TypeScript diagnostic codes to ignore (also `process.env.TS_NODE_IGNORE_WARNINGS`)
* **--disableWarnings, -d** Ignore all TypeScript errors (also `process.env.TS_NODE_DISABLE_WARNINGS`)
* **--compilerOptions, -o** Set compiler options using JSON (E.g. `--compilerOptions '{"target":"es6"}'`) (also `process.env.TS_NODE_COMPILER_OPTIONS`)

### Programmatic Usage

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
"version": "0.8.0",
"preferGlobal": true,
"description": "TypeScript execution environment and REPL for node",
"main": "dist/ts-node.js",
"main": "dist/index.js",
"bin": {
"ts-node": "dist/bin/ts-node.js"
"ts-node": "dist/bin.js"
},
"files": [
"dist/",
Expand Down
91 changes: 73 additions & 18 deletions src/bin/ts-node.ts → src/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import minimist = require('minimist')
import chalk = require('chalk')
import { diffLines } from 'diff'
import { createScript } from 'vm'
import { register, VERSION, getFile, getVersion, TSError } from '../ts-node'
import { register, VERSION, getFile, getVersion, TSError } from './index'

interface Argv {
eval?: string
Expand All @@ -20,23 +20,76 @@ interface Argv {
ignoreWarnings?: string | string[]
disableWarnings?: boolean
noProject?: boolean
compilerOptions?: any
_: string[]
}

const argv = minimist<Argv>(process.argv.slice(2), {
stopEarly: true,
string: ['eval', 'print', 'compiler', 'project', 'ignoreWarnings'],
boolean: ['help', 'version', 'disableWarnings', 'noProject'],
alias: {
v: ['version'],
e: ['eval'],
p: ['print'],
P: ['project'],
c: ['compiler'],
i: ['ignoreWarnings', 'ignore-warnings'],
d: ['disableWarnings', 'disable-warnings'],
n: ['noProject', 'no-project']
const strings = ['eval', 'print', 'compiler', 'project', 'ignoreWarnings']
const booleans = ['help', 'version', 'disableWarnings', 'noProject']

const aliases: { [key: string]: string[] } = {
help: ['h'],
version: ['v'],
eval: ['e'],
print: ['p'],
project: ['P'],
compiler: ['c'],
ignoreWarnings: ['i', 'ignore-warnings'],
disableWarnings: ['d', 'disable-warnings'],
noProject: ['n', 'no-project'],
compilerOptions: ['o', 'compiler-options']
}

let stop = process.argv.length

function isFlagOnly (arg: string) {
const name = arg.replace(/^--?/, '')

for (const bool of booleans) {
if (name === bool) {
return true
}

const alias = aliases[name]

if (alias) {
for (const other of alias) {
if (other === name) {
return true
}
}
}
}

return false
}

// Hack around known subarg issue with `stopEarly`.
for (let i = 2; i < process.argv.length; i++) {
const arg = process.argv[i]
const next = process.argv[i + 1]

if (/^\[/.test(arg) || /\]$/.test(arg)) {
continue
}

if (/^-/.test(arg)) {
// Skip next argument.
if (!isFlagOnly(arg) && !/^-/.test(next)) {
i++
}

continue
}

stop = i
break
}

const argv = minimist<Argv>(process.argv.slice(2, stop), {
string: strings,
boolean: booleans,
alias: aliases
})

if (argv.version) {
Expand All @@ -58,6 +111,7 @@ Options:
-n, --noProject Ignore the "tsconfig.json" project file
-P, --project [path] Specify the path to the TypeScript project
`)

process.exit(0)
}

Expand All @@ -78,7 +132,7 @@ process.emit = function (type, error): boolean {
const cwd = process.cwd()
const code = argv.eval == null ? argv.print : argv.eval
const isEvalScript = typeof argv.eval === 'string' || !!argv.print // Minimist struggles with empty strings.
const isEval = isEvalScript || argv._.length === 0
const isEval = isEvalScript || stop === process.argv.length
const isPrinted = argv.print != null

// Register the TypeScript compiler instance.
Expand All @@ -89,7 +143,8 @@ const service = register({
ignoreWarnings: list(argv.ignoreWarnings),
project: argv.project,
disableWarnings: argv.disableWarnings,
noProject: argv.noProject
noProject: argv.noProject,
compilerOptions: argv.compilerOptions
})

// TypeScript files must always end with `.ts`.
Expand All @@ -102,8 +157,8 @@ const evalFile = { input: '', output: '', version: 0 }
if (isEvalScript) {
evalAndExit(code, isPrinted)
} else {
if (argv._.length) {
const args = argv._.slice()
if (stop < process.argv.length) {
const args = process.argv.slice(stop)
args[0] = resolve(cwd, args[0])
process.argv = ['node'].concat(args)
process.execArgv.unshift(__filename)
Expand Down
4 changes: 2 additions & 2 deletions src/ts-node.spec.ts → src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { expect } from 'chai'
import { exec } from 'child_process'
import { join, normalize } from 'path'
import proxyquire = require('proxyquire')
import { register, VERSION } from './ts-node'
import { register, VERSION } from './index'

const cwd = join(__dirname, '../tests')
const EXEC_PATH = join(__dirname, '../dist/bin/ts-node')
const EXEC_PATH = join(__dirname, '../dist/bin')
const BIN_EXEC = `node ${EXEC_PATH} --project "${cwd}"`

describe('ts-node', function () {
Expand Down
7 changes: 7 additions & 0 deletions src/ts-node.ts → src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export interface Options {
disableWarnings?: boolean
getFile?: (fileName: string) => string
getVersion?: (fileName: string) => string
compilerOptions?: any
}

/**
Expand All @@ -91,6 +92,7 @@ function readConfig (options: Options, cwd: string, ts: TSCommon) {
module: 'commonjs'
},
result.config.compilerOptions,
options.compilerOptions,
{
sourceMap: false,
inlineSourceMap: true,
Expand Down Expand Up @@ -144,6 +146,11 @@ export function register (opts?: Options) {
options.compiler = options.compiler || 'typescript'
options.ignoreWarnings = arrify(options.ignoreWarnings).map(Number)

// Parse compiler options as JSON.
options.compilerOptions = typeof options.compilerOptions === 'string' ?
JSON.parse(options.compilerOptions) :
options.compilerOptions

const ts: typeof TS = require(options.compiler)
const config = readConfig(options, cwd, ts)

Expand Down
6 changes: 3 additions & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
"sourceMap": true
},
"files": [
"src/ts-node.ts",
"src/ts-node.spec.ts",
"src/bin/ts-node.ts",
"src/bin.ts",
"src/index.ts",
"src/index.spec.ts",
"typings/index.d.ts"
]
}

0 comments on commit 17fd10d

Please sign in to comment.