UNPKG

@s-hiroshi/bks

Version:

Cli bookmarks application

85 lines 2.58 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.EditControl = void 0; const createChoices_1 = require("../service/createChoices"); const inquirer = require('inquirer'); class EditControl { constructor(reader, writer) { this.reader = reader; this.writer = writer; this.controlCharactor = 'edit'; } getControlCharactor() { return this.controlCharactor; } async execute() { let choices = this.reader.readAll().map((item) => { return `keyword: ${item.keyword}\n url: ${item.content}`; }); if (choices.length < 1) { console.log('Content not Founded'); return false; } choices = (0, createChoices_1.createChoices)(choices, ['Exit']); const choiced = await inquirer .prompt([ { type: 'list', name: 'context', message: 'Which one do you want to edit', choices: choices, loop: false }, ]) .then((answer) => { return answer.context; }); if (choiced === 'Exit') { return false; } const choicedItem = this.getChoicedItem(choiced); const item = { keyword: '', content: '' }; await inquirer .prompt([ { name: 'context', message: 'keyword', default: choicedItem.keyword, }, ]) .then((answer) => { item.keyword = answer.context; /* * Promiseを返却 * 次のthenで受ける */ return inquirer.prompt([ { name: 'context', message: 'url', default: choicedItem.content }, ]); }) .then((answer) => { // 一番外側の戻り値 item.content = answer.context; }); this.writer.edit(item, { 'keyword': choicedItem.keyword, 'content': choicedItem.content }); console.log('Completed'); } getChoicedItem(choiced) { const choice = choiced.split('\n'); const keyword = choice[0].trim().split(' ')[1]; const url = choice[1].trim().split(' ')[1]; return { keyword: keyword, content: url }; } } exports.EditControl = EditControl; //# sourceMappingURL=EditControl.js.map