Skip to content

Commit

Permalink
fix: ignore cache when reading user pjson (#1124)
Browse files Browse the repository at this point in the history
* fix: ignore cache when reading user pjson

* refactor: useCache = true default (#1125)

---------

Co-authored-by: Shane McLaughlin <shane.mclaughlin@salesforce.com>
  • Loading branch information
mdonnalley and mshanemc committed Jun 25, 2024
1 parent 5213551 commit 6038c5c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 22 deletions.
3 changes: 2 additions & 1 deletion src/config/plugin-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<PJSON>(userPJSONPath)
// ignore cache because the file might have changed within the same process (e.g. during a JIT plugin install)
const pjson = await readJson<PJSON>(userPJSONPath, false)
if (!pjson.oclif) pjson.oclif = {schema: 1}
if (!pjson.oclif.plugins) pjson.oclif.plugins = []
await this.loadPlugins(
Expand Down
44 changes: 23 additions & 21 deletions src/util/fs.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -57,8 +57,17 @@ class ProdOnlyCache extends Map<string, string> {

const cache = new ProdOnlyCache()

export async function readJson<T = unknown>(path: string): Promise<T> {
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 <T>
*/
export async function readJson<T = unknown>(path: string, useCache = true): Promise<T> {
if (useCache && cache.has(path)) {
return JSON.parse(cache.get(path)!) as T
}

Expand All @@ -67,25 +76,18 @@ export async function readJson<T = unknown>(path: string): Promise<T> {
return JSON.parse(contents) as T
}

export function readJsonSync(path: string, parse: false): string
export function readJsonSync<T = unknown>(path: string, parse?: true): T
export function readJsonSync<T = unknown>(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<T>(path: string): Promise<T | undefined> {
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 <T> or undefined
*/
export async function safeReadJson<T>(path: string, useCache = true): Promise<T | undefined> {
try {
return await readJson<T>(path)
return await readJson<T>(path, useCache)
} catch {}
}

Expand Down

0 comments on commit 6038c5c

Please sign in to comment.