From b256d2f8d98973df93b397bf2ec8b1d84688cc8f Mon Sep 17 00:00:00 2001 From: Frank Li Date: Wed, 9 Aug 2017 15:43:30 -0700 Subject: [PATCH] Initial changes to error messages --- lib/build-files.js | 14 +++++++++++++- lib/core/nav/HeaderNav.js | 6 ++++++ lib/server/readCategories.js | 2 +- lib/server/readMetadata.js | 9 +++++++-- lib/server/server.js | 5 ++++- lib/start-server.js | 14 +++++++++++++- package.json | 1 + 7 files changed, 45 insertions(+), 6 deletions(-) diff --git a/lib/build-files.js b/lib/build-files.js index e26f4701a4..fdd692161f 100755 --- a/lib/build-files.js +++ b/lib/build-files.js @@ -11,10 +11,22 @@ require("babel-register")({ babelrc: false, - only: [__dirname, process.cwd() + "/core"], + only: [__dirname, process.cwd() + "/core"], plugins: [require("./server/translate-plugin.js")], presets: ["react", "latest"] }); +// initial check that required files are present +const chalk = require("chalk"); +const fs = require("fs"); +const CWD = process.cwd(); + +if (!fs.existsSync(CWD + "/siteConfig.js")) { + console.error( + chalk.red("Error: No siteConfig.js file found in website folder!") + ); + process.exit(1); +} + const generate = require("./server/generate.js"); generate(); diff --git a/lib/core/nav/HeaderNav.js b/lib/core/nav/HeaderNav.js index cbdf609c78..94807165d2 100644 --- a/lib/core/nav/HeaderNav.js +++ b/lib/core/nav/HeaderNav.js @@ -128,6 +128,12 @@ class HeaderNav extends React.Component { "-" + link.doc; } + if (!Metadata[id]) { + throw new Error( + "A headerLink is specified with a document that does not exist. No document exists with id: " + + link.doc + ); + } href = this.props.config.baseUrl + Metadata[id].permalink; } else if (link.page) { if (fs.existsSync(CWD + "/pages/en/" + link.page + ".js")) { diff --git a/lib/server/readCategories.js b/lib/server/readCategories.js index c90310992e..8b19614679 100644 --- a/lib/server/readCategories.js +++ b/lib/server/readCategories.js @@ -56,7 +56,7 @@ function readCategories(sidebar) { if (metadata.next) { if (!articles[metadata.next]) { throw new Error( - "`next: " + metadata.next + "` in " + metadata.id + " doesn't exist" + "Improper sidebars.json file. Make sure that documents with the ids specified in sidebars.json exist and that no ids are repeated." ); } previous[articles[metadata.next].id] = metadata.id; diff --git a/lib/server/readMetadata.js b/lib/server/readMetadata.js index 90f7463af9..9ba22a0c36 100644 --- a/lib/server/readMetadata.js +++ b/lib/server/readMetadata.js @@ -32,7 +32,12 @@ if (fs.existsSync(CWD + "/languages.js")) { } function readSidebar() { - let allSidebars = require(CWD + "/sidebars.json"); + let allSidebars; + if (fs.existsSync(CWD + "/sidebars.json")) { + allSidebars = require(CWD + "/sidebars.json"); + } else { + allSidebars = {}; + } Object.assign(allSidebars, versionFallback.sidebarData()); const order = {}; @@ -155,7 +160,7 @@ function processMetadata(file) { metadata.previous = language + "-" + order[id].previous; } } - + return { metadata, rawContent: rawContent }; } diff --git a/lib/server/server.js b/lib/server/server.js index 516e86f7bc..712ba2baac 100644 --- a/lib/server/server.js +++ b/lib/server/server.js @@ -157,6 +157,10 @@ function execute(port) { }); const metadata = Metadata[links[url]]; + if (!metadata) { + next(); + return; + } const language = metadata.language; let file; @@ -438,7 +442,6 @@ function execute(port) { res.send(cssContent); }); - /* serve static content first from user folder then from docusaurus */ app.use( siteConfig.baseUrl + "docs/assets/", express.static(CWD + "/../docs/assets") diff --git a/lib/start-server.js b/lib/start-server.js index 7183b577cb..49163d46e0 100755 --- a/lib/start-server.js +++ b/lib/start-server.js @@ -11,11 +11,23 @@ require("babel-register")({ babelrc: false, - only: [__dirname, process.cwd() + "/core"], + only: [__dirname, process.cwd() + "/core"], plugins: [require("./server/translate-plugin.js")], presets: ["react", "latest"] }); +// initial check that required files are present +const chalk = require("chalk"); +const fs = require("fs"); +const CWD = process.cwd(); + +if (!fs.existsSync(CWD + "/siteConfig.js")) { + console.error( + chalk.red("Error: No siteConfig.js file found in website folder!") + ); + process.exit(1); +} + const program = require("commander"); program.option("--port ", "Specify port number").parse(process.argv); diff --git a/package.json b/package.json index 4fb48e9e29..906b8cfd24 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "babel-register": "^6.24.1", "babel-traverse": "^6.25.0", "babylon": "^6.17.4", + "chalk": "^2.1.0", "classnames": "^2.2.5", "commander": "^2.11.0", "diff": "^3.3.0",