This commit is contained in:
Morten Olsen
2024-12-10 20:59:29 +01:00
commit ede2d56b7c
54 changed files with 6955 additions and 0 deletions

2
packages/fs-system/.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
/node_modules/
/dist/

View File

@@ -0,0 +1,20 @@
{
"name": "@plaindb/fs-system",
"version": "1.0.0",
"type": "module",
"main": "dist/exports.js",
"files": [
"dist"
],
"scripts": {
"build": "tsc --build"
},
"dependencies": {
"@plaindb/plaindb": "workspace:*",
"glob": "^11.0.0"
},
"devDependencies": {
"@types/node": "^22.10.1",
"typescript": "^5.7.2"
}
}

View File

@@ -0,0 +1,48 @@
import { FileSystem } from '@plaindb/plaindb';
import { existsSync } from 'fs';
import { mkdir, readFile, writeFile } from 'fs/promises';
import { dirname, resolve } from 'path';
import { glob } from 'glob';
type SystemFileSystemOptions = {
cwd: string;
};
class SystemFileSystem extends FileSystem {
#options: SystemFileSystemOptions;
constructor(options: SystemFileSystemOptions) {
super();
this.#options = options;
}
public get = async (location: string): Promise<Buffer | undefined> => {
const { cwd } = this.#options;
const actualLocation = resolve(cwd, location);
if (!existsSync(actualLocation)) {
return undefined;
}
return readFile(actualLocation);
};
public set = async (location: string, data: Buffer): Promise<void> => {
const { cwd } = this.#options;
const actualLocation = resolve(cwd, location);
const dir = dirname(actualLocation);
if (!existsSync(dir)) {
await mkdir(dir, { recursive: true });
}
await writeFile(actualLocation, data);
};
public update = async () => {
const { cwd } = this.#options;
const files = await glob('**/*', {
cwd,
nodir: true,
});
await this.emit('changed', files);
};
}
export { SystemFileSystem };

View File

@@ -0,0 +1,9 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"outDir": "dist"
},
"include": [
"src"
]
}