-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: settings handling on missing keys
- Loading branch information
Showing
20 changed files
with
70 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,34 @@ | ||
import { Settings } from '@/types' | ||
import { promises as fs } from 'fs' | ||
import fs from 'fs' | ||
import { merge } from 'lodash' | ||
import { DEFAULT_SETTINGS, SETTINGS_PATH } from './constants' | ||
|
||
export default async function getSettings(): Promise<Settings> { | ||
try { | ||
// Attempt to read the file | ||
const file = await fs.readFile(SETTINGS_PATH, 'utf8') | ||
const settings: Partial<Settings> = JSON.parse(file) | ||
let updated = false | ||
|
||
// Ensure connection settings | ||
const connectionSettings = { | ||
...DEFAULT_SETTINGS.connection, | ||
...settings.connection, | ||
} | ||
if ( | ||
JSON.stringify(settings.connection) !== JSON.stringify(connectionSettings) | ||
) { | ||
settings.connection = connectionSettings | ||
updated = true | ||
} | ||
|
||
// Ensure features settings | ||
const featuresSettings = { | ||
...DEFAULT_SETTINGS.features, | ||
...settings.features, | ||
} | ||
if ( | ||
JSON.stringify(settings.features) !== JSON.stringify(featuresSettings) | ||
) { | ||
settings.features = featuresSettings | ||
updated = true | ||
} | ||
export default function getSettings(): Settings { | ||
if (!fs.existsSync(SETTINGS_PATH)) { | ||
writeSettings(DEFAULT_SETTINGS) | ||
} | ||
|
||
// Ensure test setting | ||
if (settings.test === undefined) { | ||
settings.test = DEFAULT_SETTINGS.test | ||
updated = true | ||
} | ||
let settings: Settings | ||
|
||
if (updated) { | ||
await writeSettings(settings as Settings) | ||
} | ||
try { | ||
const fileContent = fs.readFileSync(SETTINGS_PATH, 'utf-8') | ||
settings = JSON.parse(fileContent) | ||
} catch (error) { | ||
console.error('[SETTINGS] - Error reading or parsing settings file!', error) | ||
settings = { ...DEFAULT_SETTINGS } | ||
writeSettings(settings) | ||
return settings | ||
} | ||
|
||
return settings as Settings | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
} catch (error: any) { | ||
// If reading fails because the file does not exist, create the file with default settings | ||
if (error.code === 'ENOENT') { | ||
console.warn('[SETTINGS] - File not found! Creating a new one...') | ||
await writeSettings(DEFAULT_SETTINGS) | ||
const updatedSettings = merge({}, DEFAULT_SETTINGS, settings) | ||
|
||
return DEFAULT_SETTINGS | ||
} else { | ||
throw new Error('[SETTINGS] - Unable to read file!') | ||
} | ||
if (JSON.stringify(updatedSettings) !== JSON.stringify(settings)) { | ||
writeSettings(updatedSettings) | ||
} | ||
|
||
return updatedSettings | ||
} | ||
|
||
async function writeSettings(settings: Settings): Promise<void> { | ||
try { | ||
await fs.writeFile(SETTINGS_PATH, JSON.stringify(settings, null, 2), 'utf8') | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
} catch (error: any) { | ||
throw new Error('[SETTINGS] - Unable to write file!') | ||
} | ||
function writeSettings(settings: Settings): void { | ||
fs.writeFileSync(SETTINGS_PATH, JSON.stringify(settings, undefined, 2)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { Settings } from '@/types' | ||
import { REQUIRED_SETTINGS } from './constants' | ||
|
||
export function checkRequiredSettings(settings: Settings): boolean { | ||
return REQUIRED_SETTINGS.every((key) => { | ||
const keys = key.split('.') | ||
|
||
// @ts-expect-error - we know this is safe, but should still look to resolve without exception | ||
return keys.reduce((acc, curr) => acc && acc[curr], settings) | ||
}) | ||
} |