Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
shahradelahi committed May 31, 2024
1 parent d9e40dc commit 1247226
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/driver/fs.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { mkdirSync, readFileSync } from 'node:fs';
import { mkdirSync, promises } from 'node:fs';
import { dirname, resolve } from 'node:path';
import process from 'node:process';
import debounce, { type DebouncedFunction } from 'debounce';
Expand Down Expand Up @@ -34,14 +34,24 @@ export default class FsDriver extends MemoryDriver {
this._encoding = opts.encoding || 'utf-8';
this._writer = new FileWriter(this._path, { encoding: this._encoding });

this.prepare().catch(() => {
throw new Error('Failed to prepare storage');
});

process.on('beforeExit', () => {
this._bouncyWriteFn.flush();
});
}

async prepare() {
// Try to create a recursive
const fileDir = dirname(this._path);
if (!access(fileDir)) {
mkdirSync(fileDir, { recursive: true });
}

if (access(this._path)) {
const rawData = readFileSync(this._path, this._encoding);
const rawData = await promises.readFile(this._path, this._encoding);
const parser = this._parser;

const _storage = rawData === '' ? new Map<string, Serializable>() : parser.parse(rawData);
Expand All @@ -54,10 +64,6 @@ export default class FsDriver extends MemoryDriver {
}
});
}

process.on('beforeExit', () => {
this._bouncyWriteFn.flush();
});
}

async write(): Promise<void> {
Expand Down

0 comments on commit 1247226

Please sign in to comment.