From 1e904ba4ec90d02f7f5db67b66becd9b87bf023e Mon Sep 17 00:00:00 2001 From: LordMathis Date: Sun, 29 Oct 2017 17:39:28 +0100 Subject: [PATCH] Scanner and compiler work properly --- src/utils/compiler.js | 19 ++++++++++--------- src/utils/scanner.js | 16 ++++++++++------ 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/src/utils/compiler.js b/src/utils/compiler.js index ce9caf2..e88492c 100644 --- a/src/utils/compiler.js +++ b/src/utils/compiler.js @@ -40,30 +40,31 @@ function compile(filepath, data, fileData, callback) { 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) { this.data = data; } -Compiler.prototype.addFile = function (filepath) { - - console.log("Foo", this.data); - +Compiler.prototype.addFile = function(filepath, callback) { async.waterfall([ fs.readFile.bind(fs, filepath, 'utf8'), compile.bind(compile, filepath, this.data), - ], (err) => { + ], (err, result) => { if (err) throw err; + + this.data.posts.push(result); + callback(); }); }; -Compiler.prototype.writeData = function (callback) { - console.log('Bar', this.data); +Compiler.prototype.writeData = function(callback) { const dataPath = path.join(process.cwd(), 'src/utils/data.json'); jsonfile.writeFile(dataPath, this.data, callback); }; diff --git a/src/utils/scanner.js b/src/utils/scanner.js index e7af96e..a0e7703 100644 --- a/src/utils/scanner.js +++ b/src/utils/scanner.js @@ -13,12 +13,16 @@ module.exports = function() { fs.readdir(config.contentPath, callback); } + function compileFile(file, callback) { + const filePath = path.join(process.cwd(), config.contentPath, file); + compiler.addFile(filePath, callback); + } + function compile(files, callback) { - files.forEach(file => { - const filePath = path.join(process.cwd(), config.contentPath, file); - compiler.addFile(filePath); + async.each(files, compileFile, (err) => { + if (err) throw err; + callback(); }); - return callback(null); } function writeData(callback) { @@ -29,7 +33,7 @@ module.exports = function() { readdir, compile, writeData - ], (err => { + ], (err) => { if(err) throw err; - })); + }); }