-
-
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.
Signed-off-by: Lexus Drumgold <unicornware@flexdevelopment.llc>
- Loading branch information
1 parent
e67a350
commit 15addba
Showing
3 changed files
with
53 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/** | ||
* @file Unit Tests - isFile | ||
* @module tsconfig-utils/internal/tests/unit/isFile | ||
*/ | ||
|
||
import testSubject from '../is-file' | ||
|
||
describe('unit:internal/isFile', () => { | ||
it('should return false if id does not exist', () => { | ||
// Arrange | ||
const cases: Parameters<typeof testSubject>[] = [ | ||
['file.mjs'], | ||
['node_modules/@flex-development/mkbuild/dist/index.mjs/package.json'] | ||
] | ||
|
||
// Act + Expect | ||
cases.forEach(([id]) => expect(testSubject(id)).to.be.false) | ||
}) | ||
|
||
it('should return false if id is directory id', () => { | ||
expect(testSubject('src')).to.be.false | ||
}) | ||
|
||
it('should return true if id is file id', () => { | ||
expect(testSubject('src/index.ts')).to.be.true | ||
}) | ||
}) |
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,25 @@ | ||
/** | ||
* @file Internal - isFile | ||
* @module tsconfig-utils/internal/isFile | ||
*/ | ||
|
||
import fs from 'node:fs' | ||
import type { URL } from 'node:url' | ||
import validateURLString from './validate-url-string' | ||
|
||
/** | ||
* Checks if a file exists at the given module `id`. | ||
* | ||
* @param {URL | string} id - Module id to evaluate | ||
* @return {boolean} `true` if file exists at `id` | ||
*/ | ||
const isFile = (id: URL | string): boolean => { | ||
try { | ||
validateURLString(id, 'id') | ||
return fs.statSync(id, { throwIfNoEntry: false })?.isFile() ?? false | ||
} catch { | ||
return false | ||
} | ||
} | ||
|
||
export default isFile |