diff --git a/custom_typings/node.d.ts b/custom_typings/node.d.ts index 9b10ab78f..6168080b0 100644 --- a/custom_typings/node.d.ts +++ b/custom_typings/node.d.ts @@ -9,12 +9,17 @@ declare module 'module' { static runMain (): void static wrap (code: string): string static _nodeModulePaths (path: string): string[] + static _load (request: string, parent?: Module, isMain?: boolean): any + static _resolveFilename (request: string, parent?: Module, isMain?: boolean): string + static _extensions: { [ext: string]: (m: Module, fileName: string) => any } - constructor (filename: string) + constructor (filename: string, parent?: Module) + parent: Module filename: string paths: string[] exports: any + loaded: boolean require (module: string): any } diff --git a/src/_bin.ts b/src/_bin.ts index ceb4591d0..907fd6a5e 100644 --- a/src/_bin.ts +++ b/src/_bin.ts @@ -21,13 +21,14 @@ interface Argv { help?: boolean compiler?: string project?: string + require?: string | string[] ignoreWarnings?: string | string[] disableWarnings?: boolean compilerOptions?: any _: string[] } -const strings = ['eval', 'print', 'compiler', 'project', 'ignoreWarnings', 'cacheDirectory'] +const strings = ['eval', 'print', 'compiler', 'project', 'ignoreWarnings', 'require', 'cacheDirectory'] const booleans = ['help', 'fast', 'lazy', 'version', 'disableWarnings', 'cache'] const aliases: { [key: string]: string[] } = { @@ -39,6 +40,7 @@ const aliases: { [key: string]: string[] } = { print: ['p'], project: ['P'], compiler: ['C'], + require: ['r'], cacheDirectory: ['cache-directory'], ignoreWarnings: ['I', 'ignore-warnings'], disableWarnings: ['D', 'disable-warnings'], @@ -119,6 +121,7 @@ Options: -e, --eval [code] Evaluate code -p, --print [code] Evaluate code and print result + -r, --require [path] Require a node module for execution -C, --compiler [name] Specify a custom TypeScript compiler -I, --ignoreWarnings [code] Ignore TypeScript warnings by diagnostic code -D, --disableWarnings Ignore every TypeScript warning @@ -167,6 +170,12 @@ const EVAL_PATH = join(cwd, EVAL_FILENAME) // Store eval contents for in-memory lookups. const evalFile = { input: '', output: '', version: 0 } +// Require specified modules before start-up. +for (const id of arrify(argv.require)) { + Module._load(id) +} + +// Execute the main contents (either eval, script or piped). if (isEvalScript) { evalAndExit(code, isPrinted) } else { diff --git a/src/index.spec.ts b/src/index.spec.ts index 7729375dd..7778726e3 100644 --- a/src/index.spec.ts +++ b/src/index.spec.ts @@ -158,6 +158,15 @@ describe('ts-node', function () { return done() }) }) + + it('should support require flags', function (done) { + exec(`${BIN_EXEC} -r ./tests/hello-world -p "console.log('success')"`, function (err, stdout) { + expect(err).to.not.exist + expect(stdout).to.equal('Hello, world!\nsuccess\nundefined\n') + + return done() + }) + }) }) describe('register', function () {