Compiler object is created properly

This commit is contained in:
LordMathis 2017-10-27 16:48:41 +02:00
parent c8f23483e8
commit 782ebb3b4b
3 changed files with 12 additions and 6 deletions

View File

@ -36,7 +36,7 @@ function fileMetadata(filepath) {
return metadata;
}
module.exports = function Compiler(data) {
function Compiler(data) {
this.data = data;
}
@ -59,7 +59,7 @@ Compiler.prototype.addFile = function (filepath) {
summary: frontMatter.attributes.summary,
};
const renderedpath = path.join(config.renderPath, `${metadata.filename}.html`);
const renderedpath = path.join(process.cwd(), config.renderPath, `${metadata.filename}.html`);
this.data.posts.push(post);
writeRenderedFile(renderedpath, rendered);
@ -68,8 +68,10 @@ Compiler.prototype.addFile = function (filepath) {
};
Compiler.prototype.writeData = function () {
const dataPath = path.join(process.cwd(), 'server/utils/data.json');
jsonfile.writeFile(dataPath, data, (err) => {
const dataPath = path.join(process.cwd(), 'src/utils/data.json');
jsonfile.writeFile(dataPath, this.data, (err) => {
if (err) throw err;
});
};
module.exports = Compiler;

1
src/utils/data.json Normal file
View File

@ -0,0 +1 @@
{"posts":[]}

View File

@ -1,15 +1,18 @@
const fs = require('fs');
const path = require('path');
const Compiler = require('./compiler');
const config = require('../static/config/config.json');
const data = require('./data.json');
module.exports = function() {
var compiler = Compiler(data);
console.log(data);
var compiler = new Compiler(data);
fs.readdir(config.contentPath, (err, files) => {
files.forEach(file => {
compiler.addFile(file);
const filePath = path.join(process.cwd(), config.contentPath, file);
compiler.addFile(filePath);
});
compiler.writeData();
});