Permalink
Browse files

add --reload option on component build --watch

  • Loading branch information...
wryk committed Apr 11, 2014
1 parent fe2aae0 commit ed5f37402efbff2aa69108d2d339bb519420873f
Showing with 27 additions and 1 deletion.
  1. +25 −0 bin/component-build
  2. +2 −1 package.json
View
@@ -7,6 +7,7 @@ var program = require('commander');
program
.usage('component build [scripts] [styles] [files]')
.option('-w, --watch', 'watch for file changes and rebuild automatically')
+ .option('-r, --reload', 'refresh livereload server on file changes (works only with --watch)')
.option('-o, --out <dir>', 'output directory defaulting to ./build', 'build')
.option('-n, --name <file>', 'base name for build files defaulting to build', 'build')
.option('-d, --dev', 'build development dependencies, use aliases, and use sourceURLs')
@@ -51,6 +52,7 @@ var fs = require('fs');
var exists = fs.existsSync || path.existsSync;
var args = program.args;
var log = utils.log;
+var slice = Array.prototype.slice;
// object of which files to build
var builds;
@@ -130,6 +132,27 @@ process.stdin.on('data', function (data) {
}
});
+if (!program.reload) return;
+
+var server = require('tiny-lr-fork')();
+server.listen(35729);
+
+server.on('error', function (err) {
+ if (err.code === 'EADDRINUSE') {
+ utils.fatal('livereload port 35729 is already in use by another process');
+ }
+
+ utils.fatal(err);
+});
+
+function reload() {
+ server.changed({
+ body: {
+ files: slice.call(arguments)
+ }
+ });
+}
+
function resolve() {
if (resolving) return;
resolving = true;
@@ -163,6 +186,7 @@ function buildScripts() {
log('build', jsPath + ' in '
+ (Date.now() - start) + 'ms - '
+ (js.length / 1024 | 0) + 'kb');
+ reload(jsPath);
})
}
@@ -177,6 +201,7 @@ function buildStyles() {
log('build', cssPath + ' in '
+ (Date.now() - start) + 'ms - '
+ (css.length / 1024 | 0) + 'kb');
+ reload(cssPath);
})
}
View
@@ -32,7 +32,8 @@
"debug": "*",
"open": "~0.0.4",
"cogent": "~0.4.3",
- "co": "^3.0.0"
+ "co": "^3.0.0",
+ "tiny-lr-fork": "0.0.5"
},
"devDependencies": {
"mocha": "1",

0 comments on commit ed5f374

Please sign in to comment.