import MFManage, {
mFconfig,
smfType,
smOption,
SchemaManager
} from 'moyan-file-model'
import ApiTemplate from './api'
import ModulesTemplate from './modules'
mFconfig.modulesTemplatePath = path.resolve(__dirname, './')
mFconfig.modulesTemplateViewsName = path.resolve(__dirname, 'view')
const schemaManager = new SchemaManager({
type: smfType.dir,
name: 'api',
children: [
{
type: smfType.ts,
name: 'index',
template: new ModulesTemplate(['user','account','sub'])
},
{
type: smfType.ts,
name: 'user',
template: new ApiTemplate([
{
describe:'创建用户',
className:'Create',
type:'POST',
data:'ok'
},
{
describe:'获取用户信息',
className:'GetInfo',
type:'GET',
data:{
name:'xxx',
sex:'man',
account:'1'
}
},
{
describe:'创建用户',
className:'DelItem',
type:'DELETE',
data:'ok'
},
])
},
{
type: smfType.ts,
name: 'account',
template: new ApiTemplate([
{
describe:'创建账户',
className:'Create',
type:'POST',
data:'ok'
},
{
describe:'获取账户信息',
className:'GetInfo',
type:'GET',
data:{
user:'1',
amount:1,
}
},
{
describe:'删除账户',
className:'DelItem',
type:'DELETE',
data:'ok'
},
])
},
{
type: smfType.dir,
name: 'sub',
children: [
{
type: smfType.ts,
name: 'index',
template: new ModulesTemplate(['test'])
},
{
type: smfType.ts,
name: 'test',
template: new ApiTemplate([
{
describe:'测试',
className:'Test',
type:'POST',
data:'ok'
},
])
}
]
}
]
})
const mFManage = new MFManage()
mFManage.run(this.schemaManager).catch((err) => {
console.info('生成失败:::', err)
})
import { Temp, Template, smfType } from 'moyan-file-model'
export default class ApiTemplate extends Temp implements Template {
public options: Array<{
describe:string
className:string,
type:string,
data:any
}> = []
constructor(options: ApiTemplate['options']) {
super(smfType.ts)
this.options = options.map((item)=>{
return {
...item,
data: typeof item.data==='object'? JSON.stringify(item.data) : (typeof item.data==='string' ? `'${item.data}'` : item.data )
}
})
}
public async run() {
try {
return await this.render('api')
} catch (err) {
console.info('===========ApiTemplate=err==========', err)
}
}
}
<% options.forEach((obj) => { %>
/**
* <%= obj.describe %>
*/
export class <%= obj.className %> {
type = '<%= obj.type%>'
getData(){
return '<%- obj.data%>'
}
}
<% })%>
<% exports.forEach((key) => { %>
import * as <%= key %> from './<%= key%>'
<% }) %>
export{
<% exports.forEach((key) => { %><%= key%>,<% }) %>
}