Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

resructure

  • Loading branch information...
commit cd0b5e0c28446fa790c4dc66865c5b571ebdf5be 1 parent 4d19dad
Kir Belevich authored
Showing with 89 additions and 69 deletions.
  1. +5 −69 index.js
  2. +41 −0 lib/phantom.js
  3. +43 −0 lib/server.js
View
74 index.js
@@ -1,79 +1,15 @@
'use strict';
-var http = require('http'),
- path = require('path'),
- Static = require('node-static'),
- spawn = require('child_process').spawn;
+var server = require('./lib/server.js'),
+ phantom = require('./lib/phantom.js');
module.exports = function(params, callback) {
// default port
params.port = params.port || '8082';
+ // poncho directory path
+ params.ponchoDir = __dirname;
- var file = new Static.Server('./', {
- cache: false,
- headers: { 'Cache-Control': 'no-cache, must-revalidate, max-age=0' }
- }),
- server;
-
- // create HTTP server
- server = http.createServer(function(req, res) {
-
- var post = '';
-
- // if coverage was requested – collect POST data…
- if(req.url === '/coverage') {
- req.on('data', function(chunk) {
- post += chunk;
- });
- }
-
- req.on('end', function() {
- if(post) {
- // …and callback it
- post = JSON.parse(post);
- callback(post.data);
-
- server.close();
- }
-
- // serve static files
- file.serve(req, res);
- }).resume();
-
- }).listen(params.port);
-
- // spawn PhantomJS with arguments
- var phantomjs = spawn('phantomjs', [
- // bridge file
- path.resolve(__dirname, './lib/bridge.js'),
- // poncho relative dir
- path.relative(path.dirname(params.file), __dirname),
- // inject file
- path.resolve(__dirname, './lib/inject.html'),
- // test file
- params.file,
- // server port
- params.port,
- // reporter
- params.reporter
- ]);
-
- // phantomjs.stderr.pipe(process.stdout);
- phantomjs.stdout.pipe(process.stdout);
-
- // bind any PhantomJS errors to current process
- phantomjs.on('exit', function(code) {
- if(code === 127) {
- /*eslint no-console:0*/
- console.error('PhantomJS is not installed?');
- }
-
- if(code > 0) {
- server.close(function() {
- process.exit(code);
- });
- }
- });
+ phantom(params, server(params.port, callback));
};
View
41 lib/phantom.js
@@ -0,0 +1,41 @@
+'use strict';
+
+var path = require('path'),
+ spawn = require('child_process').spawn;
+
+module.exports = function(params, server) {
+
+ // spawn PhantomJS with arguments
+ var phantomjs = spawn('phantomjs', [
+ // bridge file
+ path.resolve(params.ponchoDir, './lib/bridge.js'),
+ // poncho relative dir
+ path.relative(path.dirname(params.file), params.ponchoDir),
+ // inject file
+ path.resolve(params.ponchoDir, './lib/inject.html'),
+ // test file
+ params.file,
+ // server port
+ params.port,
+ // reporter
+ params.reporter
+ ]);
+
+ // phantomjs.stderr.pipe(process.stdout);
+ phantomjs.stdout.pipe(process.stdout);
+
+ // bind any PhantomJS errors to current process
+ phantomjs.on('exit', function(code) {
+ if(code === 127) {
+ /*eslint no-console:0*/
+ console.error('PhantomJS is not installed?');
+ }
+
+ if(code > 0) {
+ server.close(function() {
+ process.exit(code);
+ });
+ }
+ });
+
+};
View
43 lib/server.js
@@ -0,0 +1,43 @@
+'use strict';
+
+var http = require('http'),
+ Static = require('node-static');
+
+module.exports = function(port, callback) {
+
+ var file = new Static.Server('./', {
+ cache: false,
+ headers: { 'Cache-Control': 'no-cache, must-revalidate, max-age=0' }
+ }),
+ server;
+
+ // create HTTP server
+ server = http.createServer(function(req, res) {
+
+ var post = '';
+
+ // if coverage was requested – collect POST data…
+ if(req.url === '/coverage') {
+ req.on('data', function(chunk) {
+ post += chunk;
+ });
+ }
+
+ req.on('end', function() {
+ if(post) {
+ // …and callback it
+ post = JSON.parse(post);
+ callback(post.data);
+
+ server.close();
+ }
+
+ // serve static files
+ file.serve(req, res);
+ }).resume();
+
+ }).listen(port, '127.0.0.1');
+
+ return server;
+
+};
Please sign in to comment.
Something went wrong with that request. Please try again.