Skip to content

Commit

Permalink
feat: Added support for shorthand command generation (#126)
Browse files Browse the repository at this point in the history
* feat: Added support for shorthand command generation

* chore: fixed linting

* test: fixed failing test
  • Loading branch information
Jaryt authored Aug 29, 2022
1 parent 8a7841b commit 4ceec41
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 13 deletions.
8 changes: 2 additions & 6 deletions src/lib/Components/Commands/exports/Command.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import { Generable } from '../../index';
import { StringParameter } from '../../Parameters/types';
import {
CommandParameters,
CommandShape,
CommandShorthandShape,
} from '../types/Command.types';
import { AnyCommandShape, CommandParameters } from '../types/Command.types';

/**
* Abstract - A generic Command
Expand All @@ -22,5 +18,5 @@ export interface Command extends Generable {
* Generate the JSON shape for the Command.
* @param flatten - If true, short hand will be attempted.
*/
generate(flatten?: boolean): CommandShape | CommandShorthandShape;
generate(flatten?: boolean): AnyCommandShape;
}
12 changes: 10 additions & 2 deletions src/lib/Components/Commands/exports/Native/Checkout.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { GenerableType } from '../../../../Config/exports/Mapping';
import { StringParameter } from '../../../Parameters/types';
import { CommandParameters, CommandShape } from '../../types/Command.types';
import {
BodylessCommand,
CommandParameters,
CommandShape,
} from '../../types/Command.types';
import { Command } from '../Command';

/**
Expand All @@ -17,7 +21,11 @@ export class Checkout implements Command {
* Generate Checkout Command shape.
* @returns The generated JSON for the Checkout Command.
*/
generate(): CheckoutCommandShape {
generate(): CheckoutCommandShape | BodylessCommand {
if (this.parameters === undefined) {
return this.name;
}

return {
checkout: { ...this.parameters },
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ export class ReusedCommand implements Command {
/**
* @returns JSON representation of the reusable command being called
*/
generate(): CommandShape {
generate(): CommandShape | string {
if (this.parameters === undefined) {
return this.name;
}

return {
[this.name]: this.generateContents(),
};
Expand Down
7 changes: 6 additions & 1 deletion src/lib/Components/Commands/types/Command.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,14 @@ export type CommandParameters = ComponentParameter<CommandParameterTypes>;

export type CommandShape = Record<string, CommandParameters>;

export type BodylessCommand = string;

export type CommandShorthandShape = Record<string, string>;

export type AnyCommandShape = CommandShape | CommandShorthandShape;
export type AnyCommandShape =
| CommandShape
| CommandShorthandShape
| BodylessCommand;

export type ReusableCommandBodyShape = {
parameters?: CustomParametersListShape;
Expand Down
3 changes: 1 addition & 2 deletions tests/Commands.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ describe('Instantiate a Run step', () => {

describe('Instantiate a Checkout step', () => {
const checkout = new CircleCI.commands.Checkout();
const checkoutBasicResult = { checkout: {} };

it('Should produce checkout string', () => {
expect(checkout.generate()).toEqual(checkoutBasicResult);
expect(checkout.generate()).toEqual('checkout');
});

const checkoutWithPathResult = {
Expand Down
5 changes: 4 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"resolveJsonModule": true
"resolveJsonModule": true,
"lib": [
"es2017"
]
},
"include": ["src/**/*.ts"],
"exclude": ["node_modules", "**/*.d.ts", "**/*.test.ts"],
Expand Down

0 comments on commit 4ceec41

Please sign in to comment.