Skip to content

Commit

Permalink
feat(vue-generator): add d.ts types declaration
Browse files Browse the repository at this point in the history
  • Loading branch information
chilingling committed May 9, 2024
1 parent 2498b98 commit 960438c
Show file tree
Hide file tree
Showing 16 changed files with 180 additions and 117 deletions.
2 changes: 1 addition & 1 deletion packages/vue-generator/.eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ module.exports = {
}
},
// 忽略 expected 中的内容
ignorePatterns: ['**/**/expected/*']
ignorePatterns: ['**/**/expected/*', '**/**.ts']
}
2 changes: 2 additions & 0 deletions packages/vue-generator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
},
"main": "dist/tiny-engine-dsl-vue.js",
"module": "dist/tiny-engine-dsl-vue.mjs",
"types": "dist/index.d.ts",
"files": [
"dist"
],
Expand Down Expand Up @@ -46,6 +47,7 @@
"fs-extra": "^10.0.1",
"prettier": "^2.6.1",
"vite": "^4.3.7",
"vite-plugin-static-copy": "^1.0.4",
"vitest": "^1.4.0",
"winston": "^3.10.0"
},
Expand Down
109 changes: 3 additions & 106 deletions packages/vue-generator/src/generator/generateApp.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,115 +13,12 @@ import {
} from '../plugins'
import CodeGenerator from './codeGenerator'

/**
* @typedef {Object} FuncType
* @property {String} type
* @property {String} value
*/

/**
* @typedef {Object} DataSource
* @property {Array.<{ id: Number, name: String, data: Object }>} list
* @property {FuncType} [dataHandler]
* @property {FuncType} [errorHandler]
* @property {FuncType} [willFetch]
*/

/**
* @typedef {Object} GlobalStateItem
* @property {String} id
* @property {Object<String, any>} state
* @property {Object.<String, Object.<{ type: "JSFunction", value: String }>>} actions
* @property {Object.<String, Object.<{ type: "JSFunction", value: String }>>} getters
*/

/**
* @typedef {Object} SchemaChildrenItem
* @property {SchemaChildrenItem} children
* @property {String} componentName
* @property {String} id
* @property {Object.<String, any>} props
*/

/**
* @typedef {Object} PageOrBlockSchema
* @property {String} componentName
* @property {String} css
* @property {String} fileName
* @property {Object.<String, Object.<String, { type: "JSFunction", value: String }>>} lifeCycles
* @property {Object.<String, { type: "JSFunction", value: String }>} methods
* @property {Object<String, any>} props
* @property {Array<Object.<String, any>>} state
* @property {{ id: Number, isHome: Boolean, parentId: String, rootElement: String, route: String }} meta
* @property {Array.<SchemaChildrenItem>} children
* @property {{ properties: Array<Object.<String, any>>, events: Object.<String> }} [schema]
*/

/**
* @typedef {Object} FolderItem
* @property {String} componentName
* @property {Number} depth
* @property {String} folderName
* @property {Number} id
* @property {String} parentId
* @property {String} router
*/

/**
* @typedef {Object} ComponentMapItem
* @property {String} componentName
* @property {Boolean} destructuring
* @property {String} [exportName]
* @property {String} [package]
* @property {String} [main]
* @property {String} version
*/

/**
* @typedef {Object} MetaInfo 应用APP 元信息
* @property {String} name
* @property {String} description
*/

/**
* @typedef {Object} AppSchema
* @property {{en_US: Object.<string, string>, zh_CN: Object.<string, string>}} i18n 国际化数据
* @property {Array} bridge 桥接源
* @property {Array.<{ name: String, type: 'npm' | 'function', content: Object }>} utils 工具类
* @property {DataSource} dataSource 数据源
* @property {Array<GlobalStateItem>} globalState 全局状态
* @property {Array.<PageOrBlockSchema | FolderItem>} pageSchema 页面 schema
* @property {Array.<PageOrBlockSchema>} blockSchema 区块 schema
* @property {Array.<ComponentMapItem>} componentsMap 物料 package 信息
* @property {MetaInfo} meta 应用元信息
*/

