diff --git a/README.md b/README.md index 31c41c0..3b4dfaa 100644 --- a/README.md +++ b/README.md @@ -92,6 +92,8 @@ console.log(await dirpath("config")); | unlink | X | X | X | node:fs/promises | | dirpath | X | X | X | @cross/dir | | mkdir | X | X | X | node:fs/promises | +| cwd | X | X | X | custom | +| chdir | X | X | X | custom | | mktempdir | X | X | X | custom | | rm | X | X | X | node:fs/promises | | rmdir | X | X | X | node:fs/promises | diff --git a/deno.json b/deno.json index 75c8948..6fb272a 100644 --- a/deno.json +++ b/deno.json @@ -1,6 +1,6 @@ { "name": "@cross/fs", - "version": "0.0.8", + "version": "0.0.9", "exports": { ".": "./mod.ts", "./stat": "./src/stat/mod.ts", @@ -12,7 +12,7 @@ "@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.9.0", + "@cross/utils": "jsr:@cross/utils@^0.10.0", "@std/assert": "jsr:@std/assert@^0.221.0", "@std/path": "jsr:@std/path@^0.221.0" }, diff --git a/src/ops/chdir.test.ts b/src/ops/chdir.test.ts new file mode 100644 index 0000000..7c5ef6b --- /dev/null +++ b/src/ops/chdir.test.ts @@ -0,0 +1,25 @@ +import { assertEquals } from "@std/assert"; +import { test } from "@cross/test"; +import { chdir, cwd } from "./mod.ts"; +import { mktempdir, rmdir } from "./mod.ts"; + +test("chdir successfully changes working directory", async () => { + // Set up a temporary working directory + const tempDir = await mktempdir(); + + try { + const originalCwd = cwd(); + + // Change to the temporary directory + chdir(tempDir); + + // Assert that the current working directory has changed + assertEquals(cwd(), tempDir); + + // Change back to original working directory for cleanup + chdir(originalCwd); + } finally { + // Clean up temporary directory + await rmdir(tempDir); + } +}); diff --git a/src/ops/chdir.ts b/src/ops/chdir.ts new file mode 100644 index 0000000..6878290 --- /dev/null +++ b/src/ops/chdir.ts @@ -0,0 +1,30 @@ +import { CurrentRuntime, Runtime } from "@cross/runtime"; + +/** + * Changes the current working directory in a cross-runtime compatible manner. + * + * @param {string} path - The new working directory path. + * @throws If the directory change fails or unsupported runtime is encountered. + * @example + * import { chdir } from "@cross/fs"; // Assuming you place it in a cross/fs module + * + * try { + * chdir("/home/user/projects"); + * console.log("Current working directory changed successfully."); + * } catch (error) { + * console.error("Failed to change directory:", error); + * } + */ +export function chdir(path: string): void { + if (CurrentRuntime === Runtime.Deno) { + //@ts-ignore cross-runtime + Deno.chdir(path); + } else if ( + CurrentRuntime === Runtime.Node || CurrentRuntime === Runtime.Bun + ) { + //@ts-ignore cross-runtime + process.chdir(path); + } else { + throw new Error("Cannot change directory in the current runtime."); + } +} diff --git a/src/ops/cwd.ts b/src/ops/cwd.ts new file mode 100644 index 0000000..02199d3 --- /dev/null +++ b/src/ops/cwd.ts @@ -0,0 +1,28 @@ +import { CurrentRuntime, Runtime } from "@cross/runtime"; + +/** + * Returns the current working directory in a cross-runtime compatible manner. + * + * @returns {string} The current working directory path. + * @throws + * @example + * // import { cwd } from "@cross/utils"; + * + * const cwd = cwd(); + * console.log("The current working directory is:", cwd); + */ +export function cwd(): string { + if (CurrentRuntime === Runtime.Deno) { + //@ts-ignore cross-runtime + return Deno.cwd(); + } else if ( + CurrentRuntime === Runtime.Node || CurrentRuntime === Runtime.Bun + ) { + //@ts-ignore cross-runtime + return process.cwd(); + } else { + throw new Error( + "Cannot determine working directory using current runtime.", + ); + } +} diff --git a/src/ops/mod.ts b/src/ops/mod.ts index f1b6368..3f71eaf 100644 --- a/src/ops/mod.ts +++ b/src/ops/mod.ts @@ -15,3 +15,5 @@ export { export * from "./mktempdir.ts"; export * from "./tempfile.ts"; +export * from "./chdir.ts"; +export * from "./cwd.ts";