Skip to content

Commit

Permalink
Merge pull request #5 from remcohaszing/skip-non-packagejson-files
Browse files Browse the repository at this point in the history
fix: skip sorting non package.json files
  • Loading branch information
matzkoh authored Jul 11, 2019
2 parents af01d0b + 6adb4be commit 0a53c74
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
5 changes: 4 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ exports.parsers = {
if (parser.preprocess) {
text = parser.preprocess(text, options)
}
return sortPackageJson(text)
if (options.filepath && /(^|\\|\/)package\.json$/.test(options.filepath)) {
return sortPackageJson(text)
}
return text
},
},
}
43 changes: 38 additions & 5 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,43 @@ const expected = `{
}
`

const output = prettier.format(input, {
filepath: 'package.json',
plugins: ['.'],
;['package.json', 'foo/bar/package.json', 'foo\\bar\\package.json'].forEach(filepath => {
const output = prettier.format(input, {
filepath,
plugins: ['.'],
})
console.log('Testing', filepath)
console.assert(output === expected, 'Output does not match expected output')
if (output !== expected) {
process.exitCode = 1
}
console.log()
})
;[
'Package.json',
'package.JSON',
'package-lock.json',
'composer.json',
'package.json/composer.json',
'foo.json',
'bar.js',
undefined,
].forEach(filepath => {
const output = prettier.format(input, {
filepath,
parser: 'json-stringify',
plugins: ['.'],
})
console.log('Testing', filepath)
console.assert(output === input, 'Output does not match input')
if (output !== input) {
process.exitCode = 1
}
console.log()
})

console.assert(output === expected)
console.log('\033[32mpassed!\033[39m')
if (process.exitCode) {
console.log('\033[31mFailed!\033[39m')
} else {
console.log('\033[32mpassed!\033[39m')
}

0 comments on commit 0a53c74

Please sign in to comment.