From ab81837596787764071f1947326f98b87881384a Mon Sep 17 00:00:00 2001 From: Chris Thielen Date: Thu, 29 Mar 2018 10:38:30 -0700 Subject: [PATCH] feat(runner): Manually walk up filesystem looking for node_modules/.bin/serve (and cypress) --- cypress-runner-cli.js | 21 +++++++++++++ cypress-runner.js | 69 +++++++++++++++++++++++-------------------- index.js | 1 + package.json | 2 +- 4 files changed, 60 insertions(+), 33 deletions(-) create mode 100755 cypress-runner-cli.js create mode 100644 index.js diff --git a/cypress-runner-cli.js b/cypress-runner-cli.js new file mode 100755 index 0000000..1c0377d --- /dev/null +++ b/cypress-runner-cli.js @@ -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); diff --git a/cypress-runner.js b/cypress-runner.js index e3cb850..995c905 100755 --- a/cypress-runner.js +++ b/cypress-runner.js @@ -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; diff --git a/index.js b/index.js new file mode 100644 index 0000000..ca9412d --- /dev/null +++ b/index.js @@ -0,0 +1 @@ +export * from './cypress-runner'; diff --git a/package.json b/package.json index 8f35bb6..40a6f2d 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,7 @@ "release": "release" }, "bin": { - "cypress-runner": "./cypress-runner.js" + "cypress-runner": "./cypress-runner-cli.js" }, "dependencies": { "cypress": "^2.1.0",