forked from kriasoft/graphql-starter-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.js
More file actions
114 lines (105 loc) · 3.61 KB
/
run.js
File metadata and controls
114 lines (105 loc) · 3.61 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
#!/usr/bin/env node
/**
* Node.js API Starter Kit (https://reactstarter.com/nodejs)
*
* Copyright © 2016-present Kriasoft, LLC. All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE.txt file in the root directory of this source tree.
*/
const fs = require('fs');
const path = require('path');
const cp = require('child_process');
const pkg = require('../package.json');
const task = require('./task');
let build;
let server;
let debugPort = '9230';
const serverQueue = [];
const isDebug = process.execArgv.some(x => x.startsWith('--inspect'));
// Gracefull shutdown
process.once('cleanup', () => {
if (server) {
server.addListener('exit', () => process.exit());
server.kill('SIGTERM');
serverQueue.forEach(x => x.kill());
} else {
process.exit();
}
});
process.on('SIGINT', () => process.emit('cleanup'));
process.on('SIGTERM', () => process.emit('cleanup'));
// Ensure that Node.js modules were installed,
// at least those required to build the app
try {
build = require('./build');
} catch (err) {
if (err.code !== 'MODULE_NOT_FOUND') throw err;
// Install Node.js modules with Yarn
cp.spawnSync('yarn', ['install', '--no-progress'], { stdio: 'inherit' });
// Clear Module's internal cache
try {
const Module = require('module');
const m = new Module();
// eslint-disable-next-line
m._compile(fs.readFileSync('./tools/build.js', 'utf8'), path.resolve('./tools/build.js'));
} catch (error) { } // eslint-disable-line
// Reload dependencies
build = require('./build');
}
// Launch `node build/server.js` on a background thread
function spawnServer() {
return cp.spawn('node',
[
// Pre-load application dependencies to improve "hot reload" restart time
...Object.keys(pkg.dependencies).reduce((requires, val) => requires.concat(['--require', val]), []),
// If the parent Node.js process is running in debug (inspect) mode,
// launch a debugger for Express.js app on the next port
...process.execArgv.map((arg) => {
if (arg.startsWith('--inspect')) {
const match = arg.match(/^--inspect=(\S+:|)(\d+)$/);
if (match) debugPort = Number(match[2]) + 1;
return `--inspect=${match ? match[1] : '0.0.0.0:'}${debugPort}`;
}
return arg;
}),
'--no-lazy',
// Enable "hot reload", it only works when debugger is off
...(isDebug ? ['./server.js'] : [
'--eval',
'process.stdin.on("data", data => { if (data.toString() === "load") require("./server.js"); });',
]),
],
{ cwd: './build', stdio: ['pipe', 'inherit', 'inherit'], timeout: 3000 });
}
module.exports = task('run', () => Promise.resolve()
// Migrate database schema to the latest version
.then(() => {
cp.spawnSync('node', ['tools/db.js', 'migrate'], { stdio: 'inherit' });
})
// Compile and launch the app in watch mode, restart it after each rebuild
.then(() => build({
watch: true,
onComplete() {
if (isDebug) {
if (server) {
server.on('exit', () => { server = spawnServer(); });
server.kill('SIGTERM');
} else {
server = spawnServer();
}
} else {
if (server) server.kill('SIGTERM');
server = serverQueue.splice(0, 1)[0] || spawnServer();
server.stdin.write('load'); // this works faster than IPC
if (server) while (serverQueue.length < 3) serverQueue.push(spawnServer());
}
},
}))
// Resolve the promise on exit
.then(() => new Promise((resolve) => {
process.once('exit', () => {
if (server) server.kill();
resolve();
});
})));