Skip to content

Commit

Permalink
Use Path module functions and properties in server and generate
Browse files Browse the repository at this point in the history
We want to be more robust in our path string handling in order to not fail on various platforms.

Fixes facebook#468
  • Loading branch information
JoelMarcey committed Mar 8, 2018
1 parent e8bc81f commit 9f89784
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 43 deletions.
15 changes: 7 additions & 8 deletions lib/server/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ function execute() {
const sitemap = require('./sitemap.js');

const join = path.join;
const sep = path.sep;

// create the folder path for a file if it does not exist, then write the file
function writeFileAndCreateFolder(file, content) {
Expand Down Expand Up @@ -315,8 +316,7 @@ function execute() {
files.forEach(file => {
let targetFile = join(
buildDir,
// TODO: use x-platform path functions
file.split('/static/')[1] || ''
file.split(sep + 'static' + sep)[1] || ''
);
// parse css files to replace colors according to siteConfig
if (file.match(/\.css$/)) {
Expand Down Expand Up @@ -394,7 +394,7 @@ function execute() {

fs.writeFileSync(mainCss, cssContent);
} else if (!fs.lstatSync(file).isDirectory()) {
let parts = file.split('/static/');
let parts = file.split(sep + 'static' + sep);
let targetFile = join(buildDir, parts[1]);
mkdirp.sync(path.dirname(targetFile));
fs.copySync(file, targetFile);
Expand Down Expand Up @@ -434,8 +434,7 @@ function execute() {
// skip conversion from english file if a file exists for this language
if (
language !== 'en' &&
// TODO: use path functions
fs.existsSync(file.replace('/en/', '/' + language + '/'))
fs.existsSync(file.replace(sep + 'en' + sep, sep + language + sep))
) {
continue;
}
Expand All @@ -450,7 +449,7 @@ function execute() {
);
writeFileAndCreateFolder(
// TODO: use path functions
targetFile.replace('/en/', '/' + language + '/'),
targetFile.replace(sep + 'en' + sep, sep + language + sep),
str
);
}
Expand All @@ -463,7 +462,7 @@ function execute() {
<ReactComp language={language} />
</Site>
);
writeFileAndCreateFolder(targetFile.replace('/en/', '/'), str);
writeFileAndCreateFolder(targetFile.replace(sep + 'en' + sep, sep), str);
} else {
// allow for rendering of other files not in pages/en folder
let language = env.translation.enabled ? 'en' : '';
Expand All @@ -473,7 +472,7 @@ function execute() {
<ReactComp language={language} />
</Site>
);
writeFileAndCreateFolder(targetFile.replace('/en/', '/'), str);
writeFileAndCreateFolder(targetFile.replace(sep + en + sep, sep), str);
}
fs.removeSync(tempFile);
} else if (siteConfig.wrapPagesHTML && file.match(/\.html$/)) {
Expand Down
76 changes: 41 additions & 35 deletions lib/server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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'));
Expand Down Expand Up @@ -168,17 +171,17 @@ function execute(port) {
if (metadata.original_id) {
if (env.translation.enabled && metadata.language !== 'en') {
file =
CWD + '/translated_docs/' + metadata.language + '/' + metadata.source;
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;
join(CWD, '..', readMetadata.getDocsPath(), metadata.source);
} else {
file =
CWD + '/translated_docs/' + metadata.language + '/' + metadata.source;
join(CWD, 'translated_docs', metadata.language, metadata.source);
}
}

Expand Down Expand Up @@ -274,8 +277,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;
Expand Down Expand Up @@ -316,7 +319,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'})
Expand All @@ -333,8 +336,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
Expand All @@ -352,7 +355,7 @@ 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(
Expand All @@ -363,8 +366,8 @@ function execute(port) {
)
) {
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"
Expand Down Expand Up @@ -403,9 +406,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
Expand All @@ -415,14 +418,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)
Expand All @@ -433,8 +436,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
Expand All @@ -457,12 +460,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, '/');
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)) {
Expand Down Expand Up @@ -510,15 +516,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"
Expand Down

0 comments on commit 9f89784

Please sign in to comment.