UNPKG

4.27 kBJavaScriptView Raw
1/* eslint no-console: off */
2const path = require("path");
3const express = require("express");
4const webpack = require("webpack");
5const webpackDevMiddleware = require("webpack-dev-middleware");
6const webpackHotMiddleware = require("webpack-hot-middleware");
7const utils = require("./utils");
8const { loadAndAddHandlers, serveRelativeFilepaths } = require("./view");
9const version = require('../src/version').version;
10const chalk = require('chalk');
11const generateWebpackConfig = require("../webpack.config.js").default;
12const SUPPRESS = require('argparse').Const.SUPPRESS;
13
14const addParser = (parser) => {
15 const description = `Launch auspice in development mode.
16 This runs a local server and uses hot-reloading to allow automatic updating as you edit the code.
17 NOTE: there is a speed penalty for this ability and this should never be used for production.
18 `;
19
20 const subparser = parser.addParser('develop', {addHelp: true, description});
21 subparser.addArgument('--verbose', {action: "storeTrue", help: "Print more verbose progress messages in the terminal."});
22 subparser.addArgument('--extend', {action: "store", metavar: "JSON", help: "Client customisations to be applied. See documentation for more details. Note that hot reloading does not currently work for these customisations."});
23 subparser.addArgument('--handlers', {action: "store", metavar: "JS", help: "Overwrite the provided server handlers for client requests. See documentation for more details."});
24 subparser.addArgument('--datasetDir', {metavar: "PATH", help: "Directory where datasets (JSONs) are sourced. This is ignored if you define custom handlers."});
25 subparser.addArgument('--narrativeDir', {metavar: "PATH", help: "Directory where narratives (Markdown files) are sourced. This is ignored if you define custom handlers."});
26 /* there are some options which we deliberately do not document via `--help`. See build.js for explanations. */
27 subparser.addArgument('--includeTiming', {action: "storeTrue", help: SUPPRESS});
28 subparser.addArgument('--gh-pages', {action: "store", help: SUPPRESS});
29};
30
31
32const run = (args) => {
33 /* Basic server set up */
34 const app = express();
35 app.set('port', process.env.PORT || 4000);
36 app.set('host', process.env.HOST || "localhost");
37
38 const baseDir = path.resolve(__dirname, "..");
39 utils.verbose(`Serving index / favicon etc from "${baseDir}"`);
40 app.get("/favicon.png", (req, res) => {res.sendFile(path.join(baseDir, "favicon.png"));});
41
42 /* webpack set up */
43 const extensionPath = args.extend ? path.resolve(args.extend) : undefined;
44 const webpackConfig = generateWebpackConfig({extensionPath, devMode: true});
45 const compiler = webpack(webpackConfig);
46
47 /* variables available to babel (which is called by webpack) */
48 process.env.BABEL_INCLUDE_TIMING_FUNCTIONS = args.includeTiming;
49 process.env.BABEL_ENV = "development";
50 process.env.BABEL_EXTENSION_PATH = extensionPath;
51
52 app.use((webpackDevMiddleware)(
53 compiler,
54 {logLevel: 'warn', publicPath: webpackConfig.output.publicPath}
55 ));
56 app.use((webpackHotMiddleware)(
57 compiler,
58 {log: console.log, path: '/__webpack_hmr', heartbeat: 10 * 1000}
59 ));
60
61 let handlerMsg = "";
62 if (args.gh_pages) {
63 handlerMsg = serveRelativeFilepaths({app, dir: path.resolve(args.gh_pages)});
64 } else {
65 handlerMsg = loadAndAddHandlers({app, handlersArg: args.handlers, datasetDir: args.datasetDir, narrativeDir: args.narrativeDir});
66 }
67
68 /* this must be the last "get" handler, else the "*" swallows all other requests */
69 app.get("*", (req, res) => {
70 res.sendFile(path.join(baseDir, "index.html"));
71 });
72
73 const server = app.listen(app.get('port'), app.get('host'), () => {
74 utils.log("\n\n---------------------------------------------------");
75 utils.log("Auspice now running in development mode.");
76 const host = app.get('host');
77 const {port} = server.address();
78 console.log(chalk.blueBright("Access the client at: ") + chalk.blueBright.underline.bold(`http://${host}:${port}`));
79 utils.log(`Serving auspice version ${version}${args.extend ? " with extensions" : ""}.`);
80 utils.log(handlerMsg);
81 utils.log("---------------------------------------------------\n\n");
82 });
83
84};
85
86module.exports = {
87 addParser,
88 run
89};