Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,15 @@
"contributions": [
"fundingFinding"
]
},
{
"login": "schaab",
"name": "Jared Schaab",
"avatar_url": "https://avatars0.githubusercontent.com/u/1103255?v=4",
"profile": "https://github.com/schaab",
"contributions": [
"code"
]
}
],
"repoType": "github",
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"babel-plugin-transform-react-remove-prop-types": "^0.4.21",
"browserslist": "^4.6.6",
"concurrently": "^4.1.1",
"cosmiconfig": "^5.2.1",
"cross-env": "^5.1.4",
"cross-spawn": "^6.0.5",
"doctoc": "^1.4.0",
Expand Down
33 changes: 33 additions & 0 deletions src/__tests__/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@ jest.mock('read-pkg-up', () => ({
sync: jest.fn(() => ({package: {}, path: '/blah/package.json'})),
}))
jest.mock('which', () => ({sync: jest.fn(() => {})}))
jest.mock('cosmiconfig', () => {
const actual = jest.requireActual('cosmiconfig')

function cosmiconfig(name, options) {
return actual(name, options)
}

return jest.fn(cosmiconfig)
})

let whichSyncMock, readPkgUpSyncMock

Expand Down Expand Up @@ -137,6 +146,30 @@ test('ifFile returns the true argument if true and the false argument if false',
expect(require('../utils').ifFile('does-not-exist.blah', t, f)).toBe(f)
})

test('hasLocalConfiguration returns false if no local configuration found', () => {
mockCosmiconfig()

expect(require('../utils').hasLocalConfig('module')).toBe(false)
})

test('hasLocalConfig returns true if a local configuration found', () => {
mockCosmiconfig({config: {}, filepath: 'path/to/config'})

expect(require('../utils').hasLocalConfig('module')).toBe(true)
})

test('hasLocalConfiguration returns true if a local config found and it is empty', () => {
mockCosmiconfig({isEmpty: true})

expect(require('../utils').hasLocalConfig('module')).toBe(true)
})

function mockPkg({package: pkg = {}, path = '/blah/package.json'}) {
readPkgUpSyncMock.mockImplementationOnce(() => ({package: pkg, path}))
}

function mockCosmiconfig(result = null) {
const cosmiconfig = require('cosmiconfig')

cosmiconfig.mockImplementationOnce(() => ({searchSync: () => result}))
}
7 changes: 2 additions & 5 deletions src/scripts/format.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const path = require('path')
const spawn = require('cross-spawn')
const yargsParser = require('yargs-parser')
const {hasPkgProp, resolveBin, hasFile} = require('../utils')
const {resolveBin, hasFile, hasLocalConfig} = require('../utils')

const args = process.argv.slice(2)
const parsedArgs = yargsParser(args)
Expand All @@ -10,10 +10,7 @@ const here = p => path.join(__dirname, p)
const hereRelative = p => here(p).replace(process.cwd(), '.')

const useBuiltinConfig =
!args.includes('--config') &&
!hasFile('.prettierrc') &&
!hasFile('prettier.config.js') &&
!hasPkgProp('prettierrc')
!args.includes('--config') && !hasLocalConfig('prettier')
const config = useBuiltinConfig
? ['--config', hereRelative('../config/prettierrc.js')]
: []
Expand Down
9 changes: 9 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const arrify = require('arrify')
const has = require('lodash.has')
const readPkgUp = require('read-pkg-up')
const which = require('which')
const cosmiconfig = require('cosmiconfig')

const {package: pkg, path: pkgPath} = readPkgUp.sync({
cwd: fs.realpathSync(process.cwd()),
Expand Down Expand Up @@ -168,12 +169,20 @@ function writeExtraEntry(name, {cjs, esm}, clean = true) {
)
}

function hasLocalConfig(moduleName, searchOptions = {}) {
Copy link
Owner

Choose a reason for hiding this comment

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

doesn't prettier have a function for finding config? Maybe we should use that instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, it does.

I was thinking that this eventually this could be used for the other commands as well.

const explorer = cosmiconfig(moduleName, searchOptions)
const result = explorer.searchSync(pkgPath)

return result !== null
}

module.exports = {
appDirectory,
envIsSet,
fromRoot,
getConcurrentlyArgs,
hasFile,
hasLocalConfig,
hasPkgProp,
hasScript,
ifAnyDep,
Expand Down