Skip to content

Commit 3d1aa8e

Browse files
0.0.20
add `file()` method
1 parent 7d8e560 commit 3d1aa8e

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@kiwilan/filesystem",
33
"type": "module",
4-
"version": "0.0.19",
4+
"version": "0.0.20",
55
"description": "Node module to improve native filesystem with Laravel like helpers.",
66
"author": "Ewilan Rivière <[email protected]>",
77
"license": "MIT",

src/lib/FsFile.ts

+10
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,16 @@ export class FsFile {
297297
return await glob(pattern, options)
298298
}
299299

300+
/**
301+
* Get an `FsFileItem` instance for the given path.
302+
*/
303+
public static async file(path: string): Promise<FsFileItem> {
304+
if (!await this.exists(path))
305+
throw new Error(`File does not exist at path ${path}`)
306+
307+
return await FsFileItem.makeFromPath(path)
308+
}
309+
300310
/**
301311
* Get an array of all files in a directory.
302312
*/

tests/file.test.ts

+18
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,21 @@ it('can get file sync', () => {
2121

2222
expect(content).toContain('Test')
2323
})
24+
25+
it('can get file sync', async () => {
26+
const path = FsPath.root('tests/test.md')
27+
const fsItem = await FsFile.file(path)
28+
29+
expect(fsItem.name).toBe('test')
30+
expect(fsItem.filename).toBe('test.md')
31+
expect(fsItem.path.includes('node-filesystem/tests/test.md')).toBe(true)
32+
expect(fsItem.pathRelative).toBe('tests/test.md')
33+
expect(fsItem.extension).toBe('md')
34+
expect(fsItem.isDirectory).toBe(false)
35+
expect(fsItem.isFile).toBe(true)
36+
expect(fsItem.isSymbolicLink).toBe(false)
37+
expect(fsItem.isHidden).toBe(false)
38+
expect(fsItem.lastModified).toBeGreaterThan(0)
39+
expect(fsItem.size).toBe(29)
40+
expect(fsItem.sizeHuman).toBe('29 B')
41+
})

0 commit comments

Comments
 (0)