GeezerVison is showing github.com — feat: support UIPaaS-Component code generator solution · skymysky/lowcode-engine@1ca940c
File treeExpand file treeCollapse file tree Open diff view settings modules/code-generator/src
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: modules/code-generator/src/cli/solutions/example-solution.ts +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -559,8 +559,8 @@ codealike.json | 559 | 559 | "registry": "https://registry.npm.xxx.com" | 560 | 560 | }, | 561 | 561 | "dependencies": { | 562 | | - "@alilc/lowcode-code-generator": "^1.0.0-beta.16", | 563 | | - "@alilc/lowcode-types": "^1.0.0-beta.21", | | 562 | + "@alilc/lowcode-code-generator": "^1.0.0", | | 563 | + "@alilc/lowcode-types": "^1.0.0", | 564 | 564 | "tslib": "^2.3.0" | 565 | 565 | }, | 566 | 566 | "devDependencies": { | |
Collapse file Copy file name to clipboardExpand all lines: modules/code-generator/src/generator/ProjectBuilder.ts +6-3Lines changed: 6 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -62,10 +62,10 @@ export class ProjectBuilder implements IProjectBuilder { | 62 | 62 | private projectPostProcessors: ProjectPostProcessor[]; | 63 | 63 |
| 64 | 64 | /** 是否处于严格模式 */ | 65 | | - public readonly inStrictMode: boolean; | | 65 | + readonly inStrictMode: boolean; | 66 | 66 |
| 67 | 67 | /** 一些额外的上下文数据 */ | 68 | | - public readonly extraContextData: IContextData; | | 68 | + readonly extraContextData: IContextData; | 69 | 69 |
| 70 | 70 | constructor({ | 71 | 71 | template, | @@ -260,7 +260,10 @@ export class ProjectBuilder implements IProjectBuilder { | 260 | 260 | let finalResult = projectRoot; | 261 | 261 | for (const projectPostProcessor of this.projectPostProcessors) { | 262 | 262 | // eslint-disable-next-line no-await-in-loop | 263 | | - finalResult = await projectPostProcessor(finalResult, schema, originalSchema); | | 263 | + finalResult = await projectPostProcessor(finalResult, schema, originalSchema, { | | 264 | + template: this.template, | | 265 | + parseResult, | | 266 | + }); | 264 | 267 | } | 265 | 268 |
| 266 | 269 | return finalResult; | |
Collapse file Copy file name to clipboardExpand all lines: modules/code-generator/src/parser/SchemaParser.ts +3-4Lines changed: 3 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -32,7 +32,7 @@ import { | 32 | 32 | import { SUPPORT_SCHEMA_VERSION_LIST } from '../const'; | 33 | 33 |
| 34 | 34 | import { getErrorMessage } from '../utils/errors'; | 35 | | -import { handleSubNodes } from '../utils/schema'; | | 35 | +import { handleSubNodes, isValidContainerType } from '../utils/schema'; | 36 | 36 | import { uniqueArray } from '../utils/common'; | 37 | 37 | import { componentAnalyzer } from '../analyzer/componentAnalyzer'; | 38 | 38 | import { ensureValidClassName } from '../utils/validate'; | @@ -141,7 +141,7 @@ export class SchemaParser implements ISchemaParser { | 141 | 141 | if (schema.componentsTree.length > 0) { | 142 | 142 | const firstRoot: ContainerSchema = schema.componentsTree[0] as ContainerSchema; | 143 | 143 |
| 144 | | - if (!('fileName' in firstRoot) || !firstRoot.fileName) { | | 144 | + if (!firstRoot.fileName && !isValidContainerType(firstRoot)) { | 145 | 145 | // 整个 schema 描述一个容器,且无根节点定义 | 146 | 146 | const container: IContainerInfo = { | 147 | 147 | ...firstRoot, | @@ -259,8 +259,7 @@ export class SchemaParser implements ISchemaParser { | 259 | 259 | utils = schema.utils; | 260 | 260 | utilsDeps = schema.utils | 261 | 261 | .filter( | 262 | | - (u): u is { name: string; type: 'npm' | 'tnpm'; content: NpmInfo } => | 263 | | - u.type !== 'function', | | 262 | + (u): u is { name: string; type: 'npm' | 'tnpm'; content: NpmInfo } => u.type !== 'function', | 264 | 263 | ) | 265 | 264 | .map( | 266 | 265 | (u): IExternalDependency => ({ | |
Collapse file Copy file name to clipboardExpand all lines: modules/code-generator/src/postprocessor/prettier/index.ts +3-1Lines changed: 3 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -9,7 +9,7 @@ const PARSERS = ['css', 'scss', 'less', 'json', 'html', 'vue']; | 9 | 9 |
| 10 | 10 | export interface ProcessorConfig { | 11 | 11 | customFileTypeParser: Record<string, string>; | 12 | | - plugins?: Array<prettier.Plugin>; | | 12 | + plugins?: prettier.Plugin[]; | 13 | 13 | } | 14 | 14 |
| 15 | 15 | const factory: PostProcessorFactory<ProcessorConfig> = (config?: ProcessorConfig) => { | @@ -33,6 +33,8 @@ const factory: PostProcessorFactory<ProcessorConfig> = (config?: ProcessorConfig | 33 | 33 | return prettier.format(content, { | 34 | 34 | parser, | 35 | 35 | plugins: [parserBabel, parserPostCss, parserHtml, ...(cfg.plugins || [])], | | 36 | + singleQuote: true, | | 37 | + jsxSingleQuote: false, | 36 | 38 | }); | 37 | 39 | }; | 38 | 40 |
| |
Collapse file Copy file name to clipboardExpand all lines: modules/code-generator/src/types/core.ts +6Lines changed: 6 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -170,11 +170,17 @@ export interface IProjectBuilder { | 170 | 170 | /** 项目级别的前置处理器 */ | 171 | 171 | export type ProjectPreProcessor = (schema: ProjectSchema) => Promise<ProjectSchema> | ProjectSchema; | 172 | 172 |
| | 173 | +export interface ProjectPostProcessorOptions { | | 174 | + parseResult?: IParseResult; | | 175 | + template?: IProjectTemplate; | | 176 | +} | | 177 | + | 173 | 178 | /** 项目级别的后置处理器 */ | 174 | 179 | export type ProjectPostProcessor = ( | 175 | 180 | result: ResultDir, | 176 | 181 | schema: ProjectSchema, | 177 | 182 | originalSchema: ProjectSchema | string, | | 183 | + options: ProjectPostProcessorOptions, | 178 | 184 | ) => Promise<ResultDir> | ResultDir; | 179 | 185 |
| 180 | 186 | /** 模块级别的后置处理器的工厂方法 */ | |
Collapse file Copy file name to clipboardExpand all lines: modules/code-generator/src/utils/dataSource.ts +4-4Lines changed: 4 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import changeCase from 'change-case'; | 2 | 2 | import type { IProjectInfo } from '../types/intermediate'; | 3 | 3 |
| 4 | | -export type DataSourceDependenciesConfig = { | | 4 | +export interface DataSourceDependenciesConfig { | 5 | 5 | /** 数据源引擎的版本 */ | 6 | 6 | engineVersion?: string; | 7 | 7 | /** 数据源引擎的包名 */ | @@ -14,21 +14,21 @@ export type DataSourceDependenciesConfig = { | 14 | 14 | handlersPackages?: { | 15 | 15 | [key: string]: string; | 16 | 16 | }; | 17 | | -}; | | 17 | +} | 18 | 18 |
| 19 | 19 | export function buildDataSourceDependencies( | 20 | 20 | ir: IProjectInfo, | 21 | 21 | cfg: DataSourceDependenciesConfig = {}, | 22 | 22 | ): Record<string, string> { | 23 | 23 | return { | 24 | 24 | // 数据源引擎的依赖包 | 25 | | - [cfg.enginePackage || '@alilc/lowcode-datasource-engine']: cfg.engineVersion || 'latest', | | 25 | + [cfg.enginePackage || '@alilc/lowcode-datasource-engine']: cfg.engineVersion || '^1.0.0', | 26 | 26 |
| 27 | 27 | // 各种数据源的 handlers 的依赖包 | 28 | 28 | ...(ir.dataSourcesTypes || []).reduce( | 29 | 29 | (acc, dsType) => ({ | 30 | 30 | ...acc, | 31 | | - [getDataSourceHandlerPackageName(dsType)]: cfg.handlersVersion?.[dsType] || 'latest', | | 31 | + [getDataSourceHandlerPackageName(dsType)]: cfg.handlersVersion?.[dsType] || '^1.0.0', | 32 | 32 | }), | 33 | 33 | {}, | 34 | 34 | ), | |
Collapse file Copy file name to clipboardExpand all lines: modules/code-generator/src/utils/index.ts +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -11,6 +11,7 @@ import * as schema from './schema'; | 11 | 11 | import * as version from './version'; | 12 | 12 | import * as scope from './Scope'; | 13 | 13 | import * as expressionParser from './expressionParser'; | | 14 | +import * as dataSource from './dataSource'; | 14 | 15 |
| 15 | 16 | export { | 16 | 17 | common, | @@ -25,4 +26,5 @@ export { | 25 | 26 | version, | 26 | 27 | scope, | 27 | 28 | expressionParser, | | 29 | + dataSource, | 28 | 30 | }; |
Collapse file Copy file name to clipboardExpand all lines: modules/code-generator/src/utils/nodeToJSX.ts +3-5Lines changed: 3 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -182,7 +182,7 @@ function generateSimpleNode( | 182 | 182 | function linkPieces(pieces: CodePiece[]): string { | 183 | 183 | const tagsPieces = pieces.filter((p) => p.type === PIECE_TYPE.TAG); | 184 | 184 | if (tagsPieces.length !== 1) { | 185 | | - throw new CodeGeneratorError('One node only need one tag define'); | | 185 | + throw new CodeGeneratorError('Only one tag definition required', tagsPieces); | 186 | 186 | } | 187 | 187 | const tagName = tagsPieces[0].value; | 188 | 188 |
| @@ -270,8 +270,7 @@ export function generateReactLoopCtrl( | 270 | 270 | const loopDataExpr = pipe( | 271 | 271 | nodeItem.loop, | 272 | 272 | // 将 JSExpression 转换为 JS 表达式代码: | 273 | | - (expr) => | 274 | | - generateCompositeType(expr, scope, { | | 273 | + (expr) => generateCompositeType(expr, scope, { | 275 | 274 | handlers: config?.handlers, | 276 | 275 | tolerateEvalErrors: false, // 这个内部不需要包 try catch, 下面会统一加的 | 277 | 276 | }), | @@ -391,8 +390,7 @@ export function createNodeGenerator(cfg: NodeGeneratorConfig = {}): NodeGenerato | 391 | 390 | return `{${valueStr}}`; | 392 | 391 | }; | 393 | 392 |
| 394 | | - return (nodeItem: NodeDataType, scope: IScope) => | 395 | | - unwrapJsExprQuoteInJsx(generateNode(nodeItem, scope)); | | 393 | + return (nodeItem: NodeDataType, scope: IScope) => unwrapJsExprQuoteInJsx(generateNode(nodeItem, scope)); | 396 | 394 | } | 397 | 395 |
| 398 | 396 | const defaultReactGeneratorConfig: NodeGeneratorConfig = { | |
Collapse file Copy file name to clipboardExpand all lines: modules/code-generator/src/utils/schema.ts +8Lines changed: 8 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -138,3 +138,11 @@ export function handleSubNodes<T>( | 138 | 138 | return []; | 139 | 139 | } | 140 | 140 | } | | 141 | + | | 142 | +export function isValidContainerType(schema: NodeSchema) { | | 143 | + return [ | | 144 | + 'Page', | | 145 | + 'Component', | | 146 | + 'Block', | | 147 | + ].includes(schema.componentName); | | 148 | +} |
|