Skip to content

Commit

Permalink
ENH: Read "precise-watcher" property in package.json by default
Browse files Browse the repository at this point in the history
ENH: Add src/lib/read-config.js

TST: Test setting config via package.json in test/integration/src.test.js
  • Loading branch information
AlexisPuga committed Mar 2, 2021
1 parent 2ba2428 commit 2f147da
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,19 @@
module.exports = () => {
const readConfig = require('./lib/read-config')

module.exports = (options) => {
const {
cwd,
config: configFilename = 'package.json',
} = Object(options);
let config = readConfig(configFilename, {cwd})

if (configFilename === 'package.json') {
config = Object(config)['precise-watcher']
}

if (!config) {
return false
}

return true
}
8 changes: 8 additions & 0 deletions src/lib/read-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const path = require('path')

module.exports = (filename, {cwd}) => {
const userDirectory = cwd || process.cwd()
const filepath = path.resolve(userDirectory, filename)

return require(filepath)
}
13 changes: 13 additions & 0 deletions test/integration/src.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
describe('/src', () => {
const preciseWatcher = require('../../src')

it('Should read config from "precise-watcher" property located in ' +
'users\' package.json, by default', async () => {
// Should return early (returning false).
jest.doMock('../../package.json', () => ({
'precise-watcher': true
}), { virtual: true })

expect(preciseWatcher()).toBe(true)
})
})

0 comments on commit 2f147da

Please sign in to comment.