UNPKG

dingtalk-docs-cool-app

Version:

钉钉文档酷应用插件扩展 开发者初始化包

1 lines 4.01 kB
import _extends from"@babel/runtime/helpers/extends";import _asyncToGenerator from"@babel/runtime/helpers/asyncToGenerator";import _inheritsLoose from"@babel/runtime/helpers/inheritsLoose";import _regeneratorRuntime from"@babel/runtime/regenerator";import{join}from"path";import{existsSync,readFileSync}from"fs";import{Command}from"@oclif/core";import nodewatch from"node-watch";import express from"express";import cors from"cors";import{build}from"../../common/babel";import{getManifestByFieldDecorator,FieldExecuteCode}from"../../module/fields";import{getSafeFetch}from"../../common//safeFetch";var PORT=8088,StartField=function(e){function r(){for(var r,t=arguments.length,n=new Array(t),o=0;o<t;o++)n[o]=arguments[o];return(r=e.call.apply(e,[this].concat(n))||this).app=null,r.module=null,r.getEntryDir=function(){return process.cwd()},r}_inheritsLoose(r,e);var t=r.prototype;return t.buildAndImport=function(){var e=_asyncToGenerator(_regeneratorRuntime.mark((function e(){var r,t,n,o;return _regeneratorRuntime.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=this.getEntryDir(),t=join(r,"./src"),n=join(r,"./output/private/build"),o=join(r,"./output/private/build/index.js"),build(t,n),this.log("✅ 编译成功"),delete require.cache[require.resolve(o)],e.next=9,import(o);case 9:this.module=e.sent.default;case 10:case"end":return e.stop()}}),e,this)})));return function(){return e.apply(this,arguments)}}(),t.watchAndBuild=function(){var e=_asyncToGenerator(_regeneratorRuntime.mark((function e(){var r,t,n=this;return _regeneratorRuntime.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=this.getEntryDir(),t=join(r,"./src"),e.next=4,this.buildAndImport();case 4:nodewatch(t,{recursive:!0,filter:/\.(js|ts)$/},_asyncToGenerator(_regeneratorRuntime.mark((function e(){return _regeneratorRuntime.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,n.buildAndImport();case 2:case"end":return e.stop()}}),e)}))));case 5:case"end":return e.stop()}}),e,this)})));return function(){return e.apply(this,arguments)}}(),t.run=function(){var e=_asyncToGenerator(_regeneratorRuntime.mark((function e(){var r,t,n=this;return _regeneratorRuntime.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=process.cwd(),e.prev=1,e.next=4,this.watchAndBuild();case 4:t=join(r,"./output/private/build/index.js"),existsSync(t)||(this.log("请检查src文件夹下是否有index.ts文件"),this.exit()),this.app=express(),this.app.use((function(e,r,t){r.setHeader("Access-Control-Allow-Private-Network","true"),t()})),this.app.use(cors()),this.app.use(express.json()),this.app.get("/open_field",(function(e,r){n.module?(r.setHeader("Access-Control-Allow-Private-Network","true"),r.json(getManifestByFieldDecorator(n.module))):r.json({})})),this.app.post("/open_field_card",function(){var e=_asyncToGenerator(_regeneratorRuntime.mark((function e(t,o){var i,s,a,u,c,p,l,m,d;return _regeneratorRuntime.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(n.module){e.next=3;break}return o.json({code:FieldExecuteCode.Error,msg:"no module"}),e.abrupt("return");case 3:if(i=t.body,s=i.context,a=i.formItems,u=getManifestByFieldDecorator(n.module),c=null,p=join(r,"./config.json"),existsSync(p))try{c=JSON.parse(readFileSync(p,"utf-8")).authorizations,n.log("config:",c)}catch(e){console.error(e)}return l=getSafeFetch(n.module.getDomainList()||[],u,c),m=n.module.getDecorator(),e.next=12,null==m||null==m.execute?void 0:m.execute(_extends({},s,{fetch:l}),a);case 12:d=e.sent,o.json(d||{code:FieldExecuteCode.Error,msg:"module no execute or execute no return data"});case 14:case"end":return e.stop()}}),e)})));return function(r,t){return e.apply(this,arguments)}}()),this.app.listen(PORT,(function(){n.log("服务已经启动,端口 "+PORT)})),e.next=18;break;case 15:e.prev=15,e.t0=e.catch(1),this.error(e.t0);case 18:case"end":return e.stop()}}),e,this,[[1,15]])})));return function(){return e.apply(this,arguments)}}(),r}(Command);export{StartField as default};