Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for --require flag like node CLI #181

Merged
merged 1 commit into from
Aug 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion custom_typings/node.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
11 changes: 10 additions & 1 deletion src/_bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[] } = {
Expand All @@ -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'],
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
9 changes: 9 additions & 0 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down