-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(prompts): consolidate file path checks (#600)
* refactor(prompts): consolidate file path checks * docs: JSDoc clarification * test: add a few test cases * test: small flaky test fix
- Loading branch information
1 parent
b1e756a
commit 532c2ff
Showing
5 changed files
with
68 additions
and
31 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import fs from 'fs'; | ||
|
||
import { checkFilePath } from '../../src/lib/checkFile'; | ||
|
||
describe('#checkFilePath', () => { | ||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should return error for empty path value', () => { | ||
return expect(checkFilePath('')).toBe('An output path must be supplied.'); | ||
}); | ||
|
||
it('should return error if path already exists', () => { | ||
expect.assertions(2); | ||
const testPath = 'path-that-already-exists'; | ||
|
||
fs.existsSync = jest.fn(() => true); | ||
|
||
expect(checkFilePath(testPath)).toBe('Specified output path already exists.'); | ||
expect(fs.existsSync).toHaveBeenCalledWith(testPath); | ||
}); | ||
|
||
it("should return true if the path doesn't exist", () => { | ||
expect.assertions(2); | ||
const testPath = 'path-that-does-not-exist'; | ||
|
||
fs.existsSync = jest.fn(() => false); | ||
|
||
expect(checkFilePath(testPath)).toBe(true); | ||
expect(fs.existsSync).toHaveBeenCalledWith(testPath); | ||
}); | ||
}); |
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,30 @@ | ||
import fs from 'fs'; | ||
|
||
/** | ||
* Removes any non-alphanumeric characters and replaces them with hyphens. | ||
* | ||
* This is used for file names and for YAML keys. | ||
*/ | ||
export const cleanFileName = (input: string) => input.replace(/[^a-z0-9]/gi, '-'); | ||
|
||
/** | ||
* A validator function used in our prompts for when a user | ||
* is prompted to specify a file path. | ||
* | ||
* @param value the file name | ||
* @param getFullPath An optional function for adding a file path or any filename validations | ||
* @returns true if path is valid (i.e. is non-empty and doesn't already exist), | ||
* otherwise a string containing the error message | ||
*/ | ||
export function checkFilePath(value: string, getFullPath: (file: string) => string = file => file) { | ||
if (value.length) { | ||
const fullPath = getFullPath(value); | ||
if (!fs.existsSync(fullPath)) { | ||
return true; | ||
} | ||
|
||
return 'Specified output path already exists.'; | ||
} | ||
|
||
return 'An output path must be supplied.'; | ||
} |
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