From a2a752e67a3cc1f53b0d30a3ba23c9ed346014a3 Mon Sep 17 00:00:00 2001 From: Jaryt Bustard Date: Wed, 6 Jul 2022 12:14:07 -0400 Subject: [PATCH 1/3] feat: optionally flatten run step --- src/lib/Components/Commands/exports/Native/Run.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib/Components/Commands/exports/Native/Run.ts b/src/lib/Components/Commands/exports/Native/Run.ts index e9c366f..26154c8 100644 --- a/src/lib/Components/Commands/exports/Native/Run.ts +++ b/src/lib/Components/Commands/exports/Native/Run.ts @@ -23,10 +23,10 @@ export class Run implements Command { /** * Generate Run Command shape.* @returns The generated JSON for the Run Commands. */ - generate(): RunCommandShape | RunCommandShorthandShape { + generate(flatten?: boolean): RunCommandShape | RunCommandShorthandShape { const { command, ...parameters } = this.parameters; - if (Object.keys(parameters).length === 0) { + if (Object.keys(parameters).length === 0 && flatten) { return { run: command } as RunCommandShorthandShape; } From e59b09a3f4763133bf706ce7a93a89797b1b1f87 Mon Sep 17 00:00:00 2001 From: Jaryt Bustard Date: Wed, 6 Jul 2022 12:29:26 -0400 Subject: [PATCH 2/3] fix: persist flatten for steps --- .../Components/Commands/exports/Native/Run.ts | 2 ++ .../exports/Reusable/CustomCommand.ts | 6 +++--- .../Components/Workflow/exports/Workflow.ts | 4 ++-- .../Workflow/exports/WorkflowJob.ts | 4 ++-- .../Workflow/exports/WorkflowJobAbstract.ts | 15 +++++++------ .../Workflow/exports/WorkflowJobApproval.ts | 4 ++-- src/lib/Config/index.ts | 6 +++++- tests/Commands.test.ts | 21 ++++++++++++------- tests/Job.test.ts | 6 +++--- tests/Workflow.test.ts | 2 +- 10 files changed, 42 insertions(+), 28 deletions(-) diff --git a/src/lib/Components/Commands/exports/Native/Run.ts b/src/lib/Components/Commands/exports/Native/Run.ts index 26154c8..f41fe12 100644 --- a/src/lib/Components/Commands/exports/Native/Run.ts +++ b/src/lib/Components/Commands/exports/Native/Run.ts @@ -26,6 +26,8 @@ export class Run implements Command { generate(flatten?: boolean): RunCommandShape | RunCommandShorthandShape { const { command, ...parameters } = this.parameters; + console.log(Object.keys(parameters).length, flatten); + if (Object.keys(parameters).length === 0 && flatten) { return { run: command } as RunCommandShorthandShape; } diff --git a/src/lib/Components/Commands/exports/Reusable/CustomCommand.ts b/src/lib/Components/Commands/exports/Reusable/CustomCommand.ts index 1b94fca..8629413 100644 --- a/src/lib/Components/Commands/exports/Reusable/CustomCommand.ts +++ b/src/lib/Components/Commands/exports/Reusable/CustomCommand.ts @@ -27,7 +27,7 @@ export class CustomCommand parameters?: CustomParametersList; /** - * A string that describes the purpose of the command. + * A string that describes the purpose of the command */ description?: string; @@ -43,9 +43,9 @@ export class CustomCommand this.description = description; } - generate(): CustomCommandShape { + generate(flatten?: boolean): CustomCommandShape { const generatedSteps: AnyCommandShape[] = this.steps.map((step) => - step.generate(), + step.generate(flatten), ); return { diff --git a/src/lib/Components/Workflow/exports/Workflow.ts b/src/lib/Components/Workflow/exports/Workflow.ts index e7312d9..4522218 100644 --- a/src/lib/Components/Workflow/exports/Workflow.ts +++ b/src/lib/Components/Workflow/exports/Workflow.ts @@ -50,9 +50,9 @@ export class Workflow implements Generable, Conditional { * Generate Workflow Shape. * @returns The generated JSON for the Workflow. */ - generate(): WorkflowsShape { + generate(flatten?: boolean): WorkflowsShape { const generatedWorkflowJobs = this.jobs.map((job) => { - return job.generate(); + return job.generate(flatten); }); const generatedWhen = this.when?.generate(); diff --git a/src/lib/Components/Workflow/exports/WorkflowJob.ts b/src/lib/Components/Workflow/exports/WorkflowJob.ts index 87a2824..e5e33c7 100644 --- a/src/lib/Components/Workflow/exports/WorkflowJob.ts +++ b/src/lib/Components/Workflow/exports/WorkflowJob.ts @@ -24,13 +24,13 @@ export class WorkflowJob extends WorkflowJobAbstract { this.job = job; } - generate(): WorkflowJobShape { + generate(flatten?: boolean): WorkflowJobShape { if (this.parameters === undefined) { return this.job.name; } return { - [this.job.name]: this.generateContents(), + [this.job.name]: this.generateContents(flatten), }; } diff --git a/src/lib/Components/Workflow/exports/WorkflowJobAbstract.ts b/src/lib/Components/Workflow/exports/WorkflowJobAbstract.ts index 8b2e1a7..61335ce 100644 --- a/src/lib/Components/Workflow/exports/WorkflowJobAbstract.ts +++ b/src/lib/Components/Workflow/exports/WorkflowJobAbstract.ts @@ -22,7 +22,7 @@ export abstract class WorkflowJobAbstract implements Generable { this.parameters = parameters; } - generateContents(): WorkflowJobContentsShape { + generateContents(flatten?: boolean): WorkflowJobContentsShape { let parameters: WorkflowJobParametersShape | undefined; if (this.parameters) { @@ -30,8 +30,8 @@ export abstract class WorkflowJobAbstract implements Generable { this.parameters; parameters = { ...jobParameters, - 'pre-steps': this.generateSteps(pre_steps), - 'post-steps': this.generateSteps(post_steps), + 'pre-steps': this.generateSteps(pre_steps, flatten), + 'post-steps': this.generateSteps(post_steps, flatten), }; if (matrix) { @@ -48,10 +48,13 @@ export abstract class WorkflowJobAbstract implements Generable { return GenerableType.WORKFLOW_JOB; } - private generateSteps(steps?: StepsParameter): AnyCommandShape[] | undefined { - return steps?.map((step) => step.generate()); + private generateSteps( + steps?: StepsParameter, + flatten?: boolean, + ): AnyCommandShape[] | undefined { + return steps?.map((step) => step.generate(flatten)); } abstract get name(): string; - abstract generate(): WorkflowJobShape; + abstract generate(flatten?: boolean): WorkflowJobShape; } diff --git a/src/lib/Components/Workflow/exports/WorkflowJobApproval.ts b/src/lib/Components/Workflow/exports/WorkflowJobApproval.ts index e2f357b..c9fd3b2 100644 --- a/src/lib/Components/Workflow/exports/WorkflowJobApproval.ts +++ b/src/lib/Components/Workflow/exports/WorkflowJobApproval.ts @@ -13,9 +13,9 @@ export class WorkflowJobApproval extends WorkflowJobAbstract { this._name = name; } - generate(): WorkflowJobShape { + generate(flatten?: boolean): WorkflowJobShape { return { - [this.name]: this.generateContents(), + [this.name]: this.generateContents(flatten), }; } diff --git a/src/lib/Config/index.ts b/src/lib/Config/index.ts index 90104ba..65b26e2 100644 --- a/src/lib/Config/index.ts +++ b/src/lib/Config/index.ts @@ -185,7 +185,11 @@ export class Config * Export the CircleCI configuration as a YAML string. */ generate(flatten?: boolean): string { - const generatedWorkflows = generateList(this.workflows, {}); + const generatedWorkflows = generateList( + this.workflows, + {}, + flatten, + ); const generatedJobs = generateList(this.jobs, {}, flatten); const generatedExecutors = generateList( this.executors, diff --git a/tests/Commands.test.ts b/tests/Commands.test.ts index 6ef8425..6b0a75b 100644 --- a/tests/Commands.test.ts +++ b/tests/Commands.test.ts @@ -14,7 +14,7 @@ describe('Instantiate a Run step', () => { const run = new CircleCI.commands.Run({ command: 'echo hello world', }); - const runStep = run.generate(); + const runStep = run.generate(true); const expectedResult = { run: 'echo hello world' }; it('Should generate checkout yaml', () => { expect(runStep).toEqual(expectedResult); @@ -312,7 +312,8 @@ describe('Instantiate a Custom Command', () => { type: string default: hello world steps: - - run: echo << parameters.greeting >>`; + - run: + command: echo << parameters.greeting >>`; it('Should generate checkout yaml', () => { expect(customCommand.generate()).toEqual(parse(expectedOutput)); @@ -405,7 +406,9 @@ describe('Instantiate reusable commands', () => { steps: - run: echo << parameters.axis >>`; - expect(firstCustomCommand.generate()).toEqual(parse(firstExpectedOutput)); + expect(firstCustomCommand.generate(true)).toEqual( + parse(firstExpectedOutput), + ); }); const secondCustomCommand = new CircleCI.reusable.CustomCommand( @@ -432,7 +435,9 @@ describe('Instantiate reusable commands', () => { steps: - run: echo << parameters.year >>`; - expect(secondCustomCommand.generate()).toEqual(parse(secondExpectedOutput)); + expect(secondCustomCommand.generate(true)).toEqual( + parse(secondExpectedOutput), + ); }); const myConfig = new CircleCI.Config(); @@ -600,9 +605,9 @@ echo hello world 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 this string is a single echo hello world 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 this string is a single line, and should output as a single line `; it('Should match expectedOutput', () => { - expect(stringify(multiLineCommand.generate(), stringifyOptions)).toEqual( - expectedOutput, - ); + expect( + stringify(multiLineCommand.generate(true), stringifyOptions), + ).toEqual(expectedOutput); }); }); @@ -614,7 +619,7 @@ describe('Instantiate a Run command with 70 characters in the command string and const expectedOutput = `run: echo hello world 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 this string is a single line, and should output as a single line `; it('Should match expectedOutput', () => { - expect(stringify(longCommand.generate(), stringifyOptions)).toEqual( + expect(stringify(longCommand.generate(true), stringifyOptions)).toEqual( expectedOutput, ); }); diff --git a/tests/Job.test.ts b/tests/Job.test.ts index efb68d6..e01e268 100644 --- a/tests/Job.test.ts +++ b/tests/Job.test.ts @@ -16,7 +16,7 @@ describe('Instantiate Docker Job', () => { }; it('Should match the expected output', () => { - expect(job.generate()).toEqual({ [jobName]: jobContents }); + expect(job.generate(true)).toEqual({ [jobName]: jobContents }); }); it('Should match the expected output', () => { @@ -52,7 +52,7 @@ describe('Instantiate Parameterized Docker Job With Custom Parameters', () => { - run: echo << parameters.greeting >>`; it('Should match the expected output', () => { - expect(job.generate()).toEqual(YAML.parse(expectedOutput)); + expect(job.generate(true)).toEqual(YAML.parse(expectedOutput)); }); it('Should throw error when no enum values are provided', () => { @@ -127,7 +127,7 @@ describe('Instantiate Parameterized Docker Job With A Custom Command', () => { const myConfig = new CircleCI.Config(); myConfig.addCustomCommand(customCommand); myConfig.addJob(job); - expect(YAML.parse(myConfig.generate())).toEqual(expectedOutput); + expect(YAML.parse(myConfig.generate(true))).toEqual(expectedOutput); }); }); diff --git a/tests/Workflow.test.ts b/tests/Workflow.test.ts index 3a7941c..7e8f655 100644 --- a/tests/Workflow.test.ts +++ b/tests/Workflow.test.ts @@ -360,7 +360,7 @@ describe('Add pre/post steps to workflow', () => { ], }, }; - const generatedWorkflow = myWorkflow.generate(); + const generatedWorkflow = myWorkflow.generate(true); expect(generatedWorkflow).toEqual(expected); }); }); From 7eda9ed40ce73b23ec6be894c3292c73a9016402 Mon Sep 17 00:00:00 2001 From: Jaryt Bustard Date: Wed, 6 Jul 2022 12:42:16 -0400 Subject: [PATCH 3/3] chore: removed console log --- src/lib/Components/Commands/exports/Native/Run.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/lib/Components/Commands/exports/Native/Run.ts b/src/lib/Components/Commands/exports/Native/Run.ts index f41fe12..26154c8 100644 --- a/src/lib/Components/Commands/exports/Native/Run.ts +++ b/src/lib/Components/Commands/exports/Native/Run.ts @@ -26,8 +26,6 @@ export class Run implements Command { generate(flatten?: boolean): RunCommandShape | RunCommandShorthandShape { const { command, ...parameters } = this.parameters; - console.log(Object.keys(parameters).length, flatten); - if (Object.keys(parameters).length === 0 && flatten) { return { run: command } as RunCommandShorthandShape; }