Skip to content

Commit

Permalink
feat(internal): isFile
Browse files Browse the repository at this point in the history
Signed-off-by: Lexus Drumgold <unicornware@flexdevelopment.llc>
  • Loading branch information
unicornware committed Feb 3, 2023
1 parent e67a350 commit 15addba
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/internal/__tests__/is-file.spec.ts
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
})
})
1 change: 1 addition & 0 deletions src/internal/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/

export { default as isDirectory } from './is-directory'
export { default as isFile } from './is-file'
export { default as validateFunction } from './validate-function'
export { default as validateString } from './validate-string'
export { default as validateURLString } from './validate-url-string'
25 changes: 25 additions & 0 deletions src/internal/is-file.ts
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

0 comments on commit 15addba

Please sign in to comment.