Skip to content
This repository has been archived by the owner on Dec 4, 2023. It is now read-only.

Commit

Permalink
use userland which; closes mochajs#3639
Browse files Browse the repository at this point in the history
- refactor to use modern JS in `lib/growl.js`; update ESLint rules accordingly

Signed-off-by: Christopher Hiller <boneskull@boneskull.com>
  • Loading branch information
boneskull committed Jan 1, 2019
1 parent 1192344 commit abf943c
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 73 deletions.
1 change: 1 addition & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ overrides:
- bin/*
- lib/cli/**/*.js
- test/node-unit/**/*.js
- lib/growl.js
parserOptions:
ecmaVersion: 6
env:
Expand Down
96 changes: 35 additions & 61 deletions lib/growl.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
* @module Growl
*/

var fs = require('fs');
var os = require('os');
var path = require('path');
const os = require('os');
const path = require('path');
const {sync: which} = require('which');

/**
* @summary
Expand All @@ -23,8 +23,14 @@ var path = require('path');
* @see {@link Mocha#isGrowlCapable}
* @return {boolean} whether Growl notification support can be expected
*/
exports.isCapable = function() {
return !process.browser && which(getSupportBinaries()) !== '';
exports.isCapable = () => {
if (!process.browser) {
return getSupportBinaries().reduce(
(acc, binary) => acc || Boolean(which(binary, {nothrow: true})),
false
);
}
return false;
};

/**
Expand All @@ -34,11 +40,10 @@ exports.isCapable = function() {
* @see {@link Mocha#_growl}
* @param {Runner} runner - Runner instance.
*/
exports.notify = function(runner) {
var sendNotification = function() {
exports.notify = runner => {
runner.once('end', () => {
display(runner);
};
runner.once('end', sendNotification);
});
};

/**
Expand All @@ -47,35 +52,35 @@ exports.notify = function(runner) {
* @private
* @param {Runner} runner - Runner instance.
*/
function display(runner) {
var growl = require('growl');
var stats = runner.stats;
var symbol = {
const display = runner => {
const growl = require('growl');
const stats = runner.stats;
const symbol = {
cross: '\u274C',
tick: '\u2705'
};
var _message;
var message;
var title;
let _message;
let message;
let title;

if (stats.failures) {
_message = stats.failures + ' of ' + runner.total + ' tests failed';
message = symbol.cross + ' ' + _message;
_message = `${stats.failures} of ${runner.total} tests failed`;
message = `${symbol.cross} ${_message}`;
title = 'Failed';
} else {
_message = stats.passes + ' tests passed in ' + stats.duration + 'ms';
message = symbol.tick + ' ' + _message;
_message = `${stats.passes} tests passed in ${stats.duration}ms`;
message = `${symbol.tick} ${_message}`;
title = 'Passed';
}

// Send notification
var options = {
const options = {
image: logo(),
name: 'mocha',
title: title
title
};
growl(message, options, onCompletion);
}
};

/**
* @summary
Expand All @@ -88,15 +93,11 @@ function display(runner) {
* @private
* @callback Growl~growlCB
* @param {*} err - Error object, or <code>null</code> if successful.
* @param {string} stdout - <code>stdout</code> from notification delivery
* process.
* @param {string} stderr - <code>stderr</code> from notification delivery
* process. It will include timestamp and executed command with arguments.
*/
function onCompletion(err, stdout, stderr) {
function onCompletion(err) {
if (err) {
// As notifications are tangential to our purpose, just log the error.
var message =
const message =
err.code === 'ENOENT' ? 'prerequisite software not found' : err.message;
console.error('notification error:', message);
}
Expand All @@ -108,36 +109,9 @@ function onCompletion(err, stdout, stderr) {
* @private
* @return {string} Pathname of Mocha logo
*/
function logo() {
const logo = () => {
return path.join(__dirname, '..', 'assets', 'mocha-logo-96.png');
}

/**
* @summary
* Locates a binary in the user's `PATH` environment variable.
*
* @description
* Takes a list of command names and searches the path for each executable
* file that would be run had these commands actually been invoked.
*
* @private
* @param {string[]} binaries - Names of binary files to search for.
* @return {string} absolute path of first binary found, or empty string if none
*/
function which(binaries) {
var paths = process.env.PATH.split(path.delimiter);

for (var n = 0, blen = binaries.length; n < blen; n++) {
var binary = binaries[n];
for (var i = 0, plen = paths.length; i < plen; i++) {
var loc = path.join(paths[i], binary);
if (fs.existsSync(loc)) {
return loc;
}
}
}
return '';
}
};

/**
* @summary
Expand All @@ -151,11 +125,11 @@ function which(binaries) {
* @see {@link https://github.com/tj/node-growl/blob/master/lib/growl.js#L28-L126|setupCmd}
* @return {string[]} names of Growl support binaries
*/
function getSupportBinaries() {
var binaries = {
const getSupportBinaries = () => {
const binaries = {
Darwin: ['terminal-notifier', 'growlnotify'],
Linux: ['notify-send', 'growl'],
Windows_NT: ['growlnotify.exe']
};
return binaries[os.type()] || [];
}
};
15 changes: 3 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,7 @@
"object.assign": "4.1.0",
"strip-json-comments": "2.0.1",
"supports-color": "^6.0.0",
"which": "1.3.1",
"wide-align": "1.1.3",
"yargs": "12.0.5",
"yargs-parser": "11.1.1",
Expand Down

0 comments on commit abf943c

Please sign in to comment.