Skip to content

Commit 8c4da24

Browse files
authored
Add support for --require flag like node CLI (#181)
1 parent 44c389e commit 8c4da24

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

custom_typings/node.d.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,17 @@ declare module 'module' {
99
static runMain (): void
1010
static wrap (code: string): string
1111
static _nodeModulePaths (path: string): string[]
12+
static _load (request: string, parent?: Module, isMain?: boolean): any
13+
static _resolveFilename (request: string, parent?: Module, isMain?: boolean): string
14+
static _extensions: { [ext: string]: (m: Module, fileName: string) => any }
1215

13-
constructor (filename: string)
16+
constructor (filename: string, parent?: Module)
1417

18+
parent: Module
1519
filename: string
1620
paths: string[]
1721
exports: any
22+
loaded: boolean
1823
require (module: string): any
1924
}
2025

src/_bin.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,14 @@ interface Argv {
2121
help?: boolean
2222
compiler?: string
2323
project?: string
24+
require?: string | string[]
2425
ignoreWarnings?: string | string[]
2526
disableWarnings?: boolean
2627
compilerOptions?: any
2728
_: string[]
2829
}
2930

30-
const strings = ['eval', 'print', 'compiler', 'project', 'ignoreWarnings', 'cacheDirectory']
31+
const strings = ['eval', 'print', 'compiler', 'project', 'ignoreWarnings', 'require', 'cacheDirectory']
3132
const booleans = ['help', 'fast', 'lazy', 'version', 'disableWarnings', 'cache']
3233

3334
const aliases: { [key: string]: string[] } = {
@@ -39,6 +40,7 @@ const aliases: { [key: string]: string[] } = {
3940
print: ['p'],
4041
project: ['P'],
4142
compiler: ['C'],
43+
require: ['r'],
4244
cacheDirectory: ['cache-directory'],
4345
ignoreWarnings: ['I', 'ignore-warnings'],
4446
disableWarnings: ['D', 'disable-warnings'],
@@ -119,6 +121,7 @@ Options:
119121
120122
-e, --eval [code] Evaluate code
121123
-p, --print [code] Evaluate code and print result
124+
-r, --require [path] Require a node module for execution
122125
-C, --compiler [name] Specify a custom TypeScript compiler
123126
-I, --ignoreWarnings [code] Ignore TypeScript warnings by diagnostic code
124127
-D, --disableWarnings Ignore every TypeScript warning
@@ -167,6 +170,12 @@ const EVAL_PATH = join(cwd, EVAL_FILENAME)
167170
// Store eval contents for in-memory lookups.
168171
const evalFile = { input: '', output: '', version: 0 }
169172

173+
// Require specified modules before start-up.
174+
for (const id of arrify(argv.require)) {
175+
Module._load(id)
176+
}
177+
178+
// Execute the main contents (either eval, script or piped).
170179
if (isEvalScript) {
171180
evalAndExit(code, isPrinted)
172181
} else {

src/index.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,15 @@ describe('ts-node', function () {
158158
return done()
159159
})
160160
})
161+
162+
it('should support require flags', function (done) {
163+
exec(`${BIN_EXEC} -r ./tests/hello-world -p "console.log('success')"`, function (err, stdout) {
164+
expect(err).to.not.exist
165+
expect(stdout).to.equal('Hello, world!\nsuccess\nundefined\n')
166+
167+
return done()
168+
})
169+
})
161170
})
162171

163172
describe('register', function () {

0 commit comments

Comments
 (0)