Skip to content

Commit

Permalink
feat: optionally flatten run step (#111)
Browse files Browse the repository at this point in the history
* feat: optionally flatten run step

* fix: persist flatten for steps

* chore: removed console log
  • Loading branch information
Jaryt authored Jul 6, 2022
1 parent 61d69bf commit 48bff2c
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 30 deletions.
4 changes: 2 additions & 2 deletions src/lib/Components/Commands/exports/Native/Run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
6 changes: 3 additions & 3 deletions src/lib/Components/Commands/exports/Reusable/CustomCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class CustomCommand
parameters?: CustomParametersList<CommandParameterLiteral>;

/**
* A string that describes the purpose of the command.
* A string that describes the purpose of the command
*/
description?: string;

Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions src/lib/Components/Workflow/exports/Workflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions src/lib/Components/Workflow/exports/WorkflowJob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
};
}

Expand Down
15 changes: 9 additions & 6 deletions src/lib/Components/Workflow/exports/WorkflowJobAbstract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ export abstract class WorkflowJobAbstract implements Generable {
this.parameters = parameters;
}

generateContents(): WorkflowJobContentsShape {
generateContents(flatten?: boolean): WorkflowJobContentsShape {
let parameters: WorkflowJobParametersShape | undefined;

if (this.parameters) {
const { matrix, pre_steps, post_steps, ...jobParameters } =
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) {
Expand All @@ -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;
}
4 changes: 2 additions & 2 deletions src/lib/Components/Workflow/exports/WorkflowJobApproval.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
};
}

Expand Down
6 changes: 5 additions & 1 deletion src/lib/Config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,11 @@ export class Config
* Export the CircleCI configuration as a YAML string.
*/
generate(flatten?: boolean): string {
const generatedWorkflows = generateList<WorkflowsShape>(this.workflows, {});
const generatedWorkflows = generateList<WorkflowsShape>(
this.workflows,
{},
flatten,
);
const generatedJobs = generateList<JobsShape>(this.jobs, {}, flatten);
const generatedExecutors = generateList<ReusableExecutorsShape>(
this.executors,
Expand Down
21 changes: 13 additions & 8 deletions tests/Commands.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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(
Expand All @@ -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();
Expand Down Expand Up @@ -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);
});
});

Expand All @@ -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,
);
});
Expand Down
6 changes: 3 additions & 3 deletions tests/Job.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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);
});
});

Expand Down
2 changes: 1 addition & 1 deletion tests/Workflow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ describe('Add pre/post steps to workflow', () => {
],
},
};
const generatedWorkflow = myWorkflow.generate();
const generatedWorkflow = myWorkflow.generate(true);
expect(generatedWorkflow).toEqual(expected);
});
});

0 comments on commit 48bff2c

Please sign in to comment.