@@ -66,7 +66,7 @@ export class SharedDirectoryManager {
66
66
/**
67
67
* Gets the FileOrDirInfo for all the children of the directory at path.
68
68
* @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
70
70
*/
71
71
async listContents ( path : string ) : Promise < FileOrDirInfo [ ] > {
72
72
this . checkReady ( ) ;
@@ -119,7 +119,7 @@ export class SharedDirectoryManager {
119
119
/**
120
120
* Writes the bytes in writeData to the file at path starting at offset.
121
121
* @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
123
123
*/
124
124
async writeFile (
125
125
path : string ,
@@ -143,6 +143,29 @@ export class SharedDirectoryManager {
143
143
return writeData . length ;
144
144
}
145
145
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
+
146
169
/**
147
170
* walkPath walks a pathstr (assumed to be in the qualified Unix format specified
148
171
* in the TDP spec), returning the FileSystemDirectoryHandle | FileSystemFileHandle
0 commit comments