From 3d5bf372f9a96332216a45038118fb4438c1e298 Mon Sep 17 00:00:00 2001 From: GatsbyJS Bot Date: Mon, 19 Jul 2021 08:05:38 -0400 Subject: [PATCH] fix(gatsby): Add `directory` to GatsbyCacheLmdb (#32391) (#32421) Co-authored-by: gatsbybot Co-authored-by: Lennart --- packages/gatsby/src/utils/__tests__/cache-lmdb.ts | 2 +- packages/gatsby/src/utils/cache-lmdb.ts | 9 +++++++++ packages/gatsby/src/utils/get-cache.ts | 2 +- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/packages/gatsby/src/utils/__tests__/cache-lmdb.ts b/packages/gatsby/src/utils/__tests__/cache-lmdb.ts index 512db1adf1adf..0fc342121f057 100644 --- a/packages/gatsby/src/utils/__tests__/cache-lmdb.ts +++ b/packages/gatsby/src/utils/__tests__/cache-lmdb.ts @@ -13,7 +13,7 @@ describeWhenLMDB(`cache-lmdb`, () => { beforeAll(async () => { const { default: GatsbyCacheLmdb } = await import(`../cache-lmdb`) - cache = new GatsbyCacheLmdb({ name: `__test__` }) + cache = new GatsbyCacheLmdb({ name: `__test__` }).init() const fileDir = path.join( process.cwd(), `.cache/caches-lmdb-${process.env.JEST_WORKER_ID}` diff --git a/packages/gatsby/src/utils/cache-lmdb.ts b/packages/gatsby/src/utils/cache-lmdb.ts index 7328c08c2e60c..4182c3040f300 100644 --- a/packages/gatsby/src/utils/cache-lmdb.ts +++ b/packages/gatsby/src/utils/cache-lmdb.ts @@ -1,4 +1,5 @@ import { open, RootDatabase, Database } from "lmdb-store" +import fs from "fs-extra" import path from "path" // Since the regular GatsbyCache saves to "caches" this should be "caches-lmdb" @@ -16,9 +17,17 @@ export default class GatsbyCacheLmdb { private static store private db: Database | undefined public readonly name: string + // Needed for plugins that want to write data to the cache directory + public readonly directory: string constructor({ name = `db` }: { name: string }) { this.name = name + this.directory = path.join(process.cwd(), `.cache/caches/${name}`) + } + + init(): GatsbyCacheLmdb { + fs.ensureDirSync(this.directory) + return this } private static getStore(): RootDatabase { diff --git a/packages/gatsby/src/utils/get-cache.ts b/packages/gatsby/src/utils/get-cache.ts index b2898549dbb3a..ff595ebe2d081 100644 --- a/packages/gatsby/src/utils/get-cache.ts +++ b/packages/gatsby/src/utils/get-cache.ts @@ -8,7 +8,7 @@ export const getCache = (name: string): GatsbyCache => { if (!cache) { if (isLmdbStore()) { const GatsbyCacheLmdb = require(`./cache-lmdb`).default - cache = new GatsbyCacheLmdb({ name }) as GatsbyCache + cache = new GatsbyCacheLmdb({ name }).init() as GatsbyCache } else { cache = new GatsbyCache({ name }).init() }