Skip to content
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

refactor(scanner): modify exclude option for plugin & excluded rename #134

Merged
merged 6 commits into from
Jul 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"devDependencies": {
"@artus/eslint-config-artus": "0.0.1",
"@artus/tsconfig": "0.0.1",
"@babel/core": "^7.18.6",
noahziheng marked this conversation as resolved.
Show resolved Hide resolved
"@types/jest": "^27.4.1",
"@types/js-yaml": "^4.0.5",
"@types/koa": "^2.13.4",
Expand Down
1 change: 1 addition & 0 deletions src/plugin/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export interface PluginMetadata {
dependencies?: PluginDependencyItem[];
type?: PluginType;
configDir?: string
exclude?: string[];
}

export interface PluginDependencyItem {
Expand Down
33 changes: 18 additions & 15 deletions src/scanner/scan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import ConfigurationHandler, { ConfigObject } from '../configuration';
import { FrameworkConfig, FrameworkHandler } from '../framework';
import { BasePlugin, PluginFactory } from '../plugin';
import { ScanUtils } from './utils';
import { PluginMetadata } from '../plugin/types';

export class Scanner {
private moduleExtensions = ['.js', '.json', '.node'];
Expand All @@ -32,7 +33,7 @@ export class Scanner {
configDir: DEFAULT_CONFIG_DIR,
loaderListGenerator: (defaultLoaderList: string[]) => defaultLoaderList,
...options,
excluded: DEFAULT_EXCLUDES.concat(options.excluded ?? []),
exclude: DEFAULT_EXCLUDES.concat(options.exclude ?? []),
extensions: [...new Set(this.moduleExtensions.concat(options.extensions ?? [], ['.yaml']))],
};
}
Expand Down Expand Up @@ -110,7 +111,7 @@ export class Scanner {
this.setPluginMeta(plugin);
await this.walk(
plugin.importPath,
this.formatWalkOptions('plugin', plugin.importPath, plugin.name, plugin.metadata.configDir),
this.formatWalkOptions('plugin', plugin.importPath, plugin.name, plugin.metadata),
);
}

Expand Down Expand Up @@ -152,7 +153,7 @@ export class Scanner {
const loaderFactory = LoaderFactory.create(container);
const configItemList: (ManifestItem|null)[] = await Promise.all(configFileList.map(async filename => {
const extname = path.extname(filename);
if (ScanUtils.isExclude(filename, extname, this.options.excluded, this.options.extensions)) {
if (ScanUtils.isExclude(filename, extname, this.options.exclude, this.options.extensions)) {
return null;
}
let loader = await loaderFactory.findLoaderName({
Expand Down Expand Up @@ -216,22 +217,24 @@ export class Scanner {
return frameworkDirs;
}

private formatWalkOptions(source: string, baseDir: string, unitName?: string, configDir?: string): WalkOptions {
const commonOptions = {
extensions: this.options.extensions,
excluded: this.options.excluded,
private formatWalkOptions(source: string, baseDir: string, unitName?: string, metaInfo?: Partial<PluginMetadata>): WalkOptions {
const opts: WalkOptions = {
itemMap: this.itemMap,
source,
baseDir,
unitName: unitName ?? baseDir,
extensions: this.options.extensions,
exclude: this.options.exclude,
configDir: this.options.configDir,
};

unitName ??= baseDir;
configDir ??= this.options.configDir;
if (source === 'plugin') {
// TODO: Only support plugin meta now, need cover framework meta later
opts.exclude = DEFAULT_EXCLUDES.concat(metaInfo.exclude ?? []);
opts.configDir = metaInfo.configDir ?? this.options.configDir;
}

return Object.assign({}, commonOptions, {
source,
baseDir,
unitName,
configDir,
});
return opts;
}

private getItemsFromMap(relative: boolean, appRoot: string): ManifestItem[] {
Expand Down
4 changes: 2 additions & 2 deletions src/scanner/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export interface ScannerOptions {
extensions: string[];
needWriteFile: boolean;
useRelativePath: boolean;
excluded: string[];
exclude: string[];
configDir: string;
envs?: string[];
loaderListGenerator: (defaultLoaderList: string[]) => (string | typeof BaseLoader)[];
Expand All @@ -16,7 +16,7 @@ export interface WalkOptions {
baseDir: string;
configDir: string;
extensions: string[];
excluded: string[];
exclude: string[];
itemMap: Map<string, ManifestItem[]>;
unitName?: string;
}
Expand Down
8 changes: 4 additions & 4 deletions src/scanner/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.excluded, options.extensions)) {
if (this.isExclude(item, extname, options.exclude, options.extensions)) {
continue;
}
const itemStat = await fs.stat(realPath);
Expand Down Expand Up @@ -78,10 +78,10 @@ export class ScanUtils {
}

static isExclude(filename: string, extname: string,
excluded: string[], extensions: string[]): boolean {
exclude: string[], extensions: string[]): boolean {
let result = false;
if (!result && excluded) {
result = isMatch(filename, excluded);
if (!result && exclude) {
result = isMatch(filename, exclude);
}

if (!result && extname) {
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/app_koa_with_ts/src/redis_plugin/meta.yaml
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
name: redis
exclude:
- not_to_be_scanned_dir
- not_to_be_scanned_file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default class DummyClazz {}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default class DummyClazz {}
2 changes: 2 additions & 0 deletions test/scanner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ describe('test/scanner.test.ts', () => {
// console.log('manifest', manifest);
expect(manifest.items.length).toBe(11);

expect(manifest.items.find(item => item.filename === 'not_to_be_scanned_file.ts')).toBeFalsy();

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);
Expand Down