/
repl.js
259 lines (229 loc) · 8.47 KB
/
repl.js
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
var replServer = require('repl');
var util = require('util');
var len;
replServer.REPLServer.prototype.displayPrompt = function() {
var l = this.bufferedCommand.length ? 11 : len;
this.rli.setPrompt(this.bufferedCommand.length ? '.......... '.yellow : this.prompt, l);
this.rli.prompt();
};
require('colors');
exports.startPrompt = function(Y) {
var prompt = 'YUI@' + Y.version + '> ';
len = prompt.length;
prompt = 'YUI'.magenta + '@'.white + Y.version.yellow + '> '.white;
var debug = Y.config.debug;
var repl = replServer.start(prompt);
var ctx = repl.context;
ctx.Y = Y;
var clear = function(write) {
this.outputStream.write('Resetting Y to the default state'.magenta);
this.bufferedCommand = '';
this.context.Y = require('yui3').silent().useSync('yui-base');
this.outputStream.write(' [done]\n'.white);
if (write !== false) {
this.displayPrompt();
}
};
repl.defineCommand('help', {
help: 'Show help',
action: function() {
var self = this,
max = 0,
aliases = {};
Object.keys(this.commands).forEach(function(name) {
if (name.length > max) {
max = name.length;
}
var cmd = self.commands[name];
if (cmd.help && cmd.help.indexOf('Alias') === 0) {
var a = cmd.help.replace('Alias for ', '.');
if (!aliases[a]) {
aliases[a] = [];
}
aliases[a].push(name);
}
});
Object.keys(this.commands).sort().forEach(function(name) {
var cmd = self.commands[name],
rawName = name;
if (cmd.help && cmd.help.indexOf('Alias') === 0) {
return;
}
if (name.length < max) {
for (var i = name.length; i < max; i++) {
name += ' ';
}
}
self.outputStream.write(name.bold.yellow + ' ' + (cmd.help || '').white);
if (aliases[rawName]) {
self.outputStream.write((' (aliases: ' + aliases[rawName].join(',') + ')').yellow);
}
self.outputStream.write('\n');
});
this.displayPrompt();
}
});
repl.defineCommand('clear', {
help: 'Clear the current Y context',
action: clear
});
var load = function(url) {
var self = this;
clear.call(this, false);
self.outputStream.write('Fetching URL: '.magenta + url.yellow);
this.context.Y.load(url, function() {
self.outputStream.write(' [done]\n'.white);
self.displayPrompt();
});
};
repl.defineCommand('debug', {
help: 'Toggle the YUI debug config option',
action: function() {
var d = !debug+'';
debug = !debug;
this.outputStream.write('Setting debug on the Y instance to: '.magenta + d.white + '\n');
this.context.Y.config.debug = debug;
this.displayPrompt();
}
});
repl.defineCommand('d', {
help: 'Alias for debug',
action: function() {
this.commands['.debug'].action.apply(this, arguments);
}
});
repl.defineCommand('import', {
help: 'Import a document into this context',
action: load
});
repl.defineCommand('load', {
help: 'Alias for import',
action: load
});
repl.defineCommand('fetch', {
help: 'Alias for import',
action: load
});
repl.defineCommand('version', {
help: 'Show the YUI version',
action: function() {
this.outputStream.write('Current YUI Version: '.magenta + this.context.Y.version.yellow + '\n');
this.displayPrompt();
}
});
var use = function(l) {
var args = l.split(',');
this.outputStream.write('Using modules: '.magenta + l);
this.context.Y.useSync.apply(this.context.Y, args);
this.outputStream.write(' [done]\n'.white);
this.displayPrompt();
};
repl.defineCommand('use', {
help: 'Comma seperated list of modules to use inside this Y context',
action: use
});
repl.defineCommand('u', {
help: 'Alias for use',
action: use
});
repl.defineCommand('IO', {
help: 'Alias for io',
action: function() {
this.commands['.io'].action.apply(this, arguments);
}
});
repl.defineCommand('io', {
help: 'Make an IO request to the passed URL',
action: function(url) {
var self = this,
Y = this.context.Y;
self.outputStream.write('Making IO Request: '.magenta + url.yellow);
Y.use('io', function() {
Y.io(url, {
on: {
complete: function(id, e) {
self.outputStream.write(' [done]\n'.white);
self.outputStream.write(' (' + (e.status + '').green + ' ' + e.statusText + '): Content-Type: "' + e.headers['content-type'].green + '"\n\n'.white);
var str;
try {
var data = JSON.parse(e.responseText);
str = util.inspect(data, false, Infinity, true);
} catch (e) {
str = e.responseText;
}
if (str) {
self.outputStream.write(str);
}
self.outputStream.write('\n\n');
self.displayPrompt();
}
}
});
});
}
});
repl.defineCommand('headers', {
help: 'Make an IO request to the passed URL and only return the headers',
action: function(url) {
var self = this,
Y = this.context.Y;
self.outputStream.write('Making IO Request: '.magenta + url.yellow);
Y.use('io', function() {
Y.io(url, {
on: {
complete: function(id, e) {
self.outputStream.write(' [done]\n'.white);
self.outputStream.write(' (' + (e.status + '').green + ' ' + e.statusText + '): Content-Type: "' + e.headers['content-type'].green + '"\n\n'.white);
var str = util.inspect(e.headers, false, Infinity, true);
self.outputStream.write(str);
self.outputStream.write('\n\n');
self.displayPrompt();
}
}
});
});
}
});
repl.defineCommand('YQL', {
help: 'Alias for yql',
action: function() {
this.commands['.yql'].action.apply(this, arguments);
}
});
repl.defineCommand('yql', {
help: 'Make an YQL request with the passed SQL statement',
action: function(sql) {
var self = this,
Y = this.context.Y;
self.outputStream.write('Making YQL Request: '.magenta + sql.yellow);
Y.use('yql', function() {
Y.YQL(sql, function(r) {
self.outputStream.write(' [done]\n'.white);
var str;
try {
str = util.inspect(r.query.results, false, Infinity, true);
} catch (e) {
str = util.inspect(r, false, Infinity, true);
}
self.outputStream.write(str);
self.outputStream.write('\n\n');
self.displayPrompt();
});
});
}
});
repl.defineCommand('get', {
help: 'Get a remote/local script and eval it into this context',
action: function(url) {
var self = this;
self.outputStream.write('Fetching Script: '.magenta + url.yellow);
Y.Get.script(url, {
onSuccess: function() {
self.outputStream.write(' [done]\n'.white);
self.displayPrompt();
}
});
}
});
return repl;
};