Skip to content

Commit

Permalink
Added fs things
Browse files Browse the repository at this point in the history
  • Loading branch information
james-pre committed Apr 17, 2024
1 parent 28384ff commit a2dbdc5
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions src/json.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import * as fs from 'fs';
import type * as _FS from 'fs';

type fs = typeof _FS;

let fs: fs = await import('fs');

export function _use_fs(_fs: fs): void {
fs = _fs;
}

export function isJSON(str: string) {
try {
Expand All @@ -14,7 +22,10 @@ export type JSONObject<Key extends string | number | symbol = string> = { [K in
export type JSONValue<Key extends string | number | symbol = string> = string | number | boolean | JSONObject<Key> | Array<JSONValue>;

interface FileMapOptions {
overwrite_invalid_json: boolean;
/**
* Should an invalid JSON file be overwritten
*/
overwrite_invalid_json?: boolean;
}

/**
Expand All @@ -27,7 +38,7 @@ export class FileMap<T extends JSONValue = JSONValue> implements Map<string, T>

constructor(
public readonly path: string,
public readonly options: FileMapOptions
public readonly options: FileMapOptions = {}
) {
if (!fs.existsSync(path)) {
fs.writeFileSync(path, '{}');
Expand All @@ -38,9 +49,9 @@ export class FileMap<T extends JSONValue = JSONValue> implements Map<string, T>
const content = fs.readFileSync(this.path, 'utf8');
if (!isJSON(content)) {
if (!this.options.overwrite_invalid_json) {
throw new SyntaxError(`Invalid JSON file: ${this.path}`);
throw new SyntaxError('Invalid JSON file: ' + this.path);
}
console.warn(`Invalid JSON file: ${this.path} (overwriting)`);
console.warn('Invalid JSON file (overwriting): ' + this.path);
this.clear();
return new Map();
}
Expand Down

0 comments on commit a2dbdc5

Please sign in to comment.