@artus/core
Advanced tools
Comparing version 1.0.9 to 2.0.0-alpha.0
@@ -5,3 +5,3 @@ import 'reflect-metadata'; | ||
import { HookFunction, LifecycleManager } from './lifecycle'; | ||
import { LoaderFactory, Manifest } from './loader'; | ||
import { LoaderFactory, Manifest, ManifestV2 } from './loader'; | ||
import { Application, ApplicationInitOptions } from './types'; | ||
@@ -11,3 +11,3 @@ import ConfigurationHandler from './configuration'; | ||
export declare class ArtusApplication implements Application { | ||
manifest?: Manifest; | ||
manifest?: Manifest | ManifestV2; | ||
container: Container; | ||
@@ -23,3 +23,3 @@ protected lifecycleManager: LifecycleManager; | ||
loadDefaultClass(): void; | ||
load(manifest: Manifest, root?: string): Promise<this>; | ||
load(manifest: Manifest | ManifestV2, root?: string): Promise<this>; | ||
run(): Promise<void>; | ||
@@ -26,0 +26,0 @@ registerHook(hookName: string, hookFn: HookFunction): void; |
@@ -19,3 +19,3 @@ "use strict"; | ||
this.lifecycleManager = new lifecycle_1.LifecycleManager(this, this.container); | ||
this.loaderFactory = loader_1.LoaderFactory.create(this.container); | ||
this.loaderFactory = new loader_1.LoaderFactory(this.container); | ||
this.addLoaderListener(); | ||
@@ -92,14 +92,2 @@ this.loadDefaultClass(); | ||
}, | ||
}) | ||
.addLoaderListener('framework-config', { | ||
after: () => this.container.set({ | ||
id: constant_1.ArtusInjectEnum.Frameworks, | ||
value: this.configurationHandler.getFrameworkConfig(), | ||
}), | ||
}) | ||
.addLoaderListener('package-json', { | ||
after: () => this.container.set({ | ||
id: constant_1.ArtusInjectEnum.Packages, | ||
value: this.configurationHandler.getPackages(), | ||
}), | ||
}); | ||
@@ -106,0 +94,0 @@ } |
@@ -14,5 +14,3 @@ import { ManifestItem } from '../loader'; | ||
static getEnvFromFilename(filename: string): string; | ||
private configStore; | ||
private frameworks; | ||
private packages; | ||
configStore: Map<string, ConfigObject>; | ||
getMergedConfig(env?: string): ConfigObject; | ||
@@ -22,6 +20,2 @@ getAllConfig(): ConfigObject; | ||
setConfigByFile(fileItem: ManifestItem): Promise<void>; | ||
getFrameworkConfig(env?: string, key?: string, frameworkMap?: Map<string, FrameworkObject>): Map<string, FrameworkObject>; | ||
addFramework(source: string, framework: FrameworkObject, options: FrameworkOptions): void; | ||
getPackages(): Map<string, PackageObject[]>; | ||
addPackage(source: string, pkg: PackageObject): void; | ||
} |
@@ -9,8 +9,5 @@ "use strict"; | ||
const compatible_require_1 = tslib_1.__importDefault(require("../utils/compatible_require")); | ||
const decorator_1 = require("./decorator"); | ||
let ConfigurationHandler = ConfigurationHandler_1 = class ConfigurationHandler { | ||
constructor() { | ||
this.configStore = new Map(); | ||
this.frameworks = new Map(); | ||
this.packages = new Map(); | ||
} | ||
@@ -49,42 +46,3 @@ static getEnvFromFilename(filename) { | ||
} | ||
getFrameworkConfig(env, key = 'app', frameworkMap = new Map()) { | ||
var _a, _b, _c; | ||
if (!this.frameworks.has(key)) { | ||
return frameworkMap; | ||
} | ||
const currentEnv = (_a = env !== null && env !== void 0 ? env : process.env[constant_1.ARTUS_SERVER_ENV]) !== null && _a !== void 0 ? _a : constant_1.ARTUS_DEFAULT_CONFIG_ENV.DEV; | ||
const list = this.frameworks.get(key); | ||
const defaultConfig = (_b = list.filter(item => item.env === constant_1.ARTUS_DEFAULT_CONFIG_ENV.DEFAULT)[0]) !== null && _b !== void 0 ? _b : {}; | ||
const envConfig = (_c = list.filter(item => item.env === currentEnv)[0]) !== null && _c !== void 0 ? _c : {}; | ||
const config = (0, merge_1.mergeConfig)(defaultConfig, envConfig); | ||
frameworkMap.set(key, config); | ||
if (config.path) { | ||
this.getFrameworkConfig(env, config.path, frameworkMap); | ||
} | ||
return frameworkMap; | ||
} | ||
addFramework(source, framework, options) { | ||
var _a; | ||
const key = options.unitName || source; | ||
const list = (_a = this.frameworks.get(key)) !== null && _a !== void 0 ? _a : []; | ||
framework.env = options.env; | ||
list.push(framework); | ||
this.frameworks.set(key, list); | ||
} | ||
getPackages() { | ||
return this.packages; | ||
} | ||
addPackage(source, pkg) { | ||
var _a; | ||
const list = (_a = this.packages.get(source)) !== null && _a !== void 0 ? _a : []; | ||
list.push(pkg); | ||
this.packages.set(source, list); | ||
} | ||
}; | ||
tslib_1.__decorate([ | ||
(0, decorator_1.DefineConfigHandle)('framework-config'), | ||
tslib_1.__metadata("design:type", Function), | ||
tslib_1.__metadata("design:paramtypes", [String, Object, Object]), | ||
tslib_1.__metadata("design:returntype", Map) | ||
], ConfigurationHandler.prototype, "getFrameworkConfig", null); | ||
ConfigurationHandler = ConfigurationHandler_1 = tslib_1.__decorate([ | ||
@@ -91,0 +49,0 @@ (0, injection_1.Injectable)() |
@@ -26,4 +26,4 @@ export declare const DEFAULT_LOADER = "module"; | ||
export declare const HOOK_FILE_LOADER = "appHook:fileLoader"; | ||
export declare const HOOK_CONFIG_HANDLE = "appHook:configHandle::"; | ||
export declare const DEFAULT_EXCLUDES: string[]; | ||
export declare const DEFAULT_MODULE_EXTENSIONS: string[]; | ||
export declare const FRAMEWORK_PATTERN = "framework.*"; | ||
@@ -36,2 +36,3 @@ export declare const PLUGIN_CONFIG_PATTERN = "plugin.*"; | ||
export declare const DEFAULT_CONFIG_DIR = "src/config"; | ||
export declare const DEFAULT_MANIFEST_FILENAME = "manifest.json"; | ||
export declare const SHOULD_OVERWRITE_VALUE = "shouldOverwrite"; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.SHOULD_OVERWRITE_VALUE = exports.DEFAULT_CONFIG_DIR = exports.DEFAULT_LOADER_LIST_WITH_ORDER = exports.EXCEPTION_FILENAME = exports.PLUGIN_META_FILENAME = exports.PACKAGE_JSON = exports.PLUGIN_CONFIG_PATTERN = exports.FRAMEWORK_PATTERN = exports.DEFAULT_EXCLUDES = exports.HOOK_CONFIG_HANDLE = exports.HOOK_FILE_LOADER = exports.HOOK_NAME_META_PREFIX = exports.ARTUS_SERVER_ENV = exports.ARTUS_EXCEPTION_DEFAULT_LOCALE = exports.ScanPolicy = exports.ARTUS_DEFAULT_CONFIG_ENV = exports.ArtusInjectEnum = exports.ArtusInjectPrefix = exports.LOADER_NAME_META = exports.DEFAULT_LOADER = void 0; | ||
exports.SHOULD_OVERWRITE_VALUE = exports.DEFAULT_MANIFEST_FILENAME = exports.DEFAULT_CONFIG_DIR = exports.DEFAULT_LOADER_LIST_WITH_ORDER = exports.EXCEPTION_FILENAME = exports.PLUGIN_META_FILENAME = exports.PACKAGE_JSON = exports.PLUGIN_CONFIG_PATTERN = exports.FRAMEWORK_PATTERN = exports.DEFAULT_MODULE_EXTENSIONS = exports.DEFAULT_EXCLUDES = exports.HOOK_FILE_LOADER = exports.HOOK_NAME_META_PREFIX = exports.ARTUS_SERVER_ENV = exports.ARTUS_EXCEPTION_DEFAULT_LOCALE = exports.ScanPolicy = exports.ARTUS_DEFAULT_CONFIG_ENV = exports.ArtusInjectEnum = exports.ArtusInjectPrefix = exports.LOADER_NAME_META = exports.DEFAULT_LOADER = void 0; | ||
exports.DEFAULT_LOADER = 'module'; | ||
@@ -32,3 +32,2 @@ exports.LOADER_NAME_META = 'loader:name'; | ||
exports.HOOK_FILE_LOADER = 'appHook:fileLoader'; | ||
exports.HOOK_CONFIG_HANDLE = 'appHook:configHandle::'; | ||
exports.DEFAULT_EXCLUDES = [ | ||
@@ -45,2 +44,3 @@ 'test', | ||
]; | ||
exports.DEFAULT_MODULE_EXTENSIONS = ['.js', '.json', '.node']; | ||
exports.FRAMEWORK_PATTERN = 'framework.*'; | ||
@@ -62,2 +62,3 @@ exports.PLUGIN_CONFIG_PATTERN = 'plugin.*'; | ||
exports.DEFAULT_CONFIG_DIR = 'src/config'; | ||
exports.DEFAULT_MANIFEST_FILENAME = 'manifest.json'; | ||
exports.SHOULD_OVERWRITE_VALUE = 'shouldOverwrite'; |
import { Container } from '@artus/injection'; | ||
import { Manifest, ManifestItem, LoaderConstructor, Loader, LoaderFindOptions, LoaderFindResult } from './types'; | ||
import { Manifest, ManifestItem, LoaderConstructor, Loader, ManifestV2 } from './types'; | ||
import ConfigurationHandler from '../configuration'; | ||
import { LifecycleManager } from '../lifecycle'; | ||
import { LoaderEventListener } from './loader_event'; | ||
import { LoggerType } from '../logger'; | ||
export declare class LoaderFactory { | ||
static loaderClazzMap: Map<string, LoaderConstructor>; | ||
static register(clazz: LoaderConstructor): void; | ||
private container; | ||
private static loaderClazzMap; | ||
private loaderEmitter; | ||
static register(clazz: LoaderConstructor): void; | ||
constructor(container: Container); | ||
static create(container: Container): LoaderFactory; | ||
get lifecycleManager(): LifecycleManager; | ||
get configurationHandler(): ConfigurationHandler; | ||
get logger(): LoggerType; | ||
addLoaderListener(eventName: string, listener: LoaderEventListener): this; | ||
removeLoaderListener(eventName: string, stage?: 'before' | 'after'): this; | ||
getLoader(loaderName: string): Loader; | ||
loadManifest(manifest: Manifest, root?: string): Promise<void>; | ||
loadManifest(manifest: Manifest | ManifestV2, root?: string, envList?: string[]): Promise<void>; | ||
loadItemList(itemList?: ManifestItem[], root?: string): Promise<void>; | ||
loadItem(item: ManifestItem): Promise<any>; | ||
findLoader(opts: LoaderFindOptions): Promise<LoaderFindResult | null>; | ||
findLoaderName(opts: LoaderFindOptions): Promise<{ | ||
loader: string | null; | ||
exportNames: string[]; | ||
}>; | ||
} |
@@ -6,8 +6,7 @@ "use strict"; | ||
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 compatible_require_1 = tslib_1.__importDefault(require("../utils/compatible_require")); | ||
const loader_event_1 = tslib_1.__importDefault(require("./loader_event")); | ||
const is_1 = require("../utils/is"); | ||
const plugin_1 = require("../plugin"); | ||
const logger_1 = require("../logger"); | ||
class LoaderFactory { | ||
@@ -22,5 +21,2 @@ static register(clazz) { | ||
} | ||
static create(container) { | ||
return new LoaderFactory(container); | ||
} | ||
get lifecycleManager() { | ||
@@ -32,2 +28,5 @@ return this.container.get(constant_1.ArtusInjectEnum.LifecycleManager); | ||
} | ||
get logger() { | ||
return this.container.get(logger_1.Logger); | ||
} | ||
addLoaderListener(eventName, listener) { | ||
@@ -48,4 +47,40 @@ this.loaderEmitter.addListener(eventName, listener); | ||
} | ||
async loadManifest(manifest, root) { | ||
await this.loadItemList(manifest.items, root); | ||
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]; } | ||
if ('version' in manifest && manifest.version === '2') { | ||
// Manifest Version 2 is supported mainly | ||
// Merge plugin config with ref | ||
const validEnvList = [constant_1.ARTUS_DEFAULT_CONFIG_ENV.DEFAULT].concat(envList); | ||
for (const env of validEnvList) { | ||
this.configurationHandler.setConfig(env, { | ||
plugin: (_c = (_b = manifest.pluginConfig) === null || _b === void 0 ? void 0 : _b[env]) !== null && _c !== void 0 ? _c : {}, | ||
}); | ||
} | ||
const mergedPluginConfig = (_e = (_d = this.configurationHandler.getAllConfig()) === null || _d === void 0 ? void 0 : _d.plugin) !== null && _e !== void 0 ? _e : {}; | ||
for (const pluginConfigItem of Object.values(mergedPluginConfig)) { | ||
const refItem = manifest.refMap[pluginConfigItem.refName]; | ||
pluginConfigItem.metadata = refItem.pluginMetadata; | ||
} | ||
// sort ref(plugin) firstly | ||
const sortedPluginList = await plugin_1.PluginFactory.createFromConfig(mergedPluginConfig, { | ||
logger: this.logger, | ||
}); | ||
// Merge itemList | ||
let itemList = []; | ||
const sortedRefNameList = sortedPluginList | ||
.map(plugin => { var _a; return ((plugin.enable && ((_a = mergedPluginConfig[plugin.name]) === null || _a === void 0 ? void 0 : _a.refName)) || null); }) | ||
.concat(['_app']); | ||
for (const refName of sortedRefNameList) { | ||
const refItem = manifest.refMap[refName]; | ||
itemList = itemList.concat(refItem.items); | ||
} | ||
// Load final item list(non-ordered) | ||
await this.loadItemList(itemList, root); | ||
} | ||
else if ('items' in manifest) { | ||
// Fallback Manifest Version 1 | ||
await this.loadItemList(manifest.items, root); | ||
} | ||
} | ||
@@ -61,3 +96,7 @@ async loadItemList(itemList = [], root) { | ||
} | ||
itemMap.get(item.loader).push(Object.assign(Object.assign({}, item), { path: root ? path.join(root, item.path) : item.path, loader: (_a = item.loader) !== null && _a !== void 0 ? _a : constant_1.DEFAULT_LOADER })); | ||
let resolvedPath = item.path; | ||
if (root && !path.isAbsolute(resolvedPath)) { | ||
resolvedPath = path.resolve(root, resolvedPath); | ||
} | ||
itemMap.get(item.loader).push(Object.assign(Object.assign({}, item), { path: resolvedPath, loader: (_a = item.loader) !== null && _a !== void 0 ? _a : constant_1.DEFAULT_LOADER })); | ||
} | ||
@@ -82,65 +121,4 @@ // trigger loader | ||
} | ||
async findLoader(opts) { | ||
const { loader: loaderName, exportNames } = await this.findLoaderName(opts); | ||
if (!loaderName) { | ||
return null; | ||
} | ||
const loaderClazz = LoaderFactory.loaderClazzMap.get(loaderName); | ||
if (!loaderClazz) { | ||
throw new Error(`Cannot find loader '${loaderName}'`); | ||
} | ||
const result = { | ||
loaderName, | ||
loaderState: { exportNames }, | ||
}; | ||
if (loaderClazz.onFind) { | ||
result.loaderState = await loaderClazz.onFind(opts); | ||
} | ||
return result; | ||
} | ||
async findLoaderName(opts) { | ||
var _a, _b; | ||
for (const [loaderName, LoaderClazz] of LoaderFactory.loaderClazzMap.entries()) { | ||
if (await ((_a = LoaderClazz.is) === null || _a === void 0 ? void 0 : _a.call(LoaderClazz, opts))) { | ||
return { loader: loaderName, exportNames: [] }; | ||
} | ||
} | ||
const { root, filename, policy = constant_1.ScanPolicy.All } = opts; | ||
// require file for find loader | ||
const allExport = await (0, compatible_require_1.default)(path.join(root, filename), true); | ||
const exportNames = []; | ||
let loaders = Object.entries(allExport) | ||
.map(([name, targetClazz]) => { | ||
if (!(0, is_1.isClass)(targetClazz)) { | ||
// The file is not export with default class | ||
return null; | ||
} | ||
if (policy === constant_1.ScanPolicy.NamedExport && name === 'default') { | ||
return null; | ||
} | ||
if (policy === constant_1.ScanPolicy.DefaultExport && name !== 'default') { | ||
return null; | ||
} | ||
// get loader from reflect metadata | ||
const loaderMd = Reflect.getMetadata(constant_1.HOOK_FILE_LOADER, targetClazz); | ||
if (loaderMd === null || loaderMd === void 0 ? void 0 : loaderMd.loader) { | ||
exportNames.push(name); | ||
return loaderMd.loader; | ||
} | ||
// default loder with @Injectable | ||
const injectableMd = (0, injection_1.isInjectable)(targetClazz); | ||
if (injectableMd) { | ||
exportNames.push(name); | ||
return constant_1.DEFAULT_LOADER; | ||
} | ||
}) | ||
.filter(v => v); | ||
loaders = Array.from(new Set(loaders)); | ||
if (loaders.length > 1) { | ||
throw new Error(`Not support multiple loaders for ${path.join(root, filename)}`); | ||
} | ||
return { loader: (_b = loaders[0]) !== null && _b !== void 0 ? _b : null, exportNames }; | ||
} | ||
} | ||
exports.LoaderFactory = LoaderFactory; | ||
LoaderFactory.loaderClazzMap = new Map(); |
@@ -11,3 +11,3 @@ "use strict"; | ||
const config_file_meta_1 = require("../utils/config_file_meta"); | ||
const plugin_1 = require("../../plugin"); | ||
// import { PluginFactory } from '../../plugin'; | ||
let ConfigLoader = class ConfigLoader { | ||
@@ -34,9 +34,9 @@ constructor(container) { | ||
let configObj = await this.loadConfigFile(item); | ||
if (namespace === 'plugin') { | ||
// if (namespace === 'plugin') { | ||
// configObj = { | ||
// plugin: await PluginFactory.formatPluginConfig(configObj, item), | ||
// }; | ||
// } else | ||
if (namespace) { | ||
configObj = { | ||
plugin: await plugin_1.PluginFactory.formatPluginConfig(configObj, item), | ||
}; | ||
} | ||
else if (namespace) { | ||
configObj = { | ||
[namespace]: configObj, | ||
@@ -43,0 +43,0 @@ }; |
@@ -7,4 +7,2 @@ import ModuleLoader from './module'; | ||
import PluginMetaLoader from './plugin_meta'; | ||
import FrameworkConfigLoader from './framework_config'; | ||
import PackageLoader from './package'; | ||
export { ModuleLoader, ConfigLoader, ExceptionLoader, ExceptionFilterLoader, LifecycleLoader, PluginMetaLoader, FrameworkConfigLoader, PackageLoader, }; | ||
export { ModuleLoader, ConfigLoader, ExceptionLoader, ExceptionFilterLoader, LifecycleLoader, PluginMetaLoader, }; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.PackageLoader = exports.FrameworkConfigLoader = exports.PluginMetaLoader = exports.LifecycleLoader = exports.ExceptionFilterLoader = exports.ExceptionLoader = exports.ConfigLoader = exports.ModuleLoader = void 0; | ||
exports.PluginMetaLoader = exports.LifecycleLoader = exports.ExceptionFilterLoader = exports.ExceptionLoader = exports.ConfigLoader = exports.ModuleLoader = void 0; | ||
const tslib_1 = require("tslib"); | ||
@@ -17,5 +17,1 @@ const module_1 = tslib_1.__importDefault(require("./module")); | ||
exports.PluginMetaLoader = plugin_meta_1.default; | ||
const framework_config_1 = tslib_1.__importDefault(require("./framework_config")); | ||
exports.FrameworkConfigLoader = framework_config_1.default; | ||
const package_1 = tslib_1.__importDefault(require("./package")); | ||
exports.PackageLoader = package_1.default; |
import { LoaderFactory } from './factory'; | ||
import BaseLoader from './base'; | ||
export * from './helper'; | ||
export * from './types'; | ||
export { LoaderFactory, BaseLoader, }; |
@@ -15,2 +15,3 @@ "use strict"; | ||
} | ||
tslib_1.__exportStar(require("./helper"), exports); | ||
tslib_1.__exportStar(require("./types"), exports); |
import { Container } from '@artus/injection'; | ||
import { ScanPolicy } from '../constant'; | ||
import { PluginConfigItem } from '../plugin/types'; | ||
interface Manifest { | ||
import { PluginConfigItem, PluginMetadata } from '../plugin/types'; | ||
export interface Manifest { | ||
items: ManifestItem[]; | ||
@@ -9,3 +9,17 @@ pluginConfig?: Record<string, PluginConfigItem>; | ||
} | ||
interface ManifestItem<LoaderState = unknown> extends Record<string, any> { | ||
export type ManifestEnvMap = Record<string, Manifest>; | ||
export type ManifestV2PluginConfig = Record<string, Record<string, PluginConfigItem>>; | ||
export interface ManifestV2RefMapItem { | ||
packageVersion?: string; | ||
pluginMetadata?: PluginMetadata; | ||
items: ManifestItem[]; | ||
} | ||
export type ManifestV2RefMap = Record<string, ManifestV2RefMapItem>; | ||
export interface ManifestV2 { | ||
version: '2'; | ||
pluginConfig: ManifestV2PluginConfig; | ||
refMap: ManifestV2RefMap; | ||
relative: boolean; | ||
} | ||
export interface ManifestItem<LoaderState = unknown> extends Record<string, any> { | ||
path: string; | ||
@@ -19,3 +33,3 @@ extname: string; | ||
} | ||
interface LoaderFindOptions { | ||
export interface LoaderFindOptions { | ||
filename: string; | ||
@@ -27,11 +41,11 @@ root: string; | ||
} | ||
interface LoaderFindResult { | ||
export interface LoaderFindResult { | ||
loaderName: string; | ||
loaderState?: unknown; | ||
} | ||
interface LoaderHookUnit { | ||
export interface LoaderHookUnit { | ||
before?: Function; | ||
after?: Function; | ||
} | ||
interface LoaderConstructor { | ||
export interface LoaderConstructor { | ||
new (container: Container): Loader; | ||
@@ -41,6 +55,5 @@ is?(opts: LoaderFindOptions): Promise<boolean>; | ||
} | ||
interface Loader { | ||
export interface Loader { | ||
state?: any; | ||
load(item: ManifestItem): Promise<any>; | ||
} | ||
export { Manifest, ManifestItem, LoaderHookUnit, LoaderConstructor, Loader, LoaderFindOptions, LoaderFindResult, }; |
@@ -6,3 +6,3 @@ import { PluginConfigItem, PluginCreateOptions, PluginMap, PluginMetadata, PluginType } from './types'; | ||
importPath: string; | ||
metadata: Partial<PluginMetadata>; | ||
metadata: Partial<PluginMetadata> | null; | ||
metaFilePath: string; | ||
@@ -9,0 +9,0 @@ private logger?; |
@@ -14,3 +14,3 @@ "use strict"; | ||
this.importPath = ''; | ||
this.metadata = {}; | ||
this.metadata = null; | ||
this.metaFilePath = ''; | ||
@@ -27,3 +27,3 @@ this.name = name; | ||
} | ||
if (!importPath) { | ||
if (!importPath && !configItem.refName) { | ||
throw new Error(`Plugin ${name} need have path or package field`); | ||
@@ -33,2 +33,5 @@ } | ||
} | ||
if (configItem.metadata) { | ||
this.metadata = configItem.metadata; | ||
} | ||
this.logger = opts === null || opts === void 0 ? void 0 : opts.logger; | ||
@@ -67,2 +70,6 @@ } | ||
async checkAndLoadMetadata() { | ||
// check metadata from configItem | ||
if (this.metadata) { | ||
return; | ||
} | ||
// check import path | ||
@@ -69,0 +76,0 @@ if (!await (0, fs_1.exists)(this.importPath)) { |
@@ -20,2 +20,4 @@ import { LoggerType } from '../logger'; | ||
package?: string; | ||
refName?: string; | ||
metadata?: PluginMetadata; | ||
} | ||
@@ -22,0 +24,0 @@ export type PluginMap = Map<string, PluginType>; |
@@ -1,2 +0,2 @@ | ||
export * from './scan'; | ||
export * from './impl'; | ||
export * from './types'; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const tslib_1 = require("tslib"); | ||
tslib_1.__exportStar(require("./scan"), exports); | ||
tslib_1.__exportStar(require("./impl"), exports); | ||
tslib_1.__exportStar(require("./types"), exports); |
@@ -1,6 +0,5 @@ | ||
import { BaseLoader, ManifestItem } from "../loader"; | ||
import { FrameworkConfig } from "../framework"; | ||
import { PluginConfigItem } from "../plugin/types"; | ||
import { Application } from "../types"; | ||
import { ScanPolicy } from "../constant"; | ||
import { ManifestEnvMap, ManifestV2, ManifestV2PluginConfig, ManifestV2RefMapItem } from '../loader'; | ||
import { PluginConfigItem } from '../plugin/types'; | ||
import { Application } from '../types'; | ||
import { ScanPolicy } from '../constant'; | ||
export interface ScannerOptions { | ||
@@ -15,9 +14,6 @@ appName: string; | ||
envs?: string[]; | ||
framework?: FrameworkConfig; | ||
plugin?: Record<string, Partial<PluginConfigItem>>; | ||
loaderListGenerator: (defaultLoaderList: string[]) => (string | typeof BaseLoader)[]; | ||
app?: Application; | ||
} | ||
export interface WalkOptions { | ||
source: string; | ||
baseDir: string; | ||
@@ -28,4 +24,2 @@ configDir: string; | ||
exclude: string[]; | ||
itemMap: Map<string, ManifestItem[]>; | ||
unitName?: string; | ||
} | ||
@@ -36,1 +30,18 @@ export interface LoaderOptions { | ||
} | ||
export interface ScannerConstructor { | ||
new (opts?: Partial<ScannerOptions>): ScannerType; | ||
} | ||
export interface ScannerType { | ||
scan(root: string): Promise<ManifestEnvMap | ManifestV2>; | ||
} | ||
export interface ScanTaskItem { | ||
root: string; | ||
subPath: string; | ||
refName: string; | ||
} | ||
export interface ScanContext { | ||
taskQueue: ScanTaskItem[]; | ||
pluginConfigMap: ManifestV2PluginConfig; | ||
refMap: Record<string, ManifestV2RefMapItem>; | ||
options: ScannerOptions; | ||
} |
import 'reflect-metadata'; | ||
import { LoaderFactory } from '../loader'; | ||
import { WalkOptions } from './types'; | ||
export declare class ScanUtils { | ||
static loaderFactory: LoaderFactory; | ||
static walk(root: string, options: WalkOptions): Promise<void>; | ||
static isExclude(filename: string, extname: string, exclude: string[], extensions: string[]): boolean; | ||
static exist(dir: string, filenames: string[]): boolean; | ||
} | ||
import { PluginConfigItem } from '../plugin'; | ||
export declare const getPackageVersion: (basePath: string) => Promise<string | undefined>; | ||
export declare const existsAsync: (filePath: string) => Promise<boolean>; | ||
export declare const isExclude: (filename: string, extname: string, exclude: string[], extensions: string[]) => boolean; | ||
export declare const isPluginAsync: (basePath: string) => Promise<boolean>; | ||
export declare const getPluginRefName: (pluginConfigItem: PluginConfigItem, root: string) => string | undefined; | ||
export declare const getPluginRefPath: (pluginConfigItem: PluginConfigItem, root: string, baseDir: string) => Promise<string | undefined>; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.ScanUtils = void 0; | ||
exports.getPluginRefPath = exports.getPluginRefName = exports.isPluginAsync = exports.isExclude = exports.existsAsync = exports.getPackageVersion = void 0; | ||
const tslib_1 = require("tslib"); | ||
@@ -8,89 +8,65 @@ require("reflect-metadata"); | ||
const fs = tslib_1.__importStar(require("fs/promises")); | ||
const fs_1 = require("fs"); | ||
const injection_1 = require("@artus/injection"); | ||
const utils_1 = require("../utils"); | ||
const compatible_require_1 = tslib_1.__importDefault(require("../utils/compatible_require")); | ||
const constant_1 = require("../constant"); | ||
const loader_1 = require("../loader"); | ||
const utils_1 = require("../utils"); | ||
class ScanUtils { | ||
static async walk(root, options) { | ||
var _a; | ||
const { source, unitName, baseDir, configDir } = options; | ||
if (!(0, fs_1.existsSync)(root)) { | ||
// TODO: use artus logger instead | ||
console.warn(`[scan->walk] ${root} is not exists.`); | ||
return; | ||
} | ||
const stat = await fs.stat(root); | ||
if (!stat.isDirectory()) { | ||
return; | ||
} | ||
const items = await fs.readdir(root); | ||
for (const item of items) { | ||
const realPath = path.resolve(root, item); | ||
const extname = path.extname(realPath); | ||
if (this.isExclude(item, extname, options.exclude, options.extensions)) { | ||
continue; | ||
} | ||
const itemStat = await fs.stat(realPath); | ||
if (itemStat.isDirectory()) { | ||
// ignore plugin dir | ||
if (this.exist(realPath, [constant_1.PLUGIN_META_FILENAME])) { | ||
continue; | ||
} | ||
await ScanUtils.walk(realPath, options); | ||
continue; | ||
} | ||
if (itemStat.isFile()) { | ||
if (!extname) { | ||
// Exclude file without extname | ||
continue; | ||
} | ||
const filename = path.basename(realPath); | ||
const filenameWithoutExt = path.basename(realPath, extname); | ||
const loaderFindResult = await ScanUtils.loaderFactory.findLoader({ | ||
filename, | ||
root, | ||
baseDir, | ||
configDir, | ||
policy: options.policy, | ||
}); | ||
if (!loaderFindResult) { | ||
continue; | ||
} | ||
const { loaderName, loaderState } = loaderFindResult; | ||
const item = { | ||
path: options.extensions.includes(extname) ? path.resolve(root, filenameWithoutExt) : realPath, | ||
extname, | ||
filename, | ||
loader: loaderName, | ||
source, | ||
}; | ||
if (loaderState) { | ||
item.loaderState = loaderState; | ||
} | ||
unitName && (item.unitName = unitName); | ||
const itemList = options.itemMap.get((_a = item.loader) !== null && _a !== void 0 ? _a : constant_1.DEFAULT_LOADER); | ||
if (Array.isArray(itemList)) { | ||
itemList.push(item); | ||
} | ||
} | ||
} | ||
const common_1 = require("../plugin/common"); | ||
const getPackageVersion = async (basePath) => { | ||
try { | ||
const packageJsonPath = path.resolve(basePath, 'package.json'); | ||
const packageJson = await (0, compatible_require_1.default)(packageJsonPath); | ||
return packageJson === null || packageJson === void 0 ? void 0 : packageJson.version; | ||
} | ||
static isExclude(filename, extname, exclude, extensions) { | ||
let result = false; | ||
if (!result && exclude) { | ||
result = (0, utils_1.isMatch)(filename, exclude); | ||
} | ||
if (!result && extname) { | ||
result = !extensions.includes(extname); | ||
} | ||
return result; | ||
catch (error) { | ||
return undefined; | ||
} | ||
static exist(dir, filenames) { | ||
return filenames.some(filename => { | ||
return (0, fs_1.existsSync)(path.resolve(dir, `${filename}`)); | ||
}); | ||
}; | ||
exports.getPackageVersion = getPackageVersion; | ||
const existsAsync = async (filePath) => { | ||
try { | ||
await fs.access(filePath); | ||
return true; | ||
} | ||
} | ||
exports.ScanUtils = ScanUtils; | ||
ScanUtils.loaderFactory = loader_1.LoaderFactory.create(new injection_1.Container(constant_1.ArtusInjectEnum.DefaultContainerName)); | ||
catch (error) { | ||
return false; | ||
} | ||
}; | ||
exports.existsAsync = existsAsync; | ||
const isExclude = (filename, extname, exclude, extensions) => { | ||
let result = false; | ||
if (!result && exclude) { | ||
result = (0, utils_1.isMatch)(filename, exclude); | ||
} | ||
if (!result && extname) { | ||
result = !extensions.includes(extname); | ||
} | ||
return result; | ||
}; | ||
exports.isExclude = isExclude; | ||
const isPluginAsync = (basePath) => { | ||
return (0, exports.existsAsync)(path.resolve(basePath, constant_1.PLUGIN_META_FILENAME)); | ||
}; | ||
exports.isPluginAsync = isPluginAsync; | ||
const getPluginRefName = (pluginConfigItem, root) => { | ||
if (pluginConfigItem.package) { | ||
return pluginConfigItem.package; | ||
} | ||
if (pluginConfigItem.path) { | ||
return path.isAbsolute(pluginConfigItem.path) ? path.relative(root, pluginConfigItem.path) : pluginConfigItem.path; | ||
} | ||
return undefined; | ||
}; | ||
exports.getPluginRefName = getPluginRefName; | ||
const getPluginRefPath = async (pluginConfigItem, root, baseDir) => { | ||
if (pluginConfigItem.package) { | ||
return (0, common_1.getPackagePath)(pluginConfigItem.package, [baseDir]); | ||
} | ||
else if (pluginConfigItem.path && await (0, exports.existsAsync)(path.resolve(pluginConfigItem.path, 'package.json'))) { | ||
const pluginEntryPath = await (0, common_1.getInlinePackageEntryPath)(pluginConfigItem.path); | ||
return path.relative(root, pluginEntryPath); | ||
} | ||
else if (pluginConfigItem.path) { | ||
return path.isAbsolute(pluginConfigItem.path) ? path.relative(root, pluginConfigItem.path) : pluginConfigItem.path; | ||
} | ||
return undefined; | ||
}; | ||
exports.getPluginRefPath = getPluginRefPath; |
import { Container } from '@artus/injection'; | ||
import { BaseContext } from '@artus/pipeline'; | ||
import { HookFunction } from './lifecycle'; | ||
import { Manifest } from './loader'; | ||
import { Manifest, ManifestV2 } from './loader'; | ||
import { LoggerType } from './logger'; | ||
@@ -20,3 +20,3 @@ export interface ApplicationLifecycle { | ||
container: Container; | ||
manifest?: Manifest; | ||
manifest?: Manifest | ManifestV2; | ||
config?: Record<string, any>; | ||
@@ -23,0 +23,0 @@ logger: LoggerType; |
{ | ||
"name": "@artus/core", | ||
"version": "1.0.9", | ||
"version": "2.0.0-alpha.0", | ||
"description": "Core package of Artus", | ||
@@ -5,0 +5,0 @@ "main": "./lib/index.js", |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
118430
117
2821
1
11