From 61d92acea90869ede5560f4d9eb44d4b77081ed0 Mon Sep 17 00:00:00 2001 From: hyj1991 Date: Tue, 12 Jul 2022 15:10:46 +0800 Subject: [PATCH] Revert "chore: plugin has own excluded (#129)" (#133) This reverts commit 332fe1ef662af376a1e6b23533f1ccd9a559884e. --- src/constant.ts | 2 - src/framework/handler.ts | 41 +------------ src/plugin/types.ts | 5 +- src/scanner/scan.ts | 59 +++++-------------- src/scanner/types.ts | 4 +- src/scanner/utils.ts | 8 +-- src/types.ts | 6 -- .../src/redis_plugin/meta.yaml | 1 - .../not_to_be_scanned_dir/module.ts | 14 ----- .../redis_plugin/not_to_be_scanned_file.ts | 14 ----- test/fixtures/frameworks/bar/meta.yaml | 1 - .../frameworks/layer/foo/foo1/meta.yaml | 1 - .../frameworks/layer/foo/foo2/meta.yaml | 1 - test/scanner.test.ts | 5 -- 14 files changed, 26 insertions(+), 136 deletions(-) delete mode 100644 test/fixtures/app_koa_with_ts/src/redis_plugin/not_to_be_scanned_dir/module.ts delete mode 100644 test/fixtures/app_koa_with_ts/src/redis_plugin/not_to_be_scanned_file.ts delete mode 100644 test/fixtures/frameworks/bar/meta.yaml delete mode 100644 test/fixtures/frameworks/layer/foo/foo1/meta.yaml delete mode 100644 test/fixtures/frameworks/layer/foo/foo2/meta.yaml diff --git a/src/constant.ts b/src/constant.ts index 2242e86..59a92bd 100644 --- a/src/constant.ts +++ b/src/constant.ts @@ -43,8 +43,6 @@ export const DEFAULT_EXCLUDES = [ 'LICENSE' ]; -export const DEFAULT_EXTENSIONS = ['.js', '.json', '.node', '.yaml']; - export const FRAMEWORK_PATTERN = 'framework.*'; export const PLUGIN_CONFIG_PATTERN = 'plugin.*'; export const CONFIG_PATTERN = 'config.*'; diff --git a/src/framework/handler.ts b/src/framework/handler.ts index 1275d91..7c3237e 100644 --- a/src/framework/handler.ts +++ b/src/framework/handler.ts @@ -1,8 +1,7 @@ import path from 'path'; import ConfigurationHandler, { ConfigObject } from '../configuration'; -import { ManifestItem, Metadata } from '../types'; -import { exisis } from '../utils/fs'; -import { loadMetaFile } from '../utils/load_meta_file'; +import { ManifestItem } from '../types'; + export interface FrameworkConfig { path?: string, package?: string @@ -34,40 +33,4 @@ export class FrameworkHandler { throw new Error(`load framework faild: ${err}, framework config: ${JSON.stringify(config)}`); } } - static async checkAndLoadMetadata(frameworkDir: string): Promise{ - // check import path - if (!await exisis(frameworkDir)) { - throw new Error(`load framework import path ${frameworkDir} is not exists.`); - } - - let find = false; - const fileNameList = [ - 'meta.yaml', - 'meta.yml', - 'meta.json', - ]; - let metadata; - for (const fileName of fileNameList) { - const metaFilePath = path.resolve(frameworkDir, fileName); - try { - if (!await exisis(metaFilePath)) { - continue; - } - metadata = await loadMetaFile({ - path: metaFilePath, - extname: path.extname(metaFilePath), - filename: fileName, - }); - find = true; - break; - } catch (e) { - throw new Error(`load framework metadata <${frameworkDir}> failed, err: ${e}`); - } - } - - if (!find) { - throw new Error(`load framework import path ${frameworkDir} can't find meta file.`); - } - return metadata; - } } diff --git a/src/plugin/types.ts b/src/plugin/types.ts index b3b892d..aeef934 100644 --- a/src/plugin/types.ts +++ b/src/plugin/types.ts @@ -1,12 +1,13 @@ -import { Metadata } from '../types'; export enum PluginType { simple = 'simple', module = 'module', } -export interface PluginMetadata extends Metadata { +export interface PluginMetadata { + name: string; dependencies?: PluginDependencyItem[]; type?: PluginType; + configDir?: string } export interface PluginDependencyItem { diff --git a/src/scanner/scan.ts b/src/scanner/scan.ts index 999a1ad..762c5df 100644 --- a/src/scanner/scan.ts +++ b/src/scanner/scan.ts @@ -7,12 +7,10 @@ import { ARTUS_DEFAULT_CONFIG_ENV, DEFAULT_CONFIG_DIR, DEFAULT_EXCLUDES, - DEFAULT_EXTENSIONS, DEFAULT_LOADER_LIST_WITH_ORDER, LOADER_NAME_META, } from '../constant'; import { LoaderFactory, Manifest, ManifestItem } from '../loader'; -import { Metadata } from '../types'; import { ScannerOptions, WalkOptions } from './types'; import ConfigurationHandler, { ConfigObject } from '../configuration'; import { FrameworkConfig, FrameworkHandler } from '../framework'; @@ -20,6 +18,7 @@ import { BasePlugin, PluginFactory } from '../plugin'; import { ScanUtils } from './utils'; export class Scanner { + private moduleExtensions = ['.js', '.json', '.node']; private options: ScannerOptions; private itemMap: Map = new Map(); private tmpConfigStore: Map = new Map(); @@ -33,8 +32,8 @@ export class Scanner { configDir: DEFAULT_CONFIG_DIR, loaderListGenerator: (defaultLoaderList: string[]) => defaultLoaderList, ...options, - exclude: [...new Set(DEFAULT_EXCLUDES.concat(options.exclude ?? []))], - extensions: [...new Set(DEFAULT_EXTENSIONS.concat(options.extensions ?? []))], + excluded: DEFAULT_EXCLUDES.concat(options.excluded ?? []), + extensions: [...new Set(this.moduleExtensions.concat(options.extensions ?? [], ['.yaml']))], }; } @@ -95,11 +94,10 @@ export class Scanner { // 1. scan all file in framework const frameworkDirs = await this.getFrameworkDirs(config.framework, root, env); for (const frameworkDir of frameworkDirs) { - const frameworkMetadata = await FrameworkHandler.checkAndLoadMetadata(frameworkDir); - const frameworkOptions = this.formatWalkOptions('framework', frameworkDir, frameworkDir, frameworkMetadata); - await this.walk(frameworkDir, frameworkOptions); + await this.walk(frameworkDir, this.formatWalkOptions('framework', frameworkDir)); } + // 2. scan all file in plugin if (this.tmpConfigStore.has(env)) { const configList = this.tmpConfigStore.get(env) ?? []; @@ -110,8 +108,10 @@ export class Scanner { for (const plugin of pluginSortedList) { if (!plugin.enable) continue; this.setPluginMeta(plugin); - const pluginOpts = this.formatWalkOptions('plugin', plugin.importPath, plugin.name, plugin.metadata as Metadata); - await this.walk(plugin.importPath, pluginOpts); + await this.walk( + plugin.importPath, + this.formatWalkOptions('plugin', plugin.importPath, plugin.name, plugin.metadata.configDir) + ); } // 3. scan all file in app @@ -150,9 +150,9 @@ export class Scanner { const container = new Container(ArtusInjectEnum.DefaultContainerName); container.set({ type: ConfigurationHandler }); const loaderFactory = LoaderFactory.create(container); - const configItemList: (ManifestItem | null)[] = await Promise.all(configFileList.map(async filename => { + const configItemList: (ManifestItem|null)[] = await Promise.all(configFileList.map(async filename => { const extname = path.extname(filename); - if (ScanUtils.isExclude(filename, extname, this.options.exclude, this.options.extensions)) { + if (ScanUtils.isExclude(filename, extname, this.options.excluded, this.options.extensions)) { return null; } let loader = await loaderFactory.findLoaderName({ @@ -215,51 +215,22 @@ export class Scanner { return await this.getFrameworkDirs(configInFramework.framework, frameworkBaseDir, env, dirs); } - private formatWalkOptions(source: string, baseDir: string, unitName?: string, metadata?: Metadata): WalkOptions { + private formatWalkOptions(source: string, baseDir: string, unitName?: string, configDir?: string): WalkOptions { const commonOptions = { extensions: this.options.extensions, - exclude: this.options.exclude, + excluded: this.options.excluded, itemMap: this.itemMap, }; unitName ??= baseDir; - const configDir = this.options.configDir; + configDir ??= this.options.configDir; - let result: WalkOptions = Object.assign({}, commonOptions, { + return Object.assign({}, commonOptions, { source, baseDir, unitName, configDir, }); - // metadata takes priority - if (metadata) { - result = this.amendOptions(result, metadata); - } - return result; - } - - private amendOptions(walkOptions: WalkOptions, metadata): WalkOptions { - // plugin/framework exclude take priority over user app's - if (metadata?.exclude) { - walkOptions.exclude = DEFAULT_EXCLUDES.concat(metadata.exclude); - } else { - walkOptions.exclude = DEFAULT_EXCLUDES; - } - - // plugin/framework extensions take priority over user app's - // if (metadata?.extensions) { - // walkOptions.extensions = DEFAULT_EXTENSIONS.concat(metadata.extensions); - // } else { - // walkOptions.extensions = DEFAULT_EXTENSIONS; - // } - - // plugin/framework configDir take priority over user app's - if (metadata?.configDir) { - walkOptions.configDir = metadata.configDir; - } else { - walkOptions.configDir = DEFAULT_CONFIG_DIR; - } - return walkOptions; } private getItemsFromMap(relative: boolean, appRoot: string): ManifestItem[] { diff --git a/src/scanner/types.ts b/src/scanner/types.ts index a16247b..38df515 100644 --- a/src/scanner/types.ts +++ b/src/scanner/types.ts @@ -5,7 +5,7 @@ export interface ScannerOptions { extensions: string[]; needWriteFile: boolean; useRelativePath: boolean; - exclude: string[]; + excluded: string[]; configDir: string; envs?: string[]; loaderListGenerator: (defaultLoaderList: string[]) => (string | typeof BaseLoader)[]; @@ -16,7 +16,7 @@ export interface WalkOptions { baseDir: string; configDir: string; extensions: string[]; - exclude: string[]; + excluded: string[]; itemMap: Map; unitName?: string; } diff --git a/src/scanner/utils.ts b/src/scanner/utils.ts index a9d7e7e..20fa7df 100644 --- a/src/scanner/utils.ts +++ b/src/scanner/utils.ts @@ -32,7 +32,7 @@ export class ScanUtils { for (const item of items) { const realPath = path.resolve(root, item); const extname = path.extname(realPath); - if (this.isExclude(item, extname, options.exclude, options.extensions)) { + if (this.isExclude(item, extname, options.excluded, options.extensions)) { continue; } const itemStat = await fs.stat(realPath); @@ -78,10 +78,10 @@ export class ScanUtils { } static isExclude(filename: string, extname: string, - exclude: string[], extensions: string[]): boolean { + excluded: string[], extensions: string[]): boolean { let result = false; - if (exclude) { - result = isMatch(filename, exclude); + if (!result && excluded) { + result = isMatch(filename, excluded); } if (!result && extname) { diff --git a/src/types.ts b/src/types.ts index 0682f39..7e4760e 100644 --- a/src/types.ts +++ b/src/types.ts @@ -35,11 +35,5 @@ export interface TriggerType { initContext(...args): Promise; startPipeline(...args): Promise; } -export interface Metadata { - name: string; - exclude?: string[]; - extensions?: string[]; - configDir?: string -} export * from './loader/types'; diff --git a/test/fixtures/app_koa_with_ts/src/redis_plugin/meta.yaml b/test/fixtures/app_koa_with_ts/src/redis_plugin/meta.yaml index d489d75..8a1d960 100644 --- a/test/fixtures/app_koa_with_ts/src/redis_plugin/meta.yaml +++ b/test/fixtures/app_koa_with_ts/src/redis_plugin/meta.yaml @@ -1,2 +1 @@ name: redis -exclude: ['not_to_be_scanned_file.ts', 'not_to_be_scanned_dir'] \ No newline at end of file diff --git a/test/fixtures/app_koa_with_ts/src/redis_plugin/not_to_be_scanned_dir/module.ts b/test/fixtures/app_koa_with_ts/src/redis_plugin/not_to_be_scanned_dir/module.ts deleted file mode 100644 index 6ff776d..0000000 --- a/test/fixtures/app_koa_with_ts/src/redis_plugin/not_to_be_scanned_dir/module.ts +++ /dev/null @@ -1,14 +0,0 @@ -import 'reflect-metadata'; -import { Injectable, ScopeEnum } from '@artus/injection'; - -@Injectable({ - scope: ScopeEnum.EXECUTION -}) -export default class NotToBeScannedModule { - async index () { - return { - status: 200, - content: 'Hello Artus' - }; - } -} \ No newline at end of file diff --git a/test/fixtures/app_koa_with_ts/src/redis_plugin/not_to_be_scanned_file.ts b/test/fixtures/app_koa_with_ts/src/redis_plugin/not_to_be_scanned_file.ts deleted file mode 100644 index 0c71936..0000000 --- a/test/fixtures/app_koa_with_ts/src/redis_plugin/not_to_be_scanned_file.ts +++ /dev/null @@ -1,14 +0,0 @@ -import 'reflect-metadata'; -import { Injectable, ScopeEnum } from '@artus/injection'; - -@Injectable({ - scope: ScopeEnum.EXECUTION -}) -export default class NotToBeScannedController { - async index () { - return { - status: 200, - content: 'Hello Artus' - }; - } -} \ No newline at end of file diff --git a/test/fixtures/frameworks/bar/meta.yaml b/test/fixtures/frameworks/bar/meta.yaml deleted file mode 100644 index 2545912..0000000 --- a/test/fixtures/frameworks/bar/meta.yaml +++ /dev/null @@ -1 +0,0 @@ -name: bar \ No newline at end of file diff --git a/test/fixtures/frameworks/layer/foo/foo1/meta.yaml b/test/fixtures/frameworks/layer/foo/foo1/meta.yaml deleted file mode 100644 index 9725622..0000000 --- a/test/fixtures/frameworks/layer/foo/foo1/meta.yaml +++ /dev/null @@ -1 +0,0 @@ -name: "foo1" \ No newline at end of file diff --git a/test/fixtures/frameworks/layer/foo/foo2/meta.yaml b/test/fixtures/frameworks/layer/foo/foo2/meta.yaml deleted file mode 100644 index a90255c..0000000 --- a/test/fixtures/frameworks/layer/foo/foo2/meta.yaml +++ /dev/null @@ -1 +0,0 @@ -name: "foo2" \ No newline at end of file diff --git a/test/scanner.test.ts b/test/scanner.test.ts index 96e23da..f56a78c 100644 --- a/test/scanner.test.ts +++ b/test/scanner.test.ts @@ -14,11 +14,6 @@ describe('test/scanner.test.ts', () => { // console.log('manifest', manifest); expect(manifest.items.length).toBe(11); - const excludes = manifest.items.filter(one => { - return ['not_to_be_scanned_file.ts', 'module.ts'].includes(one.filename) - }) - expect(excludes).toHaveLength(0); - expect(manifest.items.filter(item => item.loader === 'plugin-config').length).toBe(0); expect(manifest.items.filter(item => item.loader === 'plugin-meta').length).toBe(1); expect(manifest.items.filter(item => item.loader === 'exception').length).toBe(1);