Skip to content

Commit

Permalink
Add function to write Buffer/String/JSON (#12)
Browse files Browse the repository at this point in the history
* Add function to write Buffer/String/JSON

* test plugin and json functions

* test error handling for missing plugins or file extension
  • Loading branch information
CordlessWool authored Mar 27, 2024
1 parent 1af2566 commit 044ba53
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 2 deletions.
103 changes: 102 additions & 1 deletion packages/core/src/core/file.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { InMemoryAdapterHelper } from '@loom-io/test-utils';

import { basename, dirname } from 'node:path';
import { Directory } from './dir.js';
import { FILE_SIZE_UNIT, SourceAdapter } from '../definitions.js';
import { FILE_SIZE_UNIT, LoomFileConverter, PLUGIN_TYPE, SourceAdapter } from '../definitions.js';
import { faker } from '@faker-js/faker';
import { Editor } from './editor.js';

Expand Down Expand Up @@ -139,9 +139,110 @@ describe('Test File Service', () => {
expect(content.toString()).toBe(testContent);
});

test('Write file', async () => {
const testContent = '1234k2hk3jh1fasdasfc%';
const testFile = await testHelper.createFile(undefined, testContent);

const file = LoomFile.from( adapter, testFile);
const newContent = 'new content';
await file.write(newContent);
const content = await file.text();
expect(content).toBe(newContent);
});

test('Create file', async () => {
const testFile = await testHelper.createFile(undefined, 'test');
const file = LoomFile.from( adapter, testFile);
await file.create();
expect(await file.exists()).toBeTruthy();
});

test('Delete file', async () => {
const testFile = await testHelper.createFile(undefined, 'test');
const file = LoomFile.from( adapter, testFile);
await file.delete();
expect(await file.exists()).toBeFalsy();
});



});


describe('File Plugin Tests', () => {

afterEach(() => {
FileTest.emptyPlugins();
});

test('Register a plugin', async () => {
const testContent = '1234k2hk3jh1fasdasfc%';
const testFile = await testHelper.createFile(undefined, testContent);

const file = LoomFile.from( adapter, testFile);
const plugin: LoomFileConverter = {
$type: PLUGIN_TYPE.FILE_CONVERTER,
extensions: ['test'],
parse: <T>(text: string) => JSON.parse(text) as T,
stringify: (content: unknown) => JSON.stringify(content)
};
LoomFile.register(plugin);
const content = await file.text();
expect(content).toBe(testContent);
});

test('Read file with json plugin', async () => {
const testContent = {test: true};
const testFile = await testHelper.createFile(faker.system.commonFileName('json'), JSON.stringify(testContent));

const file = LoomFile.from( adapter, testFile);
const plugin: LoomFileConverter = {
$type: PLUGIN_TYPE.FILE_CONVERTER,
extensions: ['json'],
parse: <T>(text: string) => JSON.parse(text) as T,
stringify: (content: unknown) => JSON.stringify(content)
};
LoomFile.register(plugin);
const content = await file.json();
expect(content).toStrictEqual(testContent);
});

test('Write file with json plugin', async () => {
const testContent = {test: true};
const testFile = await testHelper.createFile(faker.system.commonFileName('json'), JSON.stringify(testContent));

const file = LoomFile.from( adapter, testFile);
const plugin: LoomFileConverter = {
$type: PLUGIN_TYPE.FILE_CONVERTER,
extensions: ['json'],
parse: <T>(text: string) => JSON.parse(text) as T,
stringify: (content: unknown) => JSON.stringify(content)
};
LoomFile.register(plugin);
await file.stringify(testContent);
const content = await file.text();
expect(content).toBe(JSON.stringify(testContent));
});

test('No plugin for file', async () => {
const testContent = {test: true};
const testFile = await testHelper.createFile(faker.system.commonFileName('json'), JSON.stringify(testContent));

const file = LoomFile.from( adapter, testFile);
await expect(file.json()).rejects.toThrow(PluginNotFoundException);
await expect(file.stringify(testContent)).rejects.toThrow(PluginNotFoundException);
});

test('No extension for file', async () => {
const testContent = {test: true};
const testFile = await testHelper.createFile(faker.system.directoryPath(), JSON.stringify(testContent));

const file = LoomFile.from( adapter, testFile);
await expect(file.json()).rejects.toThrow(FileConvertException);
await expect(file.stringify(testContent)).rejects.toThrow(FileConvertException);
});
});

});


Expand Down
26 changes: 26 additions & 0 deletions packages/core/src/core/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ export class LoomFile {

}

async delete() {
await this._adapter.deleteFile(this.path);
}

async exists() {
const fullPath = joinPath(this.dir.path, this.name);
return this._adapter.fileExists(fullPath);
Expand All @@ -101,6 +105,28 @@ export class LoomFile {
return await this._adapter.readFile(this.path, encoding);
}

async write(data: string | Buffer) {
await this._adapter.writeFile(this.path, data);
}

async create() {
await this._adapter.writeFile(this.path, Buffer.alloc(0));
}

async stringify<T>(content: T) {
if(this.extension === undefined) {
throw new FileConvertException(this.path, 'File has no extension');
}

const plugin = LoomFile.converterPlugins.get(this.extension);

if(plugin === undefined) {
throw new PluginNotFoundException(this.path);
}

return plugin.stringify(content);
}

static register(plugin: LoomFileConverter) {
if(plugin.$type === PLUGIN_TYPE.FILE_CONVERTER) {
plugin.extensions.forEach(ext => {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export interface SourceAdapter {
readFile(path: string): MaybePromise<Buffer>
readFile(path: string, encoding: BufferEncoding): MaybePromise<string>
readFile(path: string, encoding?: BufferEncoding): MaybePromise<Buffer | string>
writeFile(path: string, content: string): MaybePromise<void>
writeFile(path: string, content: string | Buffer): MaybePromise<void>
deleteFile(path: string): MaybePromise<void>
openFile(path: string): MaybePromise<FileHandler>
stat(path: string): MaybePromise<FileStat>
Expand Down

0 comments on commit 044ba53

Please sign in to comment.