fix: reload siteConfig.js automatically when locally served page is refreshed (#1509)

* fix: livereload siteConfig

* fix test

* nits
This commit is contained in:
Endi 2019-05-22 22:31:51 +07:00 committed by GitHub
parent aa157969cf
commit 166816af40
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 53 additions and 42 deletions

View file

@ -60,6 +60,18 @@ function execute(port, host) {
let MetadataBlog;
let siteConfig;
function reloadSiteConfig() {
const siteConfigPath = join(CWD, 'siteConfig.js');
removeModuleAndChildrenFromCache(siteConfigPath);
const oldBaseUrl = siteConfig && siteConfig.baseUrl;
siteConfig = loadConfig(siteConfigPath);
if (oldBaseUrl && oldBaseUrl !== siteConfig.baseUrl) {
console.log('Base url has changed. Please restart server ...');
process.exit();
}
}
function reloadMetadata() {
removeModuleAndChildrenFromCache('./readMetadata.js');
readMetadata.generateMetadataDocs();
@ -72,16 +84,11 @@ function execute(port, host) {
removeModuleAndChildrenFromCache(join('..', 'core', 'MetadataBlog.js'));
fs.removeSync(join(__dirname, '..', 'core', 'MetadataBlog.js'));
}
readMetadata.generateMetadataBlog();
reloadSiteConfig();
readMetadata.generateMetadataBlog(siteConfig);
MetadataBlog = require(join('..', 'core', 'MetadataBlog.js'));
}
function reloadSiteConfig() {
const siteConfigPath = join(CWD, 'siteConfig.js');
removeModuleAndChildrenFromCache(siteConfigPath);
siteConfig = loadConfig(siteConfigPath);
}
function requestFile(url, res, notFoundCallback) {
request.get(url, (error, response, body) => {
if (!error) {
@ -119,10 +126,11 @@ function execute(port, host) {
next();
return;
}
reloadSiteConfig();
const rawContent = metadataUtils.extractMetadata(file).rawContent;
removeModuleAndChildrenFromCache('../core/DocsLayout.js');
const mdToHtml = metadataUtils.mdToHtml(Metadata, siteConfig);
res.send(docs.getMarkup(rawContent, mdToHtml, metadata));
res.send(docs.getMarkup(rawContent, mdToHtml, metadata, siteConfig));
});
app.get(routing.sitemap(siteConfig), (req, res) => {
@ -177,6 +185,7 @@ function execute(port, host) {
});
app.get(routing.page(siteConfig), (req, res, next) => {
reloadSiteConfig();
// Look for user-provided HTML file first.
let htmlFile = req.path.toString().replace(siteConfig.baseUrl, '');
htmlFile = join(CWD, 'pages', htmlFile);