Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use Prettier to write config file, if possible #356

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@
"nock": "^12.0.0",
"semantic-release": "^17.0.8"
},
"optionalDependencies": {
"prettier": "^2"
},
"eslintIgnore": [
"node_modules",
"coverage",
Expand Down
3 changes: 3 additions & 0 deletions src/util/__tests__/__stubs__/.prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"useTabs": true
}
35 changes: 35 additions & 0 deletions src/util/__tests__/formatting.js
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,
)
})
5 changes: 3 additions & 2 deletions src/util/config-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const fs = require('fs')
const pify = require('pify')
const _ = require('lodash/fp')
const jf = require('json-fixer')
const {formatConfig} = require('./formatting')

function readConfig(configPath) {
try {
Expand All @@ -14,7 +15,7 @@ function readConfig(configPath) {
}
if (changed) {
//Updates the file with fixes
fs.writeFileSync(configPath, JSON.stringify(config, null, 2))
fs.writeFileSync(configPath, formatConfig(config))
}
return config
} catch (error) {
Expand Down Expand Up @@ -43,7 +44,7 @@ function writeConfig(configPath, content) {
`Error! Project files was overridden and is empty in ${configPath}`,
)
}
return pify(fs.writeFile)(configPath, `${JSON.stringify(content, null, 2)}\n`)
return pify(fs.writeFile)(configPath, `${formatConfig(content)}\n`)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that we pass the content as config, no?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we are replacing this code by JSON.stringify(undefined, null, 2)

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🌷🌷

}

function writeContributors(configPath, contributors) {
Expand Down
21 changes: 21 additions & 0 deletions src/util/formatting.js
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,
}