diff --git a/lib/cli.js b/lib/cli.js index 3c23f0596..9d6c774c7 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -1,7 +1,6 @@ var path = require('path') var optimist = require('optimist') var fs = require('graceful-fs') -var spawn = require('child_process').spawn var Server = require('./server') var helper = require('./helper') @@ -209,21 +208,6 @@ var describeCompletion = function () { .describe('help', 'Print usage.') } -var startServer = function (config) { - var args = process.argv - var detachedIndex = args.indexOf('--detached') - if (detachedIndex === -1) { - new Server(config).start() - return - } - args.splice(detachedIndex, 1) - var child = spawn(args[0], args.slice(1), { - detached: true, - stdio: ['ignore', 'ignore', 'ignore'] - }) - child.unref() -} - exports.process = function () { var argv = optimist.parse(argsBeforeDoubleDash(process.argv.slice(2))) var options = { @@ -272,7 +256,7 @@ exports.run = function () { switch (config.cmd) { case 'start': - startServer(config) + new Server(config).start() break case 'run': require('./runner').run(config) diff --git a/lib/detached.js b/lib/detached.js new file mode 100644 index 000000000..76af943f4 --- /dev/null +++ b/lib/detached.js @@ -0,0 +1,9 @@ +var fs = require('fs') + +var Server = require('./server') +var configurationFile = process.argv[2] +var fileContents = fs.readFileSync(configurationFile, 'utf-8') +fs.unlink(configurationFile, function () {}) +var data = JSON.parse(fileContents) +var server = new Server(data) +server.start(data) diff --git a/lib/middleware/stopper.js b/lib/middleware/stopper.js index ecd2b0b90..8d41cee00 100644 --- a/lib/middleware/stopper.js +++ b/lib/middleware/stopper.js @@ -10,7 +10,7 @@ var createStopperMiddleware = function (urlRoot) { response.writeHead(200) log.info('Stopping server') response.end('OK') - process.exit(0) + process.kill(process.pid, 'SIGINT') } } diff --git a/lib/server.js b/lib/server.js index 34de4ed2c..e7f496264 100644 --- a/lib/server.js +++ b/lib/server.js @@ -2,7 +2,10 @@ var SocketIO = require('socket.io') var di = require('di') var util = require('util') var Promise = require('bluebird') - +var spawn = require('child_process').spawn +var tmp = require('tmp') +var fs = require('fs') +var path = require('path') var root = global || window || this var cfg = require('./config') @@ -131,6 +134,10 @@ Server.prototype.refreshFiles = function () { Server.prototype._start = function (config, launcher, preprocess, fileList, webServer, capturedBrowsers, socketServer, executor, done) { var self = this + if (config.detached) { + this._detach(config, done) + return + } self._fileList = fileList @@ -362,6 +369,27 @@ Server.prototype._start = function (config, launcher, preprocess, fileList, webS }) } +Server.prototype._detach = function (config, done) { + var log = this.log + var tmpFile = tmp.fileSync({keep: true}) + log.info('Starting karma detached') + log.info('Run "karma stop" to stop the server.') + log.debug('Writing config to tmp-file %s', tmpFile.name) + config.detached = false + try { + fs.writeFileSync(tmpFile.name, JSON.stringify(config), 'utf8') + } catch (e) { + log.error("Couldn't write temporary configuration file") + done(1) + return + } + var child = spawn(process.argv[0], [path.resolve(__dirname, '../lib/detached.js'), tmpFile.name], { + detached: true, + stdio: 'ignore' + }) + child.unref() +} + // Export // ------ diff --git a/package.json b/package.json index ff1bfebe1..e39f62535 100644 --- a/package.json +++ b/package.json @@ -283,6 +283,7 @@ "rimraf": "^2.3.3", "socket.io": "^1.4.5", "source-map": "^0.5.3", + "tmp": "0.0.28", "useragent": "^2.1.6" }, "devDependencies": {