-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FEATURE: Expose a resolveConfigPath method which will return the full…
… path to the resolved config up the tree
- Loading branch information
1 parent
f6ed610
commit fcbc919
Showing
5 changed files
with
200 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// @flow | ||
|
||
const fs = require('fs'); | ||
const {promisify} = require('util'); | ||
const findUp = require('find-up'); | ||
|
||
module.exports = { | ||
readFileAsync: promisify(fs.readFile), | ||
findUp, | ||
|
||
async resolvePackageJsonConfigPath( | ||
propertyName: string, | ||
cwd: string | ||
): Promise<string | void> { | ||
const filePath = await this.findUp('package.json', {cwd}); | ||
|
||
if (!filePath) { | ||
return; | ||
} | ||
|
||
const pkg = await this.readJson(filePath); | ||
|
||
if (typeof pkg[propertyName] === 'object') { | ||
return filePath; | ||
} | ||
|
||
const pathPartials = cwd.split('/'); | ||
pathPartials.pop(); | ||
|
||
return this.resolvePackageJsonConfigPath( | ||
propertyName, | ||
pathPartials.join('/') | ||
); | ||
}, | ||
|
||
async readJson(path: string | any): Promise<Object> { | ||
const contents = await this.readFileAsync(path, 'utf8'); | ||
|
||
return JSON.parse(contents); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// @flow | ||
|
||
const utils = require('./utils.js'); | ||
|
||
describe('utils.findUp()', () => { | ||
it('should be a function', () => { | ||
expect(typeof utils.findUp).toBe('function'); | ||
}); | ||
}); | ||
|
||
describe('utils.readFileAsync()', () => { | ||
it('should be a function', () => { | ||
expect(typeof utils.readFileAsync).toBe('function'); | ||
}); | ||
}); | ||
|
||
describe('utils.resolvePackageJsonConfigPath()', () => { | ||
let findUp; | ||
let readJson; | ||
|
||
beforeEach(() => { | ||
findUp = jest.spyOn(utils, 'findUp').mockImplementation(jest.fn()); | ||
readJson = jest.spyOn(utils, 'readJson').mockImplementation(jest.fn()); | ||
}); | ||
|
||
afterEach(() => { | ||
findUp.mockRestore(); | ||
readJson.mockRestore(); | ||
}); | ||
|
||
it('should be a function', () => { | ||
expect(typeof utils.resolvePackageJsonConfigPath).toBe('function'); | ||
}); | ||
|
||
it('should resolve a path to a package.json file up the tree with the given property name', async () => { | ||
findUp | ||
.mockReturnValueOnce('/foo/bar/baz/package.json') | ||
.mockReturnValueOnce('/foo/bar/package.json') | ||
.mockReturnValueOnce('/foo/package.json'); | ||
readJson | ||
.mockReturnValueOnce({}) | ||
.mockReturnValueOnce({}) | ||
.mockReturnValueOnce({foo: {}}); | ||
|
||
const path = await utils.resolvePackageJsonConfigPath( | ||
'foo', | ||
'/foo/bar/baz' | ||
); | ||
|
||
expect(path).toEqual('/foo/package.json'); | ||
}); | ||
|
||
it('should return undefined if no package.json was found up the tree', async () => { | ||
findUp.mockReturnValue(null); | ||
|
||
const path = await utils.resolvePackageJsonConfigPath( | ||
'foo', | ||
'/foo/bar/baz' | ||
); | ||
|
||
expect(path).toBe(undefined); | ||
}); | ||
}); | ||
|
||
describe('utils.readJson()', () => { | ||
let readFileAsync; | ||
|
||
beforeEach(() => { | ||
readFileAsync = jest | ||
.spyOn(utils, 'readFileAsync') | ||
.mockImplementation(jest.fn()); | ||
}); | ||
|
||
afterEach(() => { | ||
readFileAsync.mockRestore(); | ||
}); | ||
|
||
it('should be a function', () => { | ||
expect(typeof utils.readJson).toBe('function'); | ||
}); | ||
|
||
it('should call the readFileAsync method and parse the return value as JSON', async () => { | ||
readFileAsync.mockReturnValue('{"foo": "bar"}'); | ||
const contents = await utils.readJson('/foo/bar.json'); | ||
|
||
expect(contents).toEqual({foo: 'bar'}); | ||
}); | ||
}); |