UNPKG

1.2 kBJavaScriptView Raw
1const Metalsmith = require('metalsmith');
2const changed = require('./');
3const nodeStatic = require('node-static');
4const livereload = require('metalsmith-livereload');
5const watch = require('glob-watcher');
6const open = require('open');
7
8const DIR = __dirname + '/test/fixtures/';
9
10/**
11 * Build with metalsmith.
12 */
13const build = (clean = false) => (done) => {
14 console.log(`Building. clean: ${clean}.`);
15 Metalsmith(DIR)
16 .clean(clean)
17 .use(changed())
18// .use(expensivePlugin()) // ie markdown -> html
19 .use(livereload({ debug: true }))
20 .build((err, files) => {
21 let filenames = Object.keys(files).join(', ');
22 console.log('Built: ' + filenames);
23 done(err);
24 });
25};
26
27/**
28 * Serve files.
29 */
30var serve = new nodeStatic.Server(DIR + 'build');
31require('http').createServer((req, res) => {
32 req.addListener('end', () => serve.serve(req, res));
33 req.resume();
34}).listen(8080);
35
36/**
37 * Watch files.
38 */
39watch(DIR + 'src/**/*', { ignoreInitial: false }, build(false));
40// watch(DIR + 'templates/**/*', build(true)); // force build of all files
41
42/**
43 * Open browser. Wait to allow livereload server to start.
44 */
45setTimeout(() => open('http://localhost:8080'), 1000);