Skip to content

Commit

Permalink
change write to config to override the config in memory as well for test
Browse files Browse the repository at this point in the history
  • Loading branch information
Metroxe committed Oct 19, 2023
1 parent 84ec89a commit 047e864
Showing 1 changed file with 4 additions and 9 deletions.
13 changes: 4 additions & 9 deletions infrastructure/smart-contracts/utils/writeToConfig.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,12 @@ import fs from 'fs';
import path from 'path';
import { config } from '@xai-vanguard-node/core';

let isConfigWritten = false;

/**
* Updates the configuration file with the provided config object.
* @param newConfig - The new configuration object to be merged with the existing one.
* @throws {Error} If the function is called more than once during the runtime.
*/
export function writeToConfig(newConfig) {
if (isConfigWritten) {
throw new Error('writeToConfig can only be called once per runtime.');
}


// Merge the existing and new config objects
const updatedConfig = { ...config, ...newConfig };

Expand All @@ -23,16 +17,17 @@ export function writeToConfig(newConfig) {
return result;
}, {});

// override the current config in memory
config = sortedConfig;

// Convert the sorted config object to a string
const updatedConfigStr = `export const config = ${JSON.stringify(sortedConfig, null, 2)};`;
console.log("new config", updatedConfigStr);
console.log("New config written", updatedConfigStr);

// Determine the path to the config file dynamically
const configFilePath = path.resolve(process.cwd(), '../../packages/core/src/config.ts');

// Write the updated config string to the config file
fs.writeFileSync(configFilePath, updatedConfigStr, 'utf8');

isConfigWritten = true;
}

0 comments on commit 047e864

Please sign in to comment.