Skip to content

Commit

Permalink
Add which. Update docs.
Browse files Browse the repository at this point in the history
  • Loading branch information
Hexagon committed Mar 30, 2024
1 parent 4d44399 commit df9b682
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 11 deletions.
23 changes: 14 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
# @cross/fs

A cross-runtime utility library offering best practices for file system
operations in JavaScript and TypeScript.
operations in JavaScript and TypeScript. Available for Node.js, Deno, Bun, and
browsers at [jsr.io/@cross/fs](https://jsr.io/@cross/fs).

Available for Node, Deno Bun and Browser at
[jsr.io/@cross/fs](https://jsr.io/@cross/fs).

The library adheres to node:fs/promises conventions for familiarity, diverging
only for strategic performance, compatibility advantages or additional
convenience functions. To ensure cross-runtime reliability, the library
undergoes rigorous automated testing on Node LTS to current (>= 18), Deno and
Bun.
Automated testing across Node.js (>=18), Deno, and Bun ensures cross-runtime
reliability of all included methods. The library includes useful convenience
functions such as which, find, diskusage, and hash. It uses selected parts of
`node:fs/promises` as a foundation for core operations, strategically diverging
to offer performance optimizations or broader compatibility.

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.
Expand All @@ -35,6 +33,10 @@ console.log(await find("../", (path) => path.endsWith("package.json")));
// Get the sha256 hash of a file
console.log(await hash("LICENSE.md", "sha256"));
// f8c9819eb0c322eef28a0d0948877df068276f487b81326af842d3a20e7c9bbc

// Find the installation folder of bun
console.log(await which("bun"));
// /home/hexagon/.bun/bin/bun
```

Methods:
Expand All @@ -51,6 +53,7 @@ Methods:
| find | X | X | X | custom |
| diskusage | X | X | X | custom |
| hash | X | X | X | custom |
| which | X | X | X | custom |

### Io

Expand Down Expand Up @@ -104,3 +107,5 @@ console.log(await dirpath("config"));
| watch | X | X | X | node:fs/promises |
| truncate | X | X | X | node:fs/promises |
| open | X | X | X | node:fs/promises |
| access | X | X | X | node:fs/promises |
| constants | X | X | X | node:fs/promises |
3 changes: 2 additions & 1 deletion deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cross/fs",
"version": "0.0.6",
"version": "0.0.7",
"exports": {
".": "./mod.ts",
"./stat": "./src/stat/mod.ts",
Expand All @@ -9,6 +9,7 @@
},
"imports": {
"@cross/dir": "jsr:@cross/dir@^1.1.0",
"@cross/env": "jsr:@cross/env@^1.0.0",
"@cross/runtime": "jsr:@cross/runtime@^1.0.0",
"@cross/test": "jsr:@cross/test@^0.0.9",
"@cross/utils": "jsr:@cross/utils@^0.8.2",
Expand Down
3 changes: 2 additions & 1 deletion src/stat/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,10 @@ function mapDenoStats(stats: Deno.FileInfo): StatResult {
}

export { statWrap as stat };
export { lstat } from "node:fs/promises";
export { access, constants, lstat } from "node:fs/promises";
export * from "./is.ts";
export * from "./exists.ts";
export * from "./size.ts";
export * from "./find.ts";
export * from "./hash.ts";
export * from "./which.ts";
19 changes: 19 additions & 0 deletions src/stat/which.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { access, constants } from "./mod.ts";
import { getEnv } from "@cross/env";
import { join } from "@std/path";

export async function which(command: string) {
const paths = getEnv("PATH")?.split(":");
if (paths) {
for (const dir of paths) {
const fullPath = join(dir, command);
try {
await access(fullPath, constants.X_OK); // Check if executable
return fullPath;
} catch (_err) {
// Ignore and try the next path
}
}
}
return null; // Not found
}

0 comments on commit df9b682

Please sign in to comment.