1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-12 08:54:00 +02:00
joplin/packages/app-cli/app/setupCommand.ts
2022-11-01 15:28:14 +00:00

40 lines
1.1 KiB
TypeScript

import { _ } from '@joplin/lib/locale';
export default (cmd: any, stdout: Function, store: Function, gui: Function) => {
cmd.setStdout((text: string) => {
return stdout(text);
});
cmd.setDispatcher((action: any) => {
if (store()) {
return store().dispatch(action);
} else {
return () => {};
}
});
cmd.setPrompt(async (message: string, options: any) => {
if (!options) options = {};
if (!options.type) options.type = 'boolean';
if (!options.booleanAnswerDefault) options.booleanAnswerDefault = 'y';
if (!options.answers) options.answers = options.booleanAnswerDefault === 'y' ? [_('Y'), _('n')] : [_('N'), _('y')];
if (options.type === 'boolean') {
message += ` (${options.answers.join('/')})`;
}
let answer = await gui().prompt('', `${message} `, options);
if (options.type === 'boolean') {
if (answer === null) return false; // Pressed ESCAPE
if (!answer) answer = options.answers[0];
const positiveIndex = options.booleanAnswerDefault === 'y' ? 0 : 1;
return answer.toLowerCase() === options.answers[positiveIndex].toLowerCase();
} else {
return answer;
}
});
return cmd;
};