Skip to content

Instantly share code, notes, and snippets.

@Fenntasy
Created February 1, 2017 23:24
Show Gist options
  • Save Fenntasy/130f0605ce216efe745a8577e01a5bd9 to your computer and use it in GitHub Desktop.
Save Fenntasy/130f0605ce216efe745a8577e01a5bd9 to your computer and use it in GitHub Desktop.
Elm watcher
/* eslint no-console: "off" */
var spawn = require('child_process').spawn;
var fs = require("fs");
var chokidar = require('chokidar');
var sass = require('node-sass');
var chalk = require('chalk');
var connect = require('connect');
var serveStatic = require('serve-static');
var watcher = chokidar.watch(
['**/*.elm', '*.sass', '*.html'],
{
ignored: /(^|[\/\\])\../
}
);
watcher
.on('change', path => {
var ext = path.substr(path.lastIndexOf("."));
switch(ext) {
case ".elm":
var cmd = spawn("npm", ["run", "build-elm-raw", "--silent"], { stdio: "inherit" });
cmd.on("close", () => console.log("\n"));
break;
case ".sass":
sass.render({
file: "style.sass",
outFile: "build/style.css",
// outputStyle: "compressed"
}, function(error, result) {
if (error) {
console.log(chalk.red(error.formatted));
} else {
fs.writeFile("build/style.css", result.css, function(err){
if(!err){
//file written on disk
var text = "Rendering of " + path;
text += "\nCompleted in " + (result.stats.end - result.stats.start) + "ms\n";
console.log(chalk.green(text));
}
});
}
});
break;
case ".html":
console.log(chalk.green("Copying", path, "\n"));
spawn("npm", ["run", "build-html", "--silent"], { stdio: "inherit" });
break;
}
});
var cmd = spawn("npm", ["run", "build-debug", "--silent"], { stdio: "inherit" });
cmd.on("close", () => {
connect()
.use(serveStatic("./build/"))
.listen(8080, function(){
console.log('\n\nServer running on http://localhost:8080');
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment