Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions src/api/utils/project-meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,12 @@ import * as configLoader from '../../blinkmrc.js'
function projectConfig(cwd: string) {
return configLoader.projectConfig<BlinkMRC>({
cwd,
ENOENTResult: {},
})
}

async function read(cwd: string): Promise<BlinkMRC> {
try {
return await projectConfig(cwd).load()
} catch {
return {}
}
return await projectConfig(cwd).load()
}

async function write(
Expand Down
19 changes: 13 additions & 6 deletions src/blinkmrc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ async function load<T>({
ENOENTResult,
}: {
filePath: string
ENOENTResult?: T
ENOENTResult: T | undefined
}): Promise<T> {
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`)
Expand All @@ -44,20 +44,21 @@ async function load<T>({
function generateConfig<T extends object>(configOptions: {
dir: string
filename: string
ENOENTResult: T | undefined
}): Config<T> {
const filename = configOptions.filename
const filePath = path.join(configOptions.dir, filename)

const config: Config<T> = {
async load() {
return load<T>({ filePath })
return load<T>({ filePath, ENOENTResult: configOptions.ENOENTResult })
},
async update<T extends object>(updater: (obj: T) => T): Promise<T> {
async update(updater: (obj: T) => T): Promise<T> {
const data = await load<T>({
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,
Expand All @@ -70,20 +71,25 @@ function generateConfig<T extends object>(configOptions: {

export function projectConfig<T extends object>({
cwd,
ENOENTResult,
}: {
cwd: string
ENOENTResult: T | undefined
}): Config<T> {
return generateConfig<T>({
dir: cwd,
// dotfile, like .eslintrc.json or .travis.yml
filename: `.blinkmrc.json`,
ENOENTResult,
})
}

export function userConfig<T extends object>({
name,
ENOENTResult,
}: {
name: string
ENOENTResult: T
}): Config<T> {
const p = os.platform()

Expand All @@ -97,6 +103,7 @@ export function userConfig<T extends object>({
const cfg = generateConfig<T>({
dir: dirs.userConfig(),
filename: name,
ENOENTResult,
})

return cfg
Expand Down
2 changes: 1 addition & 1 deletion src/cdn/utils/config-helper.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as configLoader from '../../blinkmrc.js'

function projectConfig<T extends object>(cwd: string) {
return configLoader.projectConfig<T>({ cwd })
return configLoader.projectConfig<T>({ cwd, ENOENTResult: undefined })
}

async function read<T extends object>(cwd: string): Promise<T> {
Expand Down
19 changes: 7 additions & 12 deletions src/identity/utils/user-config.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
15 changes: 0 additions & 15 deletions test/api/cors/read.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})
16 changes: 1 addition & 15 deletions test/api/utils/project-meta.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: () => ({
Expand Down
Loading