Skip to content

Commit

Permalink
Support JavaScript for data
Browse files Browse the repository at this point in the history
  • Loading branch information
astronomersiva committed Sep 28, 2019
1 parent 34f4ed8 commit 0f99859
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
17 changes: 17 additions & 0 deletions lib/utils/loadData.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
function requireUncached(module) {
delete require.cache[require.resolve(module)];
return require(module);
}

module.exports = function(logger) {
const fs = require('fs-extra');
const filterFileList = require('./filterFileList');
Expand Down Expand Up @@ -26,5 +31,17 @@ module.exports = function(logger) {
}
});

let jsConfigs = filterFileList(files, ['js', 'json']);
jsConfigs.map((config) => {
try {
let name = path.parse(config).name;
let configFile = path.join(process.cwd(), DATA, config);

data[name] = requireUncached(configFile);
} catch (error) {
logger.fatal(`Error loading file ${chalk.red(config)} \n\n${(error)}`);
}
})

return data;
}
25 changes: 17 additions & 8 deletions lib/utils/watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,28 @@ module.exports = function(site, runTask) {
ignoreInitial: true
};

let postsWatcher = chokidar.watch([`${POSTS}/**/*md`, `${POSTS}/**/*html`, `${DATA}/*yml`], watchOptions);
let postsWatcher = chokidar.watch([
`${POSTS}/**/*md`,
`${POSTS}/**/*html`
], watchOptions);

postsWatcher.on('all', async(event, path) => {
await site.handlePostChange(event, path);
// Need to further refine this to only rebuild changed files
runTask([
runTask([[
'generatePostsFromMarkdown',
'generatePages',
'generatePagesForTags'
], site, { event, path });
]], site, { event, path });
});

let dataWatcher = chokidar.watch([`${DATA}/*yml`, `${DATA}/*yaml`], watchOptions);
let dataWatcher = chokidar.watch([
`${DATA}/*yml`,
`${DATA}/*yaml`,
`${DATA}/*js`,
`${DATA}/*json`
], watchOptions);

dataWatcher.on('all', (event, path) => {
if (event === 'add') {
let size = fs.statSync(path).size;
Expand All @@ -41,22 +50,22 @@ module.exports = function(site, runTask) {
site.loadData();

// Need to further refine this to only rebuild changed files
runTask([
runTask([[
'generatePostsFromMarkdown',
'generatePages',
'generatePagesForTags'
], site);
]], site);
})

let layoutsWatcher = chokidar.watch(`${LAYOUTS}/*html`, watchOptions);

layoutsWatcher.on('all', () => {
// Need to further refine this to only rebuild changed files
runTask([
runTask([[
'generatePostsFromMarkdown',
'generatePages',
'generatePagesForTags'
], site);
]], site);
});

let pagesWatcher = chokidar.watch(`${PAGES}/*html`, watchOptions);
Expand Down

0 comments on commit 0f99859

Please sign in to comment.