Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(feat) O3-3443 Command in the OpenMRS CLI to package up configurations #1052

Open
wants to merge 26 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
3a847a3
(feat) O3-3443 Command in the OpenMRS CLI to package up a configuratio
suubi-joshua Jun 24, 2024
0f32a94
Attemped codeql cheak
suubi-joshua Jun 24, 2024
590c84e
trying to fix codeQL failed test
suubi-joshua Jun 24, 2024
01c7bb0
Clean Path
suubi-joshua Jun 24, 2024
938cacd
CodeQL
suubi-joshua Jun 25, 2024
e5d82cb
Removed server side environment
suubi-joshua Jun 25, 2024
24a1420
Removed port number from args
suubi-joshua Jun 25, 2024
8ee14ce
Added ending colon
suubi-joshua Jun 25, 2024
85c6899
Merge remote-tracking branch 'upstream/main' into O3-3443
suubi-joshua Jun 25, 2024
417c5ed
Removed port number from MergeConfigArgs
suubi-joshua Jun 25, 2024
44109f9
Trying to integrate CLI command
suubi-joshua Jun 27, 2024
217310c
Removed args dependency
suubi-joshua Jun 27, 2024
3dc93c7
Added script merge-congigs
suubi-joshua Jun 27, 2024
99698b9
Pulled new changes from Upstream
suubi-joshua Jun 27, 2024
917c6e5
Added type
suubi-joshua Jun 27, 2024
5267e44
Changed read files from ts to JSON
suubi-joshua Jun 27, 2024
1463eb4
Removed inline markings from WorspaceContainerProps
suubi-joshua Jun 27, 2024
29e1615
Removed more inline marks
suubi-joshua Jun 27, 2024
a532438
Testing logic
suubi-joshua Jul 1, 2024
9cb2b4f
Pulled changes from upstream
suubi-joshua Jul 1, 2024
950aa60
Fuction runMergeConfig returns promise
suubi-joshua Jul 1, 2024
0f62144
Trying to add the merge-config in assemble
suubi-joshua Jul 2, 2024
fbc311d
Upstream changes
suubi-joshua Jul 2, 2024
c675229
Edits
suubi-joshua Jul 2, 2024
621c8f2
Changed naming
suubi-joshua Jul 3, 2024
2e08f68
Added Logic to return combinedConfig as AssembleConfig format
suubi-joshua Jul 3, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/tooling/openmrs/src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from './build';
export * from './debug';
export * from './develop';
export * from './start';
export * from './merge-configs';
124 changes: 124 additions & 0 deletions packages/tooling/openmrs/src/commands/merge-configs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import express from 'express';
import * as fs from 'fs/promises';
import * as path from 'path';
import { logInfo, logWarn } from '../utils';

interface Config {
[key: string]: any;
}

interface MergeConfigArgs {
directories: string[];
output: string;
port: number;
}

interface ConfigModule {
configSchema: Config;
}

async function readConfigFile(filePath: string): Promise<Config | null> {
try {
const data = await fs.readFile(filePath, 'utf8');
Fixed Show fixed Hide fixed
const configFunction = new Function('exports', 'require', 'module', '__filename', '__dirname', data);
const module: ConfigModule = { configSchema: {} };
configFunction(module, require, module, filePath, path.dirname(filePath));
return module.configSchema;
} catch (error) {
logWarn(`Error reading or parsing file ${filePath}: ${error.message}`);
return null;
}
}

function mergeConfigs(configs: (Config | null)[]): Config {
const mergedConfig: Config = {};

configs.forEach((config) => {
if (config === null) {
return;
}

Object.keys(config).forEach((key) => {
if (typeof config[key] === 'object' && !Array.isArray(config[key])) {
mergedConfig[key] = { ...mergedConfig[key], ...config[key] };
} else {
mergedConfig[key] = config[key];
}
});
});

return mergedConfig;
}

async function writeConfigFile(filePath: string, config: Config): Promise<void> {
try {
const content = `import { validators, Type } from '@openmrs/esm-framework';

export const configSchema = ${JSON.stringify(config, null, 2)};
`;
await fs.writeFile(filePath, content, 'utf8');
Fixed Show fixed Hide fixed
logInfo(`Merged configuration written to ${filePath}`);
} catch (error) {
logWarn(`Error writing to file ${filePath}: ${error.message}`);
}
}

async function packageConfigs(configDirs: string[], outputFilePath: string): Promise<void> {
const configs: (Config | null)[] = [];

for (const dir of configDirs) {
try {
const files = await fs.readdir(dir);
Fixed Show fixed Hide fixed
for (const file of files) {
if (path.extname(file) === '.ts' || path.extname(file) === '.js') {
const filePath = path.join(dir, file);
const config = await readConfigFile(filePath);
configs.push(config);
}
}
} catch (error) {
logWarn(`Error reading directory ${dir}: ${error.message}`);
}
}

const mergedConfig = mergeConfigs(configs.filter(Boolean) as Config[]);
await writeConfigFile(outputFilePath, mergedConfig);
}

//server setup
const app = express();
app.use(express.json());

app.post('/merge-configs', async (req, res) => {
const { directories, output } = req.body;

if (!directories || !output) {
return res.status(400).send('directories and output are required');
}

try {
await packageConfigs(directories, output);
res.status(200).send(`Merged configuration written to ${output}`);
Fixed Show fixed Hide fixed
} catch (error) {
logWarn(`Failed to package configs: ${error.message}`);
res.status(500).send(`Failed to package configs: ${error.message}`);
Fixed Show fixed Hide fixed
}
});

export function runMergeConfigServer(args: MergeConfigArgs) {
const { directories, output, port } = args;

app.post('/merge-configs', async (req, res) => {
try {
await packageConfigs(directories, output);
res.status(200).send(`Merged configuration written to ${output}`);
} catch (error) {
logWarn(`Failed to package configs: ${error.message}`);
res.status(500).send(`Failed to package configs: ${error.message}`);
}
});

app.listen(port, () => {
logInfo(`Server is running on port ${port}`);
});
}
Loading