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

feat(scanner): filter config with env #155

Merged
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
25 changes: 3 additions & 22 deletions src/loader/impl/config.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
import * as path from 'path';
import { Container } from '@artus/injection';
import ConfigurationHandler from '../../configuration';
import { ArtusInjectEnum, ARTUS_DEFAULT_CONFIG_ENV, CONFIG_PATTERN } from '../../constant';
import { ArtusInjectEnum, CONFIG_PATTERN } from '../../constant';
import { DefineLoader } from '../decorator';
import { ManifestItem, Loader, LoaderFindOptions } from '../types';
import compatibleRequire from '../../utils/compatible_require';
import { isMatch } from '../../utils';
import { Application } from '../../types';

export interface ConfigFileMeta {
env: string;
namespace?: string;
}
import { getConfigMetaFromFilename } from '../utils/config_file_meta';

@DefineLoader('config')
class ConfigLoader implements Loader {
Expand Down Expand Up @@ -42,7 +38,7 @@ class ConfigLoader implements Loader {
}

async load(item: ManifestItem) {
const { namespace, env } = await this.getConfigFileMeta(item);
const { namespace, env } = getConfigMetaFromFilename(item.filename);
let configObj = await this.loadConfigFile(item);
if (namespace) {
configObj = {
Expand All @@ -53,21 +49,6 @@ class ConfigLoader implements Loader {
return configObj;
}

protected async getConfigFileMeta(item: ManifestItem): Promise<ConfigFileMeta> {
let [namespace, env, extname] = item.filename.split('.');
if (!extname) {
// No env flag, set to Default
env = ARTUS_DEFAULT_CONFIG_ENV.DEFAULT;
}
const meta: ConfigFileMeta = {
env,
};
if (namespace !== 'config') {
meta.namespace = namespace;
}
return meta;
}

protected async loadConfigFile(item: ManifestItem): Promise<Record<string, any>> {
const originConfigObj = await compatibleRequire(item.path);
let configObj = originConfigObj;
Expand Down
3 changes: 2 additions & 1 deletion src/loader/impl/framework_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ManifestItem, Loader, LoaderFindOptions } from '../types';
import { FRAMEWORK_PATTERN } from '../../constant';
import ConfigLoader from './config';
import { isMatch } from '../../utils';
import { getConfigMetaFromFilename } from '../utils/config_file_meta';

@DefineLoader('framework-config')
class FrameworkConfigLoader extends ConfigLoader implements Loader {
Expand All @@ -15,7 +16,7 @@ class FrameworkConfigLoader extends ConfigLoader implements Loader {
}

async load(item: ManifestItem) {
const { env } = await this.getConfigFileMeta(item);
const { env } = getConfigMetaFromFilename(item.filename);
const configObj = (await this.loadConfigFile(item)) as FrameworkObject;
this.configurationHandler.addFramework(item.source || 'app', configObj, {
env,
Expand Down
3 changes: 2 additions & 1 deletion src/loader/impl/plugin_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { PluginConfigItem } from '../../plugin/types';
import { isMatch } from '../../utils';
import { DefineLoader } from '../decorator';
import { ManifestItem, Loader, LoaderFindOptions } from '../types';
import { getConfigMetaFromFilename } from '../utils/config_file_meta';
import ConfigLoader from './config';

@DefineLoader('plugin-config')
Expand All @@ -16,7 +17,7 @@ class PluginConfigLoader extends ConfigLoader implements Loader {
}

async load(item: ManifestItem) {
const { env } = await this.getConfigFileMeta(item);
const { env } = getConfigMetaFromFilename(item.filename);
const configObj = await this.loadConfigFile(item);
for (const pluginName of Object.keys(configObj)) {
const pluginConfigItem: PluginConfigItem = configObj[pluginName];
Expand Down
21 changes: 21 additions & 0 deletions src/loader/utils/config_file_meta.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { ARTUS_DEFAULT_CONFIG_ENV } from '../../constant';

export interface ConfigFileMeta {
env: string;
namespace?: string;
}

export const getConfigMetaFromFilename = (filename: string): ConfigFileMeta => {
let [namespace, env, extname] = filename.split('.');
if (!extname) {
// No env flag, set to Default
env = ARTUS_DEFAULT_CONFIG_ENV.DEFAULT;
}
const meta: ConfigFileMeta = {
env,
};
if (namespace !== 'config') {
meta.namespace = namespace;
}
return meta;
};
30 changes: 22 additions & 8 deletions src/scanner/scan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { FrameworkConfig, FrameworkHandler } from '../framework';
import { BasePlugin, PluginFactory } from '../plugin';
import { ScanUtils } from './utils';
import { PluginMetadata } from '../plugin/types';
import { getConfigMetaFromFilename } from '../loader/utils/config_file_meta';

export class Scanner {
private moduleExtensions = ['.js', '.json', '.node'];
Expand Down Expand Up @@ -90,16 +91,17 @@ export class Scanner {
// 0. init clean itemMap
await this.initItemMap();

// 1. Pre-Scan all config files
const config = await this.getAllConfig(root, env);

// 1. scan all file in framework
// 2. scan all file in framework
const frameworkDirs = await this.getFrameworkDirs(config.framework, root, env);
for (const frameworkDir of frameworkDirs) {
await this.walk(frameworkDir, this.formatWalkOptions('framework', frameworkDir));
}


// 2. scan all file in plugin
// 3. scan all file in plugin
if (this.tmpConfigStore.has(env)) {
const configList = this.tmpConfigStore.get(env) ?? [];
configList.forEach(config => this.configHandle.setConfig(env, config));
Expand All @@ -115,11 +117,11 @@ export class Scanner {
);
}

// 3. scan all file in app
// 4. scan all file in app
await this.walk(root, this.formatWalkOptions('app', root, ''));

const result: Manifest = {
items: this.getItemsFromMap(this.options.useRelativePath, root),
items: this.getItemsFromMap(this.options.useRelativePath, root, env),
relative: this.options.useRelativePath,
};
return result;
Expand Down Expand Up @@ -237,16 +239,28 @@ export class Scanner {
return opts;
}

private getItemsFromMap(relative: boolean, appRoot: string): ManifestItem[] {
private getItemsFromMap(relative: boolean, appRoot: string, env: string): ManifestItem[] {
let items: ManifestItem[] = [];
for (const [, unitItems] of this.itemMap) {
items = items.concat(unitItems);
}
relative && items.forEach(item => (item.path = path.relative(appRoot, item.path)));
return items.filter(item => (
return items.filter(item => {
// remove PluginConfig to avoid re-merge on application running
item.loader !== 'plugin-config'
));
if (item.loader === 'plugin-config') {
return false;
}

// remove other env config
if (item.loader === 'config' || item.loader === 'framework-config') {
noahziheng marked this conversation as resolved.
Show resolved Hide resolved
const { env: filenameEnv } = getConfigMetaFromFilename(item.filename);
if (env !== filenameEnv && filenameEnv !== ARTUS_DEFAULT_CONFIG_ENV.DEFAULT) {
return false;
}
}

return true;
});
}

private async writeFile(filename = 'manifest.json', data: string) {
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/app_koa_with_ts/src/config/config.dev.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default {
name: 'artus',
testStr: 'This is dev config',
};
3 changes: 2 additions & 1 deletion test/scanner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ describe('test/scanner.test.ts', () => {
// console.log('devManifest', devManifest);
expect(devManifest).toBeDefined();
expect(devManifest.items).toBeDefined();
expect(devManifest.items.length).toBe(12);
expect(devManifest.items.length).toBe(13);
expect(devManifest.items.filter(item => item.loader === 'config').length).toBe(2);
expect(devManifest.items.filter(item => item.loader === 'plugin-meta').length).toBe(2);
expect(devManifest.items.find(item => item.unitName === 'testDuplicate')).toBeDefined();
});
Expand Down