Skip to content

Commit

Permalink
feat: 🎸 add ability to create sub directories
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Jun 20, 2023
1 parent b6b026a commit 8f15bd9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/node-to-fsa/NodeFileSystemDirectoryHandle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ export class NodeFileSystemDirectoryHandle extends NodeFileSystemHandle {
*/
public async getDirectoryHandle(name: string, options?: GetDirectoryHandleOptions): Promise<NodeFileSystemDirectoryHandle> {
assertName(name, 'getDirectoryHandle', 'FileSystemDirectoryHandle');
const filename = this.path + this.ctx.separator! + name;
try {
const filename = this.path + this.ctx.separator! + name;
const stats = await this.fs.promises.stat(filename);
if (!stats.isDirectory()) {
throw new DOMException('The path supplied exists, but was not an entry of requested type.', 'TypeMismatchError');
Expand All @@ -71,6 +71,10 @@ export class NodeFileSystemDirectoryHandle extends NodeFileSystemHandle {
if (error && typeof error === 'object') {
switch (error.code) {
case 'ENOENT': {
if (options && options.create) {
await this.fs.promises.mkdir(filename);
return new NodeFileSystemDirectoryHandle(this.fs, filename, this.ctx);
}
throw new DOMException('A requested file or directory could not be found at the time an operation was processed.', 'NotFoundError');
}
case 'EPERM':
Expand Down
10 changes: 10 additions & 0 deletions src/node-to-fsa/__tests__/NodeFileSystemDirectoryHandle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,14 @@ describe('.getDirectoryHandle()', () => {
expect(subdir.name).toBe('subdir');
expect(subdir).toBeInstanceOf(NodeFileSystemDirectoryHandle);
});

test('can create a sub-directory', async () => {
const {dir, fs} = setup({});
expect(fs.existsSync('/subdir')).toBe(false);
const subdir = await dir.getDirectoryHandle('subdir', {create: true});
expect(fs.existsSync('/subdir')).toBe(true);
expect(subdir.kind).toBe('directory');
expect(subdir.name).toBe('subdir');
expect(subdir).toBeInstanceOf(NodeFileSystemDirectoryHandle);
});
});

0 comments on commit 8f15bd9

Please sign in to comment.