Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f37f9d1

Browse files
committed
Merge pull request jashkenas#3867 from sgentle/require-option
add -r/--require command line option
2 parents 7395ac1 + 836175b commit f37f9d1

4 files changed

Lines changed: 53 additions & 11 deletions

File tree

lib/coffee-script/command.js

Lines changed: 20 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/coffee-script/repl.js

Lines changed: 14 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/command.coffee

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ SWITCHES = [
4444
[ '--no-header', 'suppress the "Generated by" header']
4545
['-o', '--output [DIR]', 'set the output directory for compiled JavaScript']
4646
['-p', '--print', 'print out the compiled JavaScript']
47+
['-r', '--require [MODULE*]', 'require the given module before eval or REPL']
4748
['-s', '--stdio', 'listen for and compile scripts over stdio']
4849
['-l', '--literate', 'treat stdio as literate style coffee-script']
4950
['-t', '--tokens', 'print out the tokens that the lexer/rewriter produce']
@@ -69,6 +70,8 @@ exports.run = ->
6970
# `node` REPL CLI and, therefore, (b) make packages that modify native prototypes
7071
# (such as 'colors' and 'sugar') work as expected.
7172
replCliOpts = useGlobal: yes
73+
opts.prelude = makePrelude opts.require if opts.require
74+
replCliOpts.prelude = opts.prelude
7275
return forkNode() if opts.nodejs
7376
return usage() if opts.help
7477
return version() if opts.version
@@ -101,6 +104,13 @@ exports.run = ->
101104
source = path.resolve source
102105
compilePath source, yes, source
103106

107+
makePrelude = (requires) ->
108+
requires.map (module) ->
109+
[_, name, module] = match if match = module.match(/^(.*)=(.*)$/)
110+
name ||= helpers.baseFileName module, yes, useWinPathSep
111+
"#{name} = require('#{module}')"
112+
.join ';'
113+
104114
# Compile a path, which could be a script or a directory. If a directory
105115
# is passed, recursively compile all '.coffee', '.litcoffee', and '.coffee.md'
106116
# extension source files in it and all subdirectories.
@@ -167,6 +177,7 @@ compileScript = (file, input, base = null) ->
167177
printLine CoffeeScript.nodes(t.input, t.options).toString().trim()
168178
else if o.run
169179
CoffeeScript.register()
180+
CoffeeScript.eval opts.prelude, t.options if opts.prelude
170181
CoffeeScript.run t.input, t.options
171182
else if o.join and t.file isnt o.join
172183
t.input = helpers.invertLiterate t.input if helpers.isLiterate file

src/repl.coffee

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,18 @@ replDefaults =
3333
new Assign (new Value new Literal '_'), ast, '='
3434
]
3535
js = ast.compile {bare: yes, locals: Object.keys(context), referencedVars}
36-
result = if context is global
37-
vm.runInThisContext js, filename
38-
else
39-
vm.runInContext js, context, filename
40-
cb null, result
36+
cb null, runInContext js, context, filename
4137
catch err
4238
# AST's `compile` does not add source code information to syntax errors.
4339
updateSyntaxError err, input
4440
cb err
4541

42+
runInContext = (js, context, filename) ->
43+
if context is global
44+
vm.runInThisContext js, filename
45+
else
46+
vm.runInContext js, context, filename
47+
4648
addMultilineHandler = (repl) ->
4749
{rli, inputStream, outputStream} = repl
4850
# Node 0.11.12 changed API, prompt is now _prompt.
@@ -149,6 +151,7 @@ module.exports =
149151
process.argv = ['coffee'].concat process.argv[2..]
150152
opts = merge replDefaults, opts
151153
repl = nodeREPL.start opts
154+
runInContext opts.prelude, repl.context, 'prelude' if opts.prelude
152155
repl.on 'exit', -> repl.outputStream.write '\n'
153156
addMultilineHandler repl
154157
addHistory repl, opts.historyFile, opts.historyMaxInputSize if opts.historyFile

0 commit comments

Comments
 (0)