diff --git a/README.md b/README.md index 4825330..7eeff82 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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: @@ -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 @@ -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 | diff --git a/deno.json b/deno.json index 5f404d0..736c0cf 100644 --- a/deno.json +++ b/deno.json @@ -1,6 +1,6 @@ { "name": "@cross/fs", - "version": "0.0.6", + "version": "0.0.7", "exports": { ".": "./mod.ts", "./stat": "./src/stat/mod.ts", @@ -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", diff --git a/src/stat/mod.ts b/src/stat/mod.ts index fae8f47..db72be4 100644 --- a/src/stat/mod.ts +++ b/src/stat/mod.ts @@ -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"; diff --git a/src/stat/which.ts b/src/stat/which.ts new file mode 100644 index 0000000..51cfe7a --- /dev/null +++ b/src/stat/which.ts @@ -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 +}