forked from MaliceLabs/phoenix
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevelop.js
More file actions
167 lines (125 loc) · 3.28 KB
/
develop.js
File metadata and controls
167 lines (125 loc) · 3.28 KB
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
'use strict';
/* eslint no-return-assign: 0 */
var fs = require('fs');
var path = require('path');
var util = require('util');
var events = require('events');
var Promise = require('promise');
var minimatch = require('minimatch');
var childProcess = require('child_process');
function cons(head, tail) {
var list = [head];
list.push.apply(list, tail);
return list;
}
function TreeWatcher(directory, ignoreList) {
var shouldIgnore = ignoreList.some(function (ignorePattern) {
return minimatch(directory, ignorePattern);
});
if (shouldIgnore) {
return;
}
var treeWatcher = this;
function pass(event) {
return function () {
treeWatcher.emit.apply(treeWatcher, cons(event, arguments));
};
}
fs.watch(path.join(__dirname, directory), pass('change'));
fs.readdir(path.join(__dirname, directory), function (error, names) {
if (error) {
if (error.message.indexOf('ENOTDIR') === -1) {
treeWatcher.emit('error', error);
}
return;
}
names.forEach(function (name) {
var subWatcher = new TreeWatcher(path.join(directory, name), ignoreList);
subWatcher.on('error', pass('error'));
subWatcher.on('change', pass('change'));
});
});
}
util.inherits(TreeWatcher, events.EventEmitter);
function awaitableExit(child) {
child.exit = new Promise(function (resolve) {
child.on('exit', function () {
resolve();
});
});
return child;
}
function SingleProcess() {
this.last = Promise.resolve();
}
SingleProcess.prototype.spawn = function spawn(path, args, options) {
return (this.last = this.kill('SIGINT').then(function () {
return awaitableExit(childProcess.spawn(path, args, options));
}));
};
SingleProcess.prototype.kill = function kill(signal) {
return (this.last = this.last.then(function (previous) {
if (previous === undefined) {
return Promise.resolve();
}
previous.kill(signal);
return previous.exit;
}));
};
function limit(func, timeout) {
var lastCall = 0;
var timer = null;
return function limited() {
var now = Date.now();
if (now - lastCall >= timeout) {
lastCall = now;
clearTimeout(timer);
timer = null;
func.apply(this, arguments);
} else if (!timer) {
timer = setTimeout(limited, timeout - (now - lastCall));
}
};
}
var p = new SingleProcess();
var spawnOptions = {
cwd: __dirname,
stdio: 'inherit'
};
function spawnServer() {
p.spawn(process.execPath, process.execArgv.concat(['server']), spawnOptions);
}
function changed() {
p.kill('SIGINT').then(spawnServer);
}
function beginWatching() {
process.once('SIGINT', function () {
p.kill('SIGINT').then(function () {
process.exit(0);
});
});
fs.readFile(path.join(__dirname, '.watchignore'), 'utf8', function (error, content) {
if (error) {
throw error;
}
var ignoreList = content.trim().split('\n');
new TreeWatcher('.', ignoreList).on('change', limit(changed, 100));
spawnServer();
});
}
function main() {
var config = require('./config');
if (!config.session.key) {
console.error('No session.key has been provided in config.json; you’ll need one\nto sign session IDs.');
require('crypto').randomBytes(64, function (error, bytes) {
if (error) {
throw error;
}
console.error('\nHere’s a fresh, random, 64-byte key:\n' + bytes.toString('base64'));
process.exit(1);
});
return;
}
beginWatching();
}
main();