@artus/core
Advanced tools
Comparing version 2.0.0-alpha.7 to 2.0.0-alpha.8
@@ -11,11 +11,8 @@ import 'reflect-metadata'; | ||
manifest?: Manifest; | ||
envList?: string[]; | ||
container: Container; | ||
protected lifecycleManager: LifecycleManager; | ||
protected loaderFactory: LoaderFactory; | ||
constructor(opts?: ApplicationInitOptions); | ||
get config(): Record<string, any>; | ||
get frameworks(): Record<string, any>; | ||
get packages(): Record<string, any>; | ||
get configurationHandler(): ConfigurationHandler; | ||
get lifecycleManager(): LifecycleManager; | ||
get loaderFactory(): LoaderFactory; | ||
get logger(): LoggerType; | ||
@@ -30,2 +27,3 @@ loadDefaultClass(): void; | ||
protected addLoaderListener(): void; | ||
protected updateConfig(): void; | ||
} |
@@ -17,9 +17,8 @@ "use strict"; | ||
this.container = new injection_1.Container((_a = opts === null || opts === void 0 ? void 0 : opts.containerName) !== null && _a !== void 0 ? _a : constant_1.ArtusInjectEnum.DefaultContainerName); | ||
this.lifecycleManager = new lifecycle_1.LifecycleManager(this, this.container); | ||
this.loaderFactory = new loader_1.LoaderFactory(this.container); | ||
if (opts === null || opts === void 0 ? void 0 : opts.env) { | ||
this.envList = [].concat(opts.env); | ||
const envList = [].concat(opts.env); | ||
this.container.set({ id: constant_1.ArtusInjectEnum.EnvList, value: envList }); | ||
} | ||
this.loadDefaultClass(); | ||
this.addLoaderListener(); | ||
this.loadDefaultClass(); | ||
process.on('SIGINT', () => this.close(true)); | ||
@@ -31,11 +30,11 @@ process.on('SIGTERM', () => this.close(true)); | ||
} | ||
get frameworks() { | ||
return this.container.get(constant_1.ArtusInjectEnum.Frameworks); | ||
} | ||
get packages() { | ||
return this.container.get(constant_1.ArtusInjectEnum.Packages); | ||
} | ||
get configurationHandler() { | ||
return this.container.get(configuration_1.default); | ||
} | ||
get lifecycleManager() { | ||
return this.container.get(lifecycle_1.LifecycleManager); | ||
} | ||
get loaderFactory() { | ||
return this.container.get(loader_1.LoaderFactory); | ||
} | ||
get logger() { | ||
@@ -48,5 +47,6 @@ return this.container.get(logger_1.Logger); | ||
this.container.set({ id: constant_1.ArtusInjectEnum.Application, value: this }); | ||
this.container.set({ id: constant_1.ArtusInjectEnum.LifecycleManager, value: this.lifecycleManager }); | ||
this.container.set({ id: constant_1.ArtusInjectEnum.Config, value: {} }); | ||
this.container.set({ type: configuration_1.default }); | ||
this.container.set({ type: loader_1.LoaderFactory }); | ||
this.container.set({ type: lifecycle_1.LifecycleManager }); | ||
this.container.set({ type: logger_1.Logger }); | ||
@@ -90,6 +90,3 @@ } | ||
after: () => { | ||
this.container.set({ | ||
id: constant_1.ArtusInjectEnum.Config, | ||
value: this.configurationHandler.getMergedConfig(this.envList), | ||
}); | ||
this.updateConfig(); | ||
return this.lifecycleManager.emitHook('configDidLoad'); | ||
@@ -99,3 +96,12 @@ }, | ||
} | ||
updateConfig() { | ||
var _a, _b; | ||
const oldConfig = (_a = this.container.get(constant_1.ArtusInjectEnum.Config, { noThrow: true })) !== null && _a !== void 0 ? _a : {}; | ||
const newConfig = (_b = this.configurationHandler.getMergedConfig()) !== null && _b !== void 0 ? _b : {}; | ||
this.container.set({ | ||
id: constant_1.ArtusInjectEnum.Config, | ||
value: Object.assign(oldConfig, newConfig), | ||
}); | ||
} | ||
} | ||
exports.ArtusApplication = ArtusApplication; |
@@ -15,6 +15,8 @@ import { ManifestItem } from '../loader'; | ||
configStore: Map<string, ConfigObject>; | ||
getMergedConfig(env?: string | string[]): ConfigObject; | ||
private container; | ||
getMergedConfig(): ConfigObject; | ||
getAllConfig(): ConfigObject; | ||
clearStore(): void; | ||
setConfig(env: string, config: ConfigObject): void; | ||
setConfigByFile(fileItem: ManifestItem): Promise<void>; | ||
} |
@@ -20,19 +20,10 @@ "use strict"; | ||
} | ||
getMergedConfig(env) { | ||
getMergedConfig() { | ||
var _a; | ||
let currentEnvList = []; | ||
if (Array.isArray(env)) { | ||
currentEnvList = env; | ||
let envList = this.container.get(constant_1.ArtusInjectEnum.EnvList, { noThrow: true }); | ||
if (!envList) { | ||
envList = process.env[constant_1.ARTUS_SERVER_ENV] ? [process.env[constant_1.ARTUS_SERVER_ENV]] : [constant_1.ARTUS_DEFAULT_CONFIG_ENV.DEV]; | ||
} | ||
else if (env) { | ||
currentEnvList = [env]; | ||
} | ||
else if (process.env[constant_1.ARTUS_SERVER_ENV]) { | ||
currentEnvList = [process.env[constant_1.ARTUS_SERVER_ENV]]; | ||
} | ||
else { | ||
currentEnvList = [constant_1.ARTUS_DEFAULT_CONFIG_ENV.DEV]; | ||
} | ||
const defaultConfig = (_a = this.configStore.get(constant_1.ARTUS_DEFAULT_CONFIG_ENV.DEFAULT)) !== null && _a !== void 0 ? _a : {}; | ||
const envConfigList = currentEnvList.map(currentEnv => { var _a; return ((_a = this.configStore.get(currentEnv)) !== null && _a !== void 0 ? _a : {}); }); | ||
const envConfigList = envList.map(currentEnv => { var _a; return ((_a = this.configStore.get(currentEnv)) !== null && _a !== void 0 ? _a : {}); }); | ||
return (0, merge_1.mergeConfig)(defaultConfig, ...envConfigList); | ||
@@ -46,2 +37,5 @@ } | ||
} | ||
clearStore() { | ||
this.configStore.clear(); | ||
} | ||
setConfig(env, config) { | ||
@@ -60,2 +54,6 @@ var _a; | ||
}; | ||
tslib_1.__decorate([ | ||
(0, injection_1.Inject)(), | ||
tslib_1.__metadata("design:type", injection_1.Container) | ||
], ConfigurationHandler.prototype, "container", void 0); | ||
ConfigurationHandler = ConfigurationHandler_1 = tslib_1.__decorate([ | ||
@@ -62,0 +60,0 @@ (0, injection_1.Injectable)() |
@@ -8,5 +8,3 @@ export declare const DEFAULT_LOADER = "module"; | ||
DefaultContainerName = "artus#default_container", | ||
Frameworks = "artus#framework-config", | ||
LifecycleManager = "artus#lifecycle-manager", | ||
Packages = "artus#packages" | ||
EnvList = "artus#env_list" | ||
} | ||
@@ -13,0 +11,0 @@ export declare enum ARTUS_DEFAULT_CONFIG_ENV { |
@@ -12,5 +12,3 @@ "use strict"; | ||
ArtusInjectEnum["DefaultContainerName"] = "artus#default_container"; | ||
ArtusInjectEnum["Frameworks"] = "artus#framework-config"; | ||
ArtusInjectEnum["LifecycleManager"] = "artus#lifecycle-manager"; | ||
ArtusInjectEnum["Packages"] = "artus#packages"; | ||
ArtusInjectEnum["EnvList"] = "artus#env_list"; | ||
})(ArtusInjectEnum = exports.ArtusInjectEnum || (exports.ArtusInjectEnum = {})); | ||
@@ -17,0 +15,0 @@ var ARTUS_DEFAULT_CONFIG_ENV; |
@@ -1,2 +0,2 @@ | ||
import { Constructable, Container } from '@artus/injection'; | ||
import { Constructable } from '@artus/injection'; | ||
import { Application } from '../types'; | ||
@@ -12,6 +12,5 @@ export type HookFunction = <T = unknown>(hookProps: { | ||
private hookFnMap; | ||
private hookUnitSet; | ||
private app; | ||
private container; | ||
private hookUnitSet; | ||
constructor(app: Application, container: Container); | ||
insertHook(existHookName: string, newHookName: string): void; | ||
@@ -18,0 +17,0 @@ appendHook(newHookName: string): void; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.LifecycleManager = void 0; | ||
const tslib_1 = require("tslib"); | ||
const injection_1 = require("@artus/injection"); | ||
const constant_1 = require("../constant"); | ||
class LifecycleManager { | ||
constructor(app, container) { | ||
let LifecycleManager = class LifecycleManager { | ||
constructor() { | ||
this.enable = true; // Enabled default, will NOT emit when enable is false | ||
@@ -18,4 +20,2 @@ this.hookList = [ | ||
this.hookUnitSet = new Set(); | ||
this.app = app; | ||
this.container = container; | ||
} | ||
@@ -77,3 +77,14 @@ insertHook(existHookName, newHookName) { | ||
} | ||
} | ||
}; | ||
tslib_1.__decorate([ | ||
(0, injection_1.Inject)(constant_1.ArtusInjectEnum.Application), | ||
tslib_1.__metadata("design:type", Object) | ||
], LifecycleManager.prototype, "app", void 0); | ||
tslib_1.__decorate([ | ||
(0, injection_1.Inject)(), | ||
tslib_1.__metadata("design:type", injection_1.Container) | ||
], LifecycleManager.prototype, "container", void 0); | ||
LifecycleManager = tslib_1.__decorate([ | ||
(0, injection_1.Injectable)() | ||
], LifecycleManager); | ||
exports.LifecycleManager = LifecycleManager; |
@@ -1,2 +0,1 @@ | ||
import { Container } from '@artus/injection'; | ||
import { Manifest, ManifestItem, LoaderConstructor, Loader } from './types'; | ||
@@ -12,3 +11,2 @@ import ConfigurationHandler from '../configuration'; | ||
private loaderEmitter; | ||
constructor(container: Container); | ||
get lifecycleManager(): LifecycleManager; | ||
@@ -20,5 +18,5 @@ get configurationHandler(): ConfigurationHandler; | ||
getLoader(loaderName: string): Loader; | ||
loadManifest(manifest: Manifest, root?: string, envList?: string[]): Promise<void>; | ||
loadManifest(manifest: Manifest, root?: string): Promise<void>; | ||
loadItemList(itemList?: ManifestItem[], root?: string): Promise<void>; | ||
loadItem(item: ManifestItem): Promise<any>; | ||
} |
"use strict"; | ||
var LoaderFactory_1; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -6,8 +7,13 @@ exports.LoaderFactory = void 0; | ||
const path = tslib_1.__importStar(require("path")); | ||
const injection_1 = require("@artus/injection"); | ||
const constant_1 = require("../constant"); | ||
const configuration_1 = tslib_1.__importDefault(require("../configuration")); | ||
const lifecycle_1 = require("../lifecycle"); | ||
const loader_event_1 = tslib_1.__importDefault(require("./loader_event")); | ||
const plugin_1 = require("../plugin"); | ||
const logger_1 = require("../logger"); | ||
class LoaderFactory { | ||
let LoaderFactory = LoaderFactory_1 = class LoaderFactory { | ||
constructor() { | ||
this.loaderEmitter = new loader_event_1.default(); | ||
} | ||
static register(clazz) { | ||
@@ -17,8 +23,4 @@ const loaderName = Reflect.getMetadata(constant_1.LOADER_NAME_META, clazz); | ||
} | ||
constructor(container) { | ||
this.container = container; | ||
this.loaderEmitter = new loader_event_1.default(); | ||
} | ||
get lifecycleManager() { | ||
return this.container.get(constant_1.ArtusInjectEnum.LifecycleManager); | ||
return this.container.get(lifecycle_1.LifecycleManager); | ||
} | ||
@@ -40,3 +42,3 @@ get configurationHandler() { | ||
getLoader(loaderName) { | ||
const LoaderClazz = LoaderFactory.loaderClazzMap.get(loaderName); | ||
const LoaderClazz = LoaderFactory_1.loaderClazzMap.get(loaderName); | ||
if (!LoaderClazz) { | ||
@@ -47,6 +49,4 @@ throw new Error(`Cannot find loader '${loaderName}'`); | ||
} | ||
async loadManifest(manifest, root, envList) { | ||
var _a, _b, _c, _d, _e; | ||
if (root === void 0) { root = process.cwd(); } | ||
if (envList === void 0) { envList = [(_a = process.env[constant_1.ARTUS_SERVER_ENV]) !== null && _a !== void 0 ? _a : constant_1.ARTUS_DEFAULT_CONFIG_ENV.DEV]; } | ||
async loadManifest(manifest, root = process.cwd()) { | ||
var _a, _b, _c; | ||
if (!('version' in manifest) || manifest.version !== '2') { | ||
@@ -57,9 +57,8 @@ throw new Error(`invalid manifest, @artus/core@2.x only support manifest version 2.`); | ||
// Merge plugin config with ref | ||
const validEnvList = [constant_1.ARTUS_DEFAULT_CONFIG_ENV.DEFAULT].concat(envList); | ||
for (const env of validEnvList) { | ||
for (const [env, pluginConfig] of Object.entries((_a = manifest.pluginConfig) !== null && _a !== void 0 ? _a : {})) { | ||
this.configurationHandler.setConfig(env, { | ||
plugin: (_c = (_b = manifest.pluginConfig) === null || _b === void 0 ? void 0 : _b[env]) !== null && _c !== void 0 ? _c : {}, | ||
plugin: pluginConfig, | ||
}); | ||
} | ||
const mergedPluginConfig = (_e = (_d = this.configurationHandler.getAllConfig()) === null || _d === void 0 ? void 0 : _d.plugin) !== null && _e !== void 0 ? _e : {}; | ||
const mergedPluginConfig = (_c = (_b = this.configurationHandler.getMergedConfig()) === null || _b === void 0 ? void 0 : _b.plugin) !== null && _c !== void 0 ? _c : {}; | ||
for (const [pluginName, pluginConfigItem] of Object.entries(mergedPluginConfig)) { | ||
@@ -121,4 +120,11 @@ const refItem = manifest.refMap[pluginConfigItem.refName]; | ||
} | ||
} | ||
}; | ||
LoaderFactory.loaderClazzMap = new Map(); | ||
tslib_1.__decorate([ | ||
(0, injection_1.Inject)(), | ||
tslib_1.__metadata("design:type", injection_1.Container) | ||
], LoaderFactory.prototype, "container", void 0); | ||
LoaderFactory = LoaderFactory_1 = tslib_1.__decorate([ | ||
(0, injection_1.Injectable)() | ||
], LoaderFactory); | ||
exports.LoaderFactory = LoaderFactory; | ||
LoaderFactory.loaderClazzMap = new Map(); |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const tslib_1 = require("tslib"); | ||
const constant_1 = require("../../constant"); | ||
const lifecycle_1 = require("../../lifecycle"); | ||
const decorator_1 = require("../decorator"); | ||
@@ -12,3 +12,3 @@ const compatible_require_1 = tslib_1.__importDefault(require("../../utils/compatible_require")); | ||
get lifecycleManager() { | ||
return this.container.get(constant_1.ArtusInjectEnum.LifecycleManager); | ||
return this.container.get(lifecycle_1.LifecycleManager); | ||
} | ||
@@ -15,0 +15,0 @@ async load(item) { |
@@ -30,16 +30,8 @@ "use strict"; | ||
}]; | ||
// Init scan-task context | ||
const scanCtx = { | ||
root, | ||
taskQueue, | ||
waitingTaskMap: new Map(), | ||
enabledPluginSet: new Set(), | ||
refMap: {}, | ||
pluginConfigMap: {}, | ||
options: this.options, | ||
app: (_a = this.options.app) !== null && _a !== void 0 ? _a : new application_1.ArtusApplication(), | ||
}; | ||
// Init scan-task scanner | ||
const app = (_a = this.options.app) !== null && _a !== void 0 ? _a : new application_1.ArtusApplication(); | ||
const taskRunner = new task_1.ScanTaskRunner(root, app, taskQueue, this.options); | ||
// Add Task of options.plugin | ||
if (this.options.plugin) { | ||
await (0, task_1.handlePluginConfig)(this.options.plugin, root, scanCtx); | ||
await taskRunner.handlePluginConfig(this.options.plugin, root); | ||
} | ||
@@ -49,10 +41,6 @@ // Run task queue | ||
const taskItem = taskQueue.shift(); | ||
await (0, task_1.runTask)(taskItem, scanCtx); | ||
await taskRunner.run(taskItem); | ||
} | ||
// Dump manifest | ||
const manifestResult = { | ||
version: '2', | ||
pluginConfig: scanCtx.pluginConfigMap, | ||
refMap: scanCtx.refMap, | ||
}; | ||
const manifestResult = taskRunner.dump(); | ||
if (this.options.needWriteFile) { | ||
@@ -65,2 +53,4 @@ let { manifestFilePath } = this.options; | ||
} | ||
// Clean up | ||
app.configurationHandler.clearStore(); | ||
return manifestResult; | ||
@@ -67,0 +57,0 @@ } |
@@ -1,7 +0,22 @@ | ||
import { ScanContext, ScanTaskItem } from './types'; | ||
import { ScannerOptions, ScanTaskItem } from './types'; | ||
import { Manifest } from '../loader'; | ||
import { PluginConfig } from '../plugin'; | ||
export declare const handlePluginConfig: (pluginConfig: PluginConfig, basePath: string, scanCtx: ScanContext, env?: string) => Promise<void>; | ||
/** | ||
* Handler of single scan task(only a ref) | ||
*/ | ||
export declare const runTask: (taskItem: ScanTaskItem, scanCtx: ScanContext) => Promise<void>; | ||
import { Application } from '../types'; | ||
export declare class ScanTaskRunner { | ||
private root; | ||
private app; | ||
private taskQueue; | ||
private options; | ||
private waitingTaskMap; | ||
private enabledPluginSet; | ||
private pluginConfigMap; | ||
private refMap; | ||
constructor(root: string, app: Application, taskQueue: ScanTaskItem[], options: ScannerOptions); | ||
private walk; | ||
handlePluginConfig(pluginConfig: PluginConfig, basePath: string, env?: string): Promise<void>; | ||
/** | ||
* Handler of single scan task(only a ref) | ||
*/ | ||
run(taskItem: ScanTaskItem): Promise<void>; | ||
dump(): Manifest; | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.runTask = exports.handlePluginConfig = void 0; | ||
exports.ScanTaskRunner = void 0; | ||
const tslib_1 = require("tslib"); | ||
@@ -12,170 +12,193 @@ const path_1 = tslib_1.__importDefault(require("path")); | ||
const constant_1 = require("../constant"); | ||
const walkDir = async (curPath, options) => { | ||
const { baseDir, configDir } = options; | ||
if (!(await (0, utils_1.existsAsync)(curPath))) { | ||
// TODO: use artus logger instead | ||
console.warn(`[scan->walk] ${curPath} is not exists.`); | ||
return []; | ||
class ScanTaskRunner { | ||
constructor(root, app, taskQueue, options) { | ||
this.root = root; | ||
this.app = app; | ||
this.taskQueue = taskQueue; | ||
this.options = options; | ||
this.waitingTaskMap = new Map(); // Key is pluginName, waiting to detect enabled | ||
this.enabledPluginSet = new Set(); // Key is pluginName | ||
this.pluginConfigMap = {}; | ||
this.refMap = {}; | ||
} | ||
const stat = await fs.stat(curPath); | ||
if (!stat.isDirectory()) { | ||
return []; | ||
} | ||
const items = await fs.readdir(curPath); | ||
const itemWalkResult = await Promise.all(items.map(async (item) => { | ||
const realPath = path_1.default.resolve(curPath, item); | ||
const extname = path_1.default.extname(realPath); | ||
const relativePath = path_1.default.relative(baseDir, realPath); | ||
if ((0, utils_1.isExclude)(relativePath, options.exclude, options.extensions)) { | ||
/* | ||
* Handler for walk directories and files recursively | ||
*/ | ||
async walk(curPath, options) { | ||
const { baseDir, configDir } = options; | ||
if (!(await (0, utils_1.existsAsync)(curPath))) { | ||
this.app.logger.warn(`[scan->walk] ${curPath} is not exists.`); | ||
return []; | ||
} | ||
const itemStat = await fs.stat(realPath); | ||
if (itemStat.isDirectory()) { | ||
// ignore plugin dir | ||
if (await (0, utils_1.isPluginAsync)(realPath)) { | ||
return []; | ||
} | ||
return walkDir(realPath, options); | ||
const stat = await fs.stat(curPath); | ||
if (!stat.isDirectory()) { | ||
return []; | ||
} | ||
else if (itemStat.isFile()) { | ||
if (!extname) { | ||
// Exclude file without extname | ||
const items = await fs.readdir(curPath); | ||
const itemWalkResult = await Promise.all(items.map(async (item) => { | ||
const realPath = path_1.default.resolve(curPath, item); | ||
const extname = path_1.default.extname(realPath); | ||
const relativePath = path_1.default.relative(baseDir, realPath); | ||
if ((0, utils_1.isExclude)(relativePath, options.exclude, options.extensions)) { | ||
return []; | ||
} | ||
const filename = path_1.default.basename(realPath); | ||
const filenameWithoutExt = path_1.default.basename(realPath, extname); | ||
const loaderFindResult = await (0, loader_1.findLoader)({ | ||
filename, | ||
root: curPath, | ||
baseDir, | ||
configDir, | ||
policy: options.policy, | ||
}); | ||
if (!loaderFindResult) { | ||
const itemStat = await fs.stat(realPath); | ||
if (itemStat.isDirectory()) { | ||
// ignore plugin dir | ||
if (await (0, utils_1.isPluginAsync)(realPath)) { | ||
return []; | ||
} | ||
return this.walk(realPath, options); | ||
} | ||
else if (itemStat.isFile()) { | ||
if (!extname) { | ||
// Exclude file without extname | ||
return []; | ||
} | ||
const filename = path_1.default.basename(realPath); | ||
const filenameWithoutExt = path_1.default.basename(realPath, extname); | ||
const loaderFindResult = await (0, loader_1.findLoader)({ | ||
filename, | ||
root: curPath, | ||
baseDir, | ||
configDir, | ||
policy: options.policy, | ||
}); | ||
if (!loaderFindResult) { | ||
return []; | ||
} | ||
const { loaderName, loaderState } = loaderFindResult; | ||
const item = { | ||
path: path_1.default.resolve(curPath, filenameWithoutExt), | ||
extname, | ||
filename, | ||
loader: loaderName, | ||
source: options.source, | ||
unitName: options.unitName, | ||
}; | ||
if (loaderState) { | ||
item.loaderState = loaderState; | ||
} | ||
return [item]; | ||
} | ||
else { | ||
return []; | ||
} | ||
const { loaderName, loaderState } = loaderFindResult; | ||
const item = { | ||
path: path_1.default.resolve(curPath, filenameWithoutExt), | ||
extname, | ||
filename, | ||
loader: loaderName, | ||
source: options.source, | ||
unitName: options.unitName, | ||
})); | ||
return itemWalkResult.reduce((itemList, result) => itemList.concat(result), []); | ||
} | ||
/* | ||
* Handler for pluginConfig object | ||
* Will push new task for plugin, and merge config by env | ||
*/ | ||
async handlePluginConfig(pluginConfig, basePath, env = constant_1.ARTUS_DEFAULT_CONFIG_ENV.DEFAULT) { | ||
var _a, _b; | ||
const tPluginConfig = {}; | ||
for (const [pluginName, pluginConfigItem] of Object.entries(pluginConfig)) { | ||
// Set temp pluginConfig in manifest | ||
tPluginConfig[pluginName] = {}; | ||
if (pluginConfigItem.enable !== undefined) { | ||
tPluginConfig[pluginName].enable = pluginConfigItem.enable; | ||
} | ||
if (pluginConfigItem.enable) { | ||
this.enabledPluginSet.add(pluginName); | ||
} | ||
// Resolve ref and set | ||
const isPluginEnabled = this.enabledPluginSet.has(pluginName); | ||
const ref = await (0, utils_1.resolvePluginConfigItemRef)(pluginConfigItem, basePath, this.root); | ||
if (!(ref === null || ref === void 0 ? void 0 : ref.name)) { | ||
continue; | ||
} | ||
tPluginConfig[pluginName].refName = ref.name; | ||
// Generate and push scan task | ||
const curRefTask = { | ||
curPath: ref.path, | ||
refName: ref.name, | ||
checkPackageVersion: ref.isPackage, | ||
}; | ||
if (loaderState) { | ||
item.loaderState = loaderState; | ||
const waitingTaskList = (_a = this.waitingTaskMap.get(pluginName)) !== null && _a !== void 0 ? _a : []; | ||
if (isPluginEnabled) { | ||
// Ref need scan immediately and add all waiting task to queue | ||
this.taskQueue.push(curRefTask); | ||
for (const waitingTask of waitingTaskList) { | ||
this.taskQueue.push(waitingTask); | ||
} | ||
this.waitingTaskMap.delete(pluginName); | ||
} | ||
return [item]; | ||
else { | ||
// Need waiting to detect enabled, push refTask to waitingList | ||
waitingTaskList.push(curRefTask); | ||
this.waitingTaskMap.set(pluginName, waitingTaskList); | ||
} | ||
} | ||
else { | ||
return []; | ||
// Reverse Merge, The prior of top-level(exists) is higher | ||
const existsPluginConfig = (_b = this.pluginConfigMap[env]) !== null && _b !== void 0 ? _b : {}; | ||
this.pluginConfigMap[env] = (0, merge_1.mergeConfig)(tPluginConfig, existsPluginConfig); | ||
} | ||
/** | ||
* Handler of single scan task(only a ref) | ||
*/ | ||
async run(taskItem) { | ||
var _a; | ||
const { curPath = '', refName, checkPackageVersion } = taskItem; | ||
let basePath = curPath; | ||
if (!path_1.default.isAbsolute(basePath)) { | ||
basePath = path_1.default.resolve(this.root, curPath); | ||
} | ||
})); | ||
return itemWalkResult.reduce((itemList, result) => itemList.concat(result), []); | ||
}; | ||
const handlePluginConfig = async (pluginConfig, basePath, scanCtx, env = constant_1.ARTUS_DEFAULT_CONFIG_ENV.DEFAULT) => { | ||
var _a, _b; | ||
const tPluginConfig = {}; | ||
for (const [pluginName, pluginConfigItem] of Object.entries(pluginConfig)) { | ||
// Set temp pluginConfig in manifest | ||
tPluginConfig[pluginName] = {}; | ||
if (pluginConfigItem.enable !== undefined) { | ||
tPluginConfig[pluginName].enable = pluginConfigItem.enable; | ||
if (this.refMap[refName]) { | ||
// Already scanned | ||
const refItem = this.refMap[refName]; | ||
if (checkPackageVersion && refItem.packageVersion) { | ||
const curPackageVersion = await (0, utils_1.getPackageVersion)(refName === constant_1.DEFAULT_APP_REF ? basePath : refName); | ||
if (curPackageVersion && curPackageVersion !== refItem.packageVersion) { | ||
// Do NOT allow multi-version of plugin package | ||
throw new Error(`${refName} has multi version of ${curPackageVersion}, ${refItem.packageVersion}`); | ||
} | ||
} | ||
return; | ||
} | ||
if (pluginConfigItem.enable) { | ||
scanCtx.enabledPluginSet.add(pluginName); | ||
} | ||
// Resolve ref and set | ||
const isPluginEnabled = scanCtx.enabledPluginSet.has(pluginName); | ||
const ref = await (0, utils_1.resolvePluginConfigItemRef)(pluginConfigItem, basePath, scanCtx); | ||
if (!(ref === null || ref === void 0 ? void 0 : ref.name)) { | ||
continue; | ||
} | ||
tPluginConfig[pluginName].refName = ref.name; | ||
// Generate and push scan task | ||
const curRefTask = { | ||
curPath: ref.path, | ||
refName: ref.name, | ||
checkPackageVersion: ref.isPackage, | ||
const walkOpts = { | ||
baseDir: basePath, | ||
configDir: this.options.configDir, | ||
exclude: this.options.exclude, | ||
extensions: this.options.extensions, | ||
policy: this.options.policy, | ||
source: refName === constant_1.DEFAULT_APP_REF ? 'app' : 'plugin', | ||
unitName: refName, | ||
}; | ||
const waitingTaskList = (_a = scanCtx.waitingTaskMap.get(pluginName)) !== null && _a !== void 0 ? _a : []; | ||
if (isPluginEnabled) { | ||
// Ref need scan immediately and add all waiting task to queue | ||
scanCtx.taskQueue.push(curRefTask); | ||
for (const waitingTask of waitingTaskList) { | ||
scanCtx.taskQueue.push(waitingTask); | ||
} | ||
scanCtx.waitingTaskMap.delete(pluginName); | ||
const refItem = { | ||
packageVersion: await (0, utils_1.getPackageVersion)(basePath), | ||
items: [], | ||
}; | ||
if (await (0, utils_1.isPluginAsync)(basePath)) { | ||
const metaFilePath = path_1.default.resolve(basePath, constant_1.PLUGIN_META_FILENAME); | ||
const pluginMeta = await (0, load_meta_file_1.loadMetaFile)(metaFilePath); | ||
walkOpts.configDir = pluginMeta.configDir || walkOpts.configDir; | ||
walkOpts.exclude = walkOpts.exclude.concat((_a = pluginMeta.exclude) !== null && _a !== void 0 ? _a : []); | ||
walkOpts.unitName = pluginMeta.name; | ||
refItem.pluginMetadata = pluginMeta; | ||
} | ||
else { | ||
// Need waiting to detect enabled, push refTask to waitingList | ||
waitingTaskList.push(curRefTask); | ||
scanCtx.waitingTaskMap.set(pluginName, waitingTaskList); | ||
} | ||
} | ||
// Reverse Merge, The prior of top-level(exists) is higher | ||
const existsPluginConfig = (_b = scanCtx.pluginConfigMap[env]) !== null && _b !== void 0 ? _b : {}; | ||
scanCtx.pluginConfigMap[env] = (0, merge_1.mergeConfig)(tPluginConfig, existsPluginConfig); | ||
}; | ||
exports.handlePluginConfig = handlePluginConfig; | ||
/** | ||
* Handler of single scan task(only a ref) | ||
*/ | ||
const runTask = async (taskItem, scanCtx) => { | ||
var _a; | ||
const { curPath = '', refName, checkPackageVersion } = taskItem; | ||
const { root, refMap, options } = scanCtx; | ||
let basePath = curPath; | ||
if (!path_1.default.isAbsolute(basePath)) { | ||
basePath = path_1.default.resolve(root, curPath); | ||
} | ||
if (refMap[refName]) { | ||
// Already scanned | ||
const refItem = refMap[refName]; | ||
if (checkPackageVersion && refItem.packageVersion) { | ||
const curPackageVersion = await (0, utils_1.getPackageVersion)(refName === constant_1.DEFAULT_APP_REF ? basePath : refName); | ||
if (curPackageVersion && curPackageVersion !== refItem.packageVersion) { | ||
// Do NOT allow multi-version of plugin package | ||
throw new Error(`${refName} has multi version of ${curPackageVersion}, ${refItem.packageVersion}`); | ||
refItem.items = await this.walk(basePath, walkOpts); | ||
const configItemList = refItem.items.filter(item => item.loader === 'config'); | ||
const pluginConfigEnvMap = await (0, utils_1.loadConfigItemList)(configItemList, this.app); | ||
for (const [env, configObj] of Object.entries(pluginConfigEnvMap)) { | ||
const pluginConfig = configObj === null || configObj === void 0 ? void 0 : configObj.plugin; | ||
if (!pluginConfig) { | ||
continue; | ||
} | ||
await this.handlePluginConfig(pluginConfig, basePath, env); | ||
} | ||
return; | ||
} | ||
const walkOpts = { | ||
baseDir: basePath, | ||
configDir: scanCtx.options.configDir, | ||
exclude: scanCtx.options.exclude, | ||
extensions: scanCtx.options.extensions, | ||
policy: scanCtx.options.policy, | ||
source: refName === constant_1.DEFAULT_APP_REF ? 'app' : 'plugin', | ||
unitName: refName, | ||
}; | ||
const refItem = { | ||
packageVersion: await (0, utils_1.getPackageVersion)(basePath), | ||
items: [], | ||
}; | ||
if (await (0, utils_1.isPluginAsync)(basePath)) { | ||
const metaFilePath = path_1.default.resolve(basePath, constant_1.PLUGIN_META_FILENAME); | ||
const pluginMeta = await (0, load_meta_file_1.loadMetaFile)(metaFilePath); | ||
walkOpts.configDir = pluginMeta.configDir || walkOpts.configDir; | ||
walkOpts.exclude = walkOpts.exclude.concat((_a = pluginMeta.exclude) !== null && _a !== void 0 ? _a : []); | ||
walkOpts.unitName = pluginMeta.name; | ||
refItem.pluginMetadata = pluginMeta; | ||
} | ||
refItem.items = await walkDir(basePath, walkOpts); | ||
const configItemList = refItem.items.filter(item => item.loader === 'config'); | ||
const pluginConfigEnvMap = await (0, utils_1.loadConfigItemList)(configItemList, scanCtx); | ||
for (const [env, configObj] of Object.entries(pluginConfigEnvMap)) { | ||
const pluginConfig = configObj === null || configObj === void 0 ? void 0 : configObj.plugin; | ||
if (!pluginConfig) { | ||
continue; | ||
if (this.options.useRelativePath) { | ||
refItem.items = refItem.items.map(item => (Object.assign(Object.assign({}, item), { path: path_1.default.relative(this.root, item.path) }))); | ||
} | ||
await (0, exports.handlePluginConfig)(pluginConfig, basePath, scanCtx, env); | ||
this.refMap[refName] = refItem; | ||
} | ||
if (options.useRelativePath) { | ||
refItem.items = refItem.items.map(item => (Object.assign(Object.assign({}, item), { path: path_1.default.relative(root, item.path) }))); | ||
dump() { | ||
return { | ||
version: '2', | ||
pluginConfig: this.pluginConfigMap, | ||
refMap: this.refMap, | ||
}; | ||
} | ||
refMap[refName] = refItem; | ||
}; | ||
exports.runTask = runTask; | ||
} | ||
exports.ScanTaskRunner = ScanTaskRunner; |
@@ -1,2 +0,2 @@ | ||
import { Manifest, PluginConfigEnvMap, RefMap } from '../loader'; | ||
import { Manifest } from '../loader'; | ||
import { PluginConfig } from '../plugin/types'; | ||
@@ -41,11 +41,1 @@ import { Application } from '../types'; | ||
} | ||
export interface ScanContext { | ||
root: string; | ||
taskQueue: ScanTaskItem[]; | ||
waitingTaskMap: Map<string, ScanTaskItem[]>; | ||
enabledPluginSet: Set<string>; | ||
pluginConfigMap: PluginConfigEnvMap; | ||
refMap: RefMap; | ||
options: ScannerOptions; | ||
app: Application; | ||
} |
import 'reflect-metadata'; | ||
import * as path from 'path'; | ||
import { PluginConfigItem } from '../plugin'; | ||
import { ScanContext } from './types'; | ||
import { Application } from '../types'; | ||
import { ManifestItem } from '../loader'; | ||
@@ -10,4 +10,4 @@ export declare const getPackageVersion: (basePath: string) => Promise<string | undefined>; | ||
export declare const isPluginAsync: (basePath: string) => Promise<boolean>; | ||
export declare const loadConfigItemList: <T = Record<string, any>>(configItemList: ManifestItem[], scanCtx: ScanContext) => Promise<Record<string, T>>; | ||
export declare const resolvePluginConfigItemRef: (pluginConfigItem: PluginConfigItem, baseDir: string, scanCtx: ScanContext) => Promise<{ | ||
export declare const loadConfigItemList: <T = Record<string, any>>(configItemList: ManifestItem[], app: Application) => Promise<Record<string, T>>; | ||
export declare const resolvePluginConfigItemRef: (pluginConfigItem: PluginConfigItem, baseDir: string, root: string) => Promise<{ | ||
name: string; | ||
@@ -14,0 +14,0 @@ path: string; |
@@ -8,3 +8,2 @@ "use strict"; | ||
const fs = tslib_1.__importStar(require("fs/promises")); | ||
const injection_1 = require("@artus/injection"); | ||
const utils_1 = require("../utils"); | ||
@@ -14,4 +13,2 @@ const compatible_require_1 = tslib_1.__importDefault(require("../utils/compatible_require")); | ||
const common_1 = require("../plugin/common"); | ||
const loader_1 = require("../loader"); | ||
const configuration_1 = tslib_1.__importDefault(require("../configuration")); | ||
const getPackageVersion = async (basePath) => { | ||
@@ -54,20 +51,11 @@ try { | ||
exports.isPluginAsync = isPluginAsync; | ||
const loadConfigItemList = async (configItemList, scanCtx) => { | ||
const loadConfigItemList = async (configItemList, app) => { | ||
if (!configItemList.length) { | ||
return {}; | ||
} | ||
const container = new injection_1.Container('_'); | ||
container.set({ | ||
type: configuration_1.default, | ||
}); | ||
container.set({ | ||
id: constant_1.ArtusInjectEnum.Application, | ||
value: scanCtx.app, | ||
}); | ||
const loaderFactory = new loader_1.LoaderFactory(container); | ||
await loaderFactory.loadItemList(configItemList); | ||
return Object.fromEntries(loaderFactory.configurationHandler.configStore.entries()); | ||
await app.loaderFactory.loadItemList(configItemList); | ||
return Object.fromEntries(app.configurationHandler.configStore.entries()); | ||
}; | ||
exports.loadConfigItemList = loadConfigItemList; | ||
const resolvePluginConfigItemRef = async (pluginConfigItem, baseDir, scanCtx) => { | ||
const resolvePluginConfigItemRef = async (pluginConfigItem, baseDir, root) => { | ||
if (pluginConfigItem.package) { | ||
@@ -82,3 +70,3 @@ const refPath = (0, common_1.getPackagePath)(pluginConfigItem.package, [baseDir]); | ||
else if (pluginConfigItem.path) { | ||
const refName = path.isAbsolute(pluginConfigItem.path) ? path.relative(scanCtx.root, pluginConfigItem.path) : pluginConfigItem.path; | ||
const refName = path.isAbsolute(pluginConfigItem.path) ? path.relative(root, pluginConfigItem.path) : pluginConfigItem.path; | ||
let refPath = refName; | ||
@@ -85,0 +73,0 @@ const packageJsonPath = path.resolve(pluginConfigItem.path, 'package.json'); |
import { Container } from '@artus/injection'; | ||
import { HookFunction } from './lifecycle'; | ||
import { Manifest } from './loader'; | ||
import ConfigurationHandler, { ConfigObject } from './configuration'; | ||
import { HookFunction, LifecycleManager } from './lifecycle'; | ||
import { LoaderFactory, Manifest } from './loader'; | ||
import { LoggerType } from './logger'; | ||
@@ -20,3 +21,6 @@ export interface ApplicationLifecycle { | ||
manifest?: Manifest; | ||
config?: Record<string, any>; | ||
config: ConfigObject; | ||
configurationHandler: ConfigurationHandler; | ||
lifecycleManager: LifecycleManager; | ||
loaderFactory: LoaderFactory; | ||
logger: LoggerType; | ||
@@ -23,0 +27,0 @@ load(manifest: Manifest): Promise<this>; |
{ | ||
"name": "@artus/core", | ||
"version": "2.0.0-alpha.7", | ||
"version": "2.0.0-alpha.8", | ||
"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
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
119853
2877
11