Skip to content

Commit

Permalink
feat: Add option "diff" to print the difference instead of the output
Browse files Browse the repository at this point in the history
  • Loading branch information
prantlf committed May 3, 2022
1 parent 29579a3 commit cb3826c
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ Usage: `jsonlint [options] [<file, directory, pattern> ...]`
-E, --extensions [ext] file extensions to process for directory walk
(default: ["json","JSON"])
-i, --in-place overwrite the input files
-j, --diff print difference instead of writing the output
-k, --check check that the input is equal to the output
-t, --indent [num|char] number of spaces or specific characters
to use for indentation (default: 2)
Expand Down Expand Up @@ -187,6 +188,8 @@ The configuration is an object with the following properties, described above, w
| sort-keys | sortKeys |
| extensions | |
| in-place | inPlace |
| diff | |
| check | |
| indent | |
| compact | |
| mode | |
Expand Down
43 changes: 41 additions & 2 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const commander = require('commander')
.option('-s, --sort-keys', 'sort object keys (not when prettifying)')
.option('-E, --extensions [ext]', 'file extensions to process for directory walk', collectValues, ['json', 'JSON'])
.option('-i, --in-place', 'overwrite the input files')
.option('-j, --diff', 'print difference instead of writing the output')
.option('-k, --check', 'check that the input is equal to the output')
.option('-t, --indent [num|char]', 'number of spaces or specific characters to use for indentation', 2)
.option('-c, --compact', 'compact error display')
Expand Down Expand Up @@ -87,6 +88,7 @@ if (params.config === false) {
}

const extensions = options.extensions.map(extension => '.' + extension)
let reported

function convertConfig (config) {
const result = {}
Expand All @@ -108,10 +110,16 @@ function mergeOptions (target, ...sources) {
return target
}

function logNormalError (error, file) {
if (process.exitCode > 0) {
function separateBlocks() {
if (reported) {
console.log()
} else {
reported = true
}
}

function logNormalError (error, file) {
separateBlocks()
console.log('File:', file)
console.error(error.message)
}
Expand Down Expand Up @@ -227,6 +235,33 @@ function checkContents (file, source, parsed) {
}
}

function diffContents(file, source, parsed) {
const { createTwoFilesPatch, structuredPatch } = require('diff')
const compact = options.quiet || options.compact
let diff, length
if (compact) {
diff = structuredPatch(`${file}.orig`, file, source, parsed, '', '', { context: 3 })
length = diff.hunks && diff.hunks.length
} else {
diff = createTwoFilesPatch(`${file}.orig`, file, source, parsed, '', '', { context: 3 })
length = diff.split(/\r?\n/).length - 4
}
if (length > 0) {
if (compact) {
const hunk = length === 1 ? 'hunk' : 'hunks'
console.info(`${file}: ${length} ${hunk}`)
} else {
separateBlocks()
console.log('File:', file)
console.log(diff)
}
} else {
if (compact) {
console.info(`${file}: no difference`)
}
}
}

function processFile (file) {
file = normalize(file)
if (options.logFiles) {
Expand All @@ -238,6 +273,8 @@ function processFile (file) {
writeFileSync(file, ensureLineBreak(parsed, source))
} else if (options.check) {
checkContents(file, source, ensureLineBreak(parsed, source))
} else if (options.diff) {
diffContents(file, source, ensureLineBreak(parsed, source))
} else {
if (!(options.quiet || options.logFiles)) {
console.log(parsed)
Expand Down Expand Up @@ -311,6 +348,8 @@ function main () {
const parsed = processContents(source, file)
if (options.check) {
checkContents(file, source, ensureLineBreak(parsed, source))
} else if (options.diff) {
diffContents(file, source, ensureLineBreak(parsed, source))
} else if (!(options.quiet || options.logFiles)) {
console.log(parsed)
}
Expand Down

0 comments on commit cb3826c

Please sign in to comment.