Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

New lines in error messages and support for Coffee scripts. #8

Merged
merged 7 commits into from

2 participants

@wronex

Hi,

I have made some improvements on your already excellent code. always have made my life much easier, but there where some things I missed.

One of those things where new-lines in error messages -- all new-line characters where dropped when the data from stderr was cleaned. I fixed this by replacing your regexp with a function that only removes spaces at both ends of the string. The trim code was taken from http://blog.stevenlevithan.com/archives/faster-trim-javascript.

The other was always's inability to run Coffee scripts. I fixed this by checking the file extension of the app variable to choose between Node and Coffee (in initializeDevelopment()). The chosen parser/environment is written to a variable called parser which is utilized in start() when spawning the child process. Unfortunately I have not been able to test this addition on anything other than Windows.

@edwardhotchkiss edwardhotchkiss merged commit e30dd02 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 3, 2012
  1. @wronex

    Enabled newlines in error messages.

    wronex authored
    The trim code in node.stdout.on('data'... and node.stderr.on('data'... caused newlines to be removed.
  2. @wronex

    Coding style fix.

    wronex authored
  3. @wronex

    Added wronex to contributers.

    wronex authored
  4. @wronex

    add support for .coffee files

    wronex authored
  5. @wronex
  6. @wronex

    Consistent platform test.

    wronex authored
  7. @wronex
This page is out of date. Refresh to see the latest.
Showing with 34 additions and 10 deletions.
  1. +31 −7 bin/always.js
  2. +1 −2  lib/monitor.js
  3. +2 −1  package.json
View
38 bin/always.js
@@ -13,12 +13,12 @@ var fs = require('fs')
, Monitor = require('../lib/monitor')
, args = process.argv
, managed = []
- , specials = /^\s+|\s+$/gmi
, previousEvent
, directory
, node = null
, file = null
, app = null
+ , parser = 'node' // will change depending on file extension
, cleaned
, beep = false
, version = 'v1.1.1';
@@ -69,8 +69,18 @@ if (args.length === 2) {
function initializeDevelopment(){
app = npm(args[2]);
+
+ if (path.extname(app) == '.coffee') {
+ // The file contains coffee script, use coffee to run it. Does this work on
+ // other platforms?
+ parser = process.platform.substr(0,3) == 'win' ? 'coffee.cmd' : 'coffee';
+ } else {
+ // No parser required, use node.
+ parser = 'node';
+ }
+
logger(version);
- logger('Starting ' +file.green +' with Node');
+ logger('Starting '+ file.green +' with '+ parser);
start();
};
@@ -82,7 +92,7 @@ function initializeDevelopment(){
function help(){
console.log([
'',
- 'Usage: always <options> <app.js>'.cyan,
+ 'Usage: always <options> <app.js|app.coffee>'.cyan,
'=> always app.js'.green,
'',
'Options:',
@@ -189,6 +199,19 @@ function exists(file){
};
/**
+ * @method trim
+ * @url http://blog.stevenlevithan.com/archives/faster-trim-javascript
+ * @param {String} str String to trim and clean.
+ **/
+function trim(str){
+ var str = str.replace(/^\s\s*/, ''),
+ ws = /\s/,
+ i = str.length;
+ while (ws.test(str.charAt(--i)));
+ return str.slice(0, i + 1);
+}
+
+/**
* @method start
* @param {String} app NodeJS file
**/
@@ -196,16 +219,17 @@ function exists(file){
function start(){
if (!exists(app)){
return false;
- } else {
- node = spawn('node', [app]);
+ } else {
+ node = spawn(parser, [app]);
+
// watch node child process file
initializeFileMonitor(app);
node.stdout.on('data', function(data){
- cleaned = data.toString().replace(specials, '');
+ cleaned = trim(data.toString());
appLogger(cleaned);
});
node.stderr.on('data', function(data){
- cleaned = data.toString().replace(specials, '');
+ cleaned = trim(data.toString());
appLogger(cleaned, true);
});
node.stderr.on('data', function (data) {
View
3  lib/monitor.js
@@ -1,4 +1,3 @@
-
/**
* @list dependencies
**/
@@ -9,7 +8,7 @@ var monitor
, path = require('path')
, events = require('events')
, EventEmitter = events.EventEmitter
- , exts = /(.*?)\.(js|ejs|json|yaml)$/i
+ , exts = /(.*?)\.(js|ejs|json|yaml|coffee)$/i
, watched = [];
/**
View
3  package.json
@@ -11,7 +11,8 @@
"contributors": [
{ "name": "Edward Hotchkiss","email": "edwardhotchkiss@me.com" },
{ "name": "mugenen","email": "https://twitter.com/#!/Scaled_Wurm" },
- { "name": "MikeMayer", "github": "https://github.com/MikeMayer" }
+ { "name": "MikeMayer", "github": "https://github.com/MikeMayer" },
+ { "name": "Wronex", "github": "https://github.com/wronex" }
],
"keywords":["always","process","forever","error","uncaught"],
"engines":{
Something went wrong with that request. Please try again.