Skip to content

Commit d06b9ac

Browse files
committed
feat(internal): isFile
- requires `node>=14.17.0` - https://nodejs.org/api/fs.html#fsstatsyncpath-options Signed-off-by: Lexus Drumgold <unicornware@flexdevelopment.llc>
1 parent d4720d7 commit d06b9ac

File tree

4 files changed

+53
-2
lines changed

4 files changed

+53
-2
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@
187187
"vitepress@npm:1.0.0-alpha.34": "patch:vitepress@npm%3A1.0.0-alpha.34#patches/vitepress+1.0.0-alpha.34.dev.patch"
188188
},
189189
"engines": {
190-
"node": ">=14.16",
190+
"node": ">=14.17.0",
191191
"yarn": "4.0.0-rc.34"
192192
},
193193
"packageManager": "yarn@4.0.0-rc.34",
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* @file Unit Tests - isFile
3+
* @module mlly/internal/tests/unit/isFile
4+
*/
5+
6+
import testSubject from '../is-file'
7+
8+
describe('unit:internal/isFile', () => {
9+
it('should return false if id is not path to module', () => {
10+
// Arrange
11+
const cases: Parameters<typeof testSubject>[] = [
12+
['file.txt'],
13+
['node_modules/@flex-development/mkbuild/dist/index.mjs/package.json']
14+
]
15+
16+
// Act + Expect
17+
cases.forEach(([id]) => expect(testSubject(id)).to.be.false)
18+
})
19+
20+
it('should return false if id is path to directory', () => {
21+
expect(testSubject('src')).to.be.false
22+
})
23+
24+
it('should return true if id is path to file', () => {
25+
expect(testSubject('src/index.ts')).to.be.true
26+
})
27+
})

src/internal/is-file.ts

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @file Internal - isFile
3+
* @module mlly/internal/isFile
4+
*/
5+
6+
import fs from 'node:fs'
7+
import type { URL } from 'node:url'
8+
9+
/**
10+
* Checks if a file exists at `id`.
11+
*
12+
* @param {URL | string} id - Module id to evaluate
13+
* @return {boolean} `true` if file exists at `id`
14+
*/
15+
const isFile = (id: URL | string): boolean => {
16+
try {
17+
return fs.statSync(id, { throwIfNoEntry: false })?.isFile() ?? false
18+
} catch {
19+
return false
20+
}
21+
}
22+
23+
export default isFile

src/utils/read-package-json.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* @module mlly/utils/readPackageJson
44
*/
55

6+
import isFile from '#src/internal/is-file'
67
import validateString from '#src/internal/validate-string'
78
import validateURLString from '#src/internal/validate-url-string'
89
import {
@@ -57,7 +58,7 @@ const readPackageJson = (
5758
const path: string = pathe.toNamespacedPath(pathe.join(dir, 'package.json'))
5859

5960
// return null if package.json file does not exist
60-
if (!fs.existsSync(path)) return null
61+
if (!isFile(path)) return null
6162

6263
/**
6364
* Possible `package.json` object.

0 commit comments

Comments
 (0)