Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…ion/ief into if-attest
  • Loading branch information
jmcook1186 committed Oct 22, 2024
2 parents 3367b96 + a9e4bb5 commit 10bf729
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 81 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release-commit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,4 @@ jobs:
- name: Create pull request
run: gh pr create -B ${{ vars.RELEASE_BRANCH_NAME }} -H $PR_BRANCH_NAME --title "Release ${{github.event.release.tag_name}}" --body "${{github.event.release.body}}"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
2 changes: 1 addition & 1 deletion .github/workflows/release-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,4 @@ jobs:
run: npm publish
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
6 changes: 4 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 9 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
{
"name": "@grnsft/if",
"description": "Impact Framework",
"version": "0.7.0",
"version": "0.7.1",
"author": {
"name": "Green Software Foundation",
"email": "[email protected]"
},
"bin": {
"if-diff": "./build/if-diff/index.js",
"if-run": "./build/if-run/index.js",
"if-env": "./build/if-env/index.js",
"if-check": "./build/if-check/index.js",
"if-csv": "./build/if-csv/index.js",
"if-merge": "./build/if-merge/index.js"
"if-diff": "build/if-diff/index.js",
"if-run": "build/if-run/index.js",
"if-env": "build/if-env/index.js",
"if-check": "build/if-check/index.js",
"if-csv": "build/if-csv/index.js",
"if-merge": "build/if-merge/index.js"
},
"bugs": {
"url": "https://github.com/Green-Software-Foundation/if/issues/new?assignees=&labels=feedback&projects=&template=feedback.md&title=Feedback+-+"
Expand Down Expand Up @@ -73,7 +73,7 @@
"access": "public"
},
"repository": {
"url": "https://github.com/Green-Software-Foundation/if"
"url": "git+https://github.com/Green-Software-Foundation/if.git"
},
"scripts": {
"build": "npm run clean && tsc --project tsconfig.build.json",
Expand All @@ -91,6 +91,7 @@
"lint": "gts lint",
"pre-commit": "lint-staged",
"prepare": "husky install",
"prepublishOnly": "npm run build",
"release": "release-it",
"test": "jest --verbose --testPathPattern=src/__tests__/"
},
Expand Down
127 changes: 58 additions & 69 deletions src/if-run/lib/regroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,93 +7,82 @@ import {validate} from '../../common/util/validations';
import {STRINGS} from '../config';

const {InvalidGroupingError} = ERRORS;

const {INVALID_GROUP_KEY, REGROUP_ERROR} = STRINGS;

/**
* Grouping strategy.
* Creates structure to insert inputs by groups.
*/
export const Regroup = (
inputs: PluginParams[],
outputs: PluginParams[],
const appendGroup = (
value: PluginParams,
object: any,
target: string,
groups: string[]
) => {
/**
* Creates structure to insert inputs by groups.
*/
const appendGroup = (
value: PluginParams,
object: any,
target: string,
groups: string[]
) => {
if (groups.length > 0) {
const group = groups.shift() as string;

object.children = object.children ?? {};
object.children[group] = object.children[group] ?? {};
): any => {
if (groups.length === 0) {
object[target] = object[target] || [];
object[target].push(value);
return object;
}

if (groups.length === 0) {
if (
object.children[group][target] &&
object.children[group][target].length > 0
) {
object.children[group][target].push(value);
} else {
object.children[group][target] = [value];
}
}
const group = groups.shift()!;
object.children = object.children || {};
object.children[group] = object.children[group] || {};

appendGroup(value, object.children[group], target, groups);
}
return appendGroup(value, object.children[group], target, groups);
};

return object;
};
/**
* Validates the groups array.
*/
const validateGroups = (groups: string[]): string[] => {
const inputData = {regroup: groups};
const validationSchema = z.record(
z.string(),
z.array(z.string(), {message: REGROUP_ERROR}).min(1)
);

/**
* Validates groups array.
*/
const validateGroups = (regroup: string[]) => {
const inputData = {regroup};
const validationSchema = z.record(
z.string(),
z.array(z.string(), {message: REGROUP_ERROR}).min(1)
);
validate(validationSchema, inputData);

validate(validationSchema, inputData);
return groups;
};

return groups;
};
/**
* Looks up a group key value in the input.
*/
const lookupGroupKey = (input: PluginParams, groupKey: string): string => {
if (!input[groupKey]) {
throw new InvalidGroupingError(INVALID_GROUP_KEY(groupKey));
}

/**
* Interates over inputs, grabs group values for each one.
* Based on grouping, initializes the structure.
*/
return input[groupKey];
};

/**
* Regroups inputs and outputs based on the given group keys.
*/
export const Regroup = (
inputs: PluginParams[],
outputs: PluginParams[],
groups: string[]
): any => {
const validatedGroups = validateGroups(groups);

const lookupGroupKey = (input: PluginParams, groupKey: string) => {
if (!input[groupKey]) {
throw new InvalidGroupingError(INVALID_GROUP_KEY(groupKey));
const appendToAccumulator = (
items: PluginParams[],
acc: any,
target: string
) => {
for (const item of items) {
const groupsWithData = validatedGroups.map(groupKey =>
lookupGroupKey(item, groupKey)
);
appendGroup(item, acc, target, groupsWithData);
}

return input[groupKey];
};

let acc = {} as any;
for (const input of inputs) {
const groupsWithData = validatedGroups.map(groupKey =>
lookupGroupKey(input, groupKey)
);
acc = appendGroup(input, acc, 'inputs', groupsWithData);
}

for (const output of outputs) {
const groupsWithData = validatedGroups.map(groupKey =>
lookupGroupKey(output, groupKey)
);
acc = appendGroup(output, acc, 'outputs', groupsWithData);
}
const acc = {} as any;
appendToAccumulator(inputs, acc, 'inputs');
appendToAccumulator(outputs, acc, 'outputs');

return acc.children;
};

0 comments on commit 10bf729

Please sign in to comment.