Skip to content

Commit

Permalink
feat(runner): Manually walk up filesystem looking for node_modules/.b…
Browse files Browse the repository at this point in the history
…in/serve (and cypress)
  • Loading branch information
christopherthielen committed Mar 29, 2018
1 parent 118ede2 commit ab81837
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 33 deletions.
21 changes: 21 additions & 0 deletions cypress-runner-cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env node

const { launchCypress } = require('./cypress-runner');
const yargs = require('yargs')
.usage('Usage: $0 [open|run] [options]')
.command('open', 'Open the cypress UI')
.command('run', 'Runs the cypress test suite')
.demandCommand(1, 'Specify either "open" or "run"')
.option('path', {
description: 'The path to serve files from',
default: 'dist',
})
.option('port', {
description: 'The port to serve files from',
default: 4000,
}).argv;

const { path, port } = yargs;
const [ cypressCmd ] = yargs._;

launchCypress(cypressCmd, path, port);
69 changes: 37 additions & 32 deletions cypress-runner.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,42 @@
#!/usr/bin/env node

const fs = require('fs');
const { resolve } = require('path');
const yargs = require('yargs')
.usage('Usage: $0 [open|run] [options]')
.command('open', 'Open the cypress UI')
.command('run', 'Runs the cypress test suite')
.demandCommand(1, 'Specify either "open" or "run"')
.option('path', {
description: 'The path to serve files from',
default: 'dist',
})
.option('port', {
description: 'The port to serve files from',
default: 4000,
}).argv;

const { path, port } = yargs;
const [ cypressCmd ] = yargs._;

if (!fs.existsSync(path)) {
console.error(`${resolve(path)} doesn't exist, can't serve files`);
process.exit();
const { resolve } = require('path');

function findBinary(binaryName, path) {
const binary = resolve(path, 'node_modules', '.bin', binaryName);
if (fs.existsSync(binary)) {
return binary;
}

const parent = resolve(path, '..');
if (parent === path) {
throw new Error(`Couldn't find **/node_modules/.bin/${binaryName} in parents of ${path.resolve()}`);
}

return findBinary(binaryName, parent);
}

const { fork } = require('child_process');
const browserSync = fork('npx', ['serve', '-n', '-s', '-p', port, path]);
const cypress = fork('npx', ['cypress', cypressCmd]);
function launchCypress(cypressCmd, path, port) {
if (!fs.existsSync(path)) {
console.error(`${resolve(path)} doesn't exist, can't serve files`);
process.exit();
}

process.on('SIGINT', function() {
browserSync.kill();
cypress.kill();
});
const { fork } = require('child_process');
const serveBinary = findBinary('serve', '.');
const cypressBinary = findBinary('cypress', '.');
const browserSync = fork(serveBinary, [ '-n', '-s', '-p', port, path ]);
const cypress = fork(cypressBinary, [ cypressCmd ]);

process.on('SIGINT', function () {
browserSync.kill();
cypress.kill();
});

browserSync.on('exit', () => cypress.kill());
cypress.on('exit', (code) => {
browserSync.kill();
process.exit(code)
});
}

browserSync.on('exit', () => cypress.kill());
cypress.on('exit', (code) => { browserSync.kill(); process.exit(code) });
module.exports.launchCypress = launchCypress;
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './cypress-runner';
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"release": "release"
},
"bin": {
"cypress-runner": "./cypress-runner.js"
"cypress-runner": "./cypress-runner-cli.js"
},
"dependencies": {
"cypress": "^2.1.0",
Expand Down

0 comments on commit ab81837

Please sign in to comment.