Use Path module functions and properties in server and generate (#490)

This commit is contained in:
Joel Marcey 2018-03-12 16:01:40 -07:00 committed by GitHub
parent 7647ba3484
commit cbdab2ba11
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 96 additions and 75 deletions

View file

@ -31,6 +31,9 @@ function execute(port) {
const CWD = process.cwd();
const join = path.join;
const sep = path.sep;
// remove a module and child modules from require cache, so server does not have
// to be restarted
function removeModuleAndChildrenFromCache(moduleName) {
@ -68,17 +71,17 @@ function execute(port) {
}
function reloadMetadataBlog() {
if (fs.existsSync(__dirname + '/../core/MetadataBlog.js')) {
removeModuleAndChildrenFromCache('../core/MetadataBlog.js');
fs.removeSync(__dirname + '/../core/MetadataBlog.js');
if (fs.existsSync(join(__dirname, '..', 'core', 'MetadataBlog.js'))) {
removeModuleAndChildrenFromCache(join('..', 'core', 'MetadataBlog.js'));
fs.removeSync(join(__dirname, '..', 'core', 'MetadataBlog.js'));
}
readMetadata.generateMetadataBlog();
MetadataBlog = require('../core/MetadataBlog.js');
MetadataBlog = require(join('..', 'core', 'MetadataBlog.js'));
}
function reloadSiteConfig() {
removeModuleAndChildrenFromCache(CWD + '/siteConfig.js');
siteConfig = require(CWD + '/siteConfig.js');
removeModuleAndChildrenFromCache(join(CWD, 'siteConfig.js'));
siteConfig = require(join(CWD, '/siteConfig.js'));
if (siteConfig.highlight && siteConfig.highlight.hljs) {
siteConfig.highlight.hljs(require('highlight.js'));
@ -167,18 +170,15 @@ function execute(port) {
let file;
if (metadata.original_id) {
if (env.translation.enabled && metadata.language !== 'en') {
file =
CWD + '/translated_docs/' + metadata.language + '/' + metadata.source;
file = join(CWD, 'translated_docs', metadata.language, metadata.source);
} else {
file = CWD + '/versioned_docs/' + metadata.source;
file = join(CWD, 'versioned_docs' + metadata.source);
}
} else {
if (!env.translation.enabled || metadata.language === 'en') {
file =
CWD + '/../' + readMetadata.getDocsPath() + '/' + metadata.source;
file = join(CWD, '..', readMetadata.getDocsPath(), metadata.source);
} else {
file =
CWD + '/translated_docs/' + metadata.language + '/' + metadata.source;
file = join(CWD, 'translated_docs', metadata.language, metadata.source);
}
}
@ -274,8 +274,8 @@ function execute(port) {
// handle all requests for blog pages and posts
app.get(/blog\/.*html$/, (req, res) => {
// generate all of the blog pages
removeModuleAndChildrenFromCache('../core/BlogPageLayout.js');
const BlogPageLayout = require('../core/BlogPageLayout.js');
removeModuleAndChildrenFromCache(join('..', 'core', 'BlogPageLayout.js'));
const BlogPageLayout = require(join('..', 'core', 'BlogPageLayout.js'));
const blogPages = {};
// make blog pages with 10 posts per page
const perPage = 10;
@ -316,7 +316,7 @@ function execute(port) {
let file = parts[1];
file = file.replace(/\.html$/, '.md');
file = file.replace(new RegExp('/', 'g'), '-');
file = CWD + '/blog/' + file;
file = join(CWD, 'blog', file);
const result = readMetadata.extractMetadata(
fs.readFileSync(file, {encoding: 'utf8'})
@ -333,8 +333,8 @@ function execute(port) {
metadata.id = metadata.title;
let language = 'en';
removeModuleAndChildrenFromCache('../core/BlogPostLayout.js');
const BlogPostLayout = require('../core/BlogPostLayout.js');
removeModuleAndChildrenFromCache(join('..', 'core', 'BlogPostLayout.js'));
const BlogPostLayout = require(join('..', 'core', 'BlogPostLayout.js'));
const blogPostComp = (
<BlogPostLayout
@ -352,19 +352,19 @@ function execute(port) {
app.get('*.html', (req, res, next) => {
// look for user provided html file first
let htmlFile = req.path.toString().replace(siteConfig.baseUrl, '');
htmlFile = CWD + '/pages/' + htmlFile;
htmlFile = join(CWD, 'pages', htmlFile);
if (
fs.existsSync(htmlFile) ||
fs.existsSync(
(htmlFile = htmlFile.replace(
path.basename(htmlFile),
'en/' + path.basename(htmlFile)
join('en', path.basename(htmlFile))
))
)
) {
if (siteConfig.wrapPagesHTML) {
removeModuleAndChildrenFromCache('../core/Site.js');
const Site = require('../core/Site.js');
removeModuleAndChildrenFromCache(join('..', 'core', 'Site.js'));
const Site = require(join('..', 'core', 'Site.js'));
const str = renderToStaticMarkup(
<Site
language="en"
@ -388,7 +388,7 @@ function execute(port) {
// look for user provided react file either in specified path or in path for english files
let file = req.path.toString().replace(/\.html$/, '.js');
file = file.replace(siteConfig.baseUrl, '');
let userFile = CWD + '/pages/' + file;
let userFile = join(CWD, 'pages', file);
let language = env.translation.enabled ? 'en' : '';
const regexLang = /(.*)\/.*\.html$/;
@ -403,9 +403,9 @@ function execute(port) {
language = parts[i];
}
}
let englishFile = CWD + '/pages/' + file;
let englishFile = join(CWD, 'pages', file);
if (language && language !== 'en') {
englishFile = englishFile.replace('/' + language + '/', '/en/');
englishFile = englishFile.replace(sep + language + sep, sep + 'en' + sep);
}
// check for: a file for the page, an english file for page with unspecified language, or an
@ -415,14 +415,14 @@ function execute(port) {
fs.existsSync(
(userFile = userFile.replace(
path.basename(userFile),
'en/' + path.basename(userFile)
'en' + sep + path.basename(userFile)
))
) ||
fs.existsSync((userFile = englishFile))
) {
// copy into docusaurus so require paths work
let parts = userFile.split('pages/');
let tempFile = __dirname + '/../pages/' + parts[1];
let parts = userFile.split('pages' + sep);
let tempFile = join(__dirname, '..', 'pages', parts[1]);
tempFile = tempFile.replace(
path.basename(file),
'temp' + path.basename(file)
@ -433,8 +433,8 @@ function execute(port) {
// render into a string
removeModuleAndChildrenFromCache(tempFile);
const ReactComp = require(tempFile);
removeModuleAndChildrenFromCache('../core/Site.js');
const Site = require('../core/Site.js');
removeModuleAndChildrenFromCache(join('..', 'core', 'Site.js'));
const Site = require(join('..', 'core', 'Site.js'));
translate.setLanguage(language);
const str = renderToStaticMarkup(
<Site
@ -456,13 +456,15 @@ function execute(port) {
// generate the main.css file by concatenating user provided css to the end
app.get(/main\.css$/, (req, res) => {
const mainCssPath =
__dirname +
'/../static/' +
req.path.toString().replace(siteConfig.baseUrl, '/');
const mainCssPath = join(
__dirname,
'..',
'static',
req.path.toString().replace(siteConfig.baseUrl, '/')
);
let cssContent = fs.readFileSync(mainCssPath, {encoding: 'utf8'});
let files = glob.sync(CWD + '/static/**/*.css');
let files = glob.sync(join(CWD, 'static', '**', '*.css'));
files.forEach(file => {
if (isSeparateCss(file)) {
@ -510,15 +512,15 @@ function execute(port) {
// serve static assets from these locations
app.use(
siteConfig.baseUrl + 'docs/assets/',
express.static(CWD + '/../' + readMetadata.getDocsPath() + '/assets')
join(siteConfig.baseUrl, 'docs', 'assets'),
express.static(join(CWD, '..', readMetadata.getDocsPath(), 'assets'))
);
app.use(
siteConfig.baseUrl + 'blog/assets/',
express.static(CWD + '/blog/assets')
join(siteConfig.baseUrl, 'blog', 'assets'),
express.static(join(CWD, 'blog', 'assets'))
);
app.use(siteConfig.baseUrl, express.static(CWD + '/static'));
app.use(siteConfig.baseUrl, express.static(__dirname + '/../static'));
app.use(siteConfig.baseUrl, express.static(join(CWD, 'static')));
app.use(siteConfig.baseUrl, express.static(join(__dirname, '..', 'static')));
// "redirect" requests to pages ending with "/" or no extension so that,
// for example, request to "blog" returns same result as "blog/index.html"