-
Notifications
You must be signed in to change notification settings - Fork 632
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(fs/unstable): add
fs.lstat
(#6276)
- Loading branch information
Showing
4 changed files
with
66 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
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,34 @@ | ||
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. | ||
|
||
import { getNodeFsPromises, isDeno } from "./_utils.ts"; | ||
import { mapError } from "./_map_error.ts"; | ||
import { toFileInfo } from "./_to_file_info.ts"; | ||
import type { FileInfo } from "./unstable_types.ts"; | ||
|
||
/** Resolves to a {@linkcode FileInfo} for the specified `path`. If `path` is a symlink, information for the symlink will be returned instead of what it points to. | ||
* | ||
* ```ts | ||
* import { assert } from "@std/assert"; | ||
* import { lstat } from "@std/fs/unstable-lstat"; | ||
* const fileInfo = await lstat("README.md"); | ||
* assert(fileInfo.isFile); | ||
* ``` | ||
* | ||
* Requires `allow-read` permission. | ||
* | ||
* @tags allow-read | ||
* @category File System | ||
*/ | ||
export async function lstat(path: string | URL): Promise<FileInfo> { | ||
if (isDeno) { | ||
return Deno.lstat(path); | ||
} else { | ||
const fsPromises = getNodeFsPromises(); | ||
try { | ||
const stat = await fsPromises.lstat(path); | ||
return toFileInfo(stat); | ||
} catch (error) { | ||
throw mapError(error); | ||
} | ||
} | ||
} |
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 @@ | ||
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. | ||
|
||
import { assert, assertRejects } from "@std/assert"; | ||
import { lstat } from "./unstable_lstat.ts"; | ||
import { NotFound } from "./unstable_errors.js"; | ||
|
||
Deno.test("lstat() returns FileInfo for a file", async () => { | ||
const fileInfo = await lstat("README.md"); | ||
|
||
assert(fileInfo.isFile); | ||
}); | ||
|
||
Deno.test("lstat() does not follow symlinks", async () => { | ||
const linkFile = `${import.meta.dirname}/testdata/0-link`; | ||
const fileInfo = await lstat(linkFile); | ||
|
||
assert(fileInfo.isSymlink); | ||
}); | ||
|
||
Deno.test("lstat() returns FileInfo for a directory", async () => { | ||
const fileInfo = await lstat("fs"); | ||
|
||
assert(fileInfo.isDirectory); | ||
}); | ||
|
||
Deno.test("lstat() rejects with NotFound for a non-existent file", async () => { | ||
await assertRejects(async () => { | ||
await lstat("non_existent_file"); | ||
}, NotFound); | ||
}); |