Skip to content
This repository was archived by the owner on Feb 8, 2024. It is now read-only.

Commit 53510cc

Browse files
author
Isaiah Becker-Mayer
committed
Adds a move method which won't actually work until that feature is added to browsers whatwg/fs#10
1 parent 250e9b7 commit 53510cc

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

packages/teleport/src/lib/tdp/sharedDirectoryManager.ts

+25-2
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export class SharedDirectoryManager {
6666
/**
6767
* Gets the FileOrDirInfo for all the children of the directory at path.
6868
* @throws Will throw an error if a directory has not already been initialized via add().
69-
* @throws {PathDoesNotExistError} if the pathstr isn't a valid path in the shared directory
69+
* @throws {PathDoesNotExistError} if the path isn't a valid path in the shared directory
7070
*/
7171
async listContents(path: string): Promise<FileOrDirInfo[]> {
7272
this.checkReady();
@@ -119,7 +119,7 @@ export class SharedDirectoryManager {
119119
/**
120120
* Writes the bytes in writeData to the file at path starting at offset.
121121
* @throws Will throw an error if a directory has not already been initialized via add().
122-
* @throws {PathDoesNotExistError} if the pathstr isn't a valid path in the shared directory
122+
* @throws {PathDoesNotExistError} if the path isn't a valid path in the shared directory
123123
*/
124124
async writeFile(
125125
path: string,
@@ -143,6 +143,29 @@ export class SharedDirectoryManager {
143143
return writeData.length;
144144
}
145145

146+
/**
147+
* Moves the file or directory at originalPath to newPath. It's designed to work similar to
148+
* the Linux mv utility with no options: https://linux.die.net/man/1/mv.
149+
* @throws Will throw an error if a directory has not already been initialized via add().
150+
* @throws {PathDoesNotExistError} if the path isn't a valid path in the shared directory
151+
*/
152+
async move(originalPath: string, newPath: string): Promise<void> {
153+
// See https://web.dev/file-system-access/#renaming-and-moving-files-and-folders
154+
this.checkReady();
155+
156+
const originalFileOrDir = await this.walkPath(originalPath);
157+
158+
let split = newPath.split('/');
159+
const newName = split.pop();
160+
const newDirPath = split.join('/');
161+
const newDir = await this.walkPath(newDirPath);
162+
if (newDir.kind !== 'directory') {
163+
throw new Error('cannot move a file into another file');
164+
}
165+
166+
await originalFileOrDir.move(newDir, newName);
167+
}
168+
146169
/**
147170
* walkPath walks a pathstr (assumed to be in the qualified Unix format specified
148171
* in the TDP spec), returning the FileSystemDirectoryHandle | FileSystemFileHandle

0 commit comments

Comments
 (0)