Skip to content

Instantly share code, notes, and snippets.

@sp90
Created September 27, 2016 09:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sp90/886f50cdc430896788fe8e48f9073049 to your computer and use it in GitHub Desktop.
Save sp90/886f50cdc430896788fe8e48f9073049 to your computer and use it in GitHub Desktop.
Static site generator
// Native modules
var path = require('path');
var fs = require('fs');
// Third party modules
var _ = require('lodash');
var config = {
numberOfPosts: 10000,
template: function(name) {
name = name + '.hbs';
return path.join(__dirname, '/templates/', name);
},
dist: path.join(__dirname, '/dist')
};
// Self build modules
var cleanDirectory = require('./lib-cleanDirectory');
var file = require('./lib-file');
var fakeData = require('./lib-fakeData')(config.numberOfPosts);
// Start timer
var startTime = (new Date()).getTime();
// Prep template file
// - Ex. could come from a query
file.openTemplate(config.template('article'));
// Run through the fake data
_.map(fakeData, function(data, index){
// Create files on the file system
file.create(config.dist, data.slug, file.compile(data), function(message){
if (index === fakeData.length - 1) {
done();
}
});
});
function done() {
// Callback goes here
var endTime = (new Date()).getTime();
// End timer
var timeToFinish = (endTime-startTime);
console.log("Time to finish: ", timeToFinish, 'ms');
console.log("clearing dist directory");
//cleanDirectory(config.dist);
}
var fs = require('fs');
var gracefulFs = require('graceful-fs')
gracefulFs.gracefulify(fs);
var cleanDirectory = function(dirPath) {
try {
var files = fs.readdirSync(dirPath);
} catch(e) {
return;
}
if (files.length > 0) {
for (var i = 0; i < files.length; i++) {
var filePath = dirPath + '/' + files[i];
if (fs.statSync(filePath).isFile()) {
fs.unlinkSync(filePath);
} else {
rmDir(filePath);
}
}
}
}
module.exports = cleanDirectory;
// Setup functions
function dataSet(numberOfPosts) {
var dataSet = [];
for (var i = 0; i < numberOfPosts; i++) {
var data = {
slug: 'article-' + i, // Unique
title: 'article-title-' + i,
header: 'article-header-' + i
};
dataSet.push(data);
};
return dataSet;
}
module.exports = dataSet;
// Native modules
var fs = require('fs');
var gracefulFs = require('graceful-fs')
gracefulFs.gracefulify(fs);
// Third party modules
var _ = require('lodash');
var templateFile;
// Setup {{ mustage interpolaters }}
_.templateSettings.interpolate = /{{([\s\S]+?)}}/g;
// Setup functions
var file = {
openTemplate: openTemplate,
compile: compile,
create: create
};
function compile(data) {
var compiled = (_.template(templateFile))(data);
return compiled;
}
function openTemplate(templateFilePath) {
// Read the file
templateFile = fs.readFileSync(templateFilePath,'utf8');
}
function create(outputDir, filename, file, cb) {
var path = outputDir + '-' + filename + '.html';
fs.writeFile(path, file, function (err) {
if (err) { return console.log(err); }
var message = 'File written to ' + path;
cb(message);
});
}
module.exports = file;
<html>
<head>
<title>{{ title }}</title>
</head>
<body>
<h1>{{ header }}</h1>
<p>{{ Date() }}</p>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment