Skip to content

Commit

Permalink
Convert to ES2015 classes
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Mar 9, 2017
1 parent 0a43ad1 commit 1a478c5
Show file tree
Hide file tree
Showing 7 changed files with 641 additions and 612 deletions.
114 changes: 61 additions & 53 deletions lib/adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,70 +13,78 @@ const logger = require('./util/log');
*
* @constructor
*/
const TerminalAdapter = module.exports = function TerminalAdapter() {
this.promptModule = inquirer.createPromptModule();
};
class TerminalAdapter {
constructor() {
this.promptModule = inquirer.createPromptModule();
}

TerminalAdapter.prototype._colorDiffAdded = chalk.black.bgGreen;
TerminalAdapter.prototype._colorDiffRemoved = chalk.bgRed;
TerminalAdapter.prototype._colorLines = function colorLines(name, str) {
return str.split('\n').map(line => this[`_colorDiff${name}`](line)).join('\n');
};
get _colorDiffAdded() {
return chalk.black.bgGreen;
}

/**
* Prompt a user for one or more questions and pass
* the answer(s) to the provided callback.
*
* It shares its interface with `Base.prompt`
*
* (Defined inside the constructor to keep interfaces separated between
* instances)
*
* @param {Array} questions
* @param {Function} callback
*/
TerminalAdapter.prototype.prompt = function () {};
get _colorDiffRemoved() {
return chalk.bgRed;
}

/**
* Shows a color-based diff of two strings
*
* @param {string} actual
* @param {string} expected
*/
TerminalAdapter.prototype.diff = function _diff(actual, expected) {
let msg = diff.diffLines(actual, expected).map(str => {
if (str.added) {
return this._colorLines('Added', str.value);
}
_colorLines(name, str) {
return str.split('\n').map(line => this[`_colorDiff${name}`](line)).join('\n');
}

/**
* Prompt a user for one or more questions and pass
* the answer(s) to the provided callback.
*
* It shares its interface with `Base.prompt`
*
* (Defined inside the constructor to keep interfaces separated between
* instances)
*
* @param {Array} questions
* @param {Function} callback
*/
prompt(questions, cb) {
const promise = this.promptModule(questions);
promise.then(cb || _.noop);
return promise;
}

/**
* Shows a color-based diff of two strings
*
* @param {string} actual
* @param {string} expected
*/
diff(actual, expected) {
let msg = diff.diffLines(actual, expected).map(str => {
if (str.added) {
return this._colorLines('Added', str.value);
}

if (str.removed) {
return this._colorLines('Removed', str.value);
}
if (str.removed) {
return this._colorLines('Removed', str.value);
}

return str.value;
}).join('');
return str.value;
}).join('');

// Legend
msg = '\n' +
this._colorDiffRemoved('removed') +
' ' +
this._colorDiffAdded('added') +
'\n\n' +
msg +
'\n';
// Legend
msg = '\n' +
this._colorDiffRemoved('removed') +
' ' +
this._colorDiffAdded('added') +
'\n\n' +
msg +
'\n';

console.log(msg);
return msg;
};
console.log(msg);
return msg;
}
}

/**
* Logging utility
* @type {env/log}
*/
TerminalAdapter.prototype.log = logger();

TerminalAdapter.prototype.prompt = function (questions, cb) {
const promise = this.promptModule(questions);
promise.then(cb || _.noop);
return promise;
};
module.exports = TerminalAdapter;
Loading

0 comments on commit 1a478c5

Please sign in to comment.