Skip to content

Commit

Permalink
feat(util): imporve mergeManifests helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
manushak committed Aug 1, 2024
1 parent 6f99d7f commit a828eb3
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions src/if-merge/util/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,31 @@ import {IFMergeArgs} from '../types/process-args';
/**
* Merges the given manifests in the one file.
*/
export const mergeManifests = async (commandArgs: IFMergeArgs) => {
const {manifests: commandManifests, name, description} = commandArgs;
export const mergeManifests = async (mergeArgs: IFMergeArgs) => {
const {manifests: commandManifests, name, description} = mergeArgs;
let manifests = commandManifests;
let manifestPath = process.env.CURRENT_DIR || process.cwd();

if (commandManifests.length === 1) {
manifests = await getYamlFiles(commandManifests[0]);
manifestPath = commandManifests[0];
}

const context = {
name: name || 'if-merge',
description: description || 'merged manifest',
tags: null,
initialize: {plugins: {}},
};
const tree = await mergeManifestsData(manifests, context);
const outputPath = getOutputPath(name, manifestPath);

await saveMergedManifest(tree, context, outputPath);
};

/**
* Merges manifests data.
*/
const mergeManifestsData = async (manifests: string[], context: Context) => {
const tree: any = {children: {}};

for await (const manifest of manifests) {
Expand All @@ -45,12 +54,17 @@ export const mergeManifests = async (commandArgs: IFMergeArgs) => {
};
});
}
const manifestName = name
? `${name.replace(' ', '-')}.yaml`
: 'merged-manifest.yaml';
const outputPath = path.join(manifestPath, manifestName);

await saveMergedManifest(tree, context, outputPath);
return tree;
};

/**
* Gets output path.
*/
const getOutputPath = (name: string | undefined, manifestPath: string) => {
const manifestName = `${(name || 'merged-manifest').replace(' ', '-')}.yaml`;

return path.join(manifestPath, manifestName);
};

/**
Expand Down

0 comments on commit a828eb3

Please sign in to comment.