httpstatuses/build.js

79 lines
2.1 KiB
JavaScript
Raw Permalink Normal View History

2015-11-06 21:40:58 +01:00
var Metalsmith = require('metalsmith');
var markdown = require('metalsmith-markdown');
var collections = require('metalsmith-collections');
var layouts = require('metalsmith-layouts');
var branch = require('metalsmith-branch');
var sass = require('metalsmith-sass');
var elevate = require('metalsmith-elevate');
var ignore = require('metalsmith-ignore');
var collectionGrouping = require('metalsmith-collection-grouping');
2015-11-06 22:36:13 +01:00
var fingerprint = require('metalsmith-fingerprint-ignore');
var excerpts = require('metalsmith-better-excerpts');
2016-11-10 22:03:37 +01:00
var finalhandler = require('finalhandler');
var http = require('http');
var serveStatic = require('serve-static');
var path = require('path');
2015-11-06 21:40:58 +01:00
var metalsmith = Metalsmith(__dirname);
metalsmith
.source('contents')
.destination('build')
.use(collections({
pages: {
pattern: '*.md'
},
codes: {
pattern: 'codes/*.md',
sortBy: 'code'
}
}))
.use(collectionGrouping({
codes: {
groupBy: 'set', // set instead of class, because class is a reserved word...
2016-11-10 22:03:37 +01:00
meta: path.join('codes', 'classes.json')
2015-11-06 21:40:58 +01:00
}
}))
.use(branch('*.scss')
.use(sass())
)
.use(fingerprint({
pattern: ['style.css', 'favicon.ico']
}))
2015-11-06 22:36:13 +01:00
2015-11-06 21:40:58 +01:00
.use(branch('**/*.md')
.use(markdown())
.use(excerpts({pruneLength: 400}))
2015-11-06 21:40:58 +01:00
.use(layouts({
engine: 'jade',
directory: 'templates',
default: 'code.jade',
pretty: true
}))
)
.use(elevate('codes/*.html'))
.use(ignore('**/*.json'))
.build(function (err) {
if (err) throw err;
console.log('Build successful!');
if (process.argv[2] != 'without-preview') startPreviewServer();
});
function startPreviewServer() {
http.createServer(function(req, res){
var done = finalhandler(req, res)
var serve = serveStatic('build', {'index': ['index.html'], 'extensions': ['html']})
serve(req, res, done)
}).listen(4887)
console.log('A local webserver has been launched, visit localhost:4887 in your browser to preview your build.');
console.log('Exit this process to kill the webserver :-)');
console.log('To build without the local webserver use `node build without-preview`');
}