Skip to content

Commit cd6b881

Browse files
authored
Merge pull request TerriaJS#6999 from TerriaJS/update-gulp-task
Move shared terriajs-server gulp task to separate file
2 parents 30ae3e9 + 5ada26f commit cd6b881

File tree

2 files changed

+75
-58
lines changed

2 files changed

+75
-58
lines changed
+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/**
2+
* terriajs-server gulp task. Runs terriajs-server.
3+
* Used in terriajs & TerriaMap gulpfiles.
4+
* @param {number | undefined} defaultPort - the default port that terriajs-server should run on
5+
* @returns {(done: (error?: Error) => void) => void} A gulp task
6+
*/
7+
const terriajsServerGulpTask = (defaultPort = undefined) => {
8+
return (done) => {
9+
// E.g. gulp terriajs-server --terriajsServerArg port=4000 --terriajsServerArg verbose=true
10+
// or gulp dev --terriajsServerArg port=3000
11+
const { spawn } = require("child_process");
12+
const fs = require("fs");
13+
const minimist = require("minimist");
14+
// Arguments written in skewer-case can cause problems (unsure why), so stick to camelCase
15+
const options = minimist(process.argv.slice(2), {
16+
string: ["terriajsServerArg"],
17+
default: { terriajsServerArg: [] }
18+
});
19+
20+
const logFile = fs.openSync("./terriajs-server.log", "w");
21+
const serverArgs = Array.isArray(options.terriajsServerArg)
22+
? options.terriajsServerArg
23+
: [options.terriajsServerArg];
24+
if (defaultPort !== undefined) {
25+
serverArgs.splice(0, 0, `port=${defaultPort}`);
26+
}
27+
const child = spawn(
28+
"node",
29+
[
30+
require.resolve("terriajs-server/terriajs-server.js"),
31+
...serverArgs.map((arg) => `--${arg}`)
32+
],
33+
{ detached: true, stdio: ["ignore", logFile, logFile] }
34+
);
35+
child.on("exit", (exitCode, signal) => {
36+
done(
37+
new Error(
38+
"terriajs-server quit" +
39+
(exitCode !== null ? ` with exit code: ${exitCode}` : "") +
40+
(signal ? ` from signal: ${signal}` : "") +
41+
"\nCheck terriajs-server.log for more information."
42+
)
43+
);
44+
});
45+
child.on("spawn", () => {
46+
console.log("terriajs-server started - see terriajs-server.log for logs");
47+
});
48+
// Intercept SIGINT, SIGTERM and SIGHUP, cleanup terriajs-server and re-send signal
49+
// May fail to catch some relevant signals on Windows
50+
// SIGINT: ctrl+c
51+
// SIGTERM: kill <pid>
52+
// SIGHUP: terminal closed
53+
function stopServer() {
54+
child.kill("SIGTERM");
55+
console.log("terriajs-server stopped");
56+
}
57+
process.once("SIGINT", () => {
58+
stopServer();
59+
process.kill(process.pid, "SIGINT");
60+
});
61+
process.once("SIGTERM", () => {
62+
stopServer();
63+
process.kill(process.pid, "SIGTERM");
64+
});
65+
process.once("SIGHUP", () => {
66+
stopServer();
67+
process.kill(process.pid, "SIGHUP");
68+
});
69+
process.on("exit", stopServer);
70+
};
71+
};
72+
73+
module.exports = terriajsServerGulpTask;

gulpfile.js

+2-58
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
// the devDependencies available. Individual tasks, other than `post-npm-install` and any tasks it
99
// calls, may require in `devDependency` modules locally.
1010
var gulp = require("gulp");
11+
var terriajsServerGulpTask = require("./buildprocess/terriajsServerGulpTask");
1112

1213
gulp.task("build-specs", function (done) {
1314
var runWebpack = require("./buildprocess/runWebpack.js");
@@ -253,64 +254,7 @@ gulp.task(
253254
)
254255
);
255256

256-
gulp.task("terriajs-server", function (done) {
257-
// E.g. gulp terriajs-server --terriajsServerArg port=4000 --terriajsServerArg verbose=true
258-
// or gulp dev --terriajsServerArg port=3000
259-
const { spawn } = require("child_process");
260-
const fs = require("fs");
261-
const minimist = require("minimist");
262-
263-
const knownOptions = {
264-
string: ["terriajsServerArg"],
265-
default: {
266-
terriajsServerArg: []
267-
}
268-
};
269-
const options = minimist(process.argv.slice(2), knownOptions);
270-
271-
const logFile = fs.openSync("./terriajs-server.log", "a");
272-
const serverArgs = Array.isArray(options.terriajsServerArg)
273-
? options.terriajsServerArg
274-
: [options.terriajsServerArg];
275-
// Spawn detached - attached does not make terriajs-server
276-
// quit when the gulp task is stopped
277-
const child = spawn(
278-
"node",
279-
[
280-
"./node_modules/.bin/terriajs-server",
281-
"--port=3002",
282-
...serverArgs.map((arg) => `--${arg}`)
283-
],
284-
{ detached: true, stdio: ["ignore", logFile, logFile] }
285-
);
286-
child.on("exit", (exitCode, signal) => {
287-
done(
288-
new Error(
289-
"terriajs-server quit" +
290-
(exitCode !== null ? ` with exit code: ${exitCode}` : "") +
291-
(signal ? ` from signal: ${signal}` : "") +
292-
"\nCheck terriajs-server.log for more information."
293-
)
294-
);
295-
});
296-
// Intercept SIGINT, SIGTERM and SIGHUP, cleanup terriajs-server and re-send signal
297-
// May fail to catch some relevant signals on Windows
298-
// SIGINT: ctrl+c
299-
// SIGTERM: kill <pid>
300-
// SIGHUP: terminal closed
301-
process.once("SIGINT", () => {
302-
child.kill("SIGTERM");
303-
process.kill(process.pid, "SIGINT");
304-
});
305-
process.once("SIGTERM", () => {
306-
child.kill("SIGTERM");
307-
process.kill(process.pid, "SIGTERM");
308-
});
309-
process.once("SIGHUP", () => {
310-
child.kill("SIGTERM");
311-
process.kill(process.pid, "SIGHUP");
312-
});
313-
});
257+
gulp.task("terriajs-server", terriajsServerGulpTask(3002));
314258

315259
gulp.task(
316260
"copy-cesium-assets",

0 commit comments

Comments
 (0)