Skip to content

Commit

Permalink
feat(loader): add getConfigMetaFromFilename util
Browse files Browse the repository at this point in the history
  • Loading branch information
noahziheng committed Jul 29, 2022
1 parent d9d5ee2 commit 7e3763f
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 24 deletions.
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;
};

0 comments on commit 7e3763f

Please sign in to comment.