Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: PathRef - renameToDir #164

Merged
merged 1 commit into from
Aug 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/path.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,35 @@ Deno.test("rename", async () => {
});
});

Deno.test("renameToDir", async () => {
await withTempDir(async () => {
const path = createPathRef("file.txt")
.writeTextSync("text");
const dir = createPathRef("dir").mkdirSync();
const newPath = await path.renameToDir(dir);
assert(!path.existsSync());
assert(newPath.existsSync());
assertEquals(dir.join("file.txt").toString(), newPath.toString());
assertEquals(newPath.readTextSync(), "text");
const dir2 = createPathRef("dir2").mkdirSync();
const newPath2 = newPath.renameToDirSync(dir2);
assert(!newPath.existsSync());
assert(newPath2.existsSync());
assertEquals(newPath2.readTextSync(), "text");
assertEquals(newPath2.toString(), dir2.join("file.txt").toString());

// now try a directory
await dir2.renameToDir(dir);
assert(dir.join("dir2").join("file.txt").existsSync());

// try a directory sync
const subDir = dir.join("subdir");
subDir.mkdirSync();
dir.join("dir2").renameToDirSync(subDir);
assert(subDir.join("dir2").join("file.txt").existsSync());
});
});

Deno.test("pipeTo", async () => {
await withTempDir(async () => {
const largeText = "asdf".repeat(100_000);
Expand Down
28 changes: 24 additions & 4 deletions src/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,12 +268,12 @@ export class PathRef {
return stdPath.relative(this.resolve().#path, toPathRef.resolve().#path);
}

/** Gets if the path exists. Beaware of TOCTOU issues. */
/** Gets if the path exists. Beware of TOCTOU issues. */
exists(): Promise<boolean> {
return this.lstat().then((info) => info != null);
}

/** Synchronously gets if the path exists. Beaware of TOCTOU issues. */
/** Synchronously gets if the path exists. Beware of TOCTOU issues. */
existsSync(): boolean {
return this.lstatSync() != null;
}
Expand Down Expand Up @@ -842,7 +842,7 @@ export class PathRef {
}

/**
* Renames the file or directory returning a promise that resolves to
* Moves the file or directory returning a promise that resolves to
* the renamed path.
*/
rename(newPath: string | URL | PathRef): Promise<PathRef> {
Expand All @@ -851,7 +851,7 @@ export class PathRef {
}

/**
* Renames the file or directory returning a promise that resolves to
* Moves the file or directory returning a promise that resolves to
* the renamed path synchronously.
*/
renameSync(newPath: string | URL | PathRef): PathRef {
Expand All @@ -860,6 +860,26 @@ export class PathRef {
return pathRef;
}

/**
* Moves the file or directory to the specified directory.
* @returns The destination file path.
*/
renameToDir(destinationDirPath: string | URL | PathRef): Promise<PathRef> {
const destinationPath = ensurePathRef(destinationDirPath)
.join(this.basename());
return this.rename(destinationPath);
}

/**
* Moves the file or directory to the specified directory synchronously.
* @returns The destination file path.
*/
renameToDirSync(destinationDirPath: string | URL | PathRef): PathRef {
const destinationPath = ensurePathRef(destinationDirPath)
.join(this.basename());
return this.renameSync(destinationPath);
}

/** Opens the file and pipes it to the writable stream. */
async pipeTo(dest: WritableStream<Uint8Array>, options?: PipeOptions): Promise<this> {
const file = await Deno.open(this.#path, { read: true });
Expand Down