-
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.
- Loading branch information
Showing
6 changed files
with
89 additions
and
2 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 |
---|---|---|
@@ -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); | ||
} | ||
}); |
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,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."); | ||
} | ||
} |
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,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.", | ||
); | ||
} | ||
} |
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