From 6038c5c34aca5442e2034c6c574f9c20150bb2f9 Mon Sep 17 00:00:00 2001 From: Mike Donnalley Date: Tue, 25 Jun 2024 14:19:23 -0600 Subject: [PATCH] fix: ignore cache when reading user pjson (#1124) * fix: ignore cache when reading user pjson * refactor: useCache = true default (#1125) --------- Co-authored-by: Shane McLaughlin --- src/config/plugin-loader.ts | 3 ++- src/util/fs.ts | 44 +++++++++++++++++++------------------ 2 files changed, 25 insertions(+), 22 deletions(-) diff --git a/src/config/plugin-loader.ts b/src/config/plugin-loader.ts index 49f05d72..7322ebb4 100644 --- a/src/config/plugin-loader.ts +++ b/src/config/plugin-loader.ts @@ -208,7 +208,8 @@ export default class PluginLoader { try { const userPJSONPath = join(opts.dataDir, 'package.json') debug('reading user plugins pjson %s', userPJSONPath) - const pjson = await readJson(userPJSONPath) + // ignore cache because the file might have changed within the same process (e.g. during a JIT plugin install) + const pjson = await readJson(userPJSONPath, false) if (!pjson.oclif) pjson.oclif = {schema: 1} if (!pjson.oclif.plugins) pjson.oclif.plugins = [] await this.loadPlugins( diff --git a/src/util/fs.ts b/src/util/fs.ts index cf5c5f1b..9538299b 100644 --- a/src/util/fs.ts +++ b/src/util/fs.ts @@ -1,4 +1,4 @@ -import {Stats, existsSync as fsExistsSync, readFileSync} from 'node:fs' +import {Stats, existsSync as fsExistsSync} from 'node:fs' import {readFile, stat} from 'node:fs/promises' import {isProd} from './util' @@ -57,8 +57,17 @@ class ProdOnlyCache extends Map { const cache = new ProdOnlyCache() -export async function readJson(path: string): Promise { - if (cache.has(path)) { +/** + * Read a file from disk and cache its contents if in production environment. + * + * Will throw an error if the file does not exist. + * + * @param path file path of JSON file + * @param useCache if false, ignore cache and read file from disk + * @returns + */ +export async function readJson(path: string, useCache = true): Promise { + if (useCache && cache.has(path)) { return JSON.parse(cache.get(path)!) as T } @@ -67,25 +76,18 @@ export async function readJson(path: string): Promise { return JSON.parse(contents) as T } -export function readJsonSync(path: string, parse: false): string -export function readJsonSync(path: string, parse?: true): T -export function readJsonSync(path: string, parse = true): T | string { - if (cache.has(path)) { - return JSON.parse(cache.get(path)!) as T - } - - const contents = readFileSync(path, 'utf8') - cache.set(path, contents) - return parse ? (JSON.parse(contents) as T) : contents -} - -export async function safeReadJson(path: string): Promise { - if (cache.has(path)) { - return JSON.parse(cache.get(path)!) as T - } - +/** + * Safely read a file from disk and cache its contents if in production environment. + * + * Will return undefined if the file does not exist. + * + * @param path file path of JSON file + * @param useCache if false, ignore cache and read file from disk + * @returns or undefined + */ +export async function safeReadJson(path: string, useCache = true): Promise { try { - return await readJson(path) + return await readJson(path, useCache) } catch {} }