Skip to content

Commit deeeed2

Browse files
LeoYuaneternalsky
authored andcommitted
feat: support projectRemark which added in globalContextData
1 parent 995b11a commit deeeed2

File tree

5 files changed

+52
-14
lines changed

5 files changed

+52
-14
lines changed

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

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -90,31 +90,34 @@ export class ProjectBuilder implements IProjectBuilder {
9090
async generateProject(originalSchema: ProjectSchema | string): Promise<ResultDir> {
9191
// Init
9292
const { schemaParser } = this;
93-
const builders = this.createModuleBuilders();
9493

9594
const projectRoot = await this.template.generateTemplate();
9695

9796
let schema: ProjectSchema =
9897
typeof originalSchema === 'string' ? JSON.parse(originalSchema) : originalSchema;
9998

100-
// Validate
101-
if (!schemaParser.validate(schema)) {
102-
throw new CodeGeneratorError('Schema is invalid');
103-
}
104-
10599
// Parse / Format
106-
107100
// Preprocess
108101
for (const preProcessor of this.projectPreProcessors) {
109102
// eslint-disable-next-line no-await-in-loop
110103
schema = await preProcessor(schema);
111104
}
112105

106+
// Validate
107+
if (!schemaParser.validate(schema)) {
108+
throw new CodeGeneratorError('Schema is invalid');
109+
}
110+
113111
// Collect Deps
114112
// Parse JSExpression
115113
const parseResult: IParseResult = schemaParser.parse(schema);
116114
let buildResult: IModuleInfo[] = [];
117115

116+
const builders = this.createModuleBuilders({
117+
extraContextData: {
118+
projectRemark: parseResult?.project?.projectRemark,
119+
},
120+
});
118121
// Generator Code module
119122
// components
120123
// pages
@@ -253,11 +256,12 @@ export class ProjectBuilder implements IProjectBuilder {
253256
// TODO: 更多 slots 的处理??是不是可以考虑把 template 中所有的 slots 都处理下?
254257

255258
// Post Process
256-
259+
const isSingleComponent = parseResult?.project?.projectRemark?.isSingleComponent;
257260
// Combine Modules
258261
buildResult.forEach((moduleInfo) => {
259262
let targetDir = getDirFromRoot(projectRoot, moduleInfo.path);
260-
if (moduleInfo.moduleName) {
263+
// if project only contain single component, skip creation of directory.
264+
if (moduleInfo.moduleName && !isSingleComponent) {
261265
const dir = createResultDir(moduleInfo.moduleName);
262266
addDirectory(targetDir, dir);
263267
targetDir = dir;
@@ -278,7 +282,8 @@ export class ProjectBuilder implements IProjectBuilder {
278282
return finalResult;
279283
}
280284

281-
private createModuleBuilders(): Record<string, IModuleBuilder> {
285+
private createModuleBuilders(extraContextData: Record<string, unknown> = {}):
286+
Record<string, IModuleBuilder> {
282287
const builders: Record<string, IModuleBuilder> = {};
283288

284289
Object.keys(this.plugins).forEach((pluginName) => {
@@ -291,10 +296,12 @@ export class ProjectBuilder implements IProjectBuilder {
291296
plugins: this.plugins[pluginName],
292297
postProcessors: this.postProcessors,
293298
contextData: {
299+
// template: this.template,
294300
inStrictMode: this.inStrictMode,
295301
tolerateEvalErrors: true,
296302
evalErrorsHandler: '',
297303
...this.extraContextData,
304+
...extraContextData,
298305
},
299306
...options,
300307
});

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import { handleSubNodes, isValidContainerType } from '../utils/schema';
3636
import { uniqueArray } from '../utils/common';
3737
import { componentAnalyzer } from '../analyzer/componentAnalyzer';
3838
import { ensureValidClassName } from '../utils/validate';
39+
import type { ProjectRemark } from '../types/intermediate';
3940

4041
const defaultContainer: IContainerInfo = {
4142
containerType: 'Component',
@@ -319,10 +320,17 @@ export class SchemaParser implements ISchemaParser {
319320
utilsDeps,
320321
packages: npms || [],
321322
dataSourcesTypes: this.collectDataSourcesTypes(schema),
323+
projectRemark: this.getProjectRemark(containers),
322324
},
323325
};
324326
}
325327

328+
getProjectRemark(containers: IContainerInfo[]): ProjectRemark {
329+
return {
330+
isSingleComponent: containers.length === 1 && containers[0].containerType === 'Component',
331+
};
332+
}
333+
326334
getComponentNames(children: NodeDataType): string[] {
327335
return handleSubNodes<string>(
328336
children,

modules/code-generator/src/plugins/component/react/containerInjectDataSourceEngine.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import {
2727

2828
import { generateCompositeType } from '../../../utils/compositeType';
2929
import { parseExpressionConvertThis2Context } from '../../../utils/expressionParser';
30-
import { isContainerSchema } from '../../../utils/schema';
30+
import { isValidContainerType } from '../../../utils/schema';
3131
import { REACT_CHUNK_NAME } from './const';
3232

3333
export interface PluginConfig {
@@ -67,7 +67,7 @@ const pluginFactory: BuilderComponentPluginFactory<PluginConfig> = (config?) =>
6767
};
6868

6969
const scope = Scope.createRootScope();
70-
const dataSourceConfig = isContainerSchema(pre.ir) ? pre.ir.dataSource : null;
70+
const dataSourceConfig = isValidContainerType(pre.ir) ? pre.ir.dataSource : null;
7171
const dataSourceItems: InterpretDataSourceConfig[] =
7272
(dataSourceConfig && dataSourceConfig.list) || [];
7373
const dataSourceEngineOptions = { runtimeConfig: true };

modules/code-generator/src/plugins/component/react/containerInjectUtils.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
FileType,
1212
ICodeStruct,
1313
IContainerInfo,
14+
IProjectTemplate,
1415
} from '../../../types';
1516

1617
export interface PluginConfig {
@@ -32,6 +33,19 @@ const pluginFactory: BuilderComponentPluginFactory<PluginConfig> = (config?) =>
3233
next.contextData.useRefApi = true;
3334
const useRef = !!ir.analyzeResult?.isUsingRef;
3435

36+
// const isSingleComponent = next.contextData?.projectRemark?.isSingleComponent;
37+
// const template = next.contextData?.template;
38+
39+
// function getRelativeUtilsPath(template: IProjectTemplate, isSingleComponent: boolean) {
40+
// let relativeUtilsPath = '../../utils';
41+
// const utilsPath = template.slots.utils.path;
42+
// if (ir.containerType === 'Component') {
43+
// // TODO: isSingleComponent
44+
// relativeUtilsPath = getRelativePath(template.slots.components.path.join('/'), utilsPath.join('/'));
45+
// }
46+
// return relativeUtilsPath;
47+
// }
48+
3549
next.chunks.push({
3650
type: ChunkType.STRING,
3751
fileType: cfg.fileType,
@@ -89,15 +103,15 @@ const pluginFactory: BuilderComponentPluginFactory<PluginConfig> = (config?) =>
89103
type: ChunkType.STRING,
90104
fileType: cfg.fileType,
91105
name: CLASS_DEFINE_CHUNK_NAME.InsMethod,
92-
content: ` $ = () => null; `,
106+
content: ' $ = () => null; ',
93107
linkAfter: [...DEFAULT_LINK_AFTER[CLASS_DEFINE_CHUNK_NAME.InsMethod]],
94108
});
95109

96110
next.chunks.push({
97111
type: ChunkType.STRING,
98112
fileType: cfg.fileType,
99113
name: CLASS_DEFINE_CHUNK_NAME.InsMethod,
100-
content: ` $$ = () => []; `,
114+
content: ' $$ = () => []; ',
101115
linkAfter: [...DEFAULT_LINK_AFTER[CLASS_DEFINE_CHUNK_NAME.InsMethod]],
102116
});
103117
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ export interface IRouterInfo extends IWithDependency {
3333
}>;
3434
}
3535

36+
/**
37+
* project's remarks
38+
*/
39+
export interface ProjectRemark {
40+
/** if current project only contain one container which type is `Component` */
41+
isSingleComponent?: boolean;
42+
}
43+
3644
export interface IProjectInfo {
3745
css?: string;
3846
containersDeps?: IDependency[];
@@ -43,6 +51,7 @@ export interface IProjectInfo {
4351
meta?: { name?: string; title?: string } | Record<string, any>;
4452
config?: Record<string, any>;
4553
dataSourcesTypes?: string[];
54+
projectRemark?: ProjectRemark;
4655
}
4756

4857
export interface IPageMeta {

0 commit comments

Comments
 (0)