-
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.
Add stat. Disable typechecking in CI for now
- Loading branch information
Showing
7 changed files
with
201 additions
and
33 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 |
---|---|---|
@@ -1,19 +1,51 @@ | ||
# @cross/fs | ||
|
||
Available for Node, Deno Bun and Browser at | ||
[jsr.io/@cross/fs](https://jsr.io/@cross/fs). | ||
|
||
**Work in progress** Cross Runtime filesystem operations for JavaScript and | ||
TypeScript. | ||
|
||
Available for Node, Deno Bun and Browser at | ||
[jsr.io/@cross/fs](https://jsr.io/@cross/fs), works seamlessly with both | ||
JavaScript and TypeScript. | ||
|
||
For cross rutime path operations, [jsr.io/@std/path](https://jsr.io/@std/path) | ||
will cover most scenarios, this library focuses on the file system operations. | ||
|
||
## Coverage | ||
|
||
| Method | Deno | Node | Bun | Browser (LocalStorage) | | ||
| --------- | ---- | ---- | --- | ---------------------- | | ||
| access | X | X | X | | | ||
| readfile | X | X | X | | | ||
| writefile | X | X | X | | | ||
| ... | | | | | | ||
| Method | Deno | Node | Bun | Browser (LocalStorage) | | ||
| ------ | ---- | ---- | --- | ---------------------- | | ||
| stat | X | X | X | | | ||
| ... | | | | | | ||
|
||
## Contribution guide | ||
|
||
## Deno | ||
|
||
```bash | ||
# Run an example using Deno | ||
deno run -A examples/stat.ts | ||
``` | ||
|
||
## Bun | ||
|
||
```bash | ||
# Install deps locally | ||
bun jsr add @cross/runtime | ||
``` | ||
|
||
```bash | ||
# Run an example using Bun | ||
bun run examples/stat.ts | ||
``` | ||
|
||
## Node | ||
|
||
````bash | ||
# Install deps locally | ||
npx jsr add @cross/runtime | ||
`` | ||
|
||
```bash | ||
# Run an example using tsx in Node | ||
npx tsx examples/stat.ts | ||
```` |
This file was deleted.
Oops, something went wrong.
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,3 @@ | ||
import { stat } from "../mod.ts"; | ||
|
||
console.debug(await stat("mod.ts", { bigInt: false })); |
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 |
---|---|---|
@@ -1 +1 @@ | ||
export { access } from "./access/mod.ts"; | ||
export { stat } from "./stat/mod.ts"; |
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,148 @@ | ||
import { CurrentRuntime, Runtime } from "@cross/runtime"; | ||
|
||
interface StatOptions { | ||
/* Request bigInts, Only used with node */ | ||
bigInt: false | undefined; | ||
} | ||
|
||
interface StatResult { | ||
/** Common Properties */ | ||
isFile: boolean; | ||
isDirectory: boolean; | ||
isSymlink: boolean; | ||
|
||
/** Size in bytes */ | ||
size: number; | ||
|
||
/** Last modification time, may be unavailable on some systems */ | ||
mtime: Date | null; | ||
/** Last access time, may be unavailable on some systems */ | ||
atime: Date | null; | ||
/** Creation (birth) time, may be unavailable on some systems */ | ||
birthtime: Date | null; | ||
|
||
/** Unix-Specific Properties (Linux/Mac OS) */ | ||
|
||
/** ID of the device containing the file */ | ||
dev: number | null; | ||
/** Inode number */ | ||
ino: number | null; | ||
|
||
/** | ||
* Raw file mode/permissions. Interpretation depends on file type. | ||
* For regular files, it's a combination of flags defined in system headers. | ||
*/ | ||
mode: number | null; | ||
|
||
/** Number of hard links pointing to this file */ | ||
nlink: number | null; | ||
|
||
/** User ID of the file owner */ | ||
uid: number | null; | ||
/** Group ID of the file owner */ | ||
gid: number | null; | ||
|
||
/** Device ID of the file */ | ||
rdev: number | null; | ||
|
||
/** Block size for filesystem I/O */ | ||
blksize: number | null; | ||
|
||
/** Number of 512-byte blocks allocated to the file*/ | ||
blocks: number | null; | ||
|
||
/** True if this represents a block device file */ | ||
isBlockDevice: boolean | null; | ||
/** True if this represents a character device file */ | ||
isCharDevice: boolean | null; | ||
/** True if this represents a FIFO (pipe) file */ | ||
isFifo: boolean | null; | ||
/** True if this represents a socket file */ | ||
isSocket: boolean | null; | ||
} | ||
|
||
async function statWrap( | ||
path: string, | ||
options?: StatOptions, | ||
): Promise<StatResult> { | ||
switch (CurrentRuntime) { | ||
case Runtime.Node: | ||
/* Falls through */ | ||
// deno-lint-ignore no-case-declarations | ||
case Runtime.Bun: | ||
const { stat } = await import("node:fs/promises"); | ||
return mapNodeStats( | ||
await stat(path, options), | ||
options?.bigInt === undefined ? true : false, | ||
); | ||
|
||
case Runtime.Deno: | ||
return mapDenoStats(await Deno.stat(path)); | ||
|
||
case Runtime.Browser: // Add browser case for clarity | ||
throw new Error("File system access not supported in the browser"); | ||
|
||
default: | ||
throw new Error("Unsupported Runtime"); | ||
} | ||
} | ||
|
||
// deno-lint-ignore no-explicit-any | ||
function mapNodeStats(stats: any, bigInt: boolean): StatResult { | ||
return { | ||
isFile: stats.isFile(), | ||
isDirectory: stats.isDirectory(), | ||
isSymlink: stats.isSymbolicLink(), | ||
size: bigInt ? stats.size : Number(stats.size), // Handle bigInt | ||
mtime: stats.mtime, | ||
atime: stats.atime, | ||
birthtime: stats.birthtime, | ||
|
||
// Unix-specific | ||
dev: stats.dev, | ||
ino: stats.ino, | ||
mode: stats.mode, | ||
nlink: stats.nlink, | ||
uid: stats.uid, // The numeric user identifier of the user that owns the file (POSIX) | ||
gid: stats.gid, // The numeric group identifier of the group that owns the file (POSIX) | ||
rdev: stats.rdev, // A numeric device identifier if the file represents a device. | ||
blksize: stats.blksize, | ||
blocks: stats.blocks, | ||
isBlockDevice: stats.isBlockDevice(), | ||
isCharDevice: stats.isCharacterDevice(), | ||
isFifo: stats.isFIFO(), | ||
isSocket: stats.isSocket(), | ||
}; | ||
} | ||
|
||
function mapDenoStats(stats: Deno.FileInfo): StatResult { | ||
return { | ||
// Common | ||
isFile: stats.isFile, | ||
isDirectory: stats.isDirectory, | ||
isSymlink: stats.isSymlink, | ||
size: stats.size, | ||
mtime: stats.mtime, // Keep as Date | null | ||
atime: stats.atime, // Keep as Date | null | ||
birthtime: stats.birthtime, // Keep as Date | null | ||
|
||
// Unix-specific | ||
dev: null, // Deno doesn't provide this | ||
ino: stats.ino, | ||
mode: stats.mode, | ||
nlink: stats.nlink, | ||
uid: stats.uid, | ||
gid: stats.gid, | ||
blksize: stats.blksize, | ||
blocks: stats.blocks, | ||
|
||
// Not supported | ||
rdev: null, | ||
isBlockDevice: null, | ||
isCharDevice: null, | ||
isFifo: null, | ||
isSocket: null, | ||
}; | ||
} | ||
|
||
export { statWrap as stat }; |