mirror of
https://github.com/morten-olsen/plainidx.git
synced 2026-02-08 01:06:24 +01:00
init
This commit is contained in:
2
packages/fs-system/.gitignore
vendored
Normal file
2
packages/fs-system/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
/node_modules/
|
||||
/dist/
|
||||
20
packages/fs-system/package.json
Normal file
20
packages/fs-system/package.json
Normal 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"
|
||||
}
|
||||
}
|
||||
48
packages/fs-system/src/exports.ts
Normal file
48
packages/fs-system/src/exports.ts
Normal 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 };
|
||||
9
packages/fs-system/tsconfig.json
Normal file
9
packages/fs-system/tsconfig.json
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"extends": "../../tsconfig.base.json",
|
||||
"compilerOptions": {
|
||||
"outDir": "dist"
|
||||
},
|
||||
"include": [
|
||||
"src"
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user