Skip to content

Commit

Permalink
view: Improve error handling for missing build files
Browse files Browse the repository at this point in the history
When running from Auspice's root directory, this replaces an unhandled
scandir error with a more meaningful message.

A new flag (--customBuildOnly) is introduced for running from an
external project. When specified, this prevents falling back to running
from build files in Auspice's root directory, which can be
unintentional.
  • Loading branch information
victorlin committed Dec 6, 2022
1 parent f683486 commit b26aeeb
Showing 1 changed file with 28 additions and 16 deletions.
44 changes: 28 additions & 16 deletions cli/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const addParser = (parser) => {
subparser.addArgument('--handlers', {action: "store", metavar: "JS", help: "Overwrite the provided server handlers for client requests. See documentation for more details."});
subparser.addArgument('--datasetDir', {metavar: "PATH", help: "Directory where datasets (JSONs) are sourced. This is ignored if you define custom handlers."});
subparser.addArgument('--narrativeDir', {metavar: "PATH", help: "Directory where narratives (Markdown files) are sourced. This is ignored if you define custom handlers."});
subparser.addArgument('--customBuildOnly', {action: "storeTrue", help: "Error if a custom build is not found."});
/* there are some options which we deliberately do not document via `--help`. */
subparser.addArgument('--gh-pages', {action: "store", help: SUPPRESS}); /* related to the "static-site-generation" or "github-pages" */
};
Expand Down Expand Up @@ -74,28 +75,39 @@ const loadAndAddHandlers = ({app, handlersArg, datasetDir, narrativeDir}) => {
`Looking for datasets in ${datasetsPath}\nLooking for narratives in ${narrativesPath}`;
};

const getAuspiceBuild = () => {
const getAuspiceBuild = (customBuildOnly) => {
const cwd = path.resolve(process.cwd());
const sourceDir = path.resolve(__dirname, "..");
if (
cwd !== sourceDir &&
fs.existsSync(path.join(cwd, "index.html")) &&
fs.existsSync(path.join(cwd, "dist")) &&
fs.readdirSync(path.join(cwd, "dist")).filter((fn) => fn.match(/^auspice.bundle.[a-z0-9]+.js$/)).length === 1
) {
return {
message: "Serving the auspice build which exists in this directory.",
baseDir: cwd,
distDir: path.join(cwd, "dist")
};

// Default to current working directory.
let baseDir = cwd;
if (!hasAuspiceBuild(cwd)) {
if (cwd === sourceDir || customBuildOnly) {
utils.error(`Auspice build files not found under ${cwd}. Did you run \`auspice build\` in this directory?`);
process.exit(1);
} else if (!hasAuspiceBuild(sourceDir)) {
utils.error(`Auspice build files not found under ${cwd} or ${sourceDir}. Did you run \`auspice build\` in either directory?`);
process.exit(1);
} else if (cwd !== sourceDir) {
utils.log(`Auspice build files not found under ${cwd}. Using build files under ${sourceDir}.`)
baseDir = sourceDir;
}
}
return {
message: `Serving auspice version ${version}`,
baseDir: sourceDir,
distDir: path.join(sourceDir, "dist")
message: `Serving the auspice build in ${baseDir}.`,
baseDir: baseDir,
distDir: path.join(baseDir, "dist")
};
};

const hasAuspiceBuild = (directory) => {
return (
fs.existsSync(path.join(directory, "dist")) &&
fs.existsSync(path.join(directory, "dist/index.html")) &&
fs.readdirSync(path.join(directory, "dist")).filter((fn) => fn.match(/^auspice.bundle.[a-z0-9]+.js$/)).length === 1
)
}

const run = (args) => {
/* Basic server set up */
const app = express();
Expand All @@ -104,7 +116,7 @@ const run = (args) => {
app.use(compression());
app.use(nakedRedirect({reverse: true})); /* redirect www.name.org to name.org */

const auspiceBuild = getAuspiceBuild();
const auspiceBuild = getAuspiceBuild(args.customBuildOnly);
utils.verbose(`Serving favicon from "${auspiceBuild.baseDir}"`);
utils.verbose(`Serving index and built javascript from "${auspiceBuild.distDir}"`);
app.get("/favicon.png", (req, res) => {res.sendFile(path.join(auspiceBuild.baseDir, "favicon.png"));});
Expand Down

0 comments on commit b26aeeb

Please sign in to comment.