1 | 'use strict';
|
2 |
|
3 | var repl = require('repl')
|
4 | , path = require('path')
|
5 | , cardinal = require('cardinal')
|
6 | , util = require('util')
|
7 | , state = require('./lib/state')
|
8 | , config = require('./config/current')
|
9 | , initConfig = require('./config/init')
|
10 | , managePlugins = require('./lib/manage-plugins')
|
11 | , initWatcher = require('./lib/watcher-init')
|
12 | , feedEdits = require('./lib/feedEdits')
|
13 | , core = require('./lib/dox/core')
|
14 | , log = require('./lib/log')
|
15 | , instructions = require('./lib/instructions')
|
16 | , initBuiltins = require('./lib/builtins/init')
|
17 | , findexquire = require('./lib/findexquire')
|
18 | , stdin = process.stdin
|
19 | , stdout = process.stdout
|
20 | ;
|
21 |
|
22 | function createRepl(stdin) {
|
23 | var r = repl.start({
|
24 | prompt : config.prompt || 'pad > '
|
25 | , input : stdin
|
26 | , output : stdout
|
27 | , ignoreUndefined : true
|
28 | , useColors : true
|
29 | , useGlobal : true
|
30 | });
|
31 | log.repl = r;
|
32 |
|
33 |
|
34 | core(r, [ { request: 'fs', module: require('fs') }]);
|
35 |
|
36 | r.state = state;
|
37 | r.config = config;
|
38 |
|
39 | global.$repl = r;
|
40 | global.require = findexquire(path.join(process.cwd(), 'repl.js'), true);
|
41 |
|
42 | function writer(s) {
|
43 | return util.inspect(s, config.inspect.showHidden, config.inspect.depth, true);
|
44 | }
|
45 |
|
46 | r.writer = function (s) {
|
47 | if (typeof s !== 'string') return writer(s);
|
48 | if (!/^function /.test(s)) return writer(s);
|
49 | try {
|
50 |
|
51 | s = s.replace(/^function[ ]+\(/, 'function fn(');
|
52 |
|
53 | var code = cardinal.highlight(s, { linenos: true });
|
54 | r.outputStream.write(code);
|
55 | return '';
|
56 | } catch (e) {
|
57 | return writer(s);
|
58 | }
|
59 | };
|
60 |
|
61 | return r;
|
62 | }
|
63 |
|
64 | function boot(stdin) {
|
65 | instructions();
|
66 |
|
67 | var repl = createRepl(stdin);
|
68 | state.__defineGetter__('repl', function () { return repl; });
|
69 |
|
70 | managePlugins();
|
71 | initBuiltins();
|
72 | }
|
73 |
|
74 |
|
75 | module.exports = function repreprep(root) {
|
76 |
|
77 | initConfig();
|
78 |
|
79 | if (!root) {
|
80 | log.print('Watching no files since no path was specified.');
|
81 | return boot(stdin);
|
82 | }
|
83 |
|
84 | var watcher = initWatcher(root);
|
85 | watcher.on('initialized', function () {
|
86 | boot(stdin);
|
87 | var feedEdit = feedEdits(stdin, stdout);
|
88 | watcher.on('file-changed', feedEdit);
|
89 | });
|
90 | };
|