Skip to content

Commit

Permalink
Don't start server if versions.js is missing but versioning is enabled (
Browse files Browse the repository at this point in the history
#714)

* don't start server if versions.js missing but versioning is enabled

* refactor

* fix nits

* refactor & address review
  • Loading branch information
endiliey authored and yangshun committed Jun 3, 2018
1 parent 2bd9a14 commit d28b864
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
15 changes: 15 additions & 0 deletions lib/server/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
}
}

Expand Down
6 changes: 6 additions & 0 deletions lib/start-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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 <number>', 'Specify port number').parse(process.argv);
Expand Down
10 changes: 3 additions & 7 deletions lib/version.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}

Expand Down

0 comments on commit d28b864

Please sign in to comment.