Skip to content

Commit 1ca940c

Browse files
LeoYuaneternalsky
authored andcommitted
feat: support UIPaaS-Component code generator solution
1 parent 82b6a01 commit 1ca940c

9 files changed

Lines changed: 37 additions & 19 deletions

File tree

modules/code-generator/src/cli/solutions/example-solution.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -559,8 +559,8 @@ codealike.json
559559
"registry": "https://registry.npm.xxx.com"
560560
},
561561
"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",
564564
"tslib": "^2.3.0"
565565
},
566566
"devDependencies": {

modules/code-generator/src/generator/ProjectBuilder.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,10 @@ export class ProjectBuilder implements IProjectBuilder {
6262
private projectPostProcessors: ProjectPostProcessor[];
6363

6464
/** 是否处于严格模式 */
65-
public readonly inStrictMode: boolean;
65+
readonly inStrictMode: boolean;
6666

6767
/** 一些额外的上下文数据 */
68-
public readonly extraContextData: IContextData;
68+
readonly extraContextData: IContextData;
6969

7070
constructor({
7171
template,
@@ -260,7 +260,10 @@ export class ProjectBuilder implements IProjectBuilder {
260260
let finalResult = projectRoot;
261261
for (const projectPostProcessor of this.projectPostProcessors) {
262262
// 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+
});
264267
}
265268

266269
return finalResult;

modules/code-generator/src/parser/SchemaParser.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import {
3232
import { SUPPORT_SCHEMA_VERSION_LIST } from '../const';
3333

3434
import { getErrorMessage } from '../utils/errors';
35-
import { handleSubNodes } from '../utils/schema';
35+
import { handleSubNodes, isValidContainerType } from '../utils/schema';
3636
import { uniqueArray } from '../utils/common';
3737
import { componentAnalyzer } from '../analyzer/componentAnalyzer';
3838
import { ensureValidClassName } from '../utils/validate';
@@ -141,7 +141,7 @@ export class SchemaParser implements ISchemaParser {
141141
if (schema.componentsTree.length > 0) {
142142
const firstRoot: ContainerSchema = schema.componentsTree[0] as ContainerSchema;
143143

144-
if (!('fileName' in firstRoot) || !firstRoot.fileName) {
144+
if (!firstRoot.fileName && !isValidContainerType(firstRoot)) {
145145
// 整个 schema 描述一个容器,且无根节点定义
146146
const container: IContainerInfo = {
147147
...firstRoot,
@@ -259,8 +259,7 @@ export class SchemaParser implements ISchemaParser {
259259
utils = schema.utils;
260260
utilsDeps = schema.utils
261261
.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',
264263
)
265264
.map(
266265
(u): IExternalDependency => ({

modules/code-generator/src/postprocessor/prettier/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const PARSERS = ['css', 'scss', 'less', 'json', 'html', 'vue'];
99

1010
export interface ProcessorConfig {
1111
customFileTypeParser: Record<string, string>;
12-
plugins?: Array<prettier.Plugin>;
12+
plugins?: prettier.Plugin[];
1313
}
1414

1515
const factory: PostProcessorFactory<ProcessorConfig> = (config?: ProcessorConfig) => {
@@ -33,6 +33,8 @@ const factory: PostProcessorFactory<ProcessorConfig> = (config?: ProcessorConfig
3333
return prettier.format(content, {
3434
parser,
3535
plugins: [parserBabel, parserPostCss, parserHtml, ...(cfg.plugins || [])],
36+
singleQuote: true,
37+
jsxSingleQuote: false,
3638
});
3739
};
3840

modules/code-generator/src/types/core.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,11 +170,17 @@ export interface IProjectBuilder {
170170
/** 项目级别的前置处理器 */
171171
export type ProjectPreProcessor = (schema: ProjectSchema) => Promise<ProjectSchema> | ProjectSchema;
172172

173+
export interface ProjectPostProcessorOptions {
174+
parseResult?: IParseResult;
175+
template?: IProjectTemplate;
176+
}
177+
173178
/** 项目级别的后置处理器 */
174179
export type ProjectPostProcessor = (
175180
result: ResultDir,
176181
schema: ProjectSchema,
177182
originalSchema: ProjectSchema | string,
183+
options: ProjectPostProcessorOptions,
178184
) => Promise<ResultDir> | ResultDir;
179185

180186
/** 模块级别的后置处理器的工厂方法 */

modules/code-generator/src/utils/dataSource.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import changeCase from 'change-case';
22
import type { IProjectInfo } from '../types/intermediate';
33

4-
export type DataSourceDependenciesConfig = {
4+
export interface DataSourceDependenciesConfig {
55
/** 数据源引擎的版本 */
66
engineVersion?: string;
77
/** 数据源引擎的包名 */
@@ -14,21 +14,21 @@ export type DataSourceDependenciesConfig = {
1414
handlersPackages?: {
1515
[key: string]: string;
1616
};
17-
};
17+
}
1818

1919
export function buildDataSourceDependencies(
2020
ir: IProjectInfo,
2121
cfg: DataSourceDependenciesConfig = {},
2222
): Record<string, string> {
2323
return {
2424
// 数据源引擎的依赖包
25-
[cfg.enginePackage || '@alilc/lowcode-datasource-engine']: cfg.engineVersion || 'latest',
25+
[cfg.enginePackage || '@alilc/lowcode-datasource-engine']: cfg.engineVersion || '^1.0.0',
2626

2727
// 各种数据源的 handlers 的依赖包
2828
...(ir.dataSourcesTypes || []).reduce(
2929
(acc, dsType) => ({
3030
...acc,
31-
[getDataSourceHandlerPackageName(dsType)]: cfg.handlersVersion?.[dsType] || 'latest',
31+
[getDataSourceHandlerPackageName(dsType)]: cfg.handlersVersion?.[dsType] || '^1.0.0',
3232
}),
3333
{},
3434
),

modules/code-generator/src/utils/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import * as schema from './schema';
1111
import * as version from './version';
1212
import * as scope from './Scope';
1313
import * as expressionParser from './expressionParser';
14+
import * as dataSource from './dataSource';
1415

1516
export {
1617
common,
@@ -25,4 +26,5 @@ export {
2526
version,
2627
scope,
2728
expressionParser,
29+
dataSource,
2830
};

modules/code-generator/src/utils/nodeToJSX.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ function generateSimpleNode(
182182
function linkPieces(pieces: CodePiece[]): string {
183183
const tagsPieces = pieces.filter((p) => p.type === PIECE_TYPE.TAG);
184184
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);
186186
}
187187
const tagName = tagsPieces[0].value;
188188

@@ -270,8 +270,7 @@ export function generateReactLoopCtrl(
270270
const loopDataExpr = pipe(
271271
nodeItem.loop,
272272
// 将 JSExpression 转换为 JS 表达式代码:
273-
(expr) =>
274-
generateCompositeType(expr, scope, {
273+
(expr) => generateCompositeType(expr, scope, {
275274
handlers: config?.handlers,
276275
tolerateEvalErrors: false, // 这个内部不需要包 try catch, 下面会统一加的
277276
}),
@@ -391,8 +390,7 @@ export function createNodeGenerator(cfg: NodeGeneratorConfig = {}): NodeGenerato
391390
return `{${valueStr}}`;
392391
};
393392

394-
return (nodeItem: NodeDataType, scope: IScope) =>
395-
unwrapJsExprQuoteInJsx(generateNode(nodeItem, scope));
393+
return (nodeItem: NodeDataType, scope: IScope) => unwrapJsExprQuoteInJsx(generateNode(nodeItem, scope));
396394
}
397395

398396
const defaultReactGeneratorConfig: NodeGeneratorConfig = {

modules/code-generator/src/utils/schema.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,11 @@ export function handleSubNodes<T>(
138138
return [];
139139
}
140140
}
141+
142+
export function isValidContainerType(schema: NodeSchema) {
143+
return [
144+
'Page',
145+
'Component',
146+
'Block',
147+
].includes(schema.componentName);
148+
}

0 commit comments

Comments
 (0)