Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Support dev mode

  • Loading branch information...
commit ac6478c56cc18bbe3527723fae5f0f08b88f8814 1 parent 16ba9b3
Cameron Bytheway authored January 07, 2013
51  bin/startup-start
@@ -6,8 +6,8 @@
6 6
 
7 7
 var startup = require('commander')
8 8
   , join = require('path').join
9  
-  , http = require('http')
10  
-  , domain = require('domain');
  9
+  , child_process = require('child_process')
  10
+  , runner = require('../lib/runner');
11 11
 
12 12
 startup
13 13
   .version(require(join(__dirname, "../package.json")).version)
@@ -15,37 +15,26 @@ startup
15 15
   .option('-d --dev', 'run in development mode', false)
16 16
   .parse(process.argv);
17 17
 
  18
+// TODO default to package.json#main
18 19
 var path = startup.path?
19 20
             join(process.cwd(), startup.path):
20 21
             join(process.cwd(), 'app');
21 22
 
22  
-var app = require(path)
23  
-  , port = process.env.PORT || 3000;
24  
-
25  
-var serverDomain = domain.create();
26  
-
27  
-serverDomain.run(function() {
28  
-  http.createServer(function(req, res) {
29  
-    var reqd = domain.create();
30  
-    reqd.add(req);
31  
-    reqd.add(res);
32  
-    reqd.on('error', function(err) {
33  
-      console.error('Error', err, req.url);
34  
-      try {
35  
-        // TODO pass on to app's errorHandler
36  
-        res.writeHead(500);
37  
-        res.end('Error occurred, sorry.');
38  
-        res.on('close', function() {
39  
-          reqd.dispose();
40  
-        });
41  
-      } catch (err) {
42  
-        console.error('Error sending 500', err, req.url);
43  
-        reqd.dispose();
44  
-      }
45  
-    });
46  
-    // Call our app in the req domain
47  
-    reqd.bind(app)(req, res);
48  
-  }).listen(port, function(){
49  
-    console.log("Server listening on port " + port);
  23
+if (process.env.NODE_ENV=="development" || startup.dev) {
  24
+
  25
+  var child = child_process.spawn(__dirname+"/../node_modules/.bin/node-dev", [__dirname+"/../lib/dev-wrapper", path]);
  26
+
  27
+  child.stdout.on("data", function(data) {
  28
+    process.stdout.write(data.toString());
  29
+  });
  30
+  child.stderr.on("data", function(data) {
  31
+    process.stdout.write(data.toString());
50 32
   });
51  
-});
  33
+  child.on("exit", function(code) {
  34
+    exit(code);
  35
+  });
  36
+
  37
+}
  38
+else {
  39
+  runner(path);
  40
+};
6  lib/dev-wrapper.js
... ...
@@ -0,0 +1,6 @@
  1
+var path = require("path")
  2
+  , runner = require("./runner");
  3
+
  4
+var main = path.resolve(process.cwd(), process.argv[2]);
  5
+
  6
+runner(main);
39  lib/runner.js
... ...
@@ -0,0 +1,39 @@
  1
+
  2
+var domain = require('domain')
  3
+  , http = require('http');
  4
+
  5
+module.exports = function(path) {
  6
+
  7
+  var app = require(path)
  8
+    , port = process.env.PORT || 3000;
  9
+
  10
+  var serverDomain = domain.create();
  11
+
  12
+  serverDomain.run(function() {
  13
+    http.createServer(function(req, res) {
  14
+      var reqd = domain.create();
  15
+      reqd.add(req);
  16
+      reqd.add(res);
  17
+      reqd.on('error', function(err) {
  18
+        console.error('Error', err, req.url);
  19
+        try {
  20
+          // TODO pass on to app's errorHandler
  21
+          res.writeHead(500);
  22
+          res.end('Error occurred, sorry.');
  23
+          res.on('close', function() {
  24
+            reqd.dispose();
  25
+          });
  26
+        } catch (err) {
  27
+          console.error('Error sending 500', err, req.url);
  28
+          reqd.dispose();
  29
+        }
  30
+      });
  31
+      // Call our app in the req domain
  32
+      reqd.bind(app)(req, res);
  33
+    }).listen(port, function(){
  34
+      console.log("Server listening on port " + port);
  35
+    });
  36
+  });
  37
+
  38
+  return app;
  39
+};
8  package.json
... ...
@@ -1,6 +1,6 @@
1 1
 {
2 2
   "name": "startup",
3  
-  "version": "0.0.2",
  3
+  "version": "0.0.3",
4 4
   "description": "Node.js HTTP app runner",
5 5
   "scripts": {
6 6
     "test": "./node_modules/.bin/mocha"
@@ -21,13 +21,15 @@
21 21
     "runner",
22 22
     "express",
23 23
     "connect",
24  
-    "http"
  24
+    "http",
  25
+    "development"
25 26
   ],
26 27
   "author": "Cameron Bytheway <cameron@nujii.com>",
27 28
   "license": "MIT",
28 29
   "readmeFilename": "README.md",
29 30
   "dependencies": {
30 31
     "commander": "~1.1.1",
31  
-    "cli-table": "~0.2.0"
  32
+    "cli-table": "~0.2.0",
  33
+    "node-dev": "~0.2.0"
32 34
   }
33 35
 }

0 notes on commit ac6478c

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