/**
* @typedef PluginConfig
* @property {Object.<string, any>} [template]
* @property {Object.<string, any>} [block]
* @property {Object.<string, any>} [page]
* @property {Object.<string, any>} [dataSource]
* @property {Object.<string, any>} [dependencies]
* @property {Object.<string, any>} [i18n]
* @property {Object.<string, any>} [router]
* @property {Object.<string, any>} [utils]
*/

/**
* @typedef {Object} GenerateAppOptions
* @property {Object.<String, any>} customPlugins
* @property {Object.<String, any>} customContext
* @property {PluginConfig} pluginConfig
* @property {Boolean} tolerateError
*/

/**
* 整体应用出码
* @param {GenerateAppOptions} config
* @param {Object.<string, any>} context
* @returns {Promise<String>}
* @param {tinyEngineDslVue.IConfig} config
* @returns {tinyEngineDslVue.codeGenInstance}
*/

export function generateApp(config = {}) {
const defaultPlugins = {
template: genTemplatePlugin(config.pluginConfig?.template || {}),
Expand Down
151 changes: 151 additions & 0 deletions packages/vue-generator/src/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
declare namespace tinyEngineDslVue {
type defaultPlugins =
| 'template'
| 'block'
| 'page'
| 'dataSource'
| 'dependencies'
| 'globalState'
| 'i18n'
| 'router'
| 'utils'
| 'formatCode'
| 'parseSchema'

type IPluginFun = (schema: IAppSchema, context: IContext) => void

interface IConfig {
customPlugins?: {
[key in defaultPlugins]?: IPluginFun
} & {
[key in 'transformStart' | 'transform' | 'transformEnd']?: Array<IPluginFun>
}
pluginConfig?: {
[k in defaultPlugins]: Record<string, any>
}
customContext?: Record<string, any>
}

interface IContext {
config: Record<string, any>
genResult: Array<IFile>
genLogs: Array<any>
error: Array<any>
}

export function generateApp(config?: IConfig): codeGenInstance

interface codeGenInstance {
generate(IAppSchema): ICodeGenResult
}

interface ICodeGenResult {
errors: Array<any>
genResult: Array<IFile>
genLogs: Array<any>
}

interface IFile {
fileType: string
fileName: string
path: string
fileContent: string
}

interface IAppSchema {
i18n: {
en_US: Record<string, any>
zh_CN: Record<string, any>
}
utils: Array<IUtilsItem>
dataSource: IDataSource
globalState: Array<IGlobalStateItem>
pageSchema: Array<IPageSchema | IFolderItem>
blockSchema: Array<IPageSchema>
componentsMap: Array<IComponentMapItem>
meta: IMetaInfo
}

interface IUtilsItem {
name: string
type: 'npm' | 'function'
content: object
}

interface IDataSource {
list: Array<{ id: number; name: string; data: object }>
dataHandler?: IFuncType
errorHandler?: IFuncType
willFetch?: IFuncType
}

interface IFuncType {
type: 'JSFunction'
value: string
}

interface IExpressionType {
type: 'JSExpression'
value: string
}

interface IGlobalStateItem {
id: string
state: Record<string, any>
actions: Record<string, IFuncType>
getters: Record<string, IFuncType>
}

interface IPageSchema {
componentName: 'Page' | 'Block'
css: string
fileName: string
lifeCycles: {
[key: string]: Record<string, IFuncType>
}
methods: Record<string, IFuncType>
props: Record<string, any>
state: Array<Record<string, any>>
meta: {
id: number
isHome: boolean
parentId: string
rootElement: string
route: string
}
children: Array<ISchemaChildrenItem>
schema?: {
properties: Array<Record<string, any>>
events: Record<string, any>
}
}

interface IFolderItem {
componentName: 'Folder'
depth: number
folderName: string
id: string
parentId: string
router: string
}

interface ISchemaChildrenItem {
children: Array<ISchemaChildrenItem>
componentName: string
id: string
props: Record<string, any>
}

interface IComponentMapItem {
componentName: string
destructuring: boolean
exportName?: string
package?: string
version: string
}

interface IMetaInfo {
name: string
description: string
}
}
2 changes: 2 additions & 0 deletions packages/vue-generator/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
*
*/

/// <reference path="index.d.ts">

export {
generateCode,
generateBlocksCode,
Expand Down
2 changes: 1 addition & 1 deletion packages/vue-generator/src/plugins/formatCodePlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function formatCode(options = {}) {
description: 'transform block schema to code',
/**
* 格式化出码
* @param {import('../generator/generateApp').AppSchema} schema
* @param {tinyEngineDslVue.IAppSchema} schema
* @returns
*/
run(schema, context) {
Expand Down
2 changes: 1 addition & 1 deletion packages/vue-generator/src/plugins/genBlockPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function genBlockPlugin(options = {}) {
description: 'transform block schema to code',
/**
* 将区块 schema 转换成高代码
* @param {import('../generator/generateApp').AppSchema} schema
* @param {tinyEngineDslVue.IAppSchema} schema
* @returns
*/
run(schema) {
Expand Down
2 changes: 1 addition & 1 deletion packages/vue-generator/src/plugins/genDataSourcePlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function genDataSourcePlugin(options = {}) {
description: 'transform schema to dataSource plugin',
/**
* 转换 dataSource
* @param {import('../generator/generateApp').AppSchema} schema
* @param {tinyEngineDslVue.IAppSchema} schema
* @returns
*/
run(schema) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ function genDependenciesPlugin(options = {}) {
description: 'transform dependencies to package.json',
/**
* 分析依赖,写入 package.json
* @param {import('../generator/generateApp').AppSchema} schema
* @param {tinyEngineDslVue.IAppSchema} schema
* @returns
*/
run(schema) {
Expand Down
2 changes: 1 addition & 1 deletion packages/vue-generator/src/plugins/genGlobalState.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function genDependenciesPlugin(options = {}) {
description: 'transform schema to globalState',
/**
* 转换 globalState
* @param {import('../generator/generateApp').AppSchema} schema
* @param {tinyEngineDslVue.IAppSchema} schema
* @returns
*/
run(schema) {
Expand Down
2 changes: 1 addition & 1 deletion packages/vue-generator/src/plugins/genI18nPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function genI18nPlugin(options = {}) {
description: 'transform i18n schema to i18n code plugin',
/**
* 将国际化 schema 转换成 i18n 高代码
* @param {import('../generator/generateApp').AppSchema} schema
* @param {tinyEngineDslVue.IAppSchema} schema
* @returns
*/
run(schema) {
Expand Down
2 changes: 1 addition & 1 deletion packages/vue-generator/src/plugins/genPagePlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function genPagePlugin(options = {}) {
description: 'transform page schema to code',
/**
* 将页面 schema 转换成高代码
* @param {import('../generator/generateApp').AppSchema} schema
* @param {tinyEngineDslVue.IAppSchema} schema
* @returns
*/
run(schema) {
Expand Down
2 changes: 1 addition & 1 deletion packages/vue-generator/src/plugins/genRouterPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function genRouterPlugin(options = {}) {
description: 'transform router schema to router code plugin',
/**
* 根据页面生成路由配置
* @param {import('../generator/generateApp').AppSchema} schema
* @param {tinyEngineDslVue.IAppSchema} schema
* @returns
*/
run(schema) {
Expand Down
2 changes: 1 addition & 1 deletion packages/vue-generator/src/plugins/genUtilsPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function genUtilsPlugin(options = {}) {
description: 'transform utils schema to utils code',
/**
* 生成 utils 源码
* @param {import('../generator/generateApp').AppSchema} schema
* @param {tinyEngineDslVue.IAppSchema} schema
* @returns
*/
run(schema) {
Expand Down
2 changes: 1 addition & 1 deletion packages/vue-generator/src/plugins/parseSchemaPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ function parseSchema() {

/**
* 解析schema,预处理 schema
* @param {import('../generator/generateApp').AppSchema} schema
* @param {tinyEngineDslVue.IAppSchema} schema
* @returns
*/
run(schema) {
Expand Down
Loading

0 comments on commit 960438c

Please sign in to comment.