Skip to content

Commit

Permalink
feat: Path - add linkTo and linkToSync
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret committed May 4, 2024
1 parent c9eb52f commit 50badf7
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/path.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,30 @@ Deno.test("symlinkToSync", async () => {
});
});

Deno.test("linkTo", async () => {
await withTempDir(async () => {
const destFile = createPath("temp.txt").writeTextSync("data");

// async
{
const hardlinkFile = destFile.parentOrThrow().join("other.txt");
await hardlinkFile.linkTo(destFile);
const stat = hardlinkFile.statSync();
assertEquals(stat!.isFile, true);
assertEquals(stat!.isSymlink, false);
assert(!hardlinkFile.isSymlinkSync());
assertEquals(hardlinkFile.readTextSync(), "data");
}

// sync
{
const hardlinkFile = destFile.parentOrThrow().join("sync.txt");
hardlinkFile.linkToSync(destFile);
assertEquals(hardlinkFile.readTextSync(), "data");
}
});
});

Deno.test("readDir", async () => {
await withTempDir(async () => {
const dir = createPath(".").resolve();
Expand Down
20 changes: 20 additions & 0 deletions src/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,26 @@ export class Path {
}
}

/**
* Creates a hardlink to the provided target path.
*/
async linkTo(
targetPath: string | URL | Path,
): Promise<void> {
const targetPathRef = ensurePath(targetPath).resolve();
await Deno.link(targetPathRef.toString(), this.resolve().toString());
}

/**
* Synchronously creates a hardlink to the provided target path.
*/
linkToSync(
targetPath: string | URL | Path,
): void {
const targetPathRef = ensurePath(targetPath).resolve();
Deno.linkSync(targetPathRef.toString(), this.resolve().toString());
}

/** Reads the entries in the directory. */
async *readDir(): AsyncIterable<WalkEntry> {
const dir = this.resolve();
Expand Down

0 comments on commit 50badf7

Please sign in to comment.