Skip to content

Commit

Permalink
Add mkdir, mktempdir, rmdir
Browse files Browse the repository at this point in the history
  • Loading branch information
Hexagon committed Mar 27, 2024
1 parent 8f8066f commit 9995d9b
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 2 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,7 @@ will cover most scenarios, this library focuses on the file system operations.
| io | writeFile | X | X | X | |
| ------ | ---------- | ---- | ---- | --- | ---------------------- |
| ops | unlink | X | X | X | |
| ops | mkdir | X | X | X | |
| ops | mktempdir | X | X | X | |
| ops | rmdir | X | X | X | |
| ------ | ---------- | ---- | ---- | --- | ---------------------- |
7 changes: 5 additions & 2 deletions deno.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
{
"name": "@cross/fs",
"version": "0.0.3",
"version": "0.0.4",
"exports": {
".": "./mod.ts",
"./stat": "./src/stat/mod.ts",
"./io": "./src/io/mod.ts",
"./ops": "./src/ops/mod.ts"
},
"imports": {
"@cross/dir": "jsr:@cross/dir@^1.1.0",
"@cross/runtime": "jsr:@cross/runtime@^1.0.0",
"@cross/test": "jsr:@cross/test@^0.0.9",
"@std/assert": "jsr:@std/assert@^0.220.1"
"@std/assert": "jsr:@std/assert@^0.220.1",
"@std/path": "jsr:@std/path@^0.220.1",
"@std/uuid": "jsr:@std/uuid@^0.220.1"
},
"publish": {
"exclude": [".github", "*.test.ts"]
Expand Down
1 change: 1 addition & 0 deletions src/ops/mkdir.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { mkdir } from "node:fs/promises";
23 changes: 23 additions & 0 deletions src/ops/mktempdir.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { assertEquals } from "@std/assert";
import { test } from "@cross/test";
import { mktempdir, rmdir } from "./mod.ts";
import { isDir } from "../stat/mod.ts";

test("mktempdir creates a temporary directory", async () => {
const tempDir = await mktempdir();
try {
assertEquals(await isDir(tempDir), true);
} finally {
await rmdir(tempDir);
}
});

test("mktempdir creates a temporary directory with prefix", async () => {
const tempDir = await mktempdir("test");
assertEquals(tempDir.includes("test-"), true);
try {
assertEquals(await isDir(tempDir), true);
} finally {
await rmdir(tempDir);
}
});
22 changes: 22 additions & 0 deletions src/ops/mktempdir.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { dir } from "@cross/dir";
import { generate } from "@std/uuid/v1";
import { join } from "@std/path";
import { mkdir } from "./mkdir.ts";

/**
* Creates a uniquely named directory under the system temp directory.
*
* @param prefix - An optional prefix for the generated directory name.
* @returns The full path to the newly created temporary directory.
* @throws On error
*/
export async function mktempdir(prefix?: string): Promise<string> {
const tempBaseDir = await dir("tmp");
let uuid = generate();
if (prefix) {
uuid = `${prefix}-${uuid}`;
}
const tempDirPath = join(tempBaseDir, uuid.toString());
await mkdir(tempDirPath, { recursive: true });
return tempDirPath;
}
3 changes: 3 additions & 0 deletions src/ops/mod.ts
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
export * from "./unlink.ts";
export * from "./mkdir.ts";
export * from "./rmdir.ts";
export * from "./mktempdir.ts";
1 change: 1 addition & 0 deletions src/ops/rmdir.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { rmdir } from "node:fs/promises";

0 comments on commit 9995d9b

Please sign in to comment.