Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

init commit

  • Loading branch information...
commit cef88da78027a37525bb9025c3b4ec9a5006e280 0 parents
Jared Wright authored March 20, 2013
5  .gitignore
... ...
@@ -0,0 +1,5 @@
  1
+*.swp
  2
+*.swo
  3
+*~
  4
+node_modules/
  5
+nohup.out
1  .npmignore
... ...
@@ -0,0 +1 @@
  1
+/test
8  LICENSE
... ...
@@ -0,0 +1,8 @@
  1
+The MIT License (MIT)
  2
+Copyright (c) 2013 Jared Wright
  3
+
  4
+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:
  5
+
  6
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  7
+
  8
+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.
43  README.md
Source Rendered
... ...
@@ -0,0 +1,43 @@
  1
+# watchmon
  2
+A file monitor that restarts your node.js application when changes are made. Does not require anything other than node and npm. 
  3
+
  4
+Current version: 0.1.5
  5
+
  6
+# Installation
  7
+Install globally
  8
+```
  9
+$ npm install watchmon -g
  10
+```
  11
+
  12
+# Usage
  13
+```
  14
+$ watchmon server.js
  15
+```
  16
+
  17
+Help output
  18
+```
  19
+watchmon is a file monitoring program that restarts a node process whenever is sees a file change.
  20
+
  21
+Usage:
  22
+	watchmon [file] [options]
  23
+Options:
  24
+	-h, --help	    Help screen
  25
+	-v, --version	Current version
  26
+	-n, --no-stdout	Don't read stdout
  27
+	-e, --exit	    Exit the process when the app crashed
  28
+	-d, --delay t	Delays restart for t seconds
  29
+```
  30
+
  31
+# Contact
  32
+If you would like to contact me for further information on the project, see the info below.
  33
+
  34
+Email: jawerty210@gmail.com
  35
+
  36
+Github: jawerty
  37
+
  38
+Twitter: @jawerty
  39
+
  40
+Blog: <http://wrightdev.herokuapp.com>
  41
+
  42
+# License
  43
+See the file LICENSE to view the MIT License
35  package.json
... ...
@@ -0,0 +1,35 @@
  1
+{
  2
+  "name": "watchmon",
  3
+  "homepage": "http://github.com/jawerty/watchmon",
  4
+  "author": "Jared Wright (http://wrightdev.herokuapp.com/jawerty)",
  5
+  "bin": {
  6
+    "watchmon": "./watchmon.js"
  7
+  },
  8
+  "repository": {
  9
+    "type": "git",
  10
+    "url": "http://github.com/jawerty/watchmon.git"
  11
+  },
  12
+  "description": "Simple file monitor script for restarting your development node.js server",
  13
+  "keywords": [
  14
+    "monitor",
  15
+    "file",
  16
+    "development",
  17
+    "restart",
  18
+    "autoload",
  19
+    "reload",
  20
+    "terminal"
  21
+  ],
  22
+  "version": "0.1.5",
  23
+  "preferGlobal": "true",
  24
+  "licenses": [
  25
+    {
  26
+      "type": "MIT",
  27
+      "url": "http://opensource.org/licenses/MIT"
  28
+    }
  29
+  ],
  30
+  "main": "./watchmon",
  31
+  "directories": {
  32
+    "test": "test"
  33
+  },
  34
+  "license": "MIT"
  35
+}
8  test/check.js
... ...
@@ -0,0 +1,8 @@
  1
+var fs = require('fs'),
  2
+    sys = require('sys');
  3
+file = 'server.js';
  4
+fs.watchFile(file, function(curr, prev) {
  5
+    sys.puts("File was modified.");
  6
+});
  7
+
  8
+console.log('serving file %s', file)
6  test/server.js
... ...
@@ -0,0 +1,6 @@
  1
+var http = require('http');
  2
+http.createServer(function (req, res) {
  3
+  res.writeHead(200, {'Content-Type': 'text/plain'});
  4
+  res.end('Hello my World\n');
  5
+}).listen(1337, '127.0.0.1');
  6
+console.log('Server running at http://127.0.0.1:1337/');
146  watchmon.js
... ...
@@ -0,0 +1,146 @@
  1
+#!/usr/bin/env node
  2
+var fs = require('fs');
  3
+var util = require('util');
  4
+/*color set**/
  5
+green_start = '\u001b[32m';
  6
+yel_start = '\u001b[33m';
  7
+end = '\u001b[0m';
  8
+/************/
  9
+var exec = require('child_process').exec,
  10
+    child;
  11
+var help = '\nwatchmon is a file monitoring program that restarts a node process whenever is sees a file change. \
  12
+\n\nUsage: \n\twatchmon [file] [options]\nOptions:\n\t-h, --help\tHelp screen\n\t-v, --version\tCurrent version\n\t-n, \
  13
+--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 \
  14
+t seconds';
  15
