-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use Prettier to write config file, if possible (#356)
- Loading branch information
1 parent
45787a7
commit e0a5312
Showing
5 changed files
with
65 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"useTabs": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import path from 'path' | ||
import formatting from '../formatting' | ||
|
||
const content = {contributors: [{id: 'abc123'}]} | ||
|
||
const absentFile = '/!@#*&^DefinitelyDoesNotExistAllContributorsCLI$!@#' | ||
const absentConfigFileExpected = `{ | ||
"contributors": [ | ||
{ | ||
"id": "abc123" | ||
} | ||
] | ||
}` | ||
|
||
const presentFile = path.join(__dirname, '__stubs__', 'file.json') | ||
const presentConfigFileExpected = `{ | ||
"contributors": [ | ||
{ | ||
"id": "abc123" | ||
} | ||
] | ||
} | ||
` | ||
|
||
test('falls back to JSON.stringify when the configPath cannot resolve to a config', () => { | ||
expect(formatting.formatConfig(absentFile, content)).toBe( | ||
absentConfigFileExpected, | ||
) | ||
}) | ||
|
||
test('uses Prettier when the configPath can resolve to a config', () => { | ||
expect(formatting.formatConfig(presentFile, content)).toBe( | ||
presentConfigFileExpected, | ||
) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
function formatConfig(configPath, content) { | ||
const stringified = JSON.stringify(content, null, 2) | ||
try { | ||
const prettier = require('prettier') | ||
const prettierConfig = prettier.resolveConfig.sync(configPath, { | ||
useCache: false, | ||
}) | ||
|
||
return prettierConfig | ||
? prettier.format(stringified, {...prettierConfig, parser: 'json'}) | ||
: stringified | ||
} catch (error) { | ||
// If Prettier can't be required or throws in general, | ||
// assume it's not usable and we should fall back to JSON.stringify | ||
return stringified | ||
} | ||
} | ||
|
||
module.exports = { | ||
formatConfig, | ||
} |