Skip to content

Commit

Permalink
feat: 🎸 explose FSA from index file
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Jun 15, 2023
1 parent 0001df2 commit 77696f5
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/node-to-fsa/__tests__/scenarios.test.ts
@@ -0,0 +1,36 @@
import {nodeToFsa} from '..';
import { IFsWithVolume, memfs } from '../..';
import { maybe } from './util';

maybe('scenarios', () => {
test('can init FSA from an arbitrary FS folder and execute operations', async () => {
const fs = memfs({
'/tmp': null,
'/etc': null,
'/bin': null,
'/Users/kasper/Documents/shopping-list.txt': 'Milk, Eggs, Bread',
}) as IFsWithVolume;
const dir = nodeToFsa(fs, '/Users/kasper/Documents');
const shoppingListFile = await dir.getFileHandle('shopping-list.txt');
const shoppingList = await shoppingListFile.getFile();
expect(await shoppingList.text()).toBe('Milk, Eggs, Bread');
const backupsDir = await dir.getDirectoryHandle('backups', { create: true });
const backupFile = await backupsDir.getFileHandle('shopping-list.txt', { create: true });
const writable = await backupFile.createWritable();
await writable.write(await shoppingList.arrayBuffer());
await writable.close();
const logsFileHandle = await dir.getFileHandle('logs.csv', { create: true });
const logsWritable = await logsFileHandle.createWritable();
await logsWritable.write('timestamp,level,message\n');
await logsWritable.write({type: 'write', data: '2021-01-01T00:00:00Z,INFO,Hello World\n'});
await logsWritable.close();
expect(fs.__vol.toJSON()).toEqual({
'/tmp': null,
'/etc': null,
'/bin': null,
'/Users/kasper/Documents/shopping-list.txt': 'Milk, Eggs, Bread',
'/Users/kasper/Documents/backups/shopping-list.txt': 'Milk, Eggs, Bread',
'/Users/kasper/Documents/logs.csv': 'timestamp,level,message\n2021-01-01T00:00:00Z,INFO,Hello World\n'
});
});
});
11 changes: 11 additions & 0 deletions src/node-to-fsa/index.ts
@@ -0,0 +1,11 @@
import {NodeFileSystemDirectoryHandle} from './NodeFileSystemDirectoryHandle';
import {NodeFsaContext, NodeFsaFs} from './types';

export * from './types';
export * from './NodeFileSystemHandle';
export * from './NodeFileSystemDirectoryHandle';
export * from './NodeFileSystemFileHandle';

export const nodeToFsa = (fs: NodeFsaFs, dirPath: string, ctx?: Partial<NodeFsaContext>): NodeFileSystemDirectoryHandle => {
return new NodeFileSystemDirectoryHandle(fs, dirPath, ctx);
};

0 comments on commit 77696f5

Please sign in to comment.