import pathLib from 'path' import fs from 'fs-extra' import { Item } from '../types/Item' import { IStorageAdapter } from './IStorageAdapter' import Collection from './collection' export default class AdapterFile implements IStorageAdapter { get name() { return 'AdapterFile' as const } get file(): string { if (this.collection.list.singlefile) { return pathLib.join(this.collection.root, `${this.collection.name}.json`) } return pathLib.join(this.collection.root, this.collection.name, 'metadata.json') } collection!: Collection clone() { return new AdapterFile() } init(collection: Collection) { this.collection = collection return this } async restore(name?: string) { let path = this.file if (name) { const p = { ...pathLib.parse(this.file) } as Partial p.name = name delete p.base path = pathLib.format(p) } if (fs.pathExistsSync(path)) { return fs.readJSON(path) } return false } async store(name: string) { let path = this.file if (name) { const p = { ...pathLib.parse(this.file) } as Partial p.name = name delete p.base path = pathLib.format(p) } await fs.ensureFile(path) await fs.writeJSON(path, this.collection.store(), { spaces: 2, }) } }