From 36ef3905ebe8cbe6e4c0a329f910cfc91c46aad3 Mon Sep 17 00:00:00 2001 From: Gleb Bahmutov Date: Wed, 27 Dec 2017 11:10:04 -0500 Subject: [PATCH] feat: add DEBUG=xvfb option close #7 --- README.md | 4 ++++ index.js | 23 ++++++++++++++++------- package.json | 1 + 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 7bfbff16..4f15b6cd 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,10 @@ The Xvfb constructor takes four options: * silent - don't pipe Xvfb stderr to the process's stderr. * xvfb_args - Extra arguments to pass to `Xvfb`. +### Debugging + +Run with `DEBUG=xvfb` environment variable to see debug messages + ### Thanks to Forked from [node-xvfb](https://github.com/Rob--W/node-xvfb) diff --git a/index.js b/index.js index fae7b64a..fd773e4b 100644 --- a/index.js +++ b/index.js @@ -2,6 +2,7 @@ 'use strict' +const debug = require('debug')('xvfb') const once = require('lodash.once') const fs = require('fs') const path = require('path') @@ -11,7 +12,7 @@ fs.existsSync = fs.existsSync || path.existsSync function Xvfb (options) { options = options || {} - this._display = (options.displayNum ? `:${options.displayNum}` : null) + this._display = options.displayNum ? `:${options.displayNum}` : null this._reuse = options.reuse this._timeout = options.timeout || 2000 this._silent = options.silent @@ -39,8 +40,9 @@ Xvfb.prototype = { return cb && cb(e) } - let totalTime = 0; - (function checkIfStarted () { + let totalTime = 0 + ;(function checkIfStarted () { + debug('checking if started by looking at the lock file', lockFile) fs.exists(lockFile, function (exists) { if (didSpawnFail) { // When spawn fails, the callback will immediately be called. @@ -71,8 +73,9 @@ Xvfb.prototype = { self._restoreDisplayEnvVariable() let lockFile = self._lockFile() - let totalTime = 0; - (function checkIfStopped () { + debug('lock file', lockFile) + let totalTime = 0 + ;(function checkIfStopped () { fs.exists(lockFile, function (exists) { if (!exists) { return cb && cb(null, self._process) @@ -130,7 +133,9 @@ Xvfb.prototype = { let display = self.display() if (lockFileExists) { if (!self._reuse) { - throw new Error(`Display ${display} is already in use and the "reuse" option is false.`) + throw new Error( + `Display ${display} is already in use and the "reuse" option is false.` + ) } } else { const stderr = [] @@ -167,7 +172,11 @@ Xvfb.prototype = { }, _lockFile (displayNum) { - displayNum = displayNum || this.display().toString().replace(/^:/, '') + displayNum = + displayNum || + this.display() + .toString() + .replace(/^:/, '') return `/tmp/.X${displayNum}-lock` }, } diff --git a/package.json b/package.json index 4d605df6..bce04178 100644 --- a/package.json +++ b/package.json @@ -15,6 +15,7 @@ "url": "https://github.com/cypress-io/xvfb.git" }, "dependencies": { + "debug": "^3.1.0", "lodash.once": "^4.1.1" }, "license": "MIT",