flomo-terminal
Version:
terminal program for flomo
18 lines (15 loc) • 3.5 kB
JavaScript
;var t=require("commander"),n=require("inquirer"),e=require("nedb-promises"),i=require("os"),o=require("path"),r=require("axios");function a(t){return t&&"object"==typeof t&&"default"in t?t:{default:t}}var c=a(n),u=a(e),s=a(i),d=a(r),f="flomo-terminal";
/*! *****************************************************************************
Copyright (c) Microsoft Corporation.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */
function l(t,n,e,i){return new(e||(e=Promise))((function(o,r){function a(t){try{u(i.next(t))}catch(t){r(t)}}function c(t){try{u(i.throw(t))}catch(t){r(t)}}function u(t){var n;t.done?o(t.value):(n=t.value,n instanceof e?n:new e((function(t){t(n)}))).then(a,c)}u((i=i.apply(t,n||[])).next())}))}let p=null;const m=()=>{var t;return null===p&&(t="config",p=u.default.create({filename:o.join(s.default.homedir(),`/.flomo-terminal/${t}.db`),timestampData:!0,autoload:!0})),p};function v(t){return l(this,void 0,void 0,(function*(){return m().find({[t]:{$exists:!0}})}))}function y(t,n){return l(this,void 0,void 0,(function*(){const e=m(),i=yield e.find({[t]:{$exists:!0}});0===i.length?e.insert({[t]:n||""}):e.update({[t]:i[0][t]},{$set:{[t]:n||""}})}))}var h;!function(t){t.success="[32m",t.warning="[33m",t.error="[31m",t.info="[37m"}(h||(h={}));const g=t=>t.length>0||"内容不能为空!",q=[{type:"input",name:"api",message:"请输入您的 API:",validate:g}],x=[[{type:"input",name:"content",message:"现在的想法是:\n\n ",validate:g}],[{type:"editor",name:"content",message:"开始记录想法:\n\n ",validate:g}]],w=["命令行内编辑","使用编辑器"],$=[{type:"list",name:"editor",message:"请选择您的编辑方式",choices:w}],b=(t,n)=>{console.info(h[n],"\n"+t+"\n")},j=d.default.create({timeout:6e3,headers:{"Content-type":"application/json"}});function O(){return l(this,void 0,void 0,(function*(){const t=yield P(),n=function(t){const n=w.indexOf(t);return x[n]}(yield function(){return l(this,void 0,void 0,(function*(){const t=yield v("editor");return t?t[0].editor:w[0]}))}()),e=yield c.default.prompt(n);try{const n=yield j.post(t,JSON.stringify({content:e.content}));b(null==n?void 0:n.data.message,"success")}catch(t){b(t,"error")}}))}function P(t,n){return l(this,void 0,void 0,(function*(){if(t&&0!==t.length)return y("api",t[0]),t;const e=yield v("api");if(e)return n&&b(e[0].api,"info"),e[0].api;{const t=yield c.default.prompt(q);return y("api",t.api),t.api}}))}const A=new t.Command(f);A.command("send").description("发送 MEMO").action((()=>{O()})),A.command("api").description("设置 / 查看 API").action((t=>{P(t.args,!0)})),A.command("edit").description("切换编辑信息方式").action((()=>{!function(){l(this,void 0,void 0,(function*(){y("editor",(yield c.default.prompt($)).editor)}))}()}));try{A.parse(process.argv)}catch(t){A.outputHelp()}