src/deamon.js
import { watch as Watch } from 'chokidar';
import notifier from './support/Notifier';
import { livereload, liveReloadHost } from './utils/livereload';
import * as spawn from 'cross-spawn';
import * as psTree from 'ps-tree';
function LoadServer (resolve, reject, nginx) {
let PIDPromises = [];
let KoatonServer = spawn('node', ['app.js'], { shell: false, env: {liveReloadHost: liveReloadHost(), NODE_ENV: process.env.NODE_ENV} });
KoatonServer.stdout.on('data', (buffer) => {
process.stdout.write(buffer);
let text = buffer.toString();
let found = text.indexOf('Enviroment') > -1;
if (found) {
let protocol = configuration.server.https && configuration.server.https.cert ? 'https' : 'http';
notifier('Koaton', `Serving ${protocol}://${scfg.hostname}${nginx ? '' : ':' + scfg.port}/`);
livereload.reload();
if (process.env.istesting) {
Promise.all(PIDPromises).then((PIDs) => {
let pids = [];
for (const pidg of PIDs) {
pids = pids.concat(pidg);
}
psTree(KoatonServer.pid, (err, children) => {
if (err) {
reject(err);
}
let childIPIDs = children.map((p) => parseInt(p.PID, 10));
resolve(pids.concat(childIPIDs));
});
});
}
}
});
KoatonServer.stderr.on('data', (data) => {
console.log(`stderr: ${data}`);
});
KoatonServer.on('close', (code) => {
console.log(`child process exited with code ${code}`);
});
return KoatonServer;
}
async function StartKoatonServer (resolve, reject, nginx = false) {
let watcher = new Watch('./**', {
awaitWriteFinish: {
stabilityThreshold: 500,
pollInterval: 50
},
ignoreInitial: true,
ignored: [
'.git',
'node_modules',
'koaton_module_package',
'ember',
'assets',
'public',
'commands',
'koaton_modules',
'views/ember_apps/*',
'views/*.*',
'downloads',
'config/bundles.js',
'*.tmp',
'*.json',
'*.conf'
]
});
let server = LoadServer(resolve, reject, nginx);
watcher.on('all', (event, path) => {
console.log(123, event, path);
server.kill();
server = LoadServer(resolve, reject, nginx);
});
return server;
}
export default StartKoatonServer;