-
Notifications
You must be signed in to change notification settings - Fork 1
/
repl.coffee
executable file
·219 lines (183 loc) · 6.04 KB
/
repl.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#!/usr/bin/env coffee
readline = require 'readline'
readline_vim = require 'readline-vim'
{Pipeline} = require './pipeline'
moment = require 'moment'
builtins = require './builtins'
ansi = require('ansi')(process.stdout)
{prettyPrint} = require './helpers'
fs = require 'fs'
path = require 'path'
_ = require 'underscore'
argv = require('minimist')(process.argv)
# Helper functions
getHomeDir = ->
return process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE
process.on 'SIGTERM', ->
console.log 'sigterm'
process.exit()
process.on 'SIGINT', ->
console.log 'sigint'
process.exit()
# Import default modules
PipelineREPL = (@pipeline) ->
if !@pipeline
@pipeline = defaultPipeline()
# Keep a consistent context for the REPL
@context = @pipeline.subScope()
# Add command line arguments as variables
base_env = _.omit(argv, '_')
_.forEach base_env, (v, k) =>
@context.set('vars', k, v)
# Keep track of last response
@last_out = null
return @
defaultPipeline = ->
new Pipeline()
#.use(require('./modules/mongo').connect())
#.use(require('./modules/redis').connect())
.use('http')
.use('html')
.use('files')
.use('keywords')
PipelineREPL::writeSuccess = (data) ->
if data?
if @plain
console.log data
else
console.log prettyPrint data
PipelineREPL::writeError = (err) ->
ansi.fg['red']()
console.log '[ERROR] ' + err
ansi.reset()
PipelineREPL::executeScript = (script, cb) ->
try
@pipeline.exec script, @last_out, @context, (err, data) =>
@last_out = data
# TODO: Some more advanced output handling,
# trim long lists with some ellipses
if err?
@writeError err
else
@writeSuccess data
cb() if cb?
catch e
@writeError e
cb() if cb?
PipelineREPL::startReadline = ->
repl = @
# Set up readline prompt
fn_names = (n for n, f of repl.pipeline.fns).concat (n for n, f of builtins)
fnCompleter = (line) ->
line_parts = line.trim().split(/\s+/)
to_complete = line_parts.slice(-1)[0]
startsWith = (sofar) -> (s) -> s.toLowerCase().indexOf(sofar.toLowerCase()) == 0
file_commands = ['ls', 'cp', 'mv', 'ln', 'cd', 'cat', 'vim', 'coffee', 'python', 'git', 'open']
if to_complete.match '/'
base_dir = to_complete.split('/').slice(0,-1).join('/')
last_part = to_complete.split('/').slice(-1)[0]
to_complete = last_part
completions = fs.readdirSync(base_dir).filter startsWith last_part
else
completions = fs.readdirSync('.').filter startsWith to_complete
if line_parts[0] not in file_commands
completions = completions.concat fn_names.filter startsWith to_complete
return [completions, to_complete]
rl = readline.createInterface
input: process.stdin
output: process.stdout
completer: fnCompleter
rlv = readline_vim(rl)
@rl = rl
@context._readline = rl
# Overload readline's addHistory to save to our history file
rl_addHistory = rl._addHistory
rl._addHistory = ->
last = rl.history[0]
line = rl_addHistory.call(rl)
saveHistory(line) if last != line
return line
# Bootstrap history from file
loadHistory (err, saved_history) ->
rl.history.push.apply(rl.history, saved_history)
@updatePrompt()
rl.prompt()
# Interpret input as scripts and run
run_once = @run_once || !process.stdin.isTTY
rl.on 'line', (script) =>
script = script.trim()
script = 'id' if !script.length
repl.executeScript script, =>
if run_once
if script_exec = argv.exec || argv.e
repl.executeScript script_exec, ->
process.exit()
else
process.exit()
else
@updatePrompt()
rl.prompt()
rl.on 'close', ->
console.log 'bye'
process.exit()
# Prompt helpers
colorize = (s, color) ->
prefix = '\x1b[' + color + 'm'
suffix = '\x1b[0m'
prefix + s + suffix
PipelineREPL::updatePrompt = ->
time = '[' + moment().format('HH:mm') + ']'
cwd = process.cwd().replace process.env.HOME, '~'
parts = [
colorize(time, 90)
colorize(cwd, 34)
colorize('#| ', 36)
].join ' '
@rl.setPrompt parts
# History helpers
history_path = path.resolve getHomeDir(), '.pipeline_history'
saveHistory = (line) ->
fs.appendFileSync history_path, line + '\n'
loadHistory = (cb) ->
fs.readFile history_path, (err, history_data) ->
return cb null, [] if !history_data
history_lines = history_data.toString().trim().split('\n')
history_lines.reverse()
cb null, history_lines
# Going
if require.main != module
# Module mode
module.exports = PipelineREPL
else
# Stand-alone mode
repl = new PipelineREPL
if argv.plain || argv.p
repl.plain = true
if script_filename = argv.run || argv.r
doRunScript = ->
# Execute single script
script = fs.readFileSync(script_filename).toString()
setTimeout ->
repl.executeScript script, ->
process.exit()
, 50
if !process.stdin.isTTY
# Try reading in piped
piped = ''
process.stdin.on 'data', (data) ->
piped += data.toString()
process.stdin.on 'end', ->
repl.last_out = piped.trim()
doRunScript()
else
doRunScript()
else if script_filename = argv.load || argv.l
# Execute single script
console.log "Reading from #{ script_filename }..."
script = fs.readFileSync(script_filename).toString()
setTimeout ->
repl.executeScript script, ->
repl.startReadline()
, 50
else
repl.startReadline()