Skip to content

Commit

Permalink
feat: add Prompter class
Browse files Browse the repository at this point in the history
  • Loading branch information
mdonnalley committed Jun 28, 2021
1 parent a665ec2 commit 7f7861f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/deployer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import { EventEmitter } from 'events';
import { Dictionary, Nullable } from '@salesforce/ts-types';
import cli from 'cli-ux';
import { QuestionCollection } from 'inquirer';
import { Prompter } from './prompter';

export interface Preferences {
interactive: boolean;
Expand All @@ -28,15 +30,20 @@ export abstract class Deployable {
*/
export abstract class Deployer extends EventEmitter {
public deployables: Deployable[] = [];
private prompter = new Prompter();

// Standard methods implemented in the base class methods
// eslint-disable-next-line @typescript-eslint/no-unused-vars, @typescript-eslint/no-empty-function
public progress(current: number, total: number, message: string): void {}

public log(msg: string | undefined, ...args: string[]): void {
cli.log(msg, ...args);
}

public async prompt<T>(questions: QuestionCollection<T>, initialAnswers?: Partial<T>): Promise<T> {
const answers = await this.prompter.prompt<T>(questions, initialAnswers);
return answers;
}

public selectDeployables(deployables: Deployable[]): void {
this.deployables = Object.assign([], deployables);
}
Expand Down
1 change: 1 addition & 0 deletions src/exported.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@

export { generateTableChoices } from './util';
export { Deployable, Deployer, Options, Preferences } from './deployer';
export { Prompter } from './prompter';
19 changes: 19 additions & 0 deletions src/prompter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright (c) 2021, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/

import { prompt, QuestionCollection } from 'inquirer';

export class Prompter {
public async prompt<T = Prompter.Answers>(questions: QuestionCollection<T>, initialAnswers?: Partial<T>): Promise<T> {
const answers = await prompt<T>(questions, initialAnswers);
return answers;
}
}

export namespace Prompter {
export type Answers<T = Record<string, unknown>> = T & Record<string, unknown>;
}

0 comments on commit 7f7861f

Please sign in to comment.