From d28b864a59fabeea45add8c090a13de7d0530de5 Mon Sep 17 00:00:00 2001 From: Endilie Yacop Sucipto Date: Sun, 3 Jun 2018 14:45:36 +0800 Subject: [PATCH] Don't start server if versions.js is missing but versioning is enabled (#714) * don't start server if versions.js missing but versioning is enabled * refactor * fix nits * refactor & address review --- lib/server/env.js | 15 +++++++++++++++ lib/start-server.js | 6 ++++++ lib/version.js | 10 +++------- 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/lib/server/env.js b/lib/server/env.js index ea0274257b89..770d78ed2e11 100644 --- a/lib/server/env.js +++ b/lib/server/env.js @@ -8,12 +8,14 @@ const CWD = process.cwd(); const fs = require('fs-extra'); const path = require('path'); +const chalk = require('chalk'); const siteConfig = require(CWD + '/siteConfig.js'); const join = path.join; const languages_js = join(CWD, 'languages.js'); const versions_json = join(CWD, 'versions.json'); +const versions_js = join(CWD, 'pages/en/versions.js'); class Translation { constructor() { @@ -46,10 +48,19 @@ class Versioning { this.enabled = false; this.defaultVersion = null; this.versions = []; + this.missingVersionsPage = false; this._load(); } + printMissingVersionsPageError() { + console.error( + `${chalk.yellow('No versions.js file found!')}` + + `\nYou should create your versions.js file in pages/en directory.` + + `\nPlease refer to https://docusaurus.io/docs/en/versioning.html.` + ); + } + _load() { if (fs.existsSync(versions_json)) { this.enabled = true; @@ -58,6 +69,10 @@ class Versioning { ? siteConfig.defaultVersionShown : this.versions[0]; // otherwise show the latest version (other than next/master) } + + if (!fs.existsSync(versions_js)) { + this.missingVersionsPage = true; + } } } diff --git a/lib/start-server.js b/lib/start-server.js index 55c8755844b0..71576f86f81e 100755 --- a/lib/start-server.js +++ b/lib/start-server.js @@ -26,6 +26,7 @@ const chalk = require('chalk'); const fs = require('fs'); const openBrowser = require('react-dev-utils/openBrowser'); const CWD = process.cwd(); +const env = require('./server/env.js'); if (!fs.existsSync(CWD + '/siteConfig.js')) { console.error( @@ -34,6 +35,11 @@ if (!fs.existsSync(CWD + '/siteConfig.js')) { process.exit(1); } +if (env.versioning.enabled && env.versioning.missingVersionsPage) { + env.versioning.printMissingVersionsPageError(); + process.exit(1); +} + const program = require('commander'); program.option('--port ', 'Specify port number').parse(process.argv); diff --git a/lib/version.js b/lib/version.js index 875db756bb50..1d835216b175 100755 --- a/lib/version.js +++ b/lib/version.js @@ -14,6 +14,7 @@ const mkdirp = require('mkdirp'); const chalk = require('chalk'); const readMetadata = require('./server/readMetadata.js'); const versionFallback = require('./server/versionFallback.js'); +const env = require('./server/env.js'); const CWD = process.cwd(); let versions; @@ -33,13 +34,8 @@ program }) .parse(process.argv); -const hasVersionFile = fs.existsSync(CWD + '/pages/en/versions.js'); -if (!hasVersionFile) { - console.error( - `${chalk.yellow( - 'No versions.js file found!' - )}\nYou should create your versions.js file in pages/en directory.\nPlease refer to https://docusaurus.io/docs/en/versioning.html.` - ); +if (env.versioning.missingVersionsPage) { + env.versioning.printMissingVersionsPageError(); process.exit(1); }