Skip to content

Commit

Permalink
Merge pull request matter-labs#16 from zkLinkProtocol/ci
Browse files Browse the repository at this point in the history
add export env file function
  • Loading branch information
zkcarter authored Mar 30, 2024
2 parents 6b7c03a + 33e38d1 commit d767d5f
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions infrastructure/zk/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,22 @@ function loadConfigFile(path: string) {
}
}

function loadEnvFile(path: string) {
const fileContents = fs.readFileSync(path);
const envConfig = new Map<string, string>();

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<string, string> {
let variables: Map<string, string> = new Map();

Expand Down Expand Up @@ -115,10 +131,38 @@ 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<string, string>();
// 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);
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);

0 comments on commit d767d5f

Please sign in to comment.