@artus/core
Advanced tools
Comparing version 1.0.0-beta.27 to 1.0.0-beta.28
@@ -8,2 +8,3 @@ import 'reflect-metadata'; | ||
import ConfigurationHandler from './configuration'; | ||
import { LoggerType } from './logger'; | ||
export declare class ArtusApplication implements Application { | ||
@@ -20,2 +21,3 @@ manifest?: Manifest; | ||
get configurationHandler(): ConfigurationHandler; | ||
get logger(): LoggerType; | ||
loadDefaultClass(): void; | ||
@@ -22,0 +24,0 @@ load(manifest: Manifest, root?: string): Promise<this>; |
@@ -40,2 +40,5 @@ "use strict"; | ||
} | ||
get logger() { | ||
return this.container.get(logger_1.Logger); | ||
} | ||
loadDefaultClass() { | ||
@@ -42,0 +45,0 @@ // load Artus default clazz |
@@ -5,3 +5,3 @@ "use strict"; | ||
const constant_1 = require("../../constant"); | ||
const plugin_1 = require("../../plugin"); | ||
const common_1 = require("../../plugin/common"); | ||
const utils_1 = require("../../utils"); | ||
@@ -29,3 +29,3 @@ const decorator_1 = require("../decorator"); | ||
const loaderState = item.loaderState; | ||
pluginConfigItem.path = plugin_1.ArtusPlugin.getPath(pluginConfigItem.package, [loaderState === null || loaderState === void 0 ? void 0 : loaderState.baseDir]); | ||
pluginConfigItem.path = (0, common_1.getPackagePath)(pluginConfigItem.package, [loaderState === null || loaderState === void 0 ? void 0 : loaderState.baseDir]); | ||
delete pluginConfigItem.package; | ||
@@ -32,0 +32,0 @@ configObj[pluginName] = pluginConfigItem; |
@@ -1,2 +0,3 @@ | ||
import { Plugin } from './types'; | ||
export declare function topologicalSort(pluginInstanceMap: Map<string, Plugin>, pluginDepEdgeList: [string, string][]): string[]; | ||
import { PluginType } from './types'; | ||
export declare function topologicalSort(pluginInstanceMap: Map<string, PluginType>, pluginDepEdgeList: [string, string][]): string[]; | ||
export declare function getPackagePath(packageName: string, paths?: string[]): string; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.topologicalSort = void 0; | ||
exports.getPackagePath = exports.topologicalSort = void 0; | ||
const tslib_1 = require("tslib"); | ||
const path_1 = tslib_1.__importDefault(require("path")); | ||
// A utils function that toplogical sort plugins | ||
@@ -34,2 +36,8 @@ function topologicalSort(pluginInstanceMap, pluginDepEdgeList) { | ||
exports.topologicalSort = topologicalSort; | ||
// A util function of get package path for plugin | ||
function getPackagePath(packageName, paths) { | ||
const opts = paths ? { paths } : undefined; | ||
return path_1.default.resolve(require.resolve(packageName, opts), '..'); | ||
} | ||
exports.getPackagePath = getPackagePath; | ||
//# sourceMappingURL=common.js.map |
@@ -1,7 +0,5 @@ | ||
import { BasePlugin } from './base'; | ||
import { PluginConfigItem } from './types'; | ||
import { PluginConfigItem, PluginCreateOptions, PluginType } from './types'; | ||
export declare class PluginFactory { | ||
static create(name: string, item: PluginConfigItem): Promise<BasePlugin>; | ||
static createFromConfig(config: Record<string, PluginConfigItem>): Promise<BasePlugin[]>; | ||
static filterDuplicatePlugins(plugins: BasePlugin[]): BasePlugin[]; | ||
static create(name: string, item: PluginConfigItem, opts?: PluginCreateOptions): Promise<PluginType>; | ||
static createFromConfig(config: Record<string, PluginConfigItem>, opts?: PluginCreateOptions): Promise<PluginType[]>; | ||
} |
@@ -7,11 +7,11 @@ "use strict"; | ||
class PluginFactory { | ||
static async create(name, item) { | ||
const pluginInstance = new impl_1.ArtusPlugin(name, item); | ||
static async create(name, item, opts) { | ||
const pluginInstance = new impl_1.Plugin(name, item, opts); | ||
await pluginInstance.init(); | ||
return pluginInstance; | ||
} | ||
static async createFromConfig(config) { | ||
static async createFromConfig(config, opts) { | ||
const pluginInstanceMap = new Map(); | ||
for (const [name, item] of Object.entries(config)) { | ||
const pluginInstance = await PluginFactory.create(name, item); | ||
const pluginInstance = await PluginFactory.create(name, item, opts); | ||
if (pluginInstance.enable) { | ||
@@ -34,17 +34,4 @@ pluginInstanceMap.set(name, pluginInstance); | ||
} | ||
static filterDuplicatePlugins(plugins) { | ||
const exists = new Map(); | ||
const filtedPlugins = []; | ||
for (const plugin of plugins) { | ||
const key = plugin.importPath; | ||
if (exists.get(key)) { | ||
continue; | ||
} | ||
exists.set(key, true); | ||
filtedPlugins.push(plugin); | ||
} | ||
return filtedPlugins; | ||
} | ||
} | ||
exports.PluginFactory = PluginFactory; | ||
//# sourceMappingURL=factory.js.map |
@@ -1,5 +0,14 @@ | ||
import { BasePlugin } from './base'; | ||
export declare class ArtusPlugin extends BasePlugin { | ||
import { PluginConfigItem, PluginCreateOptions, PluginMap, PluginMetadata, PluginType } from './types'; | ||
export declare class Plugin implements PluginType { | ||
name: string; | ||
enable: boolean; | ||
importPath: string; | ||
metadata: Partial<PluginMetadata>; | ||
metaFilePath: string; | ||
private logger?; | ||
constructor(name: string, configItem: PluginConfigItem, opts?: PluginCreateOptions); | ||
init(): Promise<void>; | ||
checkDepExisted(pluginMap: PluginMap): void; | ||
getDepEdgeList(): [string, string][]; | ||
private checkAndLoadMetadata; | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.ArtusPlugin = void 0; | ||
exports.Plugin = void 0; | ||
const tslib_1 = require("tslib"); | ||
const path_1 = tslib_1.__importDefault(require("path")); | ||
const base_1 = require("./base"); | ||
const load_meta_file_1 = require("../utils/load_meta_file"); | ||
const fs_1 = require("../utils/fs"); | ||
const constant_1 = require("../constant"); | ||
class ArtusPlugin extends base_1.BasePlugin { | ||
const common_1 = require("./common"); | ||
class Plugin { | ||
constructor(name, configItem, opts) { | ||
var _a, _b; | ||
this.importPath = ''; | ||
this.metadata = {}; | ||
this.metaFilePath = ''; | ||
this.name = name; | ||
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 = (0, common_1.getPackagePath)(configItem.package); | ||
} | ||
if (!importPath) { | ||
throw new Error(`Plugin ${name} need have path or package field`); | ||
} | ||
this.importPath = importPath; | ||
} | ||
this.logger = opts === null || opts === void 0 ? void 0 : opts.logger; | ||
} | ||
async init() { | ||
@@ -23,2 +45,20 @@ if (!this.enable) { | ||
} | ||
checkDepExisted(pluginMap) { | ||
var _a, _b; | ||
for (const { name: pluginName, optional } of (_a = this.metadata.dependencies) !== null && _a !== void 0 ? _a : []) { | ||
const instance = pluginMap.get(pluginName); | ||
if (!instance || !instance.enable) { | ||
if (optional) { | ||
(_b = this.logger) === null || _b === void 0 ? void 0 : _b.warn(`Plugin ${this.name} need have optional dependence: ${pluginName}.`); | ||
} | ||
else { | ||
throw new Error(`Plugin ${this.name} need have dependence: ${pluginName}.`); | ||
} | ||
} | ||
} | ||
} | ||
getDepEdgeList() { | ||
var _a, _b, _c; | ||
return (_c = (_b = (_a = this.metadata.dependencies) === null || _a === void 0 ? void 0 : _a.filter(({ optional }) => !optional)) === null || _b === void 0 ? void 0 : _b.map(({ name: depPluginName }) => [this.name, depPluginName])) !== null && _c !== void 0 ? _c : []; | ||
} | ||
async checkAndLoadMetadata() { | ||
@@ -42,3 +82,3 @@ // check import path | ||
} | ||
exports.ArtusPlugin = ArtusPlugin; | ||
exports.Plugin = Plugin; | ||
//# sourceMappingURL=impl.js.map |
@@ -1,3 +0,3 @@ | ||
export * from './base'; | ||
export * from './types'; | ||
export * from './impl'; | ||
export * from './factory'; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const tslib_1 = require("tslib"); | ||
tslib_1.__exportStar(require("./base"), exports); | ||
tslib_1.__exportStar(require("./types"), exports); | ||
tslib_1.__exportStar(require("./impl"), exports); | ||
tslib_1.__exportStar(require("./factory"), exports); | ||
//# sourceMappingURL=index.js.map |
@@ -1,4 +0,4 @@ | ||
export declare enum PluginType { | ||
simple = "simple", | ||
module = "module" | ||
import { LoggerType } from '../logger'; | ||
export interface PluginCreateOptions { | ||
logger?: LoggerType; | ||
} | ||
@@ -8,3 +8,3 @@ export interface PluginMetadata { | ||
dependencies?: PluginDependencyItem[]; | ||
type?: PluginType; | ||
type?: 'simple' | 'module' | string; | ||
configDir?: string; | ||
@@ -22,3 +22,4 @@ exclude?: string[]; | ||
} | ||
export interface Plugin { | ||
export declare type PluginMap = Map<string, PluginType>; | ||
export interface PluginType { | ||
name: string; | ||
@@ -30,4 +31,4 @@ enable: boolean; | ||
init(): Promise<void>; | ||
checkDepExisted(map: Map<string, Plugin>): void; | ||
checkDepExisted(map: PluginMap): void; | ||
getDepEdgeList(): [string, string][]; | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.PluginType = void 0; | ||
var PluginType; | ||
(function (PluginType) { | ||
PluginType["simple"] = "simple"; | ||
PluginType["module"] = "module"; | ||
})(PluginType = exports.PluginType || (exports.PluginType = {})); | ||
//# sourceMappingURL=types.js.map |
@@ -64,3 +64,3 @@ "use strict"; | ||
if (this.options.needWriteFile) { | ||
await this.writeFile(`manifest.json`, JSON.stringify(result, null, 2)); | ||
await this.writeFile(path.resolve(root, 'manifest.json'), JSON.stringify(result, null, 2)); | ||
} | ||
@@ -88,3 +88,5 @@ return result; | ||
const pluginConfig = deepmerge_1.default.all([plugin || {}, this.options.plugin || {}]); | ||
const pluginSortedList = await plugin_1.PluginFactory.createFromConfig(pluginConfig); | ||
const pluginSortedList = await plugin_1.PluginFactory.createFromConfig(pluginConfig, { | ||
logger: this.app.logger, | ||
}); | ||
for (const plugin of pluginSortedList) { | ||
@@ -91,0 +93,0 @@ if (!plugin.enable) |
@@ -5,2 +5,3 @@ import { Container } from '@artus/injection'; | ||
import { Manifest } from './loader'; | ||
import { LoggerType } from './logger'; | ||
export interface ApplicationLifecycle { | ||
@@ -22,2 +23,3 @@ configWillLoad?: HookFunction; | ||
config?: Record<string, any>; | ||
logger: LoggerType; | ||
load(manifest: Manifest): Promise<this>; | ||
@@ -24,0 +26,0 @@ run(): Promise<void>; |
{ | ||
"name": "@artus/core", | ||
"version": "1.0.0-beta.27", | ||
"version": "1.0.0-beta.28", | ||
"description": "Core package of Artus", | ||
@@ -5,0 +5,0 @@ "main": "./lib/index.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
162419
159
2463