Skip to content

Commit

Permalink
Add TypeScript lazy load, replace noProject (#147)
Browse files Browse the repository at this point in the history
  • Loading branch information
blakeembrey committed Jul 19, 2016
1 parent 85745f5 commit f219c6b
Show file tree
Hide file tree
Showing 7 changed files with 209 additions and 172 deletions.
14 changes: 7 additions & 7 deletions README.md
Expand Up @@ -76,13 +76,13 @@ You can set options by passing them in before the script.
ts-node --compiler ntypescript --project src --ignoreWarnings 2304 hello-world.ts
```

* **--project, -P** Location to resolve `tsconfig.json` from (also `process.env.TS_NODE_PROJECT`)
* **--noProject, -n** Disable loading `tsconfig.json` (also `process.env.TS_NODE_NO_PROJECT`)
* **--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`)
* **--fast, -f** Use TypeScript's `transpileModule` mode (no type checking, but faster compilation) (also `process.env.TS_NODE_FAST`)
* **--project, -P** Path to resolve `tsconfig.json` from (or `false`) (also `process.env.TS_NODE_PROJECT`)
* **--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`)
* **--fast, -F** Use TypeScript's `transpileModule` mode (no type checking, but faster compilation) (also `process.env.TS_NODE_FAST`)
* **--lazy, -L** Lazily defer TypeScript initialization until first `.ts` file

### Programmatic Usage

Expand Down
1 change: 0 additions & 1 deletion package.json
Expand Up @@ -63,7 +63,6 @@
"make-error": "^1.1.1",
"minimist": "^1.2.0",
"source-map-support": "^0.4.0",
"tsconfig": "^3.0.0",
"xtend": "^4.0.0"
}
}
4 changes: 3 additions & 1 deletion register.js
@@ -1 +1,3 @@
require('./').register()
require('./').register({
lazy: true
})
45 changes: 24 additions & 21 deletions src/_bin.ts
Expand Up @@ -6,38 +6,38 @@ import minimist = require('minimist')
import chalk = require('chalk')
import { diffLines } from 'diff'
import { createScript } from 'vm'
import { register, VERSION, getFile, getVersion, getFileExists, TSError } from './index'
import { register, VERSION, getFile, getVersion, fileExists, TSError } from './index'

interface Argv {
eval?: string
print?: string
fast?: boolean
lazy?: boolean
version?: boolean
help?: boolean
compiler?: string
project?: string
ignoreWarnings?: string | string[]
disableWarnings?: boolean
noProject?: boolean
compilerOptions?: any
_: string[]
}

const strings = ['eval', 'print', 'compiler', 'project', 'ignoreWarnings']
const booleans = ['help', 'fast', 'version', 'disableWarnings', 'noProject']
const booleans = ['help', 'fast', 'lazy', 'version', 'disableWarnings']

const aliases: { [key: string]: string[] } = {
help: ['h'],
fast: ['f'],
fast: ['F'],
lazy: ['L'],
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']
compiler: ['C'],
ignoreWarnings: ['I', 'ignore-warnings'],
disableWarnings: ['D', 'disable-warnings'],
compilerOptions: ['O', 'compiler-options']
}

let stop = process.argv.length
Expand Down Expand Up @@ -98,7 +98,8 @@ const argv = minimist<Argv>(process.argv.slice(2, stop), {
})

if (argv.version) {
console.log(VERSION)
console.log(`ts-node v${VERSION}`)
console.log(`node ${process.version}`)
process.exit(0)
}

Expand All @@ -110,11 +111,13 @@ Options:
-e, --eval [code] Evaluate code
-p, --print [code] Evaluate code and print result
-c, --compiler [name] Specify a custom TypeScript compiler
-i, --ignoreWarnings [codes] Ignore TypeScript warnings by diagnostic code
-d, --disableWarnings Ignore every TypeScript warning
-n, --noProject Ignore the "tsconfig.json" project file
-P, --project [path] Specify the path to the TypeScript project
-C, --compiler [name] Specify a custom TypeScript compiler
-I, --ignoreWarnings [codes] Ignore TypeScript warnings by diagnostic code
-D, --disableWarnings Ignore every TypeScript warning
-P, --project [path] Path to TypeScript project (or \`false\`)
-O, --compilerOptions [opts] JSON compiler options to merge with compilation
-L, --lazy Lazily load TypeScript compilation
-F, --fast Run TypeScript compilation in transpile mode
`)

