Skip to content

Commit

Permalink
refactor: require named function expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
honzajavorek authored and kylef committed Dec 31, 2018
1 parent 970af54 commit 7222070
Show file tree
Hide file tree
Showing 18 changed files with 96 additions and 74 deletions.
1 change: 0 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ module.exports = {
// of the codebase possible and are to be removed in the future
'class-methods-use-this': 'off',
'consistent-return': 'off',
'func-names': 'off',
'import/no-extraneous-dependencies': 'off',
'import/no-unresolved': 'off',
'max-len': 'off',
Expand Down
14 changes: 10 additions & 4 deletions lib/childProcess.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ const crossSpawn = require('cross-spawn');

const ignorePipeErrors = require('./ignorePipeErrors');


const ASCII_CTRL_C = 3;
const IS_WINDOWS = process.platform === 'win32';
const TERM_FIRST_CHECK_TIMEOUT_MS = 1;
const TERM_DEFAULT_TIMEOUT_MS = 1000;
const TERM_DEFAULT_RETRY_MS = 300;


// Signals the child process to forcefully terminate
function signalKill(childProcess, callback) {
childProcess.emit('signalKill');
Expand All @@ -27,6 +29,7 @@ function signalKill(childProcess, callback) {
}
}


// Signals the child process to gracefully terminate
function signalTerm(childProcess, callback) {
childProcess.emit('signalTerm');
Expand Down Expand Up @@ -55,6 +58,7 @@ function signalTerm(childProcess, callback) {
process.nextTick(callback);
}


// Gracefully terminates a child process
//
// Sends a signal to the process as a heads up it should terminate.
Expand Down Expand Up @@ -91,7 +95,7 @@ function terminate(childProcess, options = {}, callback) {

// A function representing one check, whether the process already
// ended or not. It is repeatedly called until the timeout has passed.
const check = function () {
function check() {
if (terminated) {
// Successfully terminated
clearTimeout(t);
Expand All @@ -115,7 +119,7 @@ function terminate(childProcess, options = {}, callback) {
);
}
}
};
}

// Fire the first termination attempt and check the result
signalTerm(childProcess, (err) => {
Expand All @@ -124,7 +128,8 @@ function terminate(childProcess, options = {}, callback) {
});
}

const spawn = function (...args) {

function spawn(...args) {
const childProcess = crossSpawn.spawn.apply(null, args);

ignorePipeErrors(childProcess);
Expand Down Expand Up @@ -195,7 +200,8 @@ const spawn = function (...args) {
});

return childProcess;
};
}


module.exports = {
signalKill,
Expand Down
22 changes: 14 additions & 8 deletions lib/configUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ const clone = require('clone');
const fs = require('fs');
const yaml = require('js-yaml');

const configUtils = {};

configUtils.save = function (argsOrigin, path) {
function save(argsOrigin, path) {
if (!path) { path = './dredd.yml'; }

const args = clone(argsOrigin);
Expand All @@ -20,9 +19,10 @@ configUtils.save = function (argsOrigin, path) {
delete args._;

fs.writeFileSync(path, yaml.dump(args));
};
}


configUtils.load = function (path) {
function load(path) {
if (!path) { path = './dredd.yml'; }

const yamlData = fs.readFileSync(path);
Expand All @@ -34,9 +34,10 @@ configUtils.load = function (path) {
delete data.endpoint;

return data;
};
}


configUtils.parseCustom = function (customArray) {
function parseCustom(customArray) {
const output = {};
if (Array.isArray(customArray)) {
for (const string of customArray) {
Expand All @@ -45,6 +46,11 @@ configUtils.parseCustom = function (customArray) {
}
}
return output;
};
}


module.exports = configUtils;
module.exports = {
save,
load,
parseCustom,
};
21 changes: 15 additions & 6 deletions lib/reporters/ApiaryReporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const request = require('request');
const logger = require('../logger');
const packageData = require('../../package.json');


const CONNECTION_ERRORS = [
'ECONNRESET',
'ENOTFOUND',
Expand All @@ -16,6 +17,7 @@ const CONNECTION_ERRORS = [
'EPIPE',
];


function ApiaryReporter(emitter, stats, tests, config, runner) {
this.type = 'apiary';
this.stats = stats;
Expand Down Expand Up @@ -49,8 +51,9 @@ https://dredd.org/en/latest/how-to-guides/#using-apiary-reporter-and-apiary-test
if (!this.configuration.apiSuite) { this.configuration.apiSuite = 'public'; }
}


// THIS IS HIIIIGHWAY TO HELL, HIIIIIGHWAY TO HELL. Everything should have one single interface
ApiaryReporter.prototype._get = function (customProperty, envProperty, defaultVal) {
ApiaryReporter.prototype._get = function _get(customProperty, envProperty, defaultVal) {
let returnVal = defaultVal;

// This will be deprecated
Expand All @@ -77,13 +80,15 @@ ApiaryReporter.prototype._get = function (customProperty, envProperty, defaultVa
return returnVal;
};

ApiaryReporter.prototype._getKeys = function () {

ApiaryReporter.prototype._getKeys = function _getKeys() {
let returnKeys = [];
returnKeys = returnKeys.concat(Object.keys((this.config.custom && this.config.custom.apiaryReporterEnv) || {}));
return returnKeys.concat(Object.keys(process.env));
};

ApiaryReporter.prototype.configureEmitter = function (emitter) {

ApiaryReporter.prototype.configureEmitter = function configureEmitter(emitter) {
emitter.on('start', (blueprintsData, callback) => {
if (this.serverError === true) { return callback(); }
this.uuid = generateUuid();
Expand Down Expand Up @@ -187,7 +192,8 @@ ApiaryReporter.prototype.configureEmitter = function (emitter) {
});
};

ApiaryReporter.prototype._createStep = function (test, callback) {

ApiaryReporter.prototype._createStep = function _createStep(test, callback) {
if (this.serverError === true) { return callback(); }
const data = this._transformTestToReporter(test);
const path = `/apis/${this.configuration.apiSuite}/tests/steps?testRunId=${this.remoteId}`;
Expand All @@ -197,7 +203,8 @@ ApiaryReporter.prototype._createStep = function (test, callback) {
});
};

ApiaryReporter.prototype._performRequestAsync = function (path, method, reqBody, callback) {

ApiaryReporter.prototype._performRequestAsync = function _performRequestAsync(path, method, reqBody, callback) {
const handleRequest = (err, res, resBody) => {
let parsedBody;
if (err) {
Expand Down Expand Up @@ -263,7 +270,8 @@ to Apiary API: ${options.method} ${options.uri} \
}
};

ApiaryReporter.prototype._transformTestToReporter = function (test) {

ApiaryReporter.prototype._transformTestToReporter = function _transformTestToReporter(test) {
return {
testRunId: this.remoteId,
origin: test.origin,
Expand All @@ -279,4 +287,5 @@ ApiaryReporter.prototype._transformTestToReporter = function (test) {
};
};


module.exports = ApiaryReporter;
2 changes: 1 addition & 1 deletion lib/reporters/BaseReporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function BaseReporter(emitter, stats, tests) {
logger.verbose(`Using '${this.type}' reporter.`);
}

BaseReporter.prototype.configureEmitter = function (emitter) {
BaseReporter.prototype.configureEmitter = function configureEmitter(emitter) {
emitter.on('start', (rawBlueprint, callback) => {
this.stats.start = new Date();
callback();
Expand Down
2 changes: 1 addition & 1 deletion lib/reporters/CLIReporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function CLIReporter(emitter, stats, tests, inlineErrors, details) {
logger.verbose(`Using '${this.type}' reporter.`);
}

CLIReporter.prototype.configureEmitter = function (emitter) {
CLIReporter.prototype.configureEmitter = function configureEmitter(emitter) {
emitter.on('start', (rawBlueprint, callback) => {
logger.info('Beginning Dredd testing...');
callback();
Expand Down
4 changes: 2 additions & 2 deletions lib/reporters/DotReporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function DotReporter(emitter, stats, tests) {
logger.verbose(`Using '${this.type}' reporter.`);
}

DotReporter.prototype.configureEmitter = function (emitter) {
DotReporter.prototype.configureEmitter = function configureEmitter(emitter) {
emitter.on('start', (rawBlueprint, callback) => {
logger.info('Beginning Dredd testing...');
callback();
Expand Down Expand Up @@ -63,7 +63,7 @@ ${this.stats.errors} errors, ${this.stats.skipped} skipped\
});
};

DotReporter.prototype.write = function (str) {
DotReporter.prototype.write = function write(str) {
process.stdout.write(str);
};

Expand Down
4 changes: 2 additions & 2 deletions lib/reporters/HTMLReporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ function HTMLReporter(emitter, stats, tests, path, details) {
logger.verbose(`Using '${this.type}' reporter.`);
}

HTMLReporter.prototype.sanitizedPath = function (path) {
HTMLReporter.prototype.sanitizedPath = function sanitizedPath(path) {
const filePath = path ? file.path.abspath(path) : file.path.abspath('./report.html');
if (fs.existsSync(filePath)) {
logger.info(`File exists at ${filePath}, will be overwritten...`);
}
return filePath;
};

HTMLReporter.prototype.configureEmitter = function (emitter) {
HTMLReporter.prototype.configureEmitter = function configureEmitter(emitter) {
const title = str => `${Array(this.level).join('#')} ${str}`;

emitter.on('start', (rawBlueprint, callback) => {
Expand Down
4 changes: 2 additions & 2 deletions lib/reporters/MarkdownReporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ function MarkdownReporter(emitter, stats, tests, path, details) {
logger.verbose(`Using '${this.type}' reporter.`);
}

MarkdownReporter.prototype.sanitizedPath = function (path) {
MarkdownReporter.prototype.sanitizedPath = function sanitizedPath(path) {
const filePath = path ? file.path.abspath(path) : file.path.abspath('./report.md');
if (fs.existsSync(filePath)) {
logger.info(`File exists at ${filePath}, will be overwritten...`);
}
return filePath;
};

MarkdownReporter.prototype.configureEmitter = function (emitter) {
MarkdownReporter.prototype.configureEmitter = function configureEmitter(emitter) {
const title = str => `${Array(this.level).join('#')} ${str}`;

emitter.on('start', (rawBlueprint, callback) => {
Expand Down
28 changes: 14 additions & 14 deletions lib/reporters/NyanReporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function NyanCatReporter(emitter, stats, tests) {
logger.verbose(`Using '${this.type}' reporter.`);
}

NyanCatReporter.prototype.configureEmitter = function (emitter) {
NyanCatReporter.prototype.configureEmitter = function configureEmitter(emitter) {
emitter.on('start', (rawBlueprint, callback) => {
this.cursorHide();
this.draw();
Expand Down Expand Up @@ -89,15 +89,15 @@ NyanCatReporter.prototype.configureEmitter = function (emitter) {
});
};

NyanCatReporter.prototype.draw = function () {
NyanCatReporter.prototype.draw = function draw() {
this.appendRainbow();
this.drawScoreboard();
this.drawRainbow();
this.drawNyanCat();
this.tick = !this.tick;
};

NyanCatReporter.prototype.drawScoreboard = function () {
NyanCatReporter.prototype.drawScoreboard = function drawScoreboard() {
const colors = {
fail: 31,
skipped: 36,
Expand All @@ -120,7 +120,7 @@ NyanCatReporter.prototype.drawScoreboard = function () {
this.cursorUp(this.numberOfLines + 1);
};

NyanCatReporter.prototype.appendRainbow = function () {
NyanCatReporter.prototype.appendRainbow = function appendRainbow() {
const segment = (this.tick ? '_' : '-');
const rainbowified = this.rainbowify(segment);
const result = [];
Expand All @@ -135,7 +135,7 @@ NyanCatReporter.prototype.appendRainbow = function () {
return result;
};

NyanCatReporter.prototype.drawRainbow = function () {
NyanCatReporter.prototype.drawRainbow = function drawRainbow() {
this.trajectories.forEach((line) => {
this.write(`\u001b[${this.scoreboardWidth}C`);
this.write(line.join(''));
Expand All @@ -145,7 +145,7 @@ NyanCatReporter.prototype.drawRainbow = function () {
this.cursorUp(this.numberOfLines);
};

NyanCatReporter.prototype.drawNyanCat = function () {
NyanCatReporter.prototype.drawNyanCat = function drawNyanCat() {
const startWidth = this.scoreboardWidth + this.trajectories[0].length;
const color = `\u001b[${startWidth}C`;
let padding = '';
Expand All @@ -168,7 +168,7 @@ NyanCatReporter.prototype.drawNyanCat = function () {
this.cursorUp(this.numberOfLines);
};

NyanCatReporter.prototype.face = function () {
NyanCatReporter.prototype.face = function face() {
if (this.stats.failures) {
return '( x .x)';
} else if (this.stats.skipped) {
Expand All @@ -179,23 +179,23 @@ NyanCatReporter.prototype.face = function () {
return '( - .-)';
};

NyanCatReporter.prototype.cursorUp = function (n) {
NyanCatReporter.prototype.cursorUp = function cursorUp(n) {
this.write(`\u001b[${n}A`);
};

NyanCatReporter.prototype.cursorDown = function (n) {
NyanCatReporter.prototype.cursorDown = function cursorDown(n) {
this.write(`\u001b[${n}B`);
};

NyanCatReporter.prototype.cursorShow = function () {
NyanCatReporter.prototype.cursorShow = function cursorShow() {
if (this.isatty) { this.write('\u001b[?25h'); }
};

NyanCatReporter.prototype.cursorHide = function () {
NyanCatReporter.prototype.cursorHide = function cursorHide() {
if (this.isatty) { this.write('\u001b[?25l'); }
};

NyanCatReporter.prototype.generateColors = function () {
NyanCatReporter.prototype.generateColors = function generateColors() {
const colors = [];
let i = 0;

Expand All @@ -211,13 +211,13 @@ NyanCatReporter.prototype.generateColors = function () {
return colors;
};

NyanCatReporter.prototype.rainbowify = function (str) {
NyanCatReporter.prototype.rainbowify = function rainbowify(str) {
const color = this.rainbowColors[this.colorIndex % this.rainbowColors.length];
this.colorIndex += 1;
return `\u001b[38;5;${color}m${str}\u001b[0m`;
};

NyanCatReporter.prototype.write = function (str) {
NyanCatReporter.prototype.write = function write(str) {
process.stdout.write(str);
};

Expand Down
Loading

0 comments on commit 7222070

Please sign in to comment.