cli-diff
Version:
A diff utility with highlighted output for CLIs
43 lines • 1.73 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const chalk_1 = require("chalk");
const JSDiff = require("diff");
/**
* Create a ANSI-highlighted diff between the two provided files
* @param oldFile The original file. Can be passed as an object or as a plain string.
* @param newFile The new file. Can be passed as an object or as a plain string.
* @param options (optional) options to pass to JSDiff
* @return The ANSI-highlighted diff
*/
function diff(oldFile, newFile, options) {
if (typeof oldFile === 'string') {
oldFile = { content: oldFile };
}
if (typeof newFile === 'string') {
newFile = { content: newFile };
}
return (JSDiff.createTwoFilesPatch(oldFile.name || '', newFile.name || '', oldFile.content, newFile.content, oldFile.header || '', newFile.header || '', options)
.split('\n')
// remove “Index:” (if files are the same) and “====...”
.slice(oldFile.name === newFile.name ? 2 : 1)
// remove +++ and --- lines if the file name and header are blank
.slice(oldFile.name || newFile.name || oldFile.header || newFile.header ? 0 : 2)
.map(chunk => {
switch (chunk[0]) {
case '+':
return chalk_1.default.green(chunk);
case '-':
return chalk_1.default.red(chunk);
case '@':
return chalk_1.default.dim.blue(chunk);
// \ No newline at end of file
case '\\':
return chalk_1.default.dim.yellow(chunk);
default:
return chunk;
}
})
.join('\n'));
}
exports.default = diff;
//# sourceMappingURL=index.js.map