3
3
import { join , resolve } from 'path'
4
4
import { start , Recoverable } from 'repl'
5
5
import { inspect } from 'util'
6
- import arrify = require ( 'arrify' )
7
6
import Module = require ( 'module' )
8
- import minimist = require ( 'minimist ' )
7
+ import arg = require ( 'arg ' )
9
8
import { diffLines } from 'diff'
10
9
import { Script } from 'vm'
11
10
import { readFileSync , statSync } from 'fs'
12
11
import { register , VERSION , DEFAULTS , TSError , parse } from './index'
13
12
14
- interface Argv {
13
+ const args = arg ( {
15
14
// 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 ] ,
36
18
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
69
50
} )
70
51
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 ) {
72
69
console . log ( `
73
70
Usage: ts-node [options] [ -e script | script.ts ] [arguments]
74
71
75
72
Options:
76
73
77
74
-e, --eval [code] Evaluate code
78
- -p, --print [code] Evaluate code and print result
75
+ -p, --print Print result of \`--eval\`
79
76
-r, --require [path] Require a node module before execution
80
77
81
78
-h, --help Print CLI usage
@@ -85,8 +82,8 @@ Options:
85
82
-I, --ignore [pattern] Override the path patterns to skip compilation
86
83
-P, --project [path] Path to TypeScript JSON project file
87
84
-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
90
87
91
88
--files Load files from \`tsconfig.json\` on startup
92
89
--pretty Use pretty diagnostic formatter
@@ -97,38 +94,43 @@ Options:
97
94
process . exit ( 0 )
98
95
}
99
96
97
+ // Output project information.
98
+ if ( version === 1 ) {
99
+ console . log ( `v${ VERSION } ` )
100
+ process . exit ( 0 )
101
+ }
102
+
100
103
const 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
104
106
105
107
// Register the TypeScript compiler instance.
106
108
const 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
120
122
} )
121
123
122
124
// Output project information.
123
- if ( argv . version ) {
125
+ if ( version >= 2 ) {
124
126
console . log ( `ts-node v${ VERSION } ` )
125
127
console . log ( `node ${ process . version } ` )
126
- console . log ( `typescript v${ service . ts . version } ` )
128
+ console . log ( `compiler v${ service . ts . version } ` )
127
129
process . exit ( 0 )
128
130
}
129
131
130
132
// Require specified modules before start-up.
131
- ( Module as any ) . _preloadModules ( arrify ( argv . require ) )
133
+ if ( args [ '--require' ] ) ( Module as any ) . _preloadModules ( args [ '-- require' ] )
132
134
133
135
/**
134
136
* Eval helpers.
@@ -138,16 +140,16 @@ const EVAL_PATH = join(cwd, EVAL_FILENAME)
138
140
const EVAL_INSTANCE = { input : '' , output : '' , version : 0 , lines : 0 }
139
141
140
142
// 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 )
143
145
} 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 ) )
146
148
process . execArgv . unshift ( __filename )
147
149
Module . runMain ( )
148
150
} else {
149
151
// Piping of execution _only_ occurs when no other script is specified.
150
- if ( ( process . stdin as any ) . isTTY ) {
152
+ if ( process . stdin . isTTY ) {
151
153
startRepl ( )
152
154
} else {
153
155
let code = ''
0 commit comments