UNPKG

egg-xc-base

Version:

a base framework with egg.js

130 lines (119 loc) 4.35 kB
/** * Created by ThinkPad on 2019/12/18. */ const fs = require('fs') const path = require('path') const moment = require('moment') let _module = null let _ctx = null let args = process.argv let time = null let _fun = null async function getIndex(apis,fun) { let index = -1 let num = 0 for(let i in apis){ let v = apis[i] if(v.fun == fun){ index = i num++ } } if( num > 1 ){ throw new Error('apis fun 重复!'+fun) } return index } async function doThat() { if( args.length < 3 ){ throw new Error( ` 指令: yarn api Error 缺少传入参数: 1.模块 2.上下文根 3.方法名 指令构成: yarn api 模块 上下文根 方法名 eg: yarn api room room getByBimId `) }else if( args.length < 4){ throw new Error(` 指令: yarn api ${args[2]} Error 缺少传入参数: 1.上下文根 2.方法名 指令构成: yarn api 模块 上下文根 方法名 eg: yarn api room room getByBimId `) }else if( args.length < 5){ throw new Error(` 指令: yarn api ${args[2]} ${args[3]} Error 缺少传入参数: 1.方法名 指令构成: yarn api 模块 上下文根 方法名 eg: yarn api room room getByBimId `) }else{ time = new moment( Date.now()).format('YYYY-MM-DD HH:mm:ss.SSS') console.log(` 指令: yarn api ${args[2]} ${args[3]} ${args[4]} `) _module = args[2] _ctx = args[3] _fun = args[4] console.log(`传入模块--> ${_module}`) console.log(`传入上下文根--> ${_ctx}`) console.log(`传入方法名--> ${_fun}`) if( !fs.existsSync(path.join(__dirname,`../api/${_module}`))){ throw new Error(`Error-->${_module}模块不存在!_请先运行 yarn run apiInit`) } if( !fs.existsSync(path.join(__dirname,`../api/${_module}/.init`))){ throw new Error(`Error-->${path.resolve(`../api/${_module}/.init`)}不存在!`) } if( !fs.existsSync(path.join(__dirname,`../api/${_module}/.init/.init.json`))){ throw new Error(`Error-->${path.resolve(`../api/${_module}/.init/.init.json`)}不存在!`) } let initJson = require(`../api/${_module}/.init/.init.json`) if(!initJson.ctxs[_ctx]){ throw new Error(`Error-->${path.resolve(`../api/${_module}/.init/.init.json`)}该上下文根不存在! -->ctx=`+ _ctx) } let index = parseInt( await getIndex(initJson.ctxs[_ctx].apis,_fun) ) + 1 if(index < 1 ){ throw new Error(`${path.resolve(`../api/${_module}/.init/.init.json`)}方法不存在!-->ctx=${_ctx},fun=`+ _fun) } let _api = initJson.ctxs[_ctx].apis[index-1] const apiNormal = require('./.apiNormal') _api.id = index.toString(16).toUpperCase() initJson.ctxs[_ctx].apis[index-1].id = index.toString(16).toUpperCase() if(index<10){ _api.id = '0'+ index.toString(16).toUpperCase() initJson.ctxs[_ctx].apis[index-1].id = '0'+ index.toString(16).toUpperCase() } await apiNormal.doIt({ _api, _ctx, _module, }) initJson.ctxs[_ctx].api_num += 1 initJson.api_num += 1 initJson.ver += 1 initJson.update_time = time fs.writeFileSync(path.join(__dirname,`../api/${_module}/.init/.init.json`) , JSON.stringify(initJson,0,3) ) console.log(`更新.init.json文件 --> `+path.resolve( `../api/${_module}/.init/.init.json 版本号:${ initJson.ver }`)) } } async function doIt() { console.log(`------------------------------------------- apiInit start --------------------------------------------`) try { await doThat() console.log(`apiInit result --> ${_module?'模块:'+_module+' ':''}成功 *^v^* `) }catch (err){ console.error(err.stack) console.log(err.message) console.log(`apiInit result --> ${_module?'模块:'+_module+' ':''}失败 -o(╥﹏╥)o -`) } console.log(`------------------------------------------- apiInit End ----------------------------------------------`) } doIt()