Skip to content

Commit e0a5312

Browse files
authoredMay 28, 2023
feat: use Prettier to write config file, if possible (#356)
1 parent 45787a7 commit e0a5312

File tree

5 files changed

+65
-2
lines changed

5 files changed

+65
-2
lines changed
 

‎package.json

+3
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@
6161
"nock": "^12.0.0",
6262
"semantic-release": "^17.0.8"
6363
},
64+
"optionalDependencies": {
65+
"prettier": "^2"
66+
},
6467
"eslintIgnore": [
6568
"node_modules",
6669
"coverage",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"useTabs": true
3+
}

‎src/util/__tests__/formatting.js

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import path from 'path'
2+
import formatting from '../formatting'
3+
4+
const content = {contributors: [{id: 'abc123'}]}
5+
6+
const absentFile = '/!@#*&^DefinitelyDoesNotExistAllContributorsCLI$!@#'
7+
const absentConfigFileExpected = `{
8+
"contributors": [
9+
{
10+
"id": "abc123"
11+
}
12+
]
13+
}`
14+
15+
const presentFile = path.join(__dirname, '__stubs__', 'file.json')
16+
const presentConfigFileExpected = `{
17+
"contributors": [
18+
{
19+
"id": "abc123"
20+
}
21+
]
22+
}
23+
`
24+
25+
test('falls back to JSON.stringify when the configPath cannot resolve to a config', () => {
26+
expect(formatting.formatConfig(absentFile, content)).toBe(
27+
absentConfigFileExpected,
28+
)
29+
})
30+
31+
test('uses Prettier when the configPath can resolve to a config', () => {
32+
expect(formatting.formatConfig(presentFile, content)).toBe(
33+
presentConfigFileExpected,
34+
)
35+
})

‎src/util/config-file.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const fs = require('fs')
22
const pify = require('pify')
33
const _ = require('lodash/fp')
44
const jf = require('json-fixer')
5+
const {formatConfig} = require('./formatting')
56

67
function readConfig(configPath) {
78
try {
@@ -14,7 +15,7 @@ function readConfig(configPath) {
1415
}
1516
if (changed) {
1617
//Updates the file with fixes
17-
fs.writeFileSync(configPath, JSON.stringify(config, null, 2))
18+
fs.writeFileSync(configPath, formatConfig(config))
1819
}
1920
return config
2021
} catch (error) {
@@ -43,7 +44,7 @@ function writeConfig(configPath, content) {
4344
`Error! Project files was overridden and is empty in ${configPath}`,
4445
)
4546
}
46-
return pify(fs.writeFile)(configPath, `${JSON.stringify(content, null, 2)}\n`)
47+
return pify(fs.writeFile)(configPath, `${formatConfig(content)}\n`)
4748
}
4849

4950
function writeContributors(configPath, contributors) {

‎src/util/formatting.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
function formatConfig(configPath, content) {
2+
const stringified = JSON.stringify(content, null, 2)
3+
try {
4+
const prettier = require('prettier')
5+
const prettierConfig = prettier.resolveConfig.sync(configPath, {
6+
useCache: false,
7+
})
8+
9+
return prettierConfig
10+
? prettier.format(stringified, {...prettierConfig, parser: 'json'})
11+
: stringified
12+
} catch (error) {
13+
// If Prettier can't be required or throws in general,
14+
// assume it's not usable and we should fall back to JSON.stringify
15+
return stringified
16+
}
17+
}
18+
19+
module.exports = {
20+
formatConfig,
21+
}

0 commit comments

Comments
 (0)
Please sign in to comment.