feat(cli): 新增datasoucreSuperClass配置

This commit is contained in:
roymondchen 2023-10-01 12:18:15 +08:00
parent 1ce6411e05
commit 2b2a9c6706
2 changed files with 23 additions and 9 deletions

View File

@ -70,10 +70,12 @@ export interface UserConfig {
/** 组件目录或者npm包名 */ /** 组件目录或者npm包名 */
packages: (string | Record<string, string>)[]; packages: (string | Record<string, string>)[];
/** 组件文件后缀名例如vue文件为.vuetsx文件为.tsx普通js文件则为.js */ /** 组件文件后缀名例如vue文件为.vuetsx文件为.tsx普通js文件则为.js */
componentFileAffix: string; componentFileAffix?: string;
cleanTemp: boolean; /** 继承这些类的类将被判定为数据源,默认有['DataSource', 'HttpDataSource'] */
datasoucreSuperClass?: string[];
cleanTemp?: boolean;
/** 入口文件是否生成为 ts 格式 */ /** 入口文件是否生成为 ts 格式 */
useTs: boolean; useTs?: boolean;
/** npm 配置用于当packages配置有npm包名时可以自动安装npm包 */ /** npm 配置用于当packages配置有npm包名时可以自动安装npm包 */
npmConfig?: NpmConfig; npmConfig?: NpmConfig;
/** 是否使用import()加载组件 */ /** 是否使用import()加载组件 */

View File

@ -27,6 +27,7 @@ interface TypeAssertionOption {
ast: Ast; ast: Ast;
indexPath: string; indexPath: string;
componentFileAffix?: string; componentFileAffix?: string;
datasoucreSuperClass?: string[];
} }
const isFile = (filePath: string) => fs.existsSync(filePath) && fs.lstatSync(filePath).isFile(); const isFile = (filePath: string) => fs.existsSync(filePath) && fs.lstatSync(filePath).isFile();
@ -76,7 +77,12 @@ const npmInstall = function (dependencies: Record<string, string>, cwd: string,
* @param {String} indexPath * @param {String} indexPath
* @return {Object} { type: '', imports: [] } imports * @return {Object} { type: '', imports: [] } imports
*/ */
const typeAssertion = function ({ ast, indexPath, componentFileAffix }: TypeAssertionOption): TypeAssertion { const typeAssertion = function ({
ast,
indexPath,
componentFileAffix,
datasoucreSuperClass,
}: TypeAssertionOption): TypeAssertion {
const n = recast.types.namedTypes; const n = recast.types.namedTypes;
const result: TypeAssertion = { const result: TypeAssertion = {
@ -102,7 +108,12 @@ const typeAssertion = function ({ ast, indexPath, componentFileAffix }: TypeAsse
if (isFile(defaultFile)) { if (isFile(defaultFile)) {
const defaultCode = fs.readFileSync(defaultFile, { encoding: 'utf-8', flag: 'r' }); const defaultCode = fs.readFileSync(defaultFile, { encoding: 'utf-8', flag: 'r' });
const ast = recast.parse(defaultCode, { parser: require('recast/parsers/typescript') }); const ast = recast.parse(defaultCode, { parser: require('recast/parsers/typescript') });
if (isDatasource(ast.program.body.find((node: any) => node.type === 'ExportDefaultDeclaration')?.declaration)) { if (
isDatasource(
datasoucreSuperClass,
ast.program.body.find((node: any) => node.type === 'ExportDefaultDeclaration')?.declaration,
)
) {
result.type = PackageType.DATASOURCE; result.type = PackageType.DATASOURCE;
return false; return false;
} }
@ -154,7 +165,7 @@ const typeAssertion = function ({ ast, indexPath, componentFileAffix }: TypeAsse
} }
} }
if (isDatasource(exportDefaultClass)) { if (isDatasource(datasoucreSuperClass, exportDefaultClass)) {
result.type = PackageType.DATASOURCE; result.type = PackageType.DATASOURCE;
} }
@ -217,7 +228,8 @@ const isPlugin = function (properties: any[]) {
return !!match; return !!match;
}; };
const isDatasource = (exportDefaultClass: any) => exportDefaultClass?.superClass?.name === 'DataSource'; const isDatasource = (datasoucreSuperClass: string[] = [], exportDefaultClass: any) =>
[...datasoucreSuperClass, 'DataSource', 'HttpDataSource'].includes(exportDefaultClass?.superClass?.name);
const getComponentPackageImports = function ({ const getComponentPackageImports = function ({
result, result,
@ -428,7 +440,7 @@ const getDependencies = (dependencies: Record<string, string>, packagePath: stri
const setPackages = (packages: ModuleMainFilePath, app: App, packagePath: string, key?: string) => { const setPackages = (packages: ModuleMainFilePath, app: App, packagePath: string, key?: string) => {
const { options } = app; const { options } = app;
const { temp, source, componentFileAffix } = options; const { temp, source, componentFileAffix, datasoucreSuperClass } = options;
let { name: moduleName } = splitNameVersion(packagePath); let { name: moduleName } = splitNameVersion(packagePath);
@ -455,7 +467,7 @@ const setPackages = (packages: ModuleMainFilePath, app: App, packagePath: string
const indexCode = fs.readFileSync(indexPath, { encoding: 'utf-8', flag: 'r' }); const indexCode = fs.readFileSync(indexPath, { encoding: 'utf-8', flag: 'r' });
const ast: Ast = recast.parse(indexCode, { parser: require('recast/parsers/typescript') }); const ast: Ast = recast.parse(indexCode, { parser: require('recast/parsers/typescript') });
const result = typeAssertion({ ast, indexPath, componentFileAffix }); const result = typeAssertion({ ast, indexPath, componentFileAffix, datasoucreSuperClass });
// 组件&插件&数据源包 // 组件&插件&数据源包
if (result.type === PackageType.COMPONENT_PACKAGE) { if (result.type === PackageType.COMPONENT_PACKAGE) {