32 lines
1.0 KiB
TypeScript

import { join } from 'path';
import { ensureDir, writeJSON, readJSON, readdir } from 'fs-extra';
import type { BenchResults } from './types';
export const now = () => new Date().getTime();
export interface SaveBenchOptions {
rootDir?: string;
}
export const saveBench = async (
key: string,
data: Partial<BenchResults>,
options: SaveBenchOptions
) => {
const dirname = options.rootDir || process.cwd();
const existing = await ensureDir(dirname).then(() => {
return loadBench(options).catch(() => ({}));
});
await writeJSON(join(dirname, `bench/${key}.json`), { ...existing, ...data }, { spaces: 2 });
};
export const loadBench = async (options: SaveBenchOptions): Promise<Partial<BenchResults>> => {
const dirname = options.rootDir || process.cwd();
const files = await readdir(join(dirname, `bench`));
return files.reduce(async (acc, file) => {
const data = await readJSON(join(dirname, `bench/${file}`));
return { ...(await acc), ...data };
}, Promise.resolve({}));
// return readJSON(join(dirname, `bench.json`));
};