Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get all config.get* methods fail safely #227

Merged
merged 2 commits into from
Sep 22, 2024
Merged
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
27 changes: 18 additions & 9 deletions src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,28 +54,37 @@ export async function init (configPath = CONFIG_PATH, oldConfigPath = OLD_CONFIG
await set(newConfig, configPath)
}
}
function noop () {}

export async function get (configPath = CONFIG_PATH) {
const configBuffer = await readFile(configPath)

return deserialize(configBuffer.toString())
return readFile(configPath, 'utf-8')
.then(deserialize)
.catch(makeGetErrorHandler(configPath))
}

export function getSync (configPath = CONFIG_PATH): EntropyConfig {
try {
const configBuffer = readFileSync(configPath, 'utf8')
return deserialize(configBuffer)
} catch (err) {
if (err.code !== 'ENOENT') throw err

const newConfig = migrateData(allMigrations, {})
writeFileSync(configPath, serialize(newConfig))
return newConfig
return makeGetErrorHandler(configPath)(err)
}
}

export async function set (config: EntropyConfig, configPath = CONFIG_PATH) {
await mkdirp(dirname(configPath))
await writeFile(configPath, serialize(config))
}

/* util */
function noop () {}

function makeGetErrorHandler (configPath) {
return function getErrorHandler (err) {
if (err.code !== 'ENOENT') throw err

const newConfig = migrateData(allMigrations, {})
mkdirp.sync(dirname(configPath))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

caught a bug too!

YO we are gonna need to write some testing runsheets - the "fresh install" is a test-case we are missing a lot

writeFileSync(configPath, serialize(newConfig))
return newConfig
}
}
Loading