Skip to content

Commit

Permalink
fix: lazily initialize config file (#762)
Browse files Browse the repository at this point in the history
  • Loading branch information
clample committed Jun 9, 2023
1 parent e31f4ca commit 95fb19e
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 23 deletions.
13 changes: 13 additions & 0 deletions packages/cli/src/services/__tests__/config.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Conf from 'conf'
import config from '../config'
jest.mock('conf')

describe('config', () => {
it('should avoid reading config file if environment variables are set', () => {
process.env.CHECKLY_API_KEY = 'test-api-key'
const apiKey = config.getApiKey()
expect(apiKey).toEqual(process.env.CHECKLY_API_KEY)
expect(Conf).toHaveBeenCalledTimes(0)
delete process.env.CHECKLY_API_KEY
})
})
59 changes: 36 additions & 23 deletions packages/cli/src/services/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,38 @@ enum Env {
local = 'local',
}

const config = {
auth: new Conf({
projectName: '@checkly/cli',
configName: 'auth',
projectSuffix,
// @ts-ignore
schema: authSchema,
}),
data: new Conf({
projectName: '@checkly/cli',
configName: 'config',
projectSuffix,
// @ts-ignore
schema: dataSchema,
}),
class ChecklyConfig {
private _auth?: Conf<{ apiKey: unknown }>
private _data?: Conf<{ accountId: unknown, accountName: unknown }>

// Accessing auth or data will cause a config file to be created.
// We should avoid doing this unless absolutely necessary, since this operation can fail due to file permissions.
get auth () {
// Create this._auth lazily
return this._auth ?? (this._auth = new Conf({
projectName: '@checkly/cli',
configName: 'auth',
projectSuffix,
// @ts-ignore
schema: authSchema,
}))
}

get data () {
// Create this._data lazily
return this._data ?? (this._data = new Conf({
projectName: '@checkly/cli',
configName: 'config',
projectSuffix,
// @ts-ignore
schema: dataSchema,
}))
}

clear () {
this.auth.clear()
this.data.clear()
},
}

getEnv (): Env {
const environments = ['production', 'development', 'staging', 'local']
Expand All @@ -49,21 +61,21 @@ const config = {
}

return env as Env
},
}

getApiKey (): string {
return process.env.CHECKLY_API_KEY || this.auth.get<string>('apiKey') as string || ''
},
}

getAccountId (): string {
return process.env.CHECKLY_ACCOUNT_ID || this.data.get<string>('accountId') as string || ''
},
}

hasEnvVarsConfigured (): boolean {
const apiKey = process.env.CHECKLY_API_KEY || ''
const accoundId = process.env.CHECKLY_ACCOUNT_ID || ''
return apiKey !== '' || accoundId !== ''
},
}

getApiUrl (): string {
const environments = {
Expand All @@ -73,7 +85,7 @@ const config = {
production: 'https://api.checklyhq.com',
}
return environments[this.getEnv()]!
},
}

getMqttUrl (): string {
const environments = {
Expand All @@ -83,14 +95,15 @@ const config = {
production: 'wss://events.checklyhq.com',
}
return environments[this.getEnv()]!
},
}

hasValidCredentials (): boolean {
if (this.getApiKey() !== '' && this.getAccountId() !== '') {
return true
}
return false
},
}
}

const config = new ChecklyConfig()
export default config

0 comments on commit 95fb19e

Please sign in to comment.