|
| 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; |
0 commit comments