mirror of
https://github.com/all-contributors/cli.git
synced 2025-01-09 05:36:21 +00:00
feat: use Prettier to write config file, if possible (#356)
This commit is contained in:
parent
45787a7636
commit
e0a5312dc5
5 changed files with 65 additions and 2 deletions
|
@ -61,6 +61,9 @@
|
||||||
"nock": "^12.0.0",
|
"nock": "^12.0.0",
|
||||||
"semantic-release": "^17.0.8"
|
"semantic-release": "^17.0.8"
|
||||||
},
|
},
|
||||||
|
"optionalDependencies": {
|
||||||
|
"prettier": "^2"
|
||||||
|
},
|
||||||
"eslintIgnore": [
|
"eslintIgnore": [
|
||||||
"node_modules",
|
"node_modules",
|
||||||
"coverage",
|
"coverage",
|
||||||
|
|
3
src/util/__tests__/__stubs__/.prettierrc.json
Normal file
3
src/util/__tests__/__stubs__/.prettierrc.json
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
{
|
||||||
|
"useTabs": true
|
||||||
|
}
|
35
src/util/__tests__/formatting.js
Normal file
35
src/util/__tests__/formatting.js
Normal file
|
@ -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,
|
||||||
|
)
|
||||||
|
})
|
|
@ -2,6 +2,7 @@ const fs = require('fs')
|
||||||
const pify = require('pify')
|
const pify = require('pify')
|
||||||
const _ = require('lodash/fp')
|
const _ = require('lodash/fp')
|
||||||
const jf = require('json-fixer')
|
const jf = require('json-fixer')
|
||||||
|
const {formatConfig} = require('./formatting')
|
||||||
|
|
||||||
function readConfig(configPath) {
|
function readConfig(configPath) {
|
||||||
try {
|
try {
|
||||||
|
@ -14,7 +15,7 @@ function readConfig(configPath) {
|
||||||
}
|
}
|
||||||
if (changed) {
|
if (changed) {
|
||||||
//Updates the file with fixes
|
//Updates the file with fixes
|
||||||
fs.writeFileSync(configPath, JSON.stringify(config, null, 2))
|
fs.writeFileSync(configPath, formatConfig(config))
|
||||||
}
|
}
|
||||||
return config
|
return config
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
@ -43,7 +44,7 @@ function writeConfig(configPath, content) {
|
||||||
`Error! Project files was overridden and is empty in ${configPath}`,
|
`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`)
|
||||||
}
|
}
|
||||||
|
|
||||||
function writeContributors(configPath, contributors) {
|
function writeContributors(configPath, contributors) {
|
||||||
|
|
21
src/util/formatting.js
Normal file
21
src/util/formatting.js
Normal file
|
@ -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,
|
||||||
|
}
|
Loading…
Reference in a new issue