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`);
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);
};

View File

@ -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;
}));
});
}