Permalink
Browse files

init commit

  • Loading branch information...
0 parents commit cef88da78027a37525bb9025c3b4ec9a5006e280 @jawerty committed Mar 20, 2013
Showing with 252 additions and 0 deletions.
  1. +5 −0 .gitignore
  2. +1 −0 .npmignore
  3. +8 −0 LICENSE
  4. +43 −0 README.md
  5. +35 −0 package.json
  6. +8 −0 test/check.js
  7. +6 −0 test/server.js
  8. +146 −0 watchmon.js
@@ -0,0 +1,5 @@
+*.swp
+*.swo
+*~
+node_modules/
+nohup.out
@@ -0,0 +1 @@
+/test
@@ -0,0 +1,8 @@
+The MIT License (MIT)
+Copyright (c) 2013 Jared Wright
+
+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.
@@ -0,0 +1,43 @@
+# watchmon
+A file monitor that restarts your node.js application when changes are made. Does not require anything other than node and npm.
+
+Current version: 0.1.5
+
+# Installation
+Install globally
+```
+$ npm install watchmon -g
+```
+
+# Usage
+```
+$ watchmon server.js
+```
+
+Help output
+```
+watchmon is a file monitoring program that restarts a node process whenever is sees a file change.
+
+Usage:
+ watchmon [file] [options]
+Options:
+ -h, --help Help screen
+ -v, --version Current version
+ -n, --no-stdout Don't read stdout
+ -e, --exit Exit the process when the app crashed
+ -d, --delay t Delays restart for t seconds
+```
+
+# Contact
+If you would like to contact me for further information on the project, see the info below.
+
+Email: jawerty210@gmail.com
+
+Github: jawerty
+
+Twitter: @jawerty
+
+Blog: <http://wrightdev.herokuapp.com>
+
+# License
+See the file LICENSE to view the MIT License
@@ -0,0 +1,35 @@
+{
+ "name": "watchmon",
+ "homepage": "http://github.com/jawerty/watchmon",
+ "author": "Jared Wright (http://wrightdev.herokuapp.com/jawerty)",
+ "bin": {
+ "watchmon": "./watchmon.js"
+ },
+ "repository": {
+ "type": "git",
+ "url": "http://github.com/jawerty/watchmon.git"
+ },
+ "description": "Simple file monitor script for restarting your development node.js server",
+ "keywords": [
+ "monitor",
+ "file",
+ "development",
+ "restart",
+ "autoload",
+ "reload",
+ "terminal"
+ ],
+ "version": "0.1.5",
+ "preferGlobal": "true",
+ "licenses": [
+ {
+ "type": "MIT",
+ "url": "http://opensource.org/licenses/MIT"
+ }
+ ],
+ "main": "./watchmon",
+ "directories": {
+ "test": "test"
+ },
+ "license": "MIT"
+}
@@ -0,0 +1,8 @@
+var fs = require('fs'),
+ sys = require('sys');
+file = 'server.js';
+fs.watchFile(file, function(curr, prev) {
+ sys.puts("File was modified.");
+});
+
+console.log('serving file %s', file)
@@ -0,0 +1,6 @@
+var http = require('http');
+http.createServer(function (req, res) {
+ res.writeHead(200, {'Content-Type': 'text/plain'});
+ res.end('Hello my World\n');
+}).listen(1337, '127.0.0.1');
+console.log('Server running at http://127.0.0.1:1337/');
@@ -0,0 +1,146 @@
+#!/usr/bin/env node
+var fs = require('fs');
+var util = require('util');
+/*color set**/
+green_start = '\u001b[32m';
+yel_start = '\u001b[33m';
+end = '\u001b[0m';
+/************/
+var exec = require('child_process').exec,
+ child;
+var help = '\nwatchmon is a file monitoring program that restarts a node process whenever is sees a file change. \
+\n\nUsage: \n\twatchmon [file] [options]\nOptions:\n\t-h, --help\tHelp screen\n\t-v, --version\tCurrent version\n\t-n, \
+--no-stdout\tDon\'t read stdout\n\t-e, --exit\tExit the process when the app crashed\n\t-d, --delay t\tDelays restart for \
+t seconds';
+var version = '0.1.5';
+var noStdout = exitcrash = false;
+var seconds = 0;
+var allowed_args = ['-h', '--help', '-v', '--version', '-n', '--no-stdout', '-e', '--exit', '-d', '--delay'];
+
+var argv = process.argv;
+if (argv[0] === "node"){ //if you're running the file overseer.js
+ argv = process.argv.slice(2);
+}else{
+ argv = process.argv.slice(1);
+}
+
+function inArray(value,array)
+{
+ var count=array.length;
+ for(var i=0;i<count;i++)
+ {
+ if(array[i]===value){
+ return true;
+ }
+ }
+ return false;
+}
+
+exit = function(message, error){
+ util.puts(message);
+ err = error || null;
+ process.exit(err);
+}
+
+isEmpty = function(array){
+ if(array.length == 0){
+ return true;
+ }else{
+ return false;
+ }
+}
+
+argCheck = function(arg){
+ if(inArray(arg, argv) && inArray(arg, allowed_args)){
+ return true;
+ }else{
+ return false;
+ }
+}
+//options setup//
+if(isEmpty(argv)){ //running with no arguments defaults to help message
+ exit(help);
+}else{
+ if(argCheck("-h") || argCheck("--help")){
+ exit(help);
+ }
+ else if(argCheck("-v") || argCheck("--version")){
+ exit(version);
+ }
+ else if(argCheck("-n") || argCheck("--no-stdout")){
+ noStdout = true;
+ }
+ else if(argCheck("-e") || argCheck("--exit")){
+ exitcrash = true;
+ }
+ else if(argCheck("-d") || argCheck("--delay")){
+ //get the following argument from the delay option
+ position = argv.indexOf('-d') || argv.indexOf('--delay');
+ seconds = argv[position+1]*1000;
+ }
+}
+////////////////
+
+start = function(file, curr, prev, noStdout){
+ setTimeout(function(){
+ child = exec('node ' + file,
+ function (error, stdout, stderr) {
+ if (error !== null) {
+ util.error('exec error: ' + error);
+ }
+ }
+ );
+ if(noStdout === false){
+ child.stdout.pipe(process.stdout);
+ }
+
+ child.on('exit', function(code, signal){
+ if (signal === 'SIGUSR2') {
+ //start()
+ } else if (code === 0) {
+ util.puts(green_start+'watchmon clean exit'+end);
+ } else {
+ util.puts(yel_start+'watchmon app crash...'+end);
+ if(exitcrash === true){
+ process.exit()
+ }
+ }
+ });
+ }, seconds);
+}
+
+file = argv[0] || undefined;
+if (inArray(file, allowed_args)){ //if the file is an option, display help screen
+ exit(help);
+}
+
+main = function(file){
+ fs.exists(file, function(exists){
+ fs.stat(file, function(err,stats){
+ if(err) return util.error(err);
+ time = stats.mtime;
+ util.puts('watchmon monitoring '+green_start+'['+file+']'+end);
+ util.puts('loaded on: '+green_start+ time +end);
+ });
+ if(exists === true){
+ start(file, null, null, noStdout)
+ fs.watchFile(file, function(curr, prev) {
+ util.puts('\nChange made to '+green_start+'[\''+file+'\']'+end+' on: ' +green_start+ prev.mtime +end);
+ start(file, curr, prev, noStdout)
+ });
+ }else{
+ util.error(yel_start+'File does not exist'+end);
+ }
+ });
+};
+
+process.on("SIGINT", function(){ //catches app when shutdown
+ exit(yel_start+'watchmon shutting down...'+end);
+});
+process.on('uncaughtException', function (err) { //catches all uncaught exceptions
+ util.error(yel_start+err+end);
+});
+
+if (require.main === module) {
+ main(file);
+}

0 comments on commit cef88da

Please sign in to comment.