33import { join , resolve } from 'path'
44import { start , Recoverable } from 'repl'
55import { inspect } from 'util'
6- import arrify = require ( 'arrify' )
76import Module = require ( 'module' )
8- import minimist = require ( 'minimist ' )
7+ import arg = require ( 'arg ' )
98import { diffLines } from 'diff'
109import { Script } from 'vm'
1110import { readFileSync , statSync } from 'fs'
1211import { register , VERSION , DEFAULTS , TSError , parse } from './index'
1312
14- interface Argv {
13+ const args = arg ( {
1514 // Node.js-like options.
16- eval ? : string
17- print ? : string
18- require ?: string | string [ ]
19- // CLI options.
20- help ?: boolean
21- version ? : boolean
22- // Register options.
23- pretty ? : boolean
24- typeCheck ? : boolean
25- transpileOnly ? : boolean
26- files ? : boolean
27- compiler ? : string
28- ignore ?: string | string [ ]
29- project ?: string
30- skipIgnore ? : boolean
31- skipProject ? : boolean
32- ignoreDiagnostics ?: string | string [ ]
33- compilerOptions ?: string
34- _: string [ ]
35- }
15+ '--eval' : String ,
16+ '--print' : Boolean ,
17+ '--require' : [ String ] ,
3618
37- const argv = minimist < Argv > ( process . argv . slice ( 2 ) , {
38- stopEarly : true ,
39- string : [ 'eval' , 'print' , 'compiler' , 'project' , 'ignoreDiagnostics' , 'require' , 'ignore' ] ,
40- boolean : [ 'help' , 'transpileOnly' , 'typeCheck' , 'version' , 'files' , 'pretty' , 'skipProject' , 'skipIgnore' ] ,
41- alias : {
42- eval : [ 'e' ] ,
43- print : [ 'p' ] ,
44- require : [ 'r' ] ,
45- help : [ 'h' ] ,
46- version : [ 'v' ] ,
47- typeCheck : [ 'type-check' ] ,
48- transpileOnly : [ 'T' , 'transpile-only' ] ,
49- ignore : [ 'I' ] ,
50- project : [ 'P' ] ,
51- skipIgnore : [ 'skip-ignore' ] ,
52- skipProject : [ 'skip-project' ] ,
53- compiler : [ 'C' ] ,
54- ignoreDiagnostics : [ 'D' , 'ignore-diagnostics' ] ,
55- compilerOptions : [ 'O' , 'compiler-options' ]
56- } ,
57- default : {
58- files : DEFAULTS . files ,
59- pretty : DEFAULTS . pretty ,
60- typeCheck : DEFAULTS . typeCheck ,
61- transpileOnly : DEFAULTS . transpileOnly ,
62- ignore : DEFAULTS . ignore ,
63- project : DEFAULTS . project ,
64- skipIgnore : DEFAULTS . skipIgnore ,
65- skipProject : DEFAULTS . skipProject ,
66- compiler : DEFAULTS . compiler ,
67- ignoreDiagnostics : DEFAULTS . ignoreDiagnostics
68- }
19+ // CLI options.
20+ '--files' : Boolean ,
21+ '--help' : Boolean ,
22+ '--version' : arg . COUNT ,
23+
24+ // Project options.
25+ '--compiler' : String ,
26+ '--compiler-options' : parse ,
27+ '--project' : String ,
28+ '--ignore-diagnostics' : [ String ] ,
29+ '--ignore' : [ String ] ,
30+ '--transpile-only' : Boolean ,
31+ '--type-check' : Boolean ,
32+ '--pretty' : Boolean ,
33+ '--skip-project' : Boolean ,
34+ '--skip-ignore' : Boolean ,
35+
36+ // Aliases.
37+ '-e' : '--eval' ,
38+ '-p' : '--print' ,
39+ '-r' : '--require' ,
40+ '-h' : '--help' ,
41+ '-v' : '--version' ,
42+ '-T' : '--transpile-only' ,
43+ '-I' : '--ignore' ,
44+ '-P' : '--project' ,
45+ '-C' : '--compiler' ,
46+ '-D' : '--ignore-diagnostics' ,
47+ '-O' : '--compiler-options'
48+ } , {
49+ stopAtPositional : true
6950} )
7051
71- if ( argv . help ) {
52+ const {
53+ '--help' : help = false ,
54+ '--version' : version = 0 ,
55+ '--files' : files = DEFAULTS . files ,
56+ '--compiler' : compiler = DEFAULTS . compiler ,
57+ '--compiler-options' : compilerOptions = DEFAULTS . compilerOptions ,
58+ '--project' : project = DEFAULTS . project ,
59+ '--ignore-diagnostics' : ignoreDiagnostics = DEFAULTS . ignoreDiagnostics ,
60+ '--ignore' : ignore = DEFAULTS . ignore ,
61+ '--transpile-only' : transpileOnly = DEFAULTS . transpileOnly ,
62+ '--type-check' : typeCheck = DEFAULTS . typeCheck ,
63+ '--pretty' : pretty = DEFAULTS . pretty ,
64+ '--skip-project' : skipProject = DEFAULTS . skipProject ,
65+ '--skip-ignore' : skipIgnore = DEFAULTS . skipIgnore
66+ } = args
67+
68+ if ( help ) {
7269 console . log ( `
7370Usage: ts-node [options] [ -e script | script.ts ] [arguments]
7471
7572Options:
7673
7774 -e, --eval [code] Evaluate code
78- -p, --print [code] Evaluate code and print result
75+ -p, --print Print result of \`--eval\`
7976 -r, --require [path] Require a node module before execution
8077
8178 -h, --help Print CLI usage
@@ -85,8 +82,8 @@ Options:
8582 -I, --ignore [pattern] Override the path patterns to skip compilation
8683 -P, --project [path] Path to TypeScript JSON project file
8784 -C, --compiler [name] Specify a custom TypeScript compiler
88- -D, --ignoreDiagnostics [code] Ignore TypeScript warnings by diagnostic code
89- -O, --compilerOptions [opts] JSON object to merge with compiler options
85+ -D, --ignore-diagnostics [code] Ignore TypeScript warnings by diagnostic code
86+ -O, --compiler-options [opts] JSON object to merge with compiler options
9087
9188 --files Load files from \`tsconfig.json\` on startup
9289 --pretty Use pretty diagnostic formatter
@@ -97,38 +94,43 @@ Options:
9794 process . exit ( 0 )
9895}
9996
97+ // Output project information.
98+ if ( version === 1 ) {
99+ console . log ( `v${ VERSION } ` )
100+ process . exit ( 0 )
101+ }
102+
100103const cwd = process . cwd ( )
101- const code = argv . eval === undefined ? argv . print : argv . eval
102- const isEval = typeof argv . eval === 'string' || ! ! argv . print // Minimist struggles with empty strings.
103- const isPrinted = argv . print !== undefined
104+ const code = args [ '--eval' ]
105+ const isPrinted = args [ '--print' ] !== undefined
104106
105107// Register the TypeScript compiler instance.
106108const service = register ( {
107- files : argv . files ,
108- pretty : argv . pretty ,
109- typeCheck : argv . typeCheck ,
110- transpileOnly : argv . transpileOnly ,
111- ignore : argv . ignore ,
112- project : argv . project ,
113- skipIgnore : argv . skipIgnore ,
114- skipProject : argv . skipProject ,
115- compiler : argv . compiler ,
116- ignoreDiagnostics : argv . ignoreDiagnostics ,
117- compilerOptions : parse ( argv . compilerOptions ) || DEFAULTS . compilerOptions ,
118- readFile : isEval ? readFileEval : undefined ,
119- fileExists : isEval ? fileExistsEval : undefined
109+ files,
110+ pretty,
111+ typeCheck,
112+ transpileOnly,
113+ ignore,
114+ project,
115+ skipIgnore,
116+ skipProject,
117+ compiler,
118+ ignoreDiagnostics,
119+ compilerOptions,
120+ readFile : code ? readFileEval : undefined ,
121+ fileExists : code ? fileExistsEval : undefined
120122} )
121123
122124// Output project information.
123- if ( argv . version ) {
125+ if ( version >= 2 ) {
124126 console . log ( `ts-node v${ VERSION } ` )
125127 console . log ( `node ${ process . version } ` )
126- console . log ( `typescript v${ service . ts . version } ` )
128+ console . log ( `compiler v${ service . ts . version } ` )
127129 process . exit ( 0 )
128130}
129131
130132// Require specified modules before start-up.
131- ( Module as any ) . _preloadModules ( arrify ( argv . require ) )
133+ if ( args [ '--require' ] ) ( Module as any ) . _preloadModules ( args [ '-- require' ] )
132134
133135/**
134136 * Eval helpers.
@@ -138,16 +140,16 @@ const EVAL_PATH = join(cwd, EVAL_FILENAME)
138140const EVAL_INSTANCE = { input : '' , output : '' , version : 0 , lines : 0 }
139141
140142// Execute the main contents (either eval, script or piped).
141- if ( isEval ) {
142- evalAndExit ( code as string , isPrinted )
143+ if ( code ) {
144+ evalAndExit ( code , isPrinted )
143145} else {
144- if ( argv . _ . length ) {
145- process . argv = [ 'node' ] . concat ( resolve ( cwd , argv . _ [ 0 ] ) ) . concat ( argv . _ . slice ( 1 ) )
146+ if ( args . _ . length ) {
147+ process . argv = [ 'node' ] . concat ( resolve ( cwd , args . _ [ 0 ] ) ) . concat ( args . _ . slice ( 1 ) )
146148 process . execArgv . unshift ( __filename )
147149 Module . runMain ( )
148150 } else {
149151 // Piping of execution _only_ occurs when no other script is specified.
150- if ( ( process . stdin as any ) . isTTY ) {
152+ if ( process . stdin . isTTY ) {
151153 startRepl ( )
152154 } else {
153155 let code = ''
0 commit comments