import {Stats} from 'fs';
import fs from 'fs/promises';

export default interface FileSystemService {}
export class FileSystemServiceImpl implements FileSystemService {
  static async ensureDirectory(generatedBaseDataBasePath: string) {
    let stat: Stats | undefined;
    try {
      stat = await fs.stat(generatedBaseDataBasePath)
    } catch (errRaw) {
      const err = errRaw as { code: string };
      if (err.code !== 'ENOENT') {
        throw err;
      }
      await fs.mkdir(generatedBaseDataBasePath);
    }

    if (stat && !stat.isDirectory()) {
      throw new Error(`Destination directory is misconfigured, check if ${generatedBaseDataBasePath} is a directory`);
    }
  }
}
