@artus/core
Advanced tools
Comparing version 1.0.0-beta.6 to 1.0.0-beta.7
import { Container } from '@artus/injection'; | ||
import ConfigurationHandler from '../../configuration'; | ||
import { ManifestItem, Loader, LoaderFindOptions } from '../types'; | ||
import { Application } from '../../types'; | ||
export interface ConfigFileMeta { | ||
env: string; | ||
namespace?: string; | ||
} | ||
declare class ConfigLoader implements Loader { | ||
protected container: Container; | ||
constructor(container: any); | ||
protected get app(): Application; | ||
protected get configurationHandler(): ConfigurationHandler; | ||
static is(opts: LoaderFindOptions): Promise<boolean>; | ||
protected static isConfigDir(opts: LoaderFindOptions): boolean; | ||
load(item: ManifestItem): Promise<void>; | ||
protected getConfigFileMeta(item: ManifestItem): Promise<ConfigFileMeta>; | ||
protected loadConfigFile(item: ManifestItem): Promise<Record<string, any>>; | ||
} | ||
export default ConfigLoader; |
@@ -48,2 +48,8 @@ "use strict"; | ||
} | ||
get app() { | ||
return this.container.get(constant_1.ArtusInjectEnum.Application); | ||
} | ||
get configurationHandler() { | ||
return this.container.get(configuration_1.default); | ||
} | ||
static async is(opts) { | ||
@@ -60,3 +66,12 @@ if (this.isConfigDir(opts)) { | ||
async load(item) { | ||
const originConfigObj = await (0, compatible_require_1.default)(item.path); | ||
const { namespace, env } = await this.getConfigFileMeta(item); | ||
let configObj = await this.loadConfigFile(item); | ||
if (namespace) { | ||
configObj = { | ||
[namespace]: configObj | ||
}; | ||
} | ||
this.configurationHandler.setConfig(env, configObj); | ||
} | ||
async getConfigFileMeta(item) { | ||
let [namespace, env, extname] = item.filename.split('.'); | ||
@@ -67,2 +82,12 @@ if (!extname) { | ||
} | ||
const meta = { | ||
env | ||
}; | ||
if (namespace !== 'config') { | ||
meta.namespace = namespace; | ||
} | ||
return meta; | ||
} | ||
async loadConfigFile(item) { | ||
const originConfigObj = await (0, compatible_require_1.default)(item.path); | ||
let configObj = originConfigObj; | ||
@@ -73,9 +98,3 @@ if (typeof originConfigObj === 'function') { | ||
} | ||
if (namespace !== 'config') { | ||
configObj = { | ||
[namespace]: configObj | ||
}; | ||
} | ||
const configHandler = this.container.get(configuration_1.default); | ||
configHandler.setConfig(env, configObj); | ||
return configObj; | ||
} | ||
@@ -82,0 +101,0 @@ }; |
@@ -15,5 +15,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const configuration_1 = __importDefault(require("../../configuration")); | ||
const decorator_1 = require("../decorator"); | ||
const compatible_require_1 = __importDefault(require("../../utils/compatible_require")); | ||
const constant_1 = require("../../constant"); | ||
@@ -33,15 +31,5 @@ const config_1 = __importDefault(require("./config")); | ||
async load(item) { | ||
const originConfigObj = await (0, compatible_require_1.default)(item.path); | ||
let [, env, extname] = item.filename.split('.'); | ||
if (!extname) { | ||
// No env flag, set to Default | ||
env = constant_1.ARTUS_DEFAULT_CONFIG_ENV.DEFAULT; | ||
} | ||
let configObj = originConfigObj; | ||
if (typeof originConfigObj === 'function') { | ||
const app = this.container.get(constant_1.ArtusInjectEnum.Application); | ||
configObj = originConfigObj(app); | ||
} | ||
const configHandler = this.container.get(configuration_1.default); | ||
configHandler.addFramework(item.source || 'app', configObj, { | ||
const { env } = await this.getConfigFileMeta(item); | ||
const configObj = await this.loadConfigFile(item); | ||
this.configurationHandler.addFramework(item.source || 'app', configObj, { | ||
env, | ||
@@ -48,0 +36,0 @@ unitName: item.unitName || '', |
@@ -13,2 +13,3 @@ "use strict"; | ||
const constant_1 = require("../../constant"); | ||
const plugin_1 = require("../../plugin"); | ||
const utils_1 = require("../../utils"); | ||
@@ -25,3 +26,21 @@ const decorator_1 = require("../decorator"); | ||
async load(item) { | ||
await super.load(item); | ||
const { env } = await this.getConfigFileMeta(item); | ||
let configObj = await this.loadConfigFile(item); | ||
for (const pluginName of Object.keys(configObj)) { | ||
const pluginConfigItem = configObj[pluginName]; | ||
if (pluginConfigItem.package) { | ||
// convert package to path when load plugin config | ||
if (pluginConfigItem.path) { | ||
throw new Error(`Plugin ${pluginName} config can't have both package and path at ${item.path}`); | ||
} | ||
if (pluginConfigItem.enable) { | ||
pluginConfigItem.path = plugin_1.ArtusPlugin.getPath(pluginConfigItem.package); | ||
} | ||
delete pluginConfigItem.package; | ||
configObj[pluginName] = pluginConfigItem; | ||
} | ||
} | ||
this.configurationHandler.setConfig(env, { | ||
plugin: configObj | ||
}); | ||
} | ||
@@ -28,0 +47,0 @@ }; |
import { Plugin, PluginConfigItem, PluginMetadata } from "./types"; | ||
declare type PluginMap = Map<string, BasePlugin>; | ||
export declare class BasePlugin implements Plugin { | ||
static getPath(packageName: string): string; | ||
name: string; | ||
@@ -5,0 +6,0 @@ enable: boolean; |
@@ -11,15 +11,24 @@ "use strict"; | ||
var _a, _b; | ||
this.importPath = ''; | ||
this.metadata = {}; | ||
this.metaFilePath = ''; | ||
this.name = name; | ||
let importPath = (_a = configItem.path) !== null && _a !== void 0 ? _a : ''; | ||
if (configItem.package) { | ||
importPath = path_1.default.resolve(require.resolve(`${configItem.package}/package.json`), '..'); | ||
this.enable = (_a = configItem.enable) !== null && _a !== void 0 ? _a : false; | ||
if (this.enable) { | ||
let importPath = (_b = configItem.path) !== null && _b !== void 0 ? _b : ''; | ||
if (configItem.package) { | ||
if (importPath) { | ||
throw new Error(`plugin ${name} config error, package and path can't be set at the same time.`); | ||
} | ||
importPath = BasePlugin.getPath(configItem.package); | ||
} | ||
if (!importPath) { | ||
throw new Error(`Plugin ${name} need have path or package field`); | ||
} | ||
this.importPath = importPath; | ||
} | ||
if (!importPath) { | ||
throw new Error(`Plugin ${name} need have path or package field`); | ||
} | ||
this.importPath = importPath; | ||
this.enable = (_b = configItem.enable) !== null && _b !== void 0 ? _b : false; | ||
} | ||
static getPath(packageName) { | ||
return path_1.default.resolve(require.resolve(`${packageName}/package.json`), '..'); | ||
} | ||
async init() { } | ||
@@ -26,0 +35,0 @@ checkDepExisted(pluginMap) { |
@@ -13,2 +13,5 @@ "use strict"; | ||
async init() { | ||
if (!this.enable) { | ||
return; | ||
} | ||
await this.checkAndLoadMetadata(); | ||
@@ -15,0 +18,0 @@ if (!this.metadata) { |
@@ -8,5 +8,6 @@ import 'reflect-metadata'; | ||
private itemMap; | ||
private configList; | ||
private tmpConfigStore; | ||
private configHandle; | ||
constructor(options?: Partial<ScannerOptions>); | ||
private initItemMap; | ||
private scanEnvList; | ||
@@ -13,0 +14,0 @@ scan(root: string): Promise<Record<string, Manifest>>; |
@@ -35,4 +35,4 @@ "use strict"; | ||
const constant_1 = require("../constant"); | ||
const loader_1 = require("../loader"); | ||
const configuration_1 = __importDefault(require("../configuration")); | ||
const impl_1 = require("../loader/impl"); | ||
const framework_1 = require("../framework"); | ||
@@ -45,3 +45,8 @@ const plugin_1 = require("../plugin"); | ||
this.moduleExtensions = ['.js', '.json', '.node']; | ||
this.itemMap = new Map(); | ||
this.tmpConfigStore = new Map(); | ||
this.configHandle = new configuration_1.default(); | ||
this.options = Object.assign(Object.assign({ appName: 'app', needWriteFile: true, useRelativePath: true, configDir: constant_1.DEFAULT_CONFIG_DIR, loaderListGenerator: (defaultLoaderList) => defaultLoaderList }, options), { excluded: constant_1.DEFAULT_EXCLUDES.concat((_a = options.excluded) !== null && _a !== void 0 ? _a : []), extensions: [...new Set(this.moduleExtensions.concat((_b = options.extensions) !== null && _b !== void 0 ? _b : [], ['.yaml']))] }); | ||
} | ||
async initItemMap() { | ||
this.itemMap = new Map(this.options.loaderListGenerator(constant_1.DEFAULT_LOADER_LIST_WITH_ORDER).map(loaderNameOrClazz => { | ||
@@ -58,4 +63,2 @@ if (typeof loaderNameOrClazz === 'string') { | ||
})); | ||
this.configList = []; | ||
this.configHandle = new configuration_1.default(); | ||
} | ||
@@ -90,2 +93,5 @@ async scanEnvList(root) { | ||
async scanManifestByEnv(root, env) { | ||
var _a; | ||
// 0. init clean itemMap | ||
await this.initItemMap(); | ||
const config = await this.getAllConfig(root, env); | ||
@@ -98,3 +104,6 @@ // 1. scan all file in framework | ||
// 2. scan all file in plugin | ||
this.configList.forEach(config => this.configHandle.setConfig(env, config)); | ||
if (this.tmpConfigStore.has(env)) { | ||
const configList = (_a = this.tmpConfigStore.get(env)) !== null && _a !== void 0 ? _a : []; | ||
configList.forEach(config => this.configHandle.setConfig(env, config)); | ||
} | ||
const { plugin } = this.configHandle.getMergedConfig(env); | ||
@@ -131,24 +140,45 @@ const pluginSortedList = await plugin_1.PluginFactory.createFromConfig(plugin || {}); | ||
} | ||
async getAllConfig(root, env) { | ||
const configDir = this.getConfigDir(root, this.options.configDir); | ||
async getAllConfig(baseDir, env) { | ||
var _a; | ||
const configDir = this.getConfigDir(baseDir, this.options.configDir); | ||
if (!configDir) { | ||
return {}; | ||
} | ||
const configFileList = await fs.readdir(path.resolve(root, configDir)); | ||
const root = path.resolve(baseDir, configDir); | ||
const configFileList = await fs.readdir(root); | ||
const container = new injection_1.Container(constant_1.ArtusInjectEnum.DefaultContainerName); | ||
container.set({ type: configuration_1.default }); | ||
const configHandler = new impl_1.ConfigLoader(container); | ||
for (const pluginConfigFile of configFileList) { | ||
const extname = path.extname(pluginConfigFile); | ||
if (utils_1.ScanUtils.isExclude(pluginConfigFile, extname, this.options.excluded, this.options.extensions)) { | ||
continue; | ||
const loaderFactory = loader_1.LoaderFactory.create(container); | ||
const configItemList = await Promise.all(configFileList.map(async (filename) => { | ||
const extname = path.extname(filename); | ||
if (utils_1.ScanUtils.isExclude(filename, extname, this.options.excluded, this.options.extensions)) { | ||
return null; | ||
} | ||
await configHandler.load({ | ||
path: path.join(root, configDir, pluginConfigFile), | ||
extname: extname, | ||
filename: pluginConfigFile, | ||
let loader = await loaderFactory.findLoaderName({ | ||
filename, | ||
baseDir, | ||
root, | ||
configDir | ||
}); | ||
if (loader === 'framework-config') { | ||
// SEEME: framework-config is a special loader, cannot be used when scan, need refactor later | ||
loader = 'config'; | ||
} | ||
return { | ||
path: path.resolve(root, filename), | ||
extname, | ||
filename, | ||
loader, | ||
source: 'config', | ||
}; | ||
})); | ||
await loaderFactory.loadItemList(configItemList.filter(v => v)); | ||
const configurationHandler = container.get(configuration_1.default); | ||
const config = configurationHandler.getMergedConfig(env); | ||
let configList = [config]; | ||
if (this.tmpConfigStore.has(env)) { | ||
// equal unshift config to configList | ||
configList = configList.concat((_a = this.tmpConfigStore.get(env)) !== null && _a !== void 0 ? _a : []); | ||
} | ||
const config = container.get(configuration_1.default).getMergedConfig(env); | ||
this.configList.unshift(config); | ||
this.tmpConfigStore.set(env, configList); | ||
return config; | ||
@@ -196,3 +226,5 @@ } | ||
relative && items.forEach(item => (item.path = path.relative(appRoot, item.path))); | ||
return items; | ||
return items.filter(item => ( | ||
// remove PluginConfig to avoid re-merge on application running | ||
item.loader !== 'plugin-config')); | ||
} | ||
@@ -199,0 +231,0 @@ async writeFile(filename = 'manifest.json', data) { |
{ | ||
"name": "@artus/core", | ||
"version": "1.0.0-beta.6", | ||
"version": "1.0.0-beta.7", | ||
"description": "Core package of Artus", | ||
@@ -5,0 +5,0 @@ "main": "./lib/index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
121158
2832