Replace step with async

This commit is contained in:
LordMathis 2017-10-27 18:11:14 +02:00
parent 782ebb3b4b
commit 7b0e3a65fc
4 changed files with 54 additions and 46 deletions

View File

@ -15,6 +15,7 @@
"author": "Matúš Námešný", "author": "Matúš Námešný",
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"async": "^2.5.0",
"axios": "^0.17.0", "axios": "^0.17.0",
"babel-cli": "^6.24.1", "babel-cli": "^6.24.1",
"babel-polyfill": "^6.7.4", "babel-polyfill": "^6.7.4",
@ -29,8 +30,7 @@
"react": "^15.0.1", "react": "^15.0.1",
"react-dom": "^15.0.1", "react-dom": "^15.0.1",
"react-redux": "^4.4.4", "react-redux": "^4.4.4",
"react-router-dom": "^4.1.1", "react-router-dom": "^4.1.1"
"step": "^1.0.0"
}, },
"devDependencies": { "devDependencies": {
"babel-core": "^6.7.6", "babel-core": "^6.7.6",

View File

@ -3,7 +3,7 @@ const fs = require('fs');
const path = require('path'); const path = require('path');
const moment = require('moment'); const moment = require('moment');
const jsonfile = require('jsonfile'); const jsonfile = require('jsonfile');
const Step = require('step'); const async = require('async');
const fm = require('front-matter'); const fm = require('front-matter');
const config = require('../static/config/config.json'); const config = require('../static/config/config.json');
@ -11,12 +11,6 @@ function readFile(filepath, callback) {
fs.readFile(filepath, 'utf-8', callback); fs.readFile(filepath, 'utf-8', callback);
} }
function writeRenderedFile(renderedpath, result) {
fs.writeFile(renderedpath, result, (err) => {
if (err) throw err;
});
}
function render(file) { function render(file) {
const md = new MarkdownIt(); const md = new MarkdownIt();
return md.render(file); return md.render(file);
@ -36,42 +30,44 @@ function fileMetadata(filepath) {
return metadata; return metadata;
} }
function compile(fileData, callback) {
const frontMatter = fm(fileData);
const rendered = render(frontMatter.body);
const metadata = fileMetadata(filepath);
const post = {
published: moment().format('MMMM Do YYYY'),
filename: metadata.filename,
title: frontMatter.attributes.title,
summary: frontMatter.attributes.summary,
};
const renderedpath = path.join(process.cwd(), config.renderPath, `${metadata.filename}.html`);
this.data.posts.push(post);
fs.writeFile(renderedpath, rendered, callback);
}
function Compiler(data) { function Compiler(data) {
this.data = data; this.data = data;
} }
Compiler.prototype.addFile = function (filepath) { Compiler.prototype.addFile = function (filepath) {
Step(
function loadFiles() {
readFile(filepath, this.parallel());
},
(err, fileData) => {
if (err) throw err;
const frontMatter = fm(fileData); async.waterfall([
const rendered = render(frontMatter.body); readFile,
const metadata = fileMetadata(filepath); compile,
], (err) => {
const post = {
published: moment().format('MMMM Do YYYY'),
filename: metadata.filename,
title: frontMatter.attributes.title,
summary: frontMatter.attributes.summary,
};
const renderedpath = path.join(process.cwd(), config.renderPath, `${metadata.filename}.html`);
this.data.posts.push(post);
writeRenderedFile(renderedpath, rendered);
}
);
};
Compiler.prototype.writeData = function () {
const dataPath = path.join(process.cwd(), 'src/utils/data.json');
jsonfile.writeFile(dataPath, this.data, (err) => {
if (err) throw err; if (err) throw err;
}); });
};
Compiler.prototype.writeData = function (callback) {
const dataPath = path.join(process.cwd(), 'src/utils/data.json');
console.log(JSON.stringify(this.data));
jsonfile.writeFile(dataPath, this.data, callback);
}; };
module.exports = Compiler; module.exports = Compiler;

View File

@ -1,19 +1,35 @@
const fs = require('fs'); const fs = require('fs');
const path = require('path'); const path = require('path');
const async = require('async');
const Compiler = require('./compiler'); const Compiler = require('./compiler');
const config = require('../static/config/config.json'); const config = require('../static/config/config.json');
const data = require('./data.json'); const data = require('./data.json');
module.exports = function() { module.exports = function() {
console.log(data);
var compiler = new Compiler(data); var compiler = new Compiler(data);
fs.readdir(config.contentPath, (err, files) => { function readdir(callback) {
fs.readdir(config.contentPath, callback);
}
function compile(files, callback) {
files.forEach(file => { 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);
}); });
compiler.writeData(); return callback(null);
}); }
function writeData(callback) {
compiler.writeData(callback);
}
async.waterfall([
readdir,
compile,
writeData
], (err => {
if(err) throw err;
}));
} }

View File

@ -169,7 +169,7 @@ async-foreach@^0.1.3:
version "0.1.3" version "0.1.3"
resolved "https://registry.yarnpkg.com/async-foreach/-/async-foreach-0.1.3.tgz#36121f845c0578172de419a97dbeb1d16ec34542" resolved "https://registry.yarnpkg.com/async-foreach/-/async-foreach-0.1.3.tgz#36121f845c0578172de419a97dbeb1d16ec34542"
async@^2.1.2, async@^2.1.5: async@^2.1.2, async@^2.1.5, async@^2.5.0:
version "2.5.0" version "2.5.0"
resolved "https://registry.yarnpkg.com/async/-/async-2.5.0.tgz#843190fd6b7357a0b9e1c956edddd5ec8462b54d" resolved "https://registry.yarnpkg.com/async/-/async-2.5.0.tgz#843190fd6b7357a0b9e1c956edddd5ec8462b54d"
dependencies: dependencies:
@ -4059,10 +4059,6 @@ stdout-stream@^1.4.0:
dependencies: dependencies:
readable-stream "^2.0.1" readable-stream "^2.0.1"
step@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/step/-/step-1.0.0.tgz#b300e9d2ae9057d4d78633aae2303813a94bdff2"
stream-browserify@^2.0.1: stream-browserify@^2.0.1:
version "2.0.1" version "2.0.1"
resolved "https://registry.yarnpkg.com/stream-browserify/-/stream-browserify-2.0.1.tgz#66266ee5f9bdb9940a4e4514cafb43bb71e5c9db" resolved "https://registry.yarnpkg.com/stream-browserify/-/stream-browserify-2.0.1.tgz#66266ee5f9bdb9940a4e4514cafb43bb71e5c9db"