diff --git a/src/api/utils/project-meta.ts b/src/api/utils/project-meta.ts index f427b31..96bc816 100644 --- a/src/api/utils/project-meta.ts +++ b/src/api/utils/project-meta.ts @@ -5,15 +5,12 @@ import * as configLoader from '../../blinkmrc.js' function projectConfig(cwd: string) { return configLoader.projectConfig({ cwd, + ENOENTResult: {}, }) } async function read(cwd: string): Promise { - try { - return await projectConfig(cwd).load() - } catch { - return {} - } + return await projectConfig(cwd).load() } async function write( diff --git a/src/blinkmrc.ts b/src/blinkmrc.ts index 099571b..e0c86ad 100644 --- a/src/blinkmrc.ts +++ b/src/blinkmrc.ts @@ -15,13 +15,13 @@ async function load({ ENOENTResult, }: { filePath: string - ENOENTResult?: T + ENOENTResult: T | undefined }): Promise { let data: never try { data = await loadJsonFile(filePath) } catch (err) { - if (typeof err === 'object' && err && 'code' in err) { + if (err instanceof Error && 'code' in err) { switch (err.code) { case 'JSONError': { throw new Error(`${filePath} is not valid JSON`) @@ -44,20 +44,21 @@ async function load({ function generateConfig(configOptions: { dir: string filename: string + ENOENTResult: T | undefined }): Config { const filename = configOptions.filename const filePath = path.join(configOptions.dir, filename) const config: Config = { async load() { - return load({ filePath }) + return load({ filePath, ENOENTResult: configOptions.ENOENTResult }) }, - async update(updater: (obj: T) => T): Promise { + async update(updater: (obj: T) => T): Promise { const data = await load({ filePath, - ENOENTResult: {} as T, + ENOENTResult: configOptions.ENOENTResult, }) - const updatedData = updater(data as T) + const updatedData = updater(data) await writeJsonFile(filePath, updatedData, { indent: 2, mode: 0o666, @@ -70,20 +71,25 @@ function generateConfig(configOptions: { export function projectConfig({ cwd, + ENOENTResult, }: { cwd: string + ENOENTResult: T | undefined }): Config { return generateConfig({ dir: cwd, // dotfile, like .eslintrc.json or .travis.yml filename: `.blinkmrc.json`, + ENOENTResult, }) } export function userConfig({ name, + ENOENTResult, }: { name: string + ENOENTResult: T }): Config { const p = os.platform() @@ -97,6 +103,7 @@ export function userConfig({ const cfg = generateConfig({ dir: dirs.userConfig(), filename: name, + ENOENTResult, }) return cfg diff --git a/src/cdn/utils/config-helper.ts b/src/cdn/utils/config-helper.ts index 5b4c10e..c60926c 100644 --- a/src/cdn/utils/config-helper.ts +++ b/src/cdn/utils/config-helper.ts @@ -1,7 +1,7 @@ import * as configLoader from '../../blinkmrc.js' function projectConfig(cwd: string) { - return configLoader.projectConfig({ cwd }) + return configLoader.projectConfig({ cwd, ENOENTResult: undefined }) } async function read(cwd: string): Promise { diff --git a/src/identity/utils/user-config.ts b/src/identity/utils/user-config.ts index 4f1e837..4407d7d 100644 --- a/src/identity/utils/user-config.ts +++ b/src/identity/utils/user-config.ts @@ -1,19 +1,14 @@ import pkg from '../../package.js' import * as blinkmrc from '../../blinkmrc.js' -let userConfigStore: blinkmrc.Config<{ - // Setting accessToken as well as id_token to be backward compatible - accessToken?: string - id_token?: string - access_token?: string - refresh_token?: string -}> - function getStore() { - if (!userConfigStore) { - userConfigStore = blinkmrc.userConfig({ name: pkg.name }) - } - return userConfigStore + return blinkmrc.userConfig<{ + // Setting accessToken as well as id_token to be backward compatible + accessToken?: string + id_token?: string + access_token?: string + refresh_token?: string + }>({ name: pkg.name, ENOENTResult: {} }) } export default { diff --git a/test/api/cors/read.test.ts b/test/api/cors/read.test.ts index 52e73c6..8ddc1d0 100644 --- a/test/api/cors/read.test.ts +++ b/test/api/cors/read.test.ts @@ -72,19 +72,4 @@ describe('read', () => { origins: ['test'], }) }) - - test('Should return cors as false when config throws an error', async () => { - vi.doMock('../../../src/blinkmrc.js', () => ({ - projectConfig: () => ({ - load: async () => { - throw new Error('test') - }, - update: async () => ({}), - }), - })) - const { default: read } = await import('../../../src/api/cors/read.js') - - const cors = await read(CWD) - expect(cors).toBe(false) - }) }) diff --git a/test/api/utils/project-meta.test.ts b/test/api/utils/project-meta.test.ts index d555a09..1bd076b 100644 --- a/test/api/utils/project-meta.test.ts +++ b/test/api/utils/project-meta.test.ts @@ -34,24 +34,10 @@ describe('project-meta', () => { projectMeta.projectConfig(CWD) expect(mockProjectConfig).toBeCalledWith({ cwd: CWD, + ENOENTResult: {}, }) }) - test('read() should return empty object if load() rejects', async () => { - vi.doMock('../../../src/blinkmrc.js', () => ({ - projectConfig: () => ({ - load: async () => { - throw new Error() - }, - }), - })) - const { default: projectMeta } = - await import('../../../src/api/utils/project-meta.js') - - const meta = await projectMeta.read(CWD) - expect(meta).toEqual({}) - }) - test('write() should call the updater function passed', async () => { vi.doMock('../../../src/blinkmrc.js', () => ({ projectConfig: () => ({