GeezerVison is showing github.com — fix: 解决出码的数据源依赖包问题 by Clarence-pan · Pull Request #106 · alibaba/lowcode-engine
fix: 解决出码的数据源依赖包问题
#106
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and
privacy statement . We’ll occasionally send you account related emails.
Already on GitHub?
Sign in
to your account
Uh oh!
There was an error while loading. Please reload this page.
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter
Filter by extension
.json
(1)
.ts
(6)
All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Hide whitespace
Diff view
Hide whitespace
fix: 解决 package.json 中误添加了没有用到的数据源类型的 handler 的包的问题 (
#56 )
Loading branch information
Uh oh!
There was an error while loading. Please reload this page.
commit 76341c8456b227192bb65537dc3d16033db0b3a1
There are no files selected for viewing
Original file line number
Diff line number
Diff line change
Expand Up
@@ -319,6 +319,7 @@ export class SchemaParser implements ISchemaParser {
containersDeps,
utilsDeps,
packages : npms || [ ] ,
dataSourcesTypes : this . collectDataSourcesTypes ( schema ) ,
} ,
} ;
}
Expand Down
Expand Up
@@ -350,6 +351,27 @@ export class SchemaParser implements ISchemaParser {
}
return schema ;
}
private collectDataSourcesTypes ( schema : ProjectSchema ) : string [ ] {
const dataSourcesTypes = new Set < string > ( ) ;
// 数据源的默认类型为 fetch
const defaultDataSourceType = 'fetch' ;
// 收集应用级别的数据源
schema . dataSource ?. list ?. forEach ( ( ds ) => {
dataSourcesTypes . add ( ds . type || defaultDataSourceType ) ;
} ) ;
// 收集容器级别的数据源(页面/组件/区块)
schema . componentsTree . forEach ( ( rootNode ) => {
rootNode . dataSource ?. list ?. forEach ( ( ds ) => {
dataSourcesTypes . add ( ds . type || defaultDataSourceType ) ;
} ) ;
} ) ;
return Array . from ( dataSourcesTypes . values ( ) ) ;
}
}
export default SchemaParser ;
Original file line number
Diff line number
Diff line change
Expand Up
@@ -10,6 +10,7 @@ import {
ICodeStruct,
IProjectInfo,
} from '../../../../../types';
import { buildDataSourceDependencies } from '../../../../../utils/dataSource';
interface IIceJsPackageJSON extends PackageJSON {
ideMode: {
Expand All
@@ -22,7 +23,31 @@ interface IIceJsPackageJSON extends PackageJSON {
originTemplate: string;
}
const pluginFactory: BuilderComponentPluginFactory<unknown> = () => {
export type IceJsPackageJsonPluginConfig = {
/**
* 数据源配置
*/
datasourceConfig?: {
/** 数据源引擎的版本 */
engineVersion?: string;
/** 数据源引擎的包名 */
enginePackage?: string;
/** 数据源 handlers 的版本 */
handlersVersion?: {
[key: string]: string;
};
/** 数据源 handlers 的包名 */
handlersPackages?: {
[key: string]: string;
};
};
/** 包名 */
packageName?: string;
/** 版本 */
packageVersion?: string;
};
const pluginFactory: BuilderComponentPluginFactory<IceJsPackageJsonPluginConfig> = (cfg) => {
const plugin: BuilderComponentPlugin = async (pre: ICodeStruct) => {
const next: ICodeStruct = {
...pre,
Expand All
@@ -31,21 +56,18 @@ const pluginFactory: BuilderComponentPluginFactory<unknown> = () => {
const ir = next.ir as IProjectInfo;
const packageJson: IIceJsPackageJSON = {
name: '@alifd/scaffold-lite-js ',
version: '0.1.5',
name: cfg?.packageName || 'icejs-demo-app ',
version: cfg?.packageVersion || '0.1.5',
description: '轻量级模板,使用 JavaScript,仅包含基础的 Layout。',
dependencies: {
moment: '^2.24.0',
react: '^16.4.1',
'react-dom': '^16.4.1',
'@alifd/theme-design-pro': '^0.x',
'@alilc/lowcode-datasource-engine': '*',
// TODO: 如何动态获取下面这些依赖?
'@alilc/lowcode-datasource-url-params-handler': '*',
'@alilc/lowcode-datasource-fetch-handler': '*',
'@alilc/lowcode-datasource-mtop-handler': '*',
'@alilc/lowcode-datasource-mopen-handler': '*',
'intl-messageformat': '^9.3.6',
// 数据源相关的依赖:
...buildDataSourceDependencies(ir, cfg?.datasourceConfig),
},
devDependencies: {
'@ice/spec': '^1.0.0',
Expand Down
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,3 @@
import changeCase from 'change-case' ;
import { NpmInfo , PackageJSON } from '@alilc/lowcode-types' ;
import { COMMON_CHUNK_NAME } from '../../../../../const/generator' ;
Expand All
@@ -14,6 +13,7 @@ import { isNpmInfo } from '../../../../../utils/schema';
import { getErrorMessage } from '../../../../../utils/errors' ;
import { calcCompatibleVersion } from '../../../../../utils/version' ;
import { RaxFrameworkOptions } from '../types/RaxFrameworkOptions' ;
import { buildDataSourceDependencies } from '../../../../../utils/dataSource' ;
const pluginFactory : BuilderComponentPluginFactory < RaxFrameworkOptions > = ( cfg ) => {
const plugin : BuilderComponentPlugin = async ( pre : ICodeStruct ) => {
Expand All
@@ -26,7 +26,7 @@ const pluginFactory: BuilderComponentPluginFactory<RaxFrameworkOptions> = (cfg)
const npmDeps = getNpmDependencies ( ir ) ;
const packageJson : PackageJSON = {
name : cfg ?. packageName || '@alilc/ rax-app- demo' ,
name : cfg ?. packageName || 'rax-demo-app ' ,
private : true ,
version : cfg ?. packageVersion || '1.0.0' ,
scripts : {
Expand All
@@ -39,18 +39,7 @@ const pluginFactory: BuilderComponentPluginFactory<RaxFrameworkOptions> = (cfg)
} ,
dependencies : {
// 数据源相关的依赖:
[ cfg ?. datasourceConfig ?. enginePackage || '@alilc/lowcode-datasource-engine' ] :
cfg ?. datasourceConfig ?. engineVersion || 'latest' ,
// TODO: [p1] 如何动态获取下究竟用了哪些类型的数据源?
...[ 'url-params' , 'fetch' , 'mtop' , 'mopen' ] . reduce (
( acc , dsType ) => ( {
...acc ,
[ cfg ?. datasourceConfig ?. handlersPackages ?. [ dsType ] ||
`@alilc/lowcode-datasource-${ changeCase . kebab ( dsType ) } -handler` ] :
cfg ?. datasourceConfig ?. handlersVersion ?. [ dsType ] || 'latest' ,
} ) ,
{ } ,
) ,
...buildDataSourceDependencies ( ir , cfg ?. datasourceConfig ) ,
// 环境判断
'universal-env' : '^3.2.0' ,
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -42,6 +42,7 @@ export interface IProjectInfo {
packages : INpmPackage [ ] ;
meta ?: { name ?: string ; title ?: string } | Record < string , any > ;
config ?: Record < string , any > ;
dataSourcesTypes ?: string [ ] ;
}
export interface IPageMeta {
Expand Down
Original file line number
Diff line number
Diff line change
@@ -0,0 +1,43 @@
import changeCase from 'change-case' ;
import type { IProjectInfo } from '../types/intermediate' ;
export type DataSourceDependenciesConfig = {
/** 数据源引擎的版本 */
engineVersion ?: string ;
/** 数据源引擎的包名 */
enginePackage ?: string ;
/** 数据源 handlers 的版本 */
handlersVersion ?: {
[ key : string ] : string ;
} ;
/** 数据源 handlers 的包名 */
handlersPackages ?: {
[ key : string ] : string ;
} ;
} ;
export function buildDataSourceDependencies (
ir : IProjectInfo ,
cfg : DataSourceDependenciesConfig = { } ,
) : Record < string , string > {
return {
// 数据源引擎的依赖包
[ cfg . enginePackage || '@alilc/lowcode-datasource-engine' ] : cfg . engineVersion || 'latest' ,
// 各种数据源的 handlers 的依赖包
...( ir . dataSourcesTypes || [ ] ) . reduce (
( acc , dsType ) => ( {
...acc ,
[ getDataSourceHandlerPackageName ( dsType ) ] : cfg . handlersVersion ?. [ dsType ] || 'latest' ,
} ) ,
{ } ,
) ,
} ;
function getDataSourceHandlerPackageName ( dsType : string ) {
return (
cfg . handlersPackages ?. [ dsType ] ||
`@alilc/lowcode-datasource-${ changeCase . kebab ( dsType ) } -handler`
) ;
}
}
Original file line number
Diff line number
Diff line change
@@ -0,0 +1,57 @@
{
"version": "1.0.0",
"componentsMap": [
{
"package": "@alilc/lowcode-materials",
"version": "^1.0.0",
"exportName": "Page",
"destructuring": true,
"componentName": "Page"
},
{
"package": "@alilc/lowcode-materials",
"version": "^1.0.0",
"exportName": "Typography",
"destructuring": true,
"subName": "Text",
"componentName": "Text"
}
],
"componentsTree": [
{
"componentName": "Page",
"id": "node_dockcviv8fo1",
"props": {},
"fileName": "test",
"dataSource": {
"list": [
{
"id": "test",
"type": "fetch",
"options": {
"uri": "https://xxx.com/api/xxx"
}
}
]
},
"css": "body {\n font-size: 12px;\n}\n\n.botton {\n width: 100px;\n color: #ff00ff;\n}",
"lifeCycles": {},
"hidden": false,
"title": "",
"isLocked": false,
"condition": true,
"conditionGroup": "",
"state": {},
"children": [
{
"componentName": "Text",
"props": {
"text": "hello world"
}
}
]
}
],
"i18n": {}
}
Original file line number
Diff line number
Diff line change
@@ -0,0 +1,49 @@
import CodeGenerator from '../../src';
import * as fs from 'fs';
import * as path from 'path';
const testCaseBaseName = path.basename(__filename, '.test.ts');
test(testCaseBaseName, async () => {
const inputSchemaJsonFile = path.join(__dirname, `${testCaseBaseName}.schema.json`);
const outputDir = path.join(__dirname, `${testCaseBaseName}.generated`);
await exportProject(inputSchemaJsonFile, outputDir);
const generatedPackageJsonText = fs.readFileSync(
path.join(outputDir, 'demo-project/package.json'),
'utf-8',
);
const generatedPackageJson = JSON.parse(generatedPackageJsonText);
expect(generatedPackageJson.dependencies).toBeTruthy();
// 里面有的数据源则应该生成对应的 dependencies
expect(generatedPackageJson.dependencies).toMatchObject({
'@alilc/lowcode-datasource-engine': 'latest',
'@alilc/lowcode-datasource-fetch-handler': 'latest',
});
// 里面没有的,则不应该生成对应的 dependencies
expect(generatedPackageJson.dependencies).not.toMatchObject({
'@alilc/lowcode-datasource-url-params-handler': 'latest',
'@alilc/lowcode-datasource-mtop-handler': 'latest',
'@alilc/lowcode-datasource-mopen-handler': 'latest',
});
});
function exportProject(inputPath: string, outputPath: string) {
const schemaJson = fs.readFileSync(inputPath, { encoding: 'utf8' });
const newSchema = schemaJson;
const builder = CodeGenerator.solutions.icejs();
return builder.generateProject(newSchema).then(async (result) => {
// displayResultInConsole(result);
const publisher = CodeGenerator.publishers.disk();
await publisher.publish({
project: result,
outputPath,
projectSlug: 'demo-project',
createProjectFolder: true,
});
return result;
});
}
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.