Skip to content
Appearance
DSL代码生成
generateCode
基本信息
Path: /generateCode
Method: GET
接口描述:
作为npm包使用generateCode方法生成代码示例:
const path = require('path')
const fs = require('fs')
const { generateCode } = require('@opentiny/tiny-engine-dsl-ng-tiny/lib/generate-code.js')
const result = generateCode({ pageInfo, blocksData })
result.forEach((item) => fs.writeFileSync(`dist/${item.filePath}/${item.panelName}`, item.panelValue))
输入示例: { pageInfo, blocksData };
// pageInfo为页面的schema信息,
// pageInfo: { schema, name });
{
name: 'page1',
schema: {
// 页面schema
}
}
// blocksData为页面引用的区块的schema数据
// blocksData: Array<{ label, content }>
[
{
label: 'image-title',
content: {
// 区块的schema
}
},
{
// 其他区块...
}
]
输出示例:
[
{
"panelName": "page1.component.html", // 文件名
"panelValue": "xxx", // 生成代码的内容
"panelType": "html", // 生成代码的文件类型:html、css、ts
"prettierOpt": { "parser": "html", "tabWidth": 2, "printWidth": 120 }, // prettier格式化选项
"type": "page", // 生成代码类型:page、block、service
"filePath": "pages/page1" // 生成代码文件的相对目录路径
},
{
"panelName": "page1.component.ts",
"panelValue": "xxx",
"panelType": "ts",
"prettierOpt": { "parser": "typescript", "tabWidth": 2, "printWidth": 120 },
"type": "page",
"filePath": "pages/page1"
},
{
"panelName": "block1.component.ts",
"type": "block",
"panelType": "ts",
"prettierOpt": { "parser": "typescript", "tabWidth": 2, "printWidth": 120 },
"filePath": "blocks/block1",
"panelValue": "xxx"
},
{
"panelName": "fetch.service.ts",
"panelType": "ts",
"prettierOpt": { "parser": "typescript", "tabWidth": 2, "printWidth": 120 },
"type": "service",
"filePath": "service",
"panelValue": "xxx"
},
// ...
]
请求参数
返回数据
| 名称 | 类型 | 是否必须 | 默认值 | 备注 | 其他信息 |
|---|---|---|---|---|---|
| object [] | 非必须 | item 类型: object | |||
| ├─ panelName | string | 必须 | 文件名 | ||
| ├─ panelValue | string | 必须 | 文件文本 | ||
| ├─ panelType | string | 必须 | 代码类型 | ||
| ├─ prettierOpt | object | 必须 | 代码美化选项 | ||
| ├─ parser | string | 非必须 | 指定要使用的解析器 | ||
| ├─ tabWidth | number | 非必须 | 指定使用几个空格来表示一个制表符(Tab) | ||
| ├─ printWidth | number | 非必须 | 指定每行代码的最大列数 | ||
| ├─ type | string | 必须 | 代码类型 | ||
| ├─ filePath | string | 必须 | 文件路径 |