Scanner and compiler work properly

This commit is contained in:
LordMathis 2017-10-29 17:39:28 +01:00
parent 1bc2c187df
commit 1e904ba4ec
2 changed files with 20 additions and 15 deletions

View File

@ -40,30 +40,31 @@ function compile(filepath, data, fileData, callback) {
const renderedpath = path.join(process.cwd(), config.renderPath, `${metadata.filename}.html`); const renderedpath = path.join(process.cwd(), config.renderPath, `${metadata.filename}.html`);
data.posts.push(post); fs.writeFile(renderedpath, rendered, (err) => {
if (err) callback(err);
});
fs.writeFile(renderedpath, rendered, callback); callback(null, post);
} }
function Compiler(data) { function Compiler(data) {
this.data = data; this.data = data;
} }
Compiler.prototype.addFile = function (filepath) { Compiler.prototype.addFile = function(filepath, callback) {
console.log("Foo", this.data);
async.waterfall([ async.waterfall([
fs.readFile.bind(fs, filepath, 'utf8'), fs.readFile.bind(fs, filepath, 'utf8'),
compile.bind(compile, filepath, this.data), compile.bind(compile, filepath, this.data),
], (err) => { ], (err, result) => {
if (err) throw err; if (err) throw err;
this.data.posts.push(result);
callback();
}); });
}; };
Compiler.prototype.writeData = function (callback) { Compiler.prototype.writeData = function(callback) {
console.log('Bar', this.data);
const dataPath = path.join(process.cwd(), 'src/utils/data.json'); const dataPath = path.join(process.cwd(), 'src/utils/data.json');
jsonfile.writeFile(dataPath, this.data, callback); jsonfile.writeFile(dataPath, this.data, callback);
}; };

View File

@ -13,12 +13,16 @@ module.exports = function() {
fs.readdir(config.contentPath, callback); fs.readdir(config.contentPath, callback);
} }
function compile(files, callback) { function compileFile(file, callback) {
files.forEach(file => {
const filePath = path.join(process.cwd(), config.contentPath, file); const filePath = path.join(process.cwd(), config.contentPath, file);
compiler.addFile(filePath); compiler.addFile(filePath, callback);
}
function compile(files, callback) {
async.each(files, compileFile, (err) => {
if (err) throw err;
callback();
}); });
return callback(null);
} }
function writeData(callback) { function writeData(callback) {
@ -29,7 +33,7 @@ module.exports = function() {
readdir, readdir,
compile, compile,
writeData writeData
], (err => { ], (err) => {
if(err) throw err; if(err) throw err;
})); });
} }