+var version = '0.1.5';
  16
+var noStdout = exitcrash = false;
  17
+var seconds = 0;
  18
+var allowed_args = ['-h', '--help', '-v', '--version', '-n', '--no-stdout', '-e', '--exit', '-d', '--delay'];
  19
+
  20
+var argv = process.argv; 
  21
+if (argv[0] === "node"){ //if you're running the file overseer.js
  22
+	argv = process.argv.slice(2);
  23
+}else{
  24
+	argv = process.argv.slice(1);
  25
+}
  26
+
  27
+function inArray(value,array)
  28
+{
  29
+    var count=array.length;
  30
+    for(var i=0;i<count;i++)
  31
+    {
  32
+        if(array[i]===value){
  33
+        	return true;
  34
+        }
  35
+    }
  36
+    return false;
  37
+}
  38
+
  39
+exit = function(message, error){
  40
+	util.puts(message);
  41
+	err = error || null;
  42
+	process.exit(err);
  43
+}
  44
+
  45
+isEmpty = function(array){
  46
+	if(array.length == 0){
  47
+		return true;
  48
+	}else{
  49
+		return false;
  50
+	}
  51
+}
  52
+
  53
+argCheck = function(arg){
  54
+	if(inArray(arg, argv) && inArray(arg, allowed_args)){
  55
+		return true;
  56
+	}else{
  57
+		return false;
  58
+	}
  59
+}
  60
+//options setup//
  61
+if(isEmpty(argv)){ //running with no arguments defaults to help message
  62
+	exit(help);
  63
+}else{
  64
+	if(argCheck("-h") || argCheck("--help")){
  65
+		exit(help);
  66
+	}
  67
+	else if(argCheck("-v") || argCheck("--version")){
  68
+		exit(version);
  69
+	}
  70
+	else if(argCheck("-n") || argCheck("--no-stdout")){
  71
+		noStdout = true;
  72
+	}
  73
+	else if(argCheck("-e") || argCheck("--exit")){
  74
+		exitcrash = true;
  75
+	}
  76
+	else if(argCheck("-d") || argCheck("--delay")){
  77
+		//get the following argument from the delay option
  78
+		position = argv.indexOf('-d') || argv.indexOf('--delay');
  79
+		seconds = argv[position+1]*1000;
  80
+	}
  81
+}
  82
+////////////////
  83
+
  84
+start = function(file, curr, prev, noStdout){
  85
+	setTimeout(function(){
  86
+		child = exec('node ' + file,
  87
+			function (error, stdout, stderr) {
  88
+				if (error !== null) {
  89
+					util.error('exec error: ' + error);
  90
+				}
  91
+			}
  92
+		);
  93
+		if(noStdout === false){
  94
+			child.stdout.pipe(process.stdout);
  95
+		}
  96
+
  97
+		child.on('exit', function(code, signal){
  98
+			if (signal === 'SIGUSR2') {
  99
+			  //start()
  100
+			} else if (code === 0) { 
  101
+		      util.puts(green_start+'watchmon clean exit'+end);
  102
+		    } else {
  103
+		      util.puts(yel_start+'watchmon app crash...'+end);
  104
+		      if(exitcrash === true){
  105
+		      	process.exit()
  106
+		      }
  107
+		    }
  108
+		});
  109
+	}, seconds);
  110
+}
  111
+
  112
+file = argv[0] || undefined;
  113
+if (inArray(file, allowed_args)){ //if the file is an option, display help screen
  114
+	exit(help);
  115
+}
  116
+
  117
+main = function(file){
  118
+	fs.exists(file, function(exists){
  119
+		fs.stat(file, function(err,stats){
  120
+		  if(err) return util.error(err);
  121
+		  time = stats.mtime;
  122
+		  util.puts('watchmon monitoring '+green_start+'['+file+']'+end);
  123
+		  util.puts('loaded on: '+green_start+ time +end);		
  124
+		});
  125
+		if(exists === true){
  126
+			start(file, null, null, noStdout)
  127
+			fs.watchFile(file, function(curr, prev) {
  128
+		  		util.puts('\nChange made to '+green_start+'[\''+file+'\']'+end+' on: ' +green_start+ prev.mtime +end);
  129
+		  		start(file, curr, prev, noStdout)
  130
+			});
  131
+		}else{
  132
+			util.error(yel_start+'File does not exist'+end);
  133
+		}
  134
+	});
  135
+};
  136
+
  137
+process.on("SIGINT", function(){ //catches app when shutdown
  138
+	exit(yel_start+'watchmon shutting down...'+end);
  139
+});
  140
+process.on('uncaughtException', function (err) { //catches all uncaught exceptions
  141
+    util.error(yel_start+err+end);
  142
+});
  143
+
  144
+if (require.main === module) {
  145
+    main(file);   
  146
+}

0 notes on commit cef88da

Please sign in to comment.
Something went wrong with that request. Please try again.