From bc7fdbb63a75d5e358ccba73698ae98ad88d54ed Mon Sep 17 00:00:00 2001 From: Jerrys2011 <1670303003@qq.com> Date: Thu, 15 Sep 2022 18:14:25 +0800 Subject: [PATCH] fix: add module check and config file match (#196) --- src/constant.ts | 1 - src/loader/impl/config.ts | 11 ++++++----- src/loader/impl/module.ts | 3 +++ src/types.ts | 4 ++-- test/loader.test.ts | 2 +- 5 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/constant.ts b/src/constant.ts index f701634..7a081e6 100644 --- a/src/constant.ts +++ b/src/constant.ts @@ -46,7 +46,6 @@ export const DEFAULT_EXCLUDES = [ export const FRAMEWORK_PATTERN = 'framework.*'; export const PLUGIN_CONFIG_PATTERN = 'plugin.*'; -export const CONFIG_PATTERN = 'config.*'; export const PACKAGE_JSON = 'package.json'; export const PLUGIN_META_FILENAME = 'meta.json'; export const EXCEPTION_FILENAME = 'exception.json'; diff --git a/src/loader/impl/config.ts b/src/loader/impl/config.ts index e79cc86..336757c 100644 --- a/src/loader/impl/config.ts +++ b/src/loader/impl/config.ts @@ -1,7 +1,7 @@ import * as path from 'path'; import { Container } from '@artus/injection'; import ConfigurationHandler from '../../configuration'; -import { ArtusInjectEnum, CONFIG_PATTERN } from '../../constant'; +import { ArtusInjectEnum, PLUGIN_CONFIG_PATTERN, FRAMEWORK_PATTERN } from '../../constant'; import { DefineLoader } from '../decorator'; import { ManifestItem, Loader, LoaderFindOptions } from '../types'; import compatibleRequire from '../../utils/compatible_require'; @@ -26,10 +26,11 @@ class ConfigLoader implements Loader { } static async is(opts: LoaderFindOptions): Promise { - if (this.isConfigDir(opts)) { - return isMatch(opts.filename, CONFIG_PATTERN); - } - return false; + return ( + this.isConfigDir(opts) && + !isMatch(opts.filename, PLUGIN_CONFIG_PATTERN) && + !isMatch(opts.filename, FRAMEWORK_PATTERN) + ); } protected static isConfigDir(opts: LoaderFindOptions): boolean { diff --git a/src/loader/impl/module.ts b/src/loader/impl/module.ts index a17f7d6..32d4b75 100644 --- a/src/loader/impl/module.ts +++ b/src/loader/impl/module.ts @@ -21,6 +21,9 @@ class ModuleLoader implements Loader { for (const name of state.exportNames) { const moduleClazz = origin[name]; + if (typeof moduleClazz !== 'function') { + continue; + } const opts: Partial = { type: moduleClazz, scope: ScopeEnum.EXECUTION, // The class used with @artus/core will have default scope EXECUTION, can be overwritten by Injectable decorator diff --git a/src/types.ts b/src/types.ts index d5c373c..1d263b2 100644 --- a/src/types.ts +++ b/src/types.ts @@ -29,7 +29,7 @@ export interface Application { } export interface TriggerType { - use(...args): Promise; - initContext(...args): Promise; + use(...args): void | Promise; + initContext(...args): BaseContext | Promise; startPipeline(...args): Promise; } diff --git a/test/loader.test.ts b/test/loader.test.ts index 040ded2..ee6c421 100644 --- a/test/loader.test.ts +++ b/test/loader.test.ts @@ -55,7 +55,7 @@ describe('test/loader.test.ts', () => { filename: 'test_clazz.ts', root: path.resolve(__dirname, './fixtures/module_with_custom_loader/src'), baseDir: path.resolve(__dirname, './fixtures/module_with_custom_loader/src'), - configDir: '', + configDir: 'src/config', }); expect(loaderName).toBe('test-custom-loader'); jest.spyOn(console, 'log');