-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdev-server.js
executable file
·140 lines (97 loc) · 2.83 KB
/
dev-server.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
'use strict';
//core
const path = require('path');
const fs = require('fs');
const cp = require('child_process');
//npm
const chokidar = require('chokidar');
const Server = require('socket.io');
//project
const strict = process.argv.indexOf('--strict') > 0;
const io = new Server(3980, {});
const publicPath = path.resolve(__dirname + '/public');
const publicPathLen = publicPath.length;
const clients = [];
/////////////////////////////////////////////////////////////////////////////////
function getCount () {
return ' => connection count:' + clients.length
}
io.on('connection', function (socket) {
// only start watching when there is a connection
// startWatching();
clients.push(socket);
console.log(' => new dev server connection! ' + getCount());
socket.on('disconnect', function () {
console.log('dev server user disconnected ' + getCount());
clients.splice(clients.indexOf(socket), 1);
});
});
const strm = fs.createWriteStream(__dirname + '/dev-server.log');
const watcher = chokidar.watch(__dirname + '/**/*.tsx', {
// ignored: /\.[^tsx]$/,
// ignored: '!**/*.tsx',
// ignored: ['**/.git/**/*', '**/**.js', '**/.idea/**/*'],
ignoreInitial: true
});
function runTSC (path, cb) {
const shell = cp.spawn('bash');
let stderr = '';
let stdout = '';
shell.once('close', function (code) {
shell.removeAllListeners();
shell.unref();
if (code < 1 || !strict) {
cb(null)
}
else {
console.error(' => stderr from tsc child process => \n' + stderr);
}
});
console.log('path => ', String(path).trim());
shell.stderr.setEncoding('utf8');
shell.stdout.setEncoding('utf8');
shell.stderr.on('data', function (d) {
stderr += d;
});
shell.stdout.pipe(process.stdout);
shell.stdin.write('tsc --skipLibCheck --noResolve --jsx react --module amd --target es5 ' + path + '\n');
process.nextTick(function () {
shell.stdin.end();
});
}
watcher.once('ready', function () {
watcher.on('add', p => {
// const sockets = io.sockets.connected;
const sockets = clients;
sockets.forEach(function (socket) {
socket.emit('HOT_RELOAD_JSX', {
event: 'add',
path: p
});
});
});
watcher.on('change', p => {
runTSC(p, function () {
if (String(p).startsWith(publicPath)) {
p = String(p).slice(publicPathLen + 1, p.length - 4);
const sockets = clients;
console.log('alrighty then sending message to front-end...');
sockets.forEach(function (socket) {
socket.emit('HOT_RELOAD_JSX', {
event: 'change',
path: p
});
});
}
});
});
watcher.on('unlink', p => {
const sockets = clients;
sockets.forEach(function (socket) {
socket.send('HOT_RELOAD_JSX', {
event: 'unlink',
path: p
});
});
});
});