@@ -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