-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
170 lines (136 loc) · 2.88 KB
/
index.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
"use strict";
var Repl = require('repl');
var fs = require('fs');
var net = require('net');
var path = require('path');
var Emitter = require('events').EventEmitter;
var util = require('util');
var CONF_KEY = 'dynReplSocket';
function DynRepl(ctx, opts) {
if (!(this instanceof DynRepl)) {
return new DynRepl(ctx, opts);
}
var self = this;
Emitter.call(self);
var prompt_ = ':: ';
var sockPath_;
var srv_;
var watcher_;
opts = opts || {};
var verbose_ = opts.verbose || false;
var file = opts.file || './package.json';
watcher_ = getWatcher(file);
function getWatcher(file) {
file = path.resolve(file);
var watcher = fs.watch(file, {permanent:false});
watcher.on('change', function(){
watcher_.close();
watcher_ = getWatcher(file);
readUncachedFile(file, reInit);
});
return watcher;
}
function log() {
if (verbose_) {
console.log.apply(console, Array.prototype.slice.call(arguments));
}
}
function reInit(cont) {
if (!cont) {
return stopRepl();
}
var newPath = (cont.config||{})[CONF_KEY] || null;
if (newPath === sockPath_) {
return self;
}
if (newPath) {
stopRepl(function(){
sockPath_ = newPath;
prompt_ = (cont.name||'dynRepl').concat(' :: ') || prompt_;
startRepl();
});
} else {
stopRepl();
}
return self;
}
function readUncachedFile(file, cb) {
file = path.resolve(file);
if (require.cache.hasOwnProperty(file)) {
delete require.cache[file];
}
var content = null;
try {
content = require(file);
} catch(e) {}
cb(content);
return self;
}
function startRepl(cb) {
if (!sockPath_) {
return self;
}
var srv = net.createServer(function(sock){
var repl = Repl.start({
prompt : prompt_ ,
input : sock ,
output : sock ,
terminal : true ,
useColors : true ,
useGlobal : false
}).on('exit', function(){
sock.end();
});
Object.keys(ctx).forEach(function(key){
repl.context[key] = ctx[key];
});
self.emit('connection', sock, repl);
}).on('error', function(e){
log('REPL Error:', e);
self.emit('error', e);
});
srv.unref();
srv.listen(sockPath_, function(){
srv_ = srv;
log('REPL listening on', sockPath_);
self.emit('listen', srv);
callCb(cb);
});
return self;
}
function stopRepl(cb) {
if (srv_ && srv_.close) {
srv_.close(function(){
srv_ = undefined;
log('REPL on', sockPath_, 'closed');
self.emit('unlisten', srv_);
callCb(cb);
});
} else {
callCb(cb);
}
return self;
}
function callCb(cb) {
if (typeof cb === 'function') {
cb();
}
}
self.end = function(cb){
if (watcher_ && watcher_.close()) {
watcher_.close();
}
stopRepl(function(){
callCb(cb);
self.emit('end');
});
return self;
};
self.start = function(){
readUncachedFile(file, reInit);
return self;
};
return self;
}
util.inherits(DynRepl, Emitter);
module.exports = DynRepl;