UNPKG

2.02 kBJavaScriptView Raw
1"use strict";const e=require("bluebird"),t=require("inquirer"),i=require("path"),n=require("lodash"),o=require("fs-extra"),r=require("../utils/files"),l=require("../utils/messagehandler").messagesHandler,s=i.join;function c(){try{return o.readdirSync(s(config.APP_DIR,"lookup"))}catch(e){return[]}}function a(e,i){return t.prompt([{type:"confirm",message:i,name:e}]).then(t=>t[e])}function _(e,i,n){i=[{type:"list",message:i,name:e,default:[],choices:n}];return t.prompt(i).then(t=>t[e])}function u(e,t){return n.find(t.lookup_attachments,e)}function f(e){return e.actions?e.actions:[]}function d(e){return e.triggers?e.triggers:[]}function T(e,t,i){return e.filter(e=>e[t]===i).map(e=>e.field_id).filter(e=>e)}module.exports=e.coroutine(function*(){const t=yield r.getIndex();if(!t.lookup_attachments)return e.reject(l("NO_ATTACHMENTS"));if(!c().length)return e.reject(l("DETACH_ERROR"));const i=yield a("actionDetach",l("DETACH_ACTION")),g=yield a("triggerDetach",l("DETACH_TRIGGER"));if(!i&&!g)return e.reject(l("LOOKUP_ATTACH_ERROR"));if(i){const e=f(t);if(e.length){const i=yield _("aname",l("DETACH_ACTION_LIST"),e);if(u({action_id:i},t)){let e=T(t.lookup_attachments,"action_id",i);if(e.length){let o=yield _("afield",l("DETACH_ACTION_FIELD"),e),r=n.findIndex(t.lookup_attachments,{action_id:i,field_id:o});t.lookup_attachments.splice(r,1),Console.info(l("DETACH_SUCCESS",{field:o,name:i}))}}else console.log(l("LOOKUP_ENTRY_NOT_EXIST"))}else console.log(l("DETACH_ACTION_FAIL"))}if(g){const e=d(t);if(e.length){const i=yield _("tname",l("DETACH_TRIGGER_LIST"),e);if(u({trigger_id:i},t)){let e=T(t.lookup_attachments,"trigger_id",i);if(e.length){let o=yield _("tfield",l("DETACH_TRIGGER_FIELD"),e),r=n.findIndex(t.lookup_attachments,{trigger_id:i,field_id:o});t.lookup_attachments.splice(r,1),Console.info(l("DETACH_SUCCESS",{field:o,name:i}))}}else console.log(l("LOOKUP_ENTRY_NOT_EXIST"))}else console.log(l("DETACH_TRIGGER_FAIL"))}o.writeFileSync(s(config.APP_DIR,"index.json"),JSON.stringify(t,null,2))});
\No newline at end of file