-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
09971b7
commit 8c4f3c8
Showing
14 changed files
with
122 additions
and
154 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,24 +1,65 @@ | ||
import { PartnerConfig } from '.'; | ||
import { Config, ConfigSystem, PartnerConfig } from '.'; | ||
import ugandaConfig from './chis-ug'; | ||
import kenyaConfig from './chis-ke'; | ||
import togoConfig from './chis-tg'; | ||
import civConfig from './chis-civ'; | ||
import path from 'path'; | ||
import fs from 'fs'; | ||
|
||
export const CONFIG_MAP: { [key: string]: PartnerConfig } = { | ||
export const uploadedConfigFilePath: string = path.join(getConfigUploadDirectory(), 'config.json'); | ||
|
||
|
||
export const DEFAULT_CONFIG_MAP: { [key: string]: PartnerConfig } = { | ||
'CHIS-KE': kenyaConfig, | ||
'CHIS-UG': ugandaConfig, | ||
'CHIS-TG': togoConfig, | ||
'CHIS-CIV': civConfig | ||
}; | ||
|
||
export default function getConfigByKey(key: string = 'CHIS-KE'): PartnerConfig { | ||
export default async function getConfigByKey(key: string = 'CHIS-KE'): Promise<PartnerConfig> { | ||
if (fs.existsSync(uploadedConfigFilePath)) { | ||
const uploadedConfig = await readConfig(); | ||
await Config.assertValid(uploadedConfig); | ||
console.log(`Using uploaded configuration: ${uploadedConfigFilePath}`); | ||
return uploadedConfig; | ||
} | ||
|
||
const usingKey = key.toUpperCase(); | ||
console.log(`Using configuration: ${key}`); | ||
const result = CONFIG_MAP[usingKey]; | ||
const result = DEFAULT_CONFIG_MAP[usingKey]; | ||
if (!result) { | ||
const available = JSON.stringify(Object.keys(CONFIG_MAP)); | ||
const available = JSON.stringify(Object.keys(DEFAULT_CONFIG_MAP)); | ||
throw Error(`Failed to start: Cannot find configuration "${usingKey}". Configurations available are ${available}`); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
export function getConfigUploadDirectory (): string { | ||
const configDir = path.join(__dirname,'..', 'config_uploads'); | ||
if(!fs.existsSync(configDir)) { | ||
fs.mkdirSync(configDir); | ||
} | ||
return configDir; | ||
} | ||
|
||
export async function writeConfig(jsonConfigData: ConfigSystem): Promise<void> { | ||
try { | ||
const jsonString: string = JSON.stringify(jsonConfigData, null, 2); | ||
await fs.promises.writeFile(uploadedConfigFilePath, jsonString); | ||
} catch (error) { | ||
throw new Error('writeConfig: Failed to write file'); | ||
} | ||
} | ||
|
||
export async function readConfig(): Promise<PartnerConfig> { | ||
try { | ||
const fileContent = await fs.promises.readFile(uploadedConfigFilePath, 'utf-8'); | ||
|
||
const config = JSON.parse(fileContent); | ||
return { config }; | ||
} catch (error) { | ||
console.error('readConfig:Failed to read config file:', error); | ||
throw error; | ||
} | ||
} |
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
Oops, something went wrong.