-
Notifications
You must be signed in to change notification settings - Fork 343
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Discover config files in home dir, working dir (#1161)
- Loading branch information
Showing
8 changed files
with
557 additions
and
42 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* @flow */ | ||
import {fs} from 'mz'; | ||
|
||
import {isErrorWithCode} from '../errors'; | ||
|
||
type FileExistsOptions = {| | ||
fileIsReadable: (filePath: string) => Promise<boolean>, | ||
|}; | ||
|
||
/* | ||
* Resolves true if the path is a readable file. | ||
* | ||
* Usage: | ||
* | ||
* const exists = await fileExists(filePath); | ||
* if (exists) { | ||
* // ... | ||
* } | ||
* | ||
* */ | ||
export default async function fileExists( | ||
path: string, | ||
{ | ||
fileIsReadable = (f) => fs.access(f, fs.constants.R_OK), | ||
}: FileExistsOptions = {} | ||
): Promise<boolean> { | ||
try { | ||
await fileIsReadable(path); | ||
const stat = await fs.stat(path); | ||
return stat.isFile(); | ||
} catch (error) { | ||
if (isErrorWithCode(['EACCES', 'ENOENT'], error)) { | ||
return false; | ||
} | ||
throw error; | ||
} | ||
} |
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,60 @@ | ||
/* @flow */ | ||
import path from 'path'; | ||
|
||
import {assert} from 'chai'; | ||
import {describe, it} from 'mocha'; | ||
import {fs} from 'mz'; | ||
|
||
import fileExists from '../../../src/util/file-exists'; | ||
import {withTempDir} from '../../../src/util/temp-dir'; | ||
import {makeSureItFails, ErrorWithCode} from '../helpers'; | ||
|
||
|
||
describe('util/file-exists', () => { | ||
it('returns true for existing files', () => { | ||
return withTempDir( | ||
async (tmpDir) => { | ||
const someFile = path.join(tmpDir.path(), 'file.txt'); | ||
await fs.writeFile(someFile, ''); | ||
|
||
assert.equal(await fileExists(someFile), true); | ||
}); | ||
}); | ||
|
||
it('returns false for non-existent files', () => { | ||
return withTempDir( | ||
async (tmpDir) => { | ||
// This file does not exist. | ||
const someFile = path.join(tmpDir.path(), 'file.txt'); | ||
|
||
assert.equal(await fileExists(someFile), false); | ||
}); | ||
}); | ||
|
||
it('returns false for directories', () => { | ||
return withTempDir( | ||
async (tmpDir) => { | ||
assert.equal(await fileExists(tmpDir.path()), false); | ||
}); | ||
}); | ||
|
||
it('returns false for unreadable files', async () => { | ||
const exists = await fileExists('pretend/unreadable/file', { | ||
fileIsReadable: async () => { | ||
throw new ErrorWithCode('EACCES', 'permission denied'); | ||
}, | ||
}); | ||
assert.equal(exists, false); | ||
}); | ||
|
||
it('throws unexpected errors', async () => { | ||
const exists = fileExists('pretend/file', { | ||
fileIsReadable: async () => { | ||
throw new ErrorWithCode('EBUSY', 'device is busy'); | ||
}, | ||
}); | ||
await exists.then(makeSureItFails(), (error) => { | ||
assert.equal(error.message, 'EBUSY: device is busy'); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.