Skip to content

Commit

Permalink
feat(runner): Use portfinder to use "next open port". Add better logging
Browse files Browse the repository at this point in the history
  • Loading branch information
christopherthielen committed Apr 30, 2018
1 parent 8df31da commit 6d7eb86
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 44 deletions.
99 changes: 58 additions & 41 deletions cypress-runner.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const fs = require('fs');
const { resolve } = require('path');
const portfinder = require('portfinder');
const waitOn = require('wait-on');
const nodeCleanup = require('node-cleanup');
const { fork } = require('child_process');

function findBinary(binaryName, path) {
const binary = resolve(path, 'node_modules', '.bin', binaryName);
Expand All @@ -17,70 +19,85 @@ function findBinary(binaryName, path) {
return findBinary(binaryName, parent);
}

function launchCypress(cypressCmd, path, port) {
const log = (message) => console.log(`cypress-runner: ${message}`);
const error = (message) => console.log(`cypress-runner: ${message}`)

function launchCypress(cypressCmd, path, desiredPort) {
if (!fs.existsSync(path)) {
console.error(`${resolve(path)} doesn't exist, can't serve files`);
error(`${resolve(path)} doesn't exist, can't serve files`);
process.exit();
}

let serve, cypress;
let serve_process, cypress_process;

const cleanupServe = () => {
if (serve) {
console.log('Terminating serve...');
serve.kill();
if (serve_process) {
log('Terminating serve...');
serve_process.kill();
}
serve = null;
serve_process = null;
};

const cleanupCypress = () => {
if (cypress) {
console.log('Terminating cypress...')
cypress.kill();
if (cypress_process) {
log('Terminating cypress...')
cypress_process.kill();
}
cypress = null;
cypress_process = null;
};

const cleanup = () => {
cleanupServe();
cleanupCypress();
}
};

portfinder.basePort = desiredPort;
portfinder.getPort((err, port) => {
if (err) {
error(err);
process.exit(-1);
}

const baseUrl = `http://localhost:${port}`;

const { fork } = require('child_process');
const serveBinary = findBinary('serve', '.');
let serveArgs = [ '-n', '-s', '-p', port, path ];
const serveBinary = findBinary('serve', '.');
const serveArgs = [ '-n', '-s', '-p', port, path ];

const cypressBinary = findBinary('cypress', '.');
const cypressBinary = findBinary('cypress', '.');
const cypressArgs = [cypressCmd];
const cypressEnv = { CYPRESS_baseUrl: baseUrl };

console.log(`Launching ${[serveBinary].concat(serveArgs).join(' ')}`);
serve = fork(serveBinary, serveArgs);
log(`Launching ${[serveBinary].concat(serveArgs).join(' ')}`);
serve_process = fork(serveBinary, serveArgs);

waitOn({ resources: [`http://localhost:${port}`], window: 500, timeout: 30000}, () => {
console.log(`detected http service ready on port ${port}`)
console.log(`Launching ${[cypressBinary].concat(cypressCmd).join(' ')}`);
cypress = fork(cypressBinary, [ cypressCmd ]);
cypress.on('exit', (code) => {
console.log(`Cypress completed with exit code ${code}`);
cypress = null;
cleanupServe();
process.exit(code)
waitOn({ resources: [baseUrl], window: 500, timeout: 30000}, () => {
log(`Detected serve is ready on port ${port}`)
log(`Launching CYPRESS_baseUrl=${baseUrl} ${[cypressBinary].concat(cypressArgs).join(' ')}`);

cypress_process = fork(cypressBinary, [ cypressCmd ], { env: cypressEnv});
cypress_process.on('exit', (code) => {
log(`Cypress completed with exit code ${code}`);
cypress_process = null;
cleanupServe();
process.exit(code)
});
});
});

nodeCleanup((exitCode, signal) => {
if (signal) {
console.log(`cypress-runner exiting via signal ${signal}`);
} else if (exitCode) {
console.log(`cypress-runner exiting with exit code ${exitCode}`);
}
cleanup();
});
nodeCleanup((exitCode, signal) => {
if (signal) {
log(`exiting via signal ${signal}`);
} else if (exitCode) {
log(`exiting with exit code ${exitCode}`);
}
cleanup();
});

serve.on('exit', (code) => {
console.log(`Serve completed with exit code ${code}`);
serve = null;
cleanupCypress();
});
serve_process.on('exit', (code) => {
log(`Serve completed with exit code ${code}`);
serve_process = null;
cleanupCypress();
});
})
}

module.exports.launchCypress = launchCypress;
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"dependencies": {
"cypress": "^2.1.0",
"node-cleanup": "^2.1.2",
"portfinder": "^1.0.13",
"serve": "^6.5.5",
"wait-on": "^2.1.0",
"yargs": "^11.0.0"
Expand Down
14 changes: 11 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ async@2.1.4:
dependencies:
lodash "^4.14.0"

async@^1.4.0:
async@^1.4.0, async@^1.5.2:
version "1.5.2"
resolved "https://registry.yarnpkg.com/async/-/async-1.5.2.tgz#ec6a61ae56480c0c3cb241c95618e20892f9672a"

Expand Down Expand Up @@ -861,7 +861,7 @@ dateformat@^3.0.0:
version "3.0.3"
resolved "https://registry.yarnpkg.com/dateformat/-/dateformat-3.0.3.tgz#a6e37499a4d9a9cf85ef5872044d62901c9889ae"

debug@2.6.9, debug@^2.6.0:
debug@2.6.9, debug@^2.2.0, debug@^2.6.0:
version "2.6.9"
resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.9.tgz#5d128515df134ff327e90a4c93f4e077a536341f"
dependencies:
Expand Down Expand Up @@ -1954,7 +1954,7 @@ mkdirp@0.5.0:
dependencies:
minimist "0.0.8"

"mkdirp@>=0.5 0":
mkdirp@0.5.x, "mkdirp@>=0.5 0":
version "0.5.1"
resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-0.5.1.tgz#30057438eac6cf7f8c4767f38648d6697d75c903"
dependencies:
Expand Down Expand Up @@ -2252,6 +2252,14 @@ pkgresolve@^1.1.4:
dependencies:
minimist "~1.2.0"

portfinder@^1.0.13:
version "1.0.13"
resolved "https://registry.yarnpkg.com/portfinder/-/portfinder-1.0.13.tgz#bb32ecd87c27104ae6ee44b5a3ccbf0ebb1aede9"
dependencies:
async "^1.5.2"
debug "^2.2.0"
mkdirp "0.5.x"

pretty-bytes@^4.0.2:
version "4.0.2"
resolved "https://registry.yarnpkg.com/pretty-bytes/-/pretty-bytes-4.0.2.tgz#b2bf82e7350d65c6c33aa95aaa5a4f6327f61cd9"
Expand Down

0 comments on commit 6d7eb86

Please sign in to comment.