moyan-file-model
v1.0.10
Published
File model manager
Downloads
22
Readme
moyan-file-model
代码生成库
安装
$ npm i moyan-file-model
演示
演示目录结果
main.ts
api.ts
modules.ts
view
api.ejs
modules.ejs
main.ts
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)
})
app.ts
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)
}
}
}
modules.ts
import { Temp, Template, smfType } from 'moyan-file-model'
export default class ModulesTemplate extends Temp implements Template {
public exports:Array<string> = []
constructor(options: ModulesTemplate['exports']) {
super(smfType.ts)
this.exports = options
}
public async run() {
return await this.render('modules')
}
}
view/api.ejs
<% options.forEach((obj) => { %>
/**
* <%= obj.describe %>
*/
export class <%= obj.className %> {
type = '<%= obj.type%>'
getData(){
return '<%- obj.data%>'
}
}
<% })%>
view/modules.ejs
<% exports.forEach((key) => { %>
import * as <%= key %> from './<%= key%>'
<% }) %>
export{
<% exports.forEach((key) => { %><%= key%>,<% }) %>
}