diff --git a/infrastructure/zk/src/config.ts b/infrastructure/zk/src/config.ts index 6d2b722c0d25..1a3a45e20814 100644 --- a/infrastructure/zk/src/config.ts +++ b/infrastructure/zk/src/config.ts @@ -39,6 +39,22 @@ function loadConfigFile(path: string) { } } +function loadEnvFile(path: string) { + const fileContents = fs.readFileSync(path); + const envConfig = new Map(); + + const lines = fileContents.toString().split('\n'); + for (const line of lines) { + if (line.trim() === '') { + continue; + } + const [key, value] = line.split('='); + envConfig.set(key, value); + } + + return envConfig; +} + export function collectVariables(config: any, prefix: string = ''): Map { let variables: Map = new Map(); @@ -115,6 +131,30 @@ export function compileConfig(environment?: string) { console.log(`Configs compiled for ${environment}`); } +export function exportConfig(environment?: string) { + environment ??= process.env.ZKSYNC_ENV!; + let config = new Map(); + // Load the environment config + const envConfig = loadEnvFile(`etc/env/${environment}.env`); + for (const [key, value] of envConfig) { + config.set(key, value); + } + // Load initial config + const initialConfig = loadEnvFile('etc/env/.init.env'); + for (const [key, value] of initialConfig) { + config.set(key, value); + } + + let outputFileContents = ''; + config.forEach((value: string, key: string) => { + outputFileContents += `${key}=${value}\n`; + }); + + const outputFileName = `etc/env/export_${environment}.env`; + fs.writeFileSync(outputFileName, outputFileContents); + console.log(`Configs of ${environment} exported to ${outputFileName}`); +} + export const command = new Command('config').description('config management'); command.command('load [environment]').description('load the config for a certain environment').action(printAllConfigs); @@ -122,3 +162,7 @@ command .command('compile [environment]') .description('compile the config for a certain environment') .action(compileConfig); +command + .command('export [environment]') + .description('export the config for a certain environment') + .action(exportConfig);