diff options
Diffstat (limited to 'node_modules/dir-compare/src/cli')
-rwxr-xr-x | node_modules/dir-compare/src/cli/dircompare.js | 147 | ||||
-rw-r--r-- | node_modules/dir-compare/src/cli/print.js | 192 |
2 files changed, 339 insertions, 0 deletions
diff --git a/node_modules/dir-compare/src/cli/dircompare.js b/node_modules/dir-compare/src/cli/dircompare.js new file mode 100755 index 0000000..593621d --- /dev/null +++ b/node_modules/dir-compare/src/cli/dircompare.js @@ -0,0 +1,147 @@ +#!/usr/bin/env node + +var program = require('commander') +var dircompare = require('../index') +var fs = require('fs') +var util = require('util') +var print = require('./print') +var pjson = require('../../package.json') + +program + .version(pjson.version) + .usage('[options] leftdir rightdir') + .option('-c, --compare-content', 'compare files by content') + .option('-D, --compare-date', 'compare files by date') + .option('--date-tolerance [type]', 'tolerance to be used in date comparison (milliseconds)') + .option('--compare-symlink', 'compare files and directories by symlink') + .option('-f, --filter [type]', 'file name filter', undefined) + .option('-x, --exclude [type]', 'file/directory name exclude filter', undefined) + .option('-S, --skip-subdirs', 'do not recurse into subdirectories') + .option('-L, --skip-symlinks', 'ignore symlinks') + .option('-i, --ignore-case', 'ignores case when comparing file names') + .option('-l, --show-left', 'report - show entries occurring in left dir') + .option('-r, --show-right', 'report - show entries occurring in right dir') + .option('-e, --show-equal', 'report - show identic entries occurring in both dirs') + .option('-d, --show-distinct', 'report - show distinct entries occurring in both dirs') + .option('-a, --show-all', 'report - show all entries') + .option('-w, --whole-report', 'report - include directories in detailed report') + .option('--reason', 'report - show reason when entries are distinct') + .option('--csv', 'report - print details as csv') + .option('--nocolors', 'don\'t use console colors') + .option('--async', 'Make use of multiple cores') + + +program.on('--help', function () { + console.log(' By default files are compared by size.') + console.log(' --date-tolerance defaults to 1000 ms. Two files are considered to have') + console.log(' the same date if the difference between their modification dates fits') + console.log(' within date tolerance.') + console.log() + console.log(' Exit codes:') + console.log(' 0 - entries are identical') + console.log(' 1 - entries are different') + console.log(' 2 - error occurred') + console.log() + console.log(' Examples:') + console.log(' compare by content dircompare -c dir1 dir2') + console.log(' show only different files dircompare -d dir1 dir2') + console.log() + console.log(' exclude filter dircompare -x ".git,node_modules" dir1 dir2') + console.log(' dircompare -x "/tests/expected" dir1 dir2') + console.log(' dircompare -x "**/expected" dir1 dir2') + console.log(' dircompare -x "**/tests/**/*.ts" dir1 dir2') + console.log() + console.log(' include filter dircompare -f "*.js,*.yml" dir1 dir2') + console.log(' dircompare -f "/tests/**/*.js" dir1 dir2') + console.log(' dircompare -f "**/tests/**/*.ts" dir1 dir2') +}) + +// Fix for https://github.com/tj/commander.js/issues/125 +program.allowUnknownOption() +program.parse(process.argv) +var parsed = program.parseOptions(program.normalize(process.argv.slice(2))) +if (parsed.unknown.length > 0) { + console.error('Unknown options: ' + parsed.unknown) + process.exit(2) +} + +var run = function () { + try { + if (program.args.length !== 2) { + program.outputHelp() + process.exit(2) + } else { + var options = {} + + + options.compareContent = program.compareContent + options.compareDate = program.compareDate + options.compareSymlink = program.compareSymlink + options.compareSize = true + options.skipSubdirs = program.skipSubdirs + options.skipSymlinks = program.skipSymlinks + options.ignoreCase = program.ignoreCase + options.includeFilter = program.filter + options.excludeFilter = program.exclude + options.noDiffSet = !(program.showAll || program.showEqual || program.showLeft || program.showRight || program.showDistinct) + options.dateTolerance = program.dateTolerance || 1000 + + var async = program.async + + var path1 = program.args[0] + var path2 = program.args[1] + var abort = false + if (!isNumeric(options.dateTolerance)) { + console.error("Numeric value expected for --date-tolerance") + abort = true + } + if (!fs.existsSync(path1)) { + console.error(util.format("Path '%s' missing"), path1) + abort = true + } + if (!fs.existsSync(path2)) { + console.error(util.format("Path '%s' missing"), path2) + abort = true + } + if (!abort) { + // compare + var comparePromise + if (async) { + comparePromise = dircompare.compare(path1, path2, options) + } else { + comparePromise = new Promise(function (resolve, reject) { + resolve(dircompare.compareSync(path1, path2, options)) + }) + } + + comparePromise.then( + function (res) { + // PRINT DETAILS + print(res, process.stdout, program) + if (res.same) { + process.exit(0) + } else { + process.exit(1) + } + }, + function (error) { + console.error('Error occurred: ' + (error instanceof Error ? error.stack : error)) + process.exit(2) + }) + } else { + process.exit(2) + } + } + } catch (e) { + console.error(e.stack) + process.exit(2) + } +} + +function isNumeric(n) { + return !isNaN(parseFloat(n)) && isFinite(n) +} + + + +run() diff --git a/node_modules/dir-compare/src/cli/print.js b/node_modules/dir-compare/src/cli/print.js new file mode 100644 index 0000000..9861896 --- /dev/null +++ b/node_modules/dir-compare/src/cli/print.js @@ -0,0 +1,192 @@ +var colors = require('colors') +var util = require('util') +var pathUtils = require('path') + +var PATH_SEP = pathUtils.sep + +// Prints dir compare results. +// 'program' represents display options and correspond to dircompare command line parameters. +// Example: 'dircompare --show-all --exclude *.js dir1 dir2' translates into +// program: {showAll: true, exclude: '*.js'} +// +var print = function (res, writer, program) { + var noColor = function (str) { return str } + var colorEqual = program.nocolors ? noColor : colors.green + var colorDistinct = program.nocolors ? noColor : colors.red + var colorLeft = noColor + var colorRight = noColor + var colorDir = noColor + var colorBrokenLinks = noColor + var colorMissing = program.nocolors ? noColor : colors.yellow + + // calculate relative path length for pretty print + var relativePathMaxLength = 0, fileNameMaxLength = 0 + if (!program.csv && res.diffSet) { + res.diffSet.forEach(function (diff) { + if (diff.relativePath.length > relativePathMaxLength) { + relativePathMaxLength = diff.relativePath.length + } + var len = getCompareFile(diff, '??', colorMissing).length + if (len > fileNameMaxLength) { + fileNameMaxLength = len + } + }) + } + + // csv header + if (program.csv) { + writer.write('path,name,state,type,size1,size2,date1,date2,reason\n') + } + if (res.diffSet) { + for (var i = 0; i < res.diffSet.length; i++) { + var detail = res.diffSet[i] + var color, show = true + + if (!program.wholeReport) { + // show only files or broken links + var type = detail.type1 !== 'missing' ? detail.type1 : detail.type2 + if (type !== 'file' && type !== 'broken-link') { + show = false + } + } + if (show) { + switch (detail.state) { + case 'equal': + color = colorEqual + show = program.showAll || program.showEqual ? true : false + break + case 'left': + color = colorLeft + show = program.showAll || program.showLeft ? true : false + break + case 'right': + color = colorRight + show = program.showAll || program.showRight ? true : false + break + case 'distinct': + color = colorDistinct + show = program.showAll || program.showDistinct ? true : false + break + default: + show = true + color = colors.gray + } + if (show) { + if (program.csv) { + printCsv(writer, detail, color) + } else { + printPretty(writer, program, detail, color, colorDir, colorMissing, relativePathMaxLength, fileNameMaxLength) + } + } + } + } + } + + // PRINT STATISTICS + var statTotal, statEqual, statLeft, statRight, statDistinct + if (program.wholeReport) { + statTotal = res.total + statEqual = res.equal + statLeft = res.left + statRight = res.right + statDistinct = res.distinct + } else { + var brokenLInksStats = res.brokenLinks + statTotal = res.totalFiles + brokenLInksStats.totalBrokenLinks + statEqual = res.equalFiles + statLeft = res.leftFiles + brokenLInksStats.leftBrokenLinks + statRight = res.rightFiles + brokenLInksStats.rightBrokenLinks + statDistinct = res.distinctFiles + brokenLInksStats.distinctBrokenLinks + } + if (!program.noDiffIndicator) { + writer.write(res.same ? colorEqual('Entries are identical\n') : colorDistinct('Entries are different\n')) + } + var stats = util.format('total: %s, equal: %s, distinct: %s, only left: %s, only right: %s', + statTotal, + colorEqual(statEqual), + colorDistinct(statDistinct), + colorLeft(statLeft), + colorRight(statRight) + ) + if (res.brokenLinks.totalBrokenLinks > 0) { + stats += util.format(', broken links: %s', colorBrokenLinks(res.brokenLinks.totalBrokenLinks)) + } + stats += '\n' + writer.write(stats) +} + +/** + * Print details for default view mode + */ +var printPretty = function (writer, program, detail, color, dirColor, missingColor, relativePathMaxLength, fileNameMaxLength) { + var path = detail.relativePath === '' ? PATH_SEP : detail.relativePath + + var state + switch (detail.state) { + case 'equal': + state = '==' + break + case 'left': + state = '->' + break + case 'right': + state = '<-' + break + case 'distinct': + state = '<>' + break + default: + state = '?' + } + var type = '' + type = detail.type1 !== 'missing' ? detail.type1 : detail.type2 + if (type === 'directory') { + type = dirColor(type) + } + var cmpEntry = getCompareFile(detail, color(state), missingColor) + var reason = '' + if (program.reason && detail.reason) { + reason = util.format(' <%s>', detail.reason) + } + if (program.wholeReport || type === 'broken-link') { + writer.write(util.format('[%s] %s (%s)%s\n', path, cmpEntry, type, reason)) + } else { + writer.write(util.format('[%s] %s%s\n', path, cmpEntry, reason)) + } +} + +var getCompareFile = function (detail, state, missingcolor) { + p1 = detail.name1 ? detail.name1 : '' + p2 = detail.name2 ? detail.name2 : '' + var missing1 = detail.type1 === 'missing' ? missingcolor('missing') : '' + var missing2 = detail.type2 === 'missing' ? missingcolor('missing') : '' + return util.format('%s%s %s %s%s', missing1, p1, state, missing2, p2) +} + +/** + * Print csv details. + */ +var printCsv = function (writer, detail, color) { + var size1 = '', size2 = '' + if (detail.type1 === 'file') { + size1 = detail.size1 !== undefined ? detail.size1 : '' + } + if (detail.type2 === 'file') { + size2 = detail.size2 !== undefined ? detail.size2 : '' + } + + var date1 = '', date2 = '' + date1 = detail.date1 !== undefined ? detail.date1.toISOString() : '' + date2 = detail.date2 !== undefined ? detail.date2.toISOString() : '' + + var type = '' + type = detail.type1 !== 'missing' ? detail.type1 : detail.type2 + + var path = detail.relativePath ? detail.relativePath : PATH_SEP + var name = (detail.name1 ? detail.name1 : detail.name2) + var reason = detail.reason || '' + + writer.write(util.format('%s,%s,%s,%s,%s,%s,%s,%s,%s\n', path, name, color(detail.state), type, size1, size2, date1, date2, reason)) +} + +module.exports = print |