-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdev-server2.js
executable file
·118 lines (80 loc) · 2.46 KB
/
dev-server2.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
#!/usr/bin/env node
'use strict';
const tscServer = require('speedy-tsc');
const strict = process.argv.indexOf('--strict') > 0;
const chokidar = require('chokidar');
const path = require('path');
const fs = require('fs');
const Server = require('socket.io');
const io = new Server(3980, {});
const cp = require('child_process');
const k = cp.spawn('tsc', ['-w']);
k.stdout.pipe(process.stdout);
k.stderr.pipe(process.stderr);
k.once('error', function (err) {
console.log('error => ', err.stack || err);
});
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
});
const tsconfig = require('./tsconfig-fe.json');
const compileFiles = tscServer(tsconfig.compilerOptions);
watcher.once('ready', function () {
watcher.on('add', path => {
// const sockets = io.sockets.connected;
const sockets = clients;
sockets.forEach(function (socket) {
socket.emit('HOT_RELOAD_JSX', {
event: 'add',
path: path
});
});
});
watcher.on('change', path => {
if (String(path).startsWith(publicPath)) {
compileFiles([path], function (err) {
if(err){
console.error(err.stack || err);
}
path = String(path).slice(publicPathLen + 1, path.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: path
});
});
});
}
});
watcher.on('unlink', path => {
const sockets = clients;
sockets.forEach(function (socket) {
socket.emit('HOT_RELOAD_JSX', {
event: 'unlink',
path: path
});
});
});
});