diff --git a/.github/workflows/release-commit.yml b/.github/workflows/release-commit.yml index 8ffa665f7..7172083c1 100644 --- a/.github/workflows/release-commit.yml +++ b/.github/workflows/release-commit.yml @@ -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 }} diff --git a/.github/workflows/release-publish.yml b/.github/workflows/release-publish.yml index 3261fa840..a71a694aa 100644 --- a/.github/workflows/release-publish.yml +++ b/.github/workflows/release-publish.yml @@ -70,4 +70,4 @@ jobs: run: npm publish env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} \ No newline at end of file + NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} diff --git a/package-lock.json b/package-lock.json index b8c49f3a5..a23fb8aaa 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@grnsft/if", - "version": "0.7.0", + "version": "0.7.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@grnsft/if", - "version": "0.7.0", + "version": "0.7.1", "license": "MIT", "dependencies": { "@commitlint/cli": "^18.6.0", @@ -5071,6 +5071,7 @@ "version": "4.3.7", "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.7.tgz", "integrity": "sha512-Er2nc/H7RrMXZBFCEim6TCmMk02Z8vLC2Rbi1KEBggpo0fS6l0S1nnapwmIi3yW/+GOJap1Krg4w0Hg80oCqgQ==", + "dev": true, "dependencies": { "ms": "^2.1.3" }, @@ -6648,6 +6649,7 @@ "version": "2.3.3", "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.3.tgz", "integrity": "sha512-5xoDfX+fL7faATnagmWPpbFtwh/R77WmMMqqHGS65C3vvB0YHrgF+B1YmZ3441tMj5n63k0212XNoJwzlhffQw==", + "dev": true, "hasInstallScript": true, "optional": true, "os": [ diff --git a/package.json b/package.json index 9e276997c..f7204f968 100644 --- a/package.json +++ b/package.json @@ -1,18 +1,18 @@ { "name": "@grnsft/if", "description": "Impact Framework", - "version": "0.7.0", + "version": "0.7.1", "author": { "name": "Green Software Foundation", "email": "info@gsf.com" }, "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+-+" @@ -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", @@ -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__/" }, diff --git a/src/if-run/lib/regroup.ts b/src/if-run/lib/regroup.ts index 7e39b1c89..55d8f8221 100644 --- a/src/if-run/lib/regroup.ts +++ b/src/if-run/lib/regroup.ts @@ -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; };