Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

factored out spawnProcess into lib/spawn.js

  • Loading branch information...
commit 8776e374d3ae82a2f23a8264a6199bbb805f7900 1 parent 2bb370d
@substack authored
Showing with 44 additions and 36 deletions.
  1. +1 −36 index.js
  2. +43 −0 lib/spawn.js
View
37 index.js
@@ -6,8 +6,8 @@ var path = require('path');
var fs = require('fs');
var clone = require('clone');
-var parseQuote = require('shell-quote').parse;
var spawn = require('child_process').spawn;
+var spawnProcess = require('./lib/spawn');
module.exports = function (opts) {
if (!opts) opts = {};
@@ -226,38 +226,3 @@ Ploy.prototype.handle = function (req, res) {
res.end();
}
};
-
-function spawnProcess (commit, env, cb) {
- fs.readFile(path.join(commit.dir, 'package.json'), function (err, src) {
- if (err && err.code === 'ENOENT') {
- src = JSON.stringify({ scripts: { start: 'node server.js' } });
- }
- else if (err) return cb(err);
-
- try { var pkg = JSON.parse(src) }
- catch (e) { return cb(e) }
-
- var start = pkg.scripts && pkg.scripts.start || 'node server.js';
- if (typeof start === 'string') {
- start = { 'index': start };
- }
-
- Object.keys(start).forEach(function (key) {
- var pEnv = clone(env);
- pEnv.PORT = Math.floor(Math.random()*(Math.pow(2,16)-1024)+1024);
- var host = (key === 'index' ? '' : key + '.') + commit.branch;
- runCommands(host, start[key], pEnv);
- });
- });
-
- function runCommands (host, cmd, env) {
- if (!Array.isArray(cmd)) cmd = parseQuote(cmd);
- var ps = commit.spawn(cmd, { env: env });
- ps.port = env.PORT;
- ps.host = host;
- ps.stdout.pipe(process.stdout, { end: false });
- ps.stderr.pipe(process.stderr, { end: false });
- ps.respawn = function () { runCommands(host, cmd, env) };
- cb(null, ps);
- }
-}
View
43 lib/spawn.js
@@ -0,0 +1,43 @@
+var fs = require('fs');
+var path = require('path');
+var parseQuote = require('shell-quote').parse;
+var clone = require('clone');
+
+module.exports = function (commit, env, cb) {
+ fs.readFile(path.join(commit.dir, 'package.json'), function (err, src) {
+ if (err && err.code === 'ENOENT') {
+ src = JSON.stringify({ scripts: { start: 'node server.js' } });
+ }
+ else if (err) return cb(err);
+
+ try { var pkg = JSON.parse(src) }
+ catch (e) { return cb(e) }
+
+ var start = pkg.scripts && pkg.scripts.start || 'node server.js';
+ if (typeof start === 'string') {
+ start = { 'index': start };
+ }
+
+ runServers(start);
+ });
+
+ function runServers (start) {
+ Object.keys(start).forEach(function (key) {
+ var pEnv = clone(env);
+ pEnv.PORT = Math.floor(Math.random()*(Math.pow(2,16)-1024)+1024);
+ var host = (key === 'index' ? '' : key + '.') + commit.branch;
+ runCommands(host, start[key], pEnv);
+ });
+ }
+
+ function runCommands (host, cmd, env) {
+ if (!Array.isArray(cmd)) cmd = parseQuote(cmd);
+ var ps = commit.spawn(cmd, { env: env });
+ ps.port = env.PORT;
+ ps.host = host;
+ ps.stdout.pipe(process.stdout, { end: false });
+ ps.stderr.pipe(process.stderr, { end: false });
+ ps.respawn = function () { runCommands(host, cmd, env) };
+ cb(null, ps);
+ }
+};
Please sign in to comment.
Something went wrong with that request. Please try again.