Permalink
Browse files

Support dev mode

  • Loading branch information...
1 parent 16ba9b3 commit ac6478c56cc18bbe3527723fae5f0f08b88f8814 @camshaft committed Jan 7, 2013
Showing with 70 additions and 34 deletions.
  1. +20 −31 bin/startup-start
  2. +6 −0 lib/dev-wrapper.js
  3. +39 −0 lib/runner.js
  4. +5 −3 package.json
View
@@ -6,46 +6,35 @@
var startup = require('commander')
, join = require('path').join
- , http = require('http')
- , domain = require('domain');
+ , child_process = require('child_process')
+ , runner = require('../lib/runner');
startup
.version(require(join(__dirname, "../package.json")).version)
.option('-p --path', 'path to file', 'app.js')
.option('-d --dev', 'run in development mode', false)
.parse(process.argv);
+// TODO default to package.json#main
var path = startup.path?
join(process.cwd(), startup.path):
join(process.cwd(), 'app');
-var app = require(path)
- , port = process.env.PORT || 3000;
-
-var serverDomain = domain.create();
-
-serverDomain.run(function() {
- http.createServer(function(req, res) {
- var reqd = domain.create();
- reqd.add(req);
- reqd.add(res);
- reqd.on('error', function(err) {
- console.error('Error', err, req.url);
- try {
- // TODO pass on to app's errorHandler
- res.writeHead(500);
- res.end('Error occurred, sorry.');
- res.on('close', function() {
- reqd.dispose();
- });
- } catch (err) {
- console.error('Error sending 500', err, req.url);
- reqd.dispose();
- }
- });
- // Call our app in the req domain
- reqd.bind(app)(req, res);
- }).listen(port, function(){
- console.log("Server listening on port " + port);
+if (process.env.NODE_ENV=="development" || startup.dev) {
+
+ var child = child_process.spawn(__dirname+"/../node_modules/.bin/node-dev", [__dirname+"/../lib/dev-wrapper", path]);
+
+ child.stdout.on("data", function(data) {
+ process.stdout.write(data.toString());
+ });
+ child.stderr.on("data", function(data) {
+ process.stdout.write(data.toString());
});
-});
+ child.on("exit", function(code) {
+ exit(code);
+ });
+
+}
+else {
+ runner(path);
+};
View
@@ -0,0 +1,6 @@
+var path = require("path")
+ , runner = require("./runner");
+
+var main = path.resolve(process.cwd(), process.argv[2]);
+
+runner(main);
View
@@ -0,0 +1,39 @@
+
+var domain = require('domain')
+ , http = require('http');
+
+module.exports = function(path) {
+
+ var app = require(path)
+ , port = process.env.PORT || 3000;
+
+ var serverDomain = domain.create();
+
+ serverDomain.run(function() {
+ http.createServer(function(req, res) {
+ var reqd = domain.create();
+ reqd.add(req);
+ reqd.add(res);
+ reqd.on('error', function(err) {
+ console.error('Error', err, req.url);
+ try {
+ // TODO pass on to app's errorHandler
+ res.writeHead(500);
+ res.end('Error occurred, sorry.');
+ res.on('close', function() {
+ reqd.dispose();
+ });
+ } catch (err) {
+ console.error('Error sending 500', err, req.url);
+ reqd.dispose();
+ }
+ });
+ // Call our app in the req domain
+ reqd.bind(app)(req, res);
+ }).listen(port, function(){
+ console.log("Server listening on port " + port);
+ });
+ });
+
+ return app;
+};
View
@@ -1,6 +1,6 @@
{
"name": "startup",
- "version": "0.0.2",
+ "version": "0.0.3",
"description": "Node.js HTTP app runner",
"scripts": {
"test": "./node_modules/.bin/mocha"
@@ -21,13 +21,15 @@
"runner",
"express",
"connect",
- "http"
+ "http",
+ "development"
],
"author": "Cameron Bytheway <cameron@nujii.com>",
"license": "MIT",
"readmeFilename": "README.md",
"dependencies": {
"commander": "~1.1.1",
- "cli-table": "~0.2.0"
+ "cli-table": "~0.2.0",
+ "node-dev": "~0.2.0"
}
}

0 comments on commit ac6478c

Please sign in to comment.