Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Point to healthcheck inside of the dist folder always #72

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 7 additions & 24 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,22 @@ import * as path from 'path'
declare const Cypress: any
declare const cy: any

// it will always live in the node_modules path
const HEALTHCHECK_PATH = 'node_modules/@percy/cypress/dist/percy-healthcheck';

Cypress.Commands.add('percySnapshot', (name: string, options: any = {}) => {
const percyAgentClient = new PercyAgent({ handleAgentCommunication: false })

// Use cy.exec(...) to check if percy agent is running. Ideally this would be
// done using something like cy.request(...), but that's not currently possible,
// for details, see: https://github.com/cypress-io/cypress/issues/3161
const healthcheck = `node ${_healthcheckPath()} ${percyAgentClient.port}`
cy.exec(healthcheck, {failOnNonZeroExit: false}).then((result: any) => {
if (result.code != 0) {
const healthcheckCmd = `node ${HEALTHCHECK_PATH} ${percyAgentClient.port}`
cy.exec(healthcheckCmd, { failOnNonZeroExit: false }).then((result: any) => {
if (result.code !== 0) {
// Percy server not available, or we failed to find the healthcheck.
cy.log('[percy] Percy agent is not running. Skipping snapshots')
cy.log(`[percy] Healthcheck output: ${result.stdout}\n${result.stderr}`)

return
}

Expand All @@ -42,24 +46,3 @@ Cypress.Commands.add('percySnapshot', (name: string, options: any = {}) => {
})
})
})


// An attempt to resiliently find the path to the 'percy-healthcheck' script, and to do so
// in a cross-platform manner.
function _healthcheckPath() {
try {
// Try to resolve with respect to the install local module.
return require.resolve('@percy/cypress/dist/percy-healthcheck')
} catch {
try {
// Try to resolve relative to the current file.
return require.resolve('./percy-healthcheck')
} catch {
// Oh well. Assume it's in the local node_modules.
// It would be nice to use __dirname here, but this code is entirely executed inside of
// Cypress' unusual context, making __dirname always '/dist' for this file, which is
// unhelpful when trying to find a working filesystem path to percy-healthcheck.
return path.join('.', './node_modules/@percy/cypress/dist/percy-healthcheck')
}
}
}