process.exit(0)
Expand Down Expand Up @@ -144,13 +147,13 @@ const isPrinted = argv.print != null
const service = register({
getFile: isEval ? getFileEval : getFile,
getVersion: isEval ? getVersionEval : getVersion,
getFileExists: isEval ? getFileExistsEval : getFileExists,
fileExists: isEval ? fileExistsEval : fileExists,
fast: argv.fast,
lazy: argv.lazy,
compiler: argv.compiler,
ignoreWarnings: list(argv.ignoreWarnings),
project: argv.project,
disableWarnings: argv.disableWarnings,
noProject: argv.noProject,
compilerOptions: argv.compilerOptions
})

Expand Down Expand Up @@ -246,7 +249,7 @@ function _eval (code: string, context: any) {

// Undo on TypeScript compilation errors.
try {
output = service.compile(EVAL_PATH)
output = service().compile(EVAL_PATH)
} catch (error) {
evalFile.input = undo

Expand Down Expand Up @@ -311,7 +314,7 @@ function startRepl () {
evalFile.input += identifier
evalFile.version++

const { name, comment } = service.getTypeInfo(EVAL_PATH, evalFile.input.length)
const { name, comment } = service().getTypeInfo(EVAL_PATH, evalFile.input.length)

;(repl as any).outputStream.write(`${chalk.bold(name)}\n${comment ? `${comment}\n` : ''}`)
;(repl as any).displayPrompt()
Expand Down Expand Up @@ -371,6 +374,6 @@ function getVersionEval (fileName: string) {
/**
* Get whether the file exists.
*/
function getFileExistsEval (fileName: string) {
return fileName === EVAL_PATH ? true : getFileExists(fileName)
function fileExistsEval (fileName: string) {
return fileName === EVAL_PATH ? true : fileExists(fileName)
}
12 changes: 6 additions & 6 deletions src/index.spec.ts
Expand Up @@ -6,9 +6,9 @@ import ts = require('typescript')
import proxyquire = require('proxyquire')
import { register, VERSION } from './index'

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

describe('ts-node', function () {
this.timeout(10000)
Expand All @@ -28,7 +28,7 @@ describe('ts-node', function () {
})

it('should execute cli with absolute path', function (done) {
exec(`${BIN_EXEC} "${join(cwd, '../tests/hello-world')}"`, function (err, stdout) {
exec(`${BIN_EXEC} "${join(testDir, 'hello-world')}"`, function (err, stdout) {
expect(err).to.not.exist
expect(stdout).to.equal('Hello, world!\n')

Expand All @@ -50,7 +50,7 @@ describe('ts-node', function () {
exec(
[
BIN_EXEC,
'-o "{\\\"allowJs\\\":true}"',
'-O "{\\\"allowJs\\\":true}"',
'-p "import { main } from \'./tests/allow-js/run\';main()"'
].join(' '),
function (err, stdout) {
Expand Down Expand Up @@ -125,7 +125,7 @@ describe('ts-node', function () {
})

it('should ignore all warnings', function (done) {
exec(`${BIN_EXEC} -d -p "x"`, function (err) {
exec(`${BIN_EXEC} -D -p "x"`, function (err) {
expect(err.message).to.contain('ReferenceError: x is not defined')

return done()
Expand Down Expand Up @@ -161,7 +161,7 @@ describe('ts-node', function () {
})

describe('register', function () {
register({ project: cwd })
register({ project: join(testDir, '..') })

it('should be able to require typescript', function () {
const m = require('../tests/module')
Expand Down

0 comments on commit f219c6b

Please sign in to comment.