Skip to content

Commit 76341c8

Browse files
committed
fix: 解决 package.json 中误添加了没有用到的数据源类型的 handler 的包的问题 (alibaba#56)
1 parent 4a01c97 commit 76341c8

File tree

7 files changed

+206
-23
lines changed

7 files changed

+206
-23
lines changed

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,7 @@ export class SchemaParser implements ISchemaParser {
319319
containersDeps,
320320
utilsDeps,
321321
packages: npms || [],
322+
dataSourcesTypes: this.collectDataSourcesTypes(schema),
322323
},
323324
};
324325
}
@@ -350,6 +351,27 @@ export class SchemaParser implements ISchemaParser {
350351
}
351352
return schema;
352353
}
354+
355+
private collectDataSourcesTypes(schema: ProjectSchema): string[] {
356+
const dataSourcesTypes = new Set<string>();
357+
358+
// 数据源的默认类型为 fetch
359+
const defaultDataSourceType = 'fetch';
360+
361+
// 收集应用级别的数据源
362+
schema.dataSource?.list?.forEach((ds) => {
363+
dataSourcesTypes.add(ds.type || defaultDataSourceType);
364+
});
365+
366+
// 收集容器级别的数据源(页面/组件/区块)
367+
schema.componentsTree.forEach((rootNode) => {
368+
rootNode.dataSource?.list?.forEach((ds) => {
369+
dataSourcesTypes.add(ds.type || defaultDataSourceType);
370+
});
371+
});
372+
373+
return Array.from(dataSourcesTypes.values());
374+
}
353375
}
354376

355377
export default SchemaParser;

modules/code-generator/src/plugins/project/framework/icejs/plugins/packageJSON.ts

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
ICodeStruct,
1111
IProjectInfo,
1212
} from '../../../../../types';
13+
import { buildDataSourceDependencies } from '../../../../../utils/dataSource';
1314

