Skip to content

Commit

Permalink
feat(exposed): adds confirm prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
rafamel committed Apr 26, 2019
1 parent 3cd6594 commit 76f99e5
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 5 deletions.
13 changes: 8 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
"@types/js-yaml": "^3.12.1",
"@types/object-hash": "^1.2.0",
"@types/pify": "^3.0.2",
"@types/prompts": "^1.2.0",
"@types/signal-exit": "^3.0.0",
"@types/uuid": "^3.4.4",
"@typescript-eslint/eslint-plugin": "^1.6.0",
Expand Down Expand Up @@ -128,6 +129,7 @@
"object-hash": "^1.3.1",
"pify": "^4.0.1",
"promist": "^0.5.3",
"prompts": "^2.0.4",
"signal-exit": "^3.0.2",
"slimconf": "^0.9.0",
"string-argv": "^0.3.0",
Expand Down
1 change: 1 addition & 0 deletions src/exposed/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './exec';
export * from './prompt';
export { default as options } from './options';
export { default as line } from './line';
34 changes: 34 additions & 0 deletions src/exposed/prompt/confirm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { TScript } from '~/types';
import prompts from 'prompts';
import { status } from 'promist';

export interface IConfirmOptions {
message?: string;
initial?: boolean;
yes?: TScript;
no?: TScript;
timeout?: number;
}

// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
export default function confirm(options: IConfirmOptions = {}) {
return async function confirm(): Promise<TScript> {
const promise = status(
prompts({
type: 'confirm',
name: 'value',
message: options.message || 'Continue?',
initial: options.initial || false
})
);

if (options.timeout) {
setTimeout(() => {
if (promise.status === 'pending') process.stdin.emit('data', '\n');
}, options.timeout);
}

const response = await promise;
return (response.value ? options.yes : options.no) || undefined;
};
}
1 change: 1 addition & 0 deletions src/exposed/prompt/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as confirm } from './confirm';

0 comments on commit 76f99e5

Please sign in to comment.