Skip to content

Commit

Permalink
Add chdir and cwd
Browse files Browse the repository at this point in the history
  • Loading branch information
Hexagon committed Apr 8, 2024
1 parent 9c86303 commit d31f06a
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
4 changes: 2 additions & 2 deletions deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cross/fs",
"version": "0.0.8",
"version": "0.0.9",
"exports": {
".": "./mod.ts",
"./stat": "./src/stat/mod.ts",
Expand All @@ -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"
},
Expand Down
25 changes: 25 additions & 0 deletions src/ops/chdir.test.ts
Original file line number Diff line number Diff line change
@@ -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);
}
});
30 changes: 30 additions & 0 deletions src/ops/chdir.ts
Original file line number Diff line number Diff line change
@@ -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.");
}
}
28 changes: 28 additions & 0 deletions src/ops/cwd.ts
Original file line number Diff line number Diff line change
@@ -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.",
);
}
}
2 changes: 2 additions & 0 deletions src/ops/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ export {

export * from "./mktempdir.ts";
export * from "./tempfile.ts";
export * from "./chdir.ts";
export * from "./cwd.ts";

0 comments on commit d31f06a

Please sign in to comment.