Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

First commit

  • Loading branch information...
commit ec9c6f0ecdbb7a27c1e03510f9b05b28987d9a10 0 parents
Nicholas C. Zakas authored
Showing with 145 additions and 0 deletions.
  1. +2 −0  .gitignore
  2. +20 −0 LICENSE
  3. +22 −0 README.md
  4. +73 −0 cli.js
  5. +28 −0 package.json
2  .gitignore
@@ -0,0 +1,2 @@
+node_modules/
+tmp/
20 LICENSE
@@ -0,0 +1,20 @@
+Rebuild
+Copyright (c) 2012 Nicholas C. Zakas. All rights reserved.
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
22 README.md
@@ -0,0 +1,22 @@
+# Rebuild
+
+Rebuild is a simple utility that does just one thing: it watches one or more files or directories for changes and runs a command when they change. That's it. It's intended to be used with command line build systems, but you can't really use it for anything.
+
+## Installation
+
+ npm install rebuild -g
+
+## Usage
+
+Specify any number of files or directories using `-w` or `--watch`. Then just include whatever command you want to run. For example, if you just want to run `ls` whenever files in the `tmp` directory change, you would do this:
+
+ rebuild -w tmp ls
+
+The directory is resolved based on the current working directory for both the files and directories to watch and the command to run. Other examples:
+
+ # Run "ant test" when the files in src change
+ rebuild -w src ant test
+
+ # Automatically check in files in src and docs when they change
+ rebuild -w src -w docs git commit -am "Just wanted to check these in"
+
73 cli.js
@@ -0,0 +1,73 @@
+#!/usr/bin/env node
+var path = require("path"),
+ fs = require("fs"),
+ child = require('child_process'),
+ optimist= require('optimist'),
+ argv = optimist.usage("Usage: $0 [command to run]")
+ .options("w", {
+ alias: "watch",
+ default: process.cwd(),
+ describe: "A file or directory to watch for changes."
+ })
+ .options("h", {
+ alias: "help",
+ }).argv;
+
+
+if (argv.help || !argv.watch) {
+ optimist.showHelp();
+} else {
+
+ if (!argv._.length) {
+ console.log("You forgot to tell me what to run when the files changes!");
+ process.exit(1);
+ }
+
+ var watcherCount = 0,
+ directories = Array.isArray(argv.watch) ? argv.watch : [argv.watch],
+ command = argv._.join(" "),
+ lastUpdate = Date.now();
+
+ console.log("When files changes, I'll run this for you: %s", command);
+
+ function handleFileChange(event, filename) {
+
+ // Sometimes a whole bunch of events get fired, so make sure some time has passed
+ var now = Date.now();
+ if (now - lastUpdate > 50) {
+ console.log(now);
+ console.log("Okay, running '" + command + "'");
+
+ child.exec(command, function (error, stdout, stderr) {
+ console.log(stdout);
+ //console.log(stderr);
+
+ if (error !== null) {
+ console.log('exec error: ' + error);
+ }
+ });
+
+ lastUpdate = now;
+ }
+ }
+
+ // Setup watches
+ directories.forEach(function(dir) {
+ var fullPath = path.resolve(dir);
+
+ if (fs.existsSync(fullPath)) {
+ console.log("Watching %s for changes.", fullPath);
+
+ fs.watch(fullPath, handleFileChange);
+ watcherCount++;
+ } else {
+ console.log("Path %s doesn't exist.", fullPath);
+ }
+ });
+
+ // If no watches are set, exist
+ if (!watcherCount) {
+ console.log("No directories to watch, exiting. :(");
+ process.exit(1);
+ }
+}
28 package.json
@@ -0,0 +1,28 @@
+{
+ "name": "rebuild",
+ "version": "0.1.2",
+ "description": "Rebuild",
+ "author": "Nicholas C. Zakas",
+ "engines": {
+ "node" : ">=0.8.8"
+ },
+ "directories": {
+ "lib" : "lib"
+ },
+ "bin": {
+ "rebuild": "./cli.js"
+ },
+ "dependencies" : {
+ "optimist" : "0.3.x"
+ },
+ "licenses":[
+ {
+ "type" : "MIT",
+ "url" : "https://github.com/nzakas/rebuild/blob/master/LICENSE"
+ }
+ ],
+ "repository": {
+ "type":"git",
+ "url":"http://github.com/nzakas/rebuild.git"
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.