1415
interface IIceJsPackageJSON extends PackageJSON {
1516
ideMode: {
@@ -22,7 +23,31 @@ interface IIceJsPackageJSON extends PackageJSON {
2223
originTemplate: string;
2324
}
2425

25-
const pluginFactory: BuilderComponentPluginFactory<unknown> = () => {
26+
export type IceJsPackageJsonPluginConfig = {
27+
/**
28+
* 数据源配置
29+
*/
30+
datasourceConfig?: {
31+
/** 数据源引擎的版本 */
32+
engineVersion?: string;
33+
/** 数据源引擎的包名 */
34+
enginePackage?: string;
35+
/** 数据源 handlers 的版本 */
36+
handlersVersion?: {
37+
[key: string]: string;
38+
};
39+
/** 数据源 handlers 的包名 */
40+
handlersPackages?: {
41+
[key: string]: string;
42+
};
43+
};
44+
/** 包名 */
45+
packageName?: string;
46+
/** 版本 */
47+
packageVersion?: string;
48+
};
49+
50+
const pluginFactory: BuilderComponentPluginFactory<IceJsPackageJsonPluginConfig> = (cfg) => {
2651
const plugin: BuilderComponentPlugin = async (pre: ICodeStruct) => {
2752
const next: ICodeStruct = {
2853
...pre,
@@ -31,21 +56,18 @@ const pluginFactory: BuilderComponentPluginFactory<unknown> = () => {
3156
const ir = next.ir as IProjectInfo;
3257

3358
const packageJson: IIceJsPackageJSON = {
34-
name: '@alifd/scaffold-lite-js',
35-
version: '0.1.5',
59+
name: cfg?.packageName || 'icejs-demo-app',
60+
version: cfg?.packageVersion || '0.1.5',
3661
description: '轻量级模板,使用 JavaScript,仅包含基础的 Layout。',
3762
dependencies: {
3863
moment: '^2.24.0',
3964
react: '^16.4.1',
4065
'react-dom': '^16.4.1',
4166
'@alifd/theme-design-pro': '^0.x',
42-
'@alilc/lowcode-datasource-engine': '*',
43-
// TODO: 如何动态获取下面这些依赖?
44-
'@alilc/lowcode-datasource-url-params-handler': '*',
45-
'@alilc/lowcode-datasource-fetch-handler': '*',
46-
'@alilc/lowcode-datasource-mtop-handler': '*',
47-
'@alilc/lowcode-datasource-mopen-handler': '*',
4867
'intl-messageformat': '^9.3.6',
68+
69+
// 数据源相关的依赖:
70+
...buildDataSourceDependencies(ir, cfg?.datasourceConfig),
4971
},
5072
devDependencies: {
5173
'@ice/spec': '^1.0.0',

modules/code-generator/src/plugins/project/framework/rax/plugins/packageJSON.ts

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import changeCase from 'change-case';
21
import { NpmInfo, PackageJSON } from '@alilc/lowcode-types';
32
import { COMMON_CHUNK_NAME } from '../../../../../const/generator';
43

@@ -14,6 +13,7 @@ import { isNpmInfo } from '../../../../../utils/schema';
1413
import { getErrorMessage } from '../../../../../utils/errors';
1514
import { calcCompatibleVersion } from '../../../../../utils/version';
1615
import { RaxFrameworkOptions } from '../types/RaxFrameworkOptions';
16+
import { buildDataSourceDependencies } from '../../../../../utils/dataSource';
1717

1818
const pluginFactory: BuilderComponentPluginFactory<RaxFrameworkOptions> = (cfg) => {
1919
const plugin: BuilderComponentPlugin = async (pre: ICodeStruct) => {
@@ -26,7 +26,7 @@ const pluginFactory: BuilderComponentPluginFactory<RaxFrameworkOptions> = (cfg)
2626
const npmDeps = getNpmDependencies(ir);
2727

2828
const packageJson: PackageJSON = {
29-
name: cfg?.packageName || '@alilc/rax-app-demo',
29+
name: cfg?.packageName || 'rax-demo-app',
3030
private: true,
3131
version: cfg?.packageVersion || '1.0.0',
3232
scripts: {
@@ -39,18 +39,7 @@ const pluginFactory: BuilderComponentPluginFactory<RaxFrameworkOptions> = (cfg)
3939
},
4040
dependencies: {
4141
// 数据源相关的依赖:
42-
[cfg?.datasourceConfig?.enginePackage || '@alilc/lowcode-datasource-engine']:
43-
cfg?.datasourceConfig?.engineVersion || 'latest',
44-
// TODO: [p1] 如何动态获取下究竟用了哪些类型的数据源?
45-
...['url-params', 'fetch', 'mtop', 'mopen'].reduce(
46-
(acc, dsType) => ({
47-
...acc,
48-
[cfg?.datasourceConfig?.handlersPackages?.[dsType] ||
49-
`@alilc/lowcode-datasource-${changeCase.kebab(dsType)}-handler`]:
50-
cfg?.datasourceConfig?.handlersVersion?.[dsType] || 'latest',
51-
}),
52-
{},
53-
),
42+
...buildDataSourceDependencies(ir, cfg?.datasourceConfig),
5443

5544
// 环境判断
5645
'universal-env': '^3.2.0',

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export interface IProjectInfo {
4242
packages: INpmPackage[];
4343
meta?: { name?: string; title?: string } | Record<string, any>;
4444
config?: Record<string, any>;
45+
dataSourcesTypes?: string[];
4546
}
4647

4748
export interface IPageMeta {
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import changeCase from 'change-case';
2+
import type { IProjectInfo } from '../types/intermediate';
3+
4+
export type DataSourceDependenciesConfig = {
5+
/** 数据源引擎的版本 */
6+
engineVersion?: string;
7+
/** 数据源引擎的包名 */
8+
enginePackage?: string;
9+
/** 数据源 handlers 的版本 */
10+
handlersVersion?: {
11+
[key: string]: string;
12+
};
13+
/** 数据源 handlers 的包名 */
14+
handlersPackages?: {
15+
[key: string]: string;
16+
};
17+
};
18+
19+
export function buildDataSourceDependencies(
20+
ir: IProjectInfo,
21+
cfg: DataSourceDependenciesConfig = {},
22+
): Record<string, string> {
23+
return {
24+
// 数据源引擎的依赖包
25+
[cfg.enginePackage || '@alilc/lowcode-datasource-engine']: cfg.engineVersion || 'latest',
26+
27+
// 各种数据源的 handlers 的依赖包
28+
...(ir.dataSourcesTypes || []).reduce(
29+
(acc, dsType) => ({
30+
...acc,
31+
[getDataSourceHandlerPackageName(dsType)]: cfg.handlersVersion?.[dsType] || 'latest',
32+
}),
33+
{},
34+
),
35+
};
36+
37+
function getDataSourceHandlerPackageName(dsType: string) {
38+
return (
39+
cfg.handlersPackages?.[dsType] ||
40+
`@alilc/lowcode-datasource-${changeCase.kebab(dsType)}-handler`
41+
);
42+
}
43+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
{
2+
"version": "1.0.0",
3+
"componentsMap": [
4+
{
5+
"package": "@alilc/lowcode-materials",
6+
"version": "^1.0.0",
7+
"exportName": "Page",
8+
"destructuring": true,
9+
"componentName": "Page"
10+
},
11+
{
12+
"package": "@alilc/lowcode-materials",
13+
"version": "^1.0.0",
14+
"exportName": "Typography",
15+
"destructuring": true,
16+
"subName": "Text",
17+
"componentName": "Text"
18+
}
19+
],
20+
"componentsTree": [
21+
{
22+
"componentName": "Page",
23+
"id": "node_dockcviv8fo1",
24+
"props": {},
25+
"fileName": "test",
26+
"dataSource": {
27+
"list": [
28+
{
29+
"id": "test",
30+
"type": "fetch",
31+
"options": {
32+
"uri": "https://xxx.com/api/xxx"
33+
}
34+
}
35+
]
36+
},
37+
"css": "body {\n font-size: 12px;\n}\n\n.botton {\n width: 100px;\n color: #ff00ff;\n}",
38+
"lifeCycles": {},
39+
40+
"hidden": false,
41+
"title": "",
42+
"isLocked": false,
43+
"condition": true,
44+
"conditionGroup": "",
45+
"state": {},
46+
"children": [
47+
{
48+
"componentName": "Text",
49+
"props": {
50+
"text": "hello world"
51+
}
52+
}
53+
]
54+
}
55+
],
56+
"i18n": {}
57+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import CodeGenerator from '../../src';
2+
import * as fs from 'fs';
3+
import * as path from 'path';
4+
5+
const testCaseBaseName = path.basename(__filename, '.test.ts');
6+
7+
test(testCaseBaseName, async () => {
8+
const inputSchemaJsonFile = path.join(__dirname, `${testCaseBaseName}.schema.json`);
9+
const outputDir = path.join(__dirname, `${testCaseBaseName}.generated`);
10+
await exportProject(inputSchemaJsonFile, outputDir);
11+
12+
const generatedPackageJsonText = fs.readFileSync(
13+
path.join(outputDir, 'demo-project/package.json'),
14+
'utf-8',
15+
);
16+
const generatedPackageJson = JSON.parse(generatedPackageJsonText);
17+
expect(generatedPackageJson.dependencies).toBeTruthy();
18+
19+
// 里面有的数据源则应该生成对应的 dependencies
20+
expect(generatedPackageJson.dependencies).toMatchObject({
21+
'@alilc/lowcode-datasource-engine': 'latest',
22+
'@alilc/lowcode-datasource-fetch-handler': 'latest',
23+
});
24+
25+
// 里面没有的,则不应该生成对应的 dependencies
26+
expect(generatedPackageJson.dependencies).not.toMatchObject({
27+
'@alilc/lowcode-datasource-url-params-handler': 'latest',
28+
'@alilc/lowcode-datasource-mtop-handler': 'latest',
29+
'@alilc/lowcode-datasource-mopen-handler': 'latest',
30+
});
31+
});
32+
33+
function exportProject(inputPath: string, outputPath: string) {
34+
const schemaJson = fs.readFileSync(inputPath, { encoding: 'utf8' });
35+
const newSchema = schemaJson;
36+
const builder = CodeGenerator.solutions.icejs();
37+
38+
return builder.generateProject(newSchema).then(async (result) => {
39+
// displayResultInConsole(result);
40+
const publisher = CodeGenerator.publishers.disk();
41+
await publisher.publish({
42+
project: result,
43+
outputPath,
44+
projectSlug: 'demo-project',
45+
createProjectFolder: true,
46+
});
47+
return result;
48+
});
49+
}

0 commit comments

Comments
 (0)