crunch-node/build.js

158 lines
5 KiB
JavaScript

var fs = require('fs');
var mkdirp = require('mkdirp');
var jade = require('jade');
var markdown = require( "markdown" ).markdown;
var async = require('async');
var ncp = require('ncp').ncp;
var helper = require('./helper.js');
var db = require('./db.js');
// Build all archive pages associated with a particular post ID
// This currently calls the functions to make the yearly and monthly archives.
var buildPostArchives = function (id, cb) {
db.getPostById(id, function(post) {
console.log('Building dependencies for Post: '+id);
var date = new Date(post.postDate);
var year = date.getFullYear();
console.log('Building archive for year: '+year);
var month = date.getMonth()+1;
async.parallel({
monthArchiveErr: function(callback) {
buildPostMonthArchive(year, function(err) {
callback(null, err);
});
},
yearArchiveErr: function(callback) {
buildPostYearArchive(month, function(err) {
callback(null, err);
});
}
}, function(err, results) {
if (!yearArchiveErr && !monthArchiveErr) {
cb(null);
}
else if (yearArchiveErr) {
cb(yearArchiveErr);
}
else if (monthArchiveErr) {
cb(monthArchiveErr);
}
else {
cb(null);
}
});
});
}
exports.buildPostArchives = buildPostArchives;
// Function that builds the yearly archive pages.
// TODO: make this not a skeleton.
// TODO: create pagination.
var buildPostYearArchive = function (year, cb) {
console.log('Building archive for year: '+year);
cb(null);
}
exports.buildPostYearArchive = buildPostYearArchive;
// Function that builds the monthly archive pages.
// TODO: Make this not a skeleton.
// TODO: create pagination.
var buildPostMonthArchive = function (month, cb) {
console.log('Building archive for month: '+month);
cb(null);
}
exports.buildPostMonthArchive = buildPostMonthArchive;
// Function to build a single post page.
// TODO: (re)build images as required.
var buildPost = function (id, cb) {
db.getPostById(id, function(post) {
console.log('Fetching post id: '+id);
var title = post.title;
var content = markdown.toHTML(post.markdown);
var slug = post.slug;
var postDate = helper.epochToShortDateString(post.postDate);
var url = '/blog/'+postDate.slice(0,4)+'/'+postDate.slice(5,7)+'/'+slug+'/';
var filepath = 'build'+url;
var filename = filepath+'index.html';
var options = {
pretty: false,
title: title,
content: content,
slug: slug,
url: url,
postDate: postDate
};
console.log('Rendering post: '+title);
var jadeOut = jade.renderFile('views/render-post.jade', options);
console.log('Creating directory: '+filepath);
mkdirp(filepath, function(err) {
if (err) {
console.log(err);
}
else {
console.log('Writing to file: '+filename);
fs.writeFile(filename, jadeOut, 'utf-8', function(err) {
if (err) console.log(err);
cb(err);
});
}
});
});
}
exports.buildPost = buildPost;
// Function to build copy over the static folder to build directory if
// it has a earlier modified time.
var buildStatic = function(cb) {
var sourceFolder = 'data/static';
var destFolder = 'build/static';
async.parallel({
sourceMtime: function(cb) {
console.log('Getting stats for '+sourceFolder);
fs.stat(sourceFolder, function(err, sstats) {
if (err) console.log(err);
console.log(sourceFolder+' mtime: '+sstats.mtime);
cb(null, sstats.mtime);
});
},
destMtime: function(cb) {
console.log('Getting stats for '+destFolder);
fs.stat(destFolder, function(err, dstats) {
if (err) console.log(err);
console.log(destFolder+' mtime: '+dstats.mtime);
cb(null, dstats.mtime);
});
}
}, function(err, results) {
if (results.sourceMtime > results.destMtime) {
console.log(destFolder+' is older than '+sourceFolder);
fs.unlink(destFolder, function(err) {
console.log(destFolder+' deleted.');
ncp(sourceFolder, destFolder, function(err) {
if (!err) {
console.log(sourceFolder+' copied to '+destFolder);
cb(null);
}
else {
cb(err);
}
});
});
}
else {
console.log(destFolder+' is not older than '+sourceFolder+' ignoring...');
}
});
}
exports.buildStatic = buildStatic;