Skip to content

Commit

Permalink
Merge pull request #28 from fika-dev/feature/iss/#27
Browse files Browse the repository at this point in the history
[fika-cli] create -y flag for for fika command that a question
  • Loading branch information
croquies committed Nov 15, 2022
2 parents 44a2b99 + 047f607 commit 15f1624
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/domain/service/i-prompt.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ export interface IPromptService {
askBranchName(message: string, defaultName: string, candidates: string[]): Promise<string>;
askRemoteUrl(): Promise<string>;
confirmAction(message: string): Promise<boolean>;
setAcceptsAllPromptsAsYes(): void;
}
9 changes: 9 additions & 0 deletions src/domain/service/prompt.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,16 @@ const white: TerminalColor = {

@injectable()
export class PromptService implements IPromptService {
private acceptsAllPromptsAsYes: boolean = false;

public setAcceptsAllPromptsAsYes() {
this.acceptsAllPromptsAsYes = true;
}

async confirmAction(message: string): Promise<boolean> {
if (this.acceptsAllPromptsAsYes) {
return true;
}
const answer = await promptly.confirm(`${this.bold(this.colorize(green, "?"))} ${message}: `);
return answer;
}
Expand Down
8 changes: 8 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ import { setCommand } from "./command/set";
import { startCommand } from "./command/start";
import SERVICE_IDENTIFIER from "./config/constants/identifiers";
import container from "./config/ioc_config";
import { IPromptService } from "./domain/service/i-prompt.service";
import { IErrorHandlingService } from "./domain/service/i_error_handling.service";
import BaseException from "./domain/value_object/exceptions/base_exception";
import { UnknownError } from "./domain/value_object/exceptions/unknown_error";

try {
dotenv.config();
program.name("fika").description("CLI for advanced your workflow").version(version);
program.option("-y, --yes", "accepting all the prompts as yes");
program.addCommand(startCommand);
program.addCommand(finishCommand);
program.addCommand(connectCommand);
Expand All @@ -32,6 +34,12 @@ try {
program.addCommand(checkoutDevelopCommand);
program.addCommand(pullCommand);
program.parse(process.argv);

const option = program.opts();
if (option.yes) {
const promptService = container.get<IPromptService>(SERVICE_IDENTIFIER.PromptService);
promptService.setAcceptsAllPromptsAsYes();
}
} catch (e) {
const errorHandlingService = container.get<IErrorHandlingService>(
SERVICE_IDENTIFIER.ErrorHandlingService
Expand Down
39 changes: 39 additions & 0 deletions test/service/prompt.service.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import SERVICE_IDENTIFIER from "@/config/constants/identifiers";
import container from "@/config/ioc_config";
import { IPromptService } from "@/domain/service/i-prompt.service";
import promptly from "promptly";
const promptService = container.get<IPromptService>(SERVICE_IDENTIFIER.PromptService);

beforeAll(async () => {
jest.restoreAllMocks();
});

beforeEach(async()=>{

});

afterEach(async ()=>{


})

afterAll(() => {

});

describe("1. test confirmAction", () => {
test("1.1. test confirm action without flag", async () => {
const spy = jest.spyOn(promptly, "confirm").mockImplementation(()=>Promise.resolve(false));
const answer = await promptService.confirmAction("any message");
expect(answer).toBe(false);
expect(spy).toHaveBeenCalledTimes(1);
});

test("1.2. test confirm action with flag", async () => {
promptService.setAcceptsAllPromptsAsYes();
const spy = jest.spyOn(promptly, "confirm").mockImplementation(()=>Promise.resolve(false));
const answer = await promptService.confirmAction("any message");
expect(answer).toBe(true);
expect(spy).toHaveBeenCalledTimes(0);
});
});

0 comments on commit 15f1624

Please sign in to comment.