@artus/core
Advanced tools
Comparing version 1.0.0-beta.21 to 1.0.0-beta.22
@@ -14,4 +14,2 @@ export declare const DEFAULT_LOADER = "module"; | ||
} | ||
export declare const ARTUS_EXCEPTION_DEFAULT_LOCALE = "en"; | ||
export declare const ARTUS_SERVER_ENV = "ARTUS_SERVER_ENV"; | ||
export declare enum ARTUS_DEFAULT_CONFIG_ENV { | ||
@@ -22,2 +20,9 @@ DEV = "development", | ||
} | ||
export declare enum ScanPolicy { | ||
NamedExport = "named_export", | ||
DefaultExport = "default_export", | ||
All = "all" | ||
} | ||
export declare const ARTUS_EXCEPTION_DEFAULT_LOCALE = "en"; | ||
export declare const ARTUS_SERVER_ENV = "ARTUS_SERVER_ENV"; | ||
export declare const HOOK_NAME_META_PREFIX = "hookName:"; | ||
@@ -24,0 +29,0 @@ export declare const HOOK_FILE_LOADER = "appHook:fileLoader"; |
"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.CONFIG_PATTERN = 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_DEFAULT_CONFIG_ENV = exports.ARTUS_SERVER_ENV = exports.ARTUS_EXCEPTION_DEFAULT_LOCALE = exports.ArtusInjectEnum = exports.ArtusInjectPrefix = exports.LOADER_NAME_META = exports.DEFAULT_LOADER = void 0; | ||
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.CONFIG_PATTERN = 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.DEFAULT_LOADER = 'module'; | ||
@@ -18,4 +18,2 @@ exports.LOADER_NAME_META = 'loader:name'; | ||
})(ArtusInjectEnum = exports.ArtusInjectEnum || (exports.ArtusInjectEnum = {})); | ||
exports.ARTUS_EXCEPTION_DEFAULT_LOCALE = 'en'; | ||
exports.ARTUS_SERVER_ENV = 'ARTUS_SERVER_ENV'; | ||
var ARTUS_DEFAULT_CONFIG_ENV; | ||
@@ -27,2 +25,10 @@ (function (ARTUS_DEFAULT_CONFIG_ENV) { | ||
})(ARTUS_DEFAULT_CONFIG_ENV = exports.ARTUS_DEFAULT_CONFIG_ENV || (exports.ARTUS_DEFAULT_CONFIG_ENV = {})); | ||
var ScanPolicy; | ||
(function (ScanPolicy) { | ||
ScanPolicy["NamedExport"] = "named_export"; | ||
ScanPolicy["DefaultExport"] = "default_export"; | ||
ScanPolicy["All"] = "all"; | ||
})(ScanPolicy = exports.ScanPolicy || (exports.ScanPolicy = {})); | ||
exports.ARTUS_EXCEPTION_DEFAULT_LOCALE = 'en'; | ||
exports.ARTUS_SERVER_ENV = 'ARTUS_SERVER_ENV'; | ||
exports.HOOK_NAME_META_PREFIX = 'hookName:'; | ||
@@ -29,0 +35,0 @@ exports.HOOK_FILE_LOADER = 'appHook:fileLoader'; |
@@ -22,3 +22,6 @@ import { Container } from '@artus/injection'; | ||
findLoader(opts: LoaderFindOptions): Promise<LoaderFindResult | null>; | ||
findLoaderName(opts: LoaderFindOptions): Promise<string | null>; | ||
findLoaderName(opts: LoaderFindOptions): Promise<{ | ||
loader: string | null; | ||
exportNames: string[]; | ||
}>; | ||
} |
@@ -76,3 +76,3 @@ "use strict"; | ||
async findLoader(opts) { | ||
const loaderName = await this.findLoaderName(opts); | ||
const { loader: loaderName, exportNames } = await this.findLoaderName(opts); | ||
if (!loaderName) { | ||
@@ -87,2 +87,3 @@ return null; | ||
loaderName, | ||
loaderState: { exportNames }, | ||
}; | ||
@@ -95,26 +96,43 @@ if (loaderClazz.onFind) { | ||
async findLoaderName(opts) { | ||
var _a; | ||
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 loaderName; | ||
return { loader: loaderName, exportNames: [] }; | ||
} | ||
} | ||
const { root, filename } = opts; | ||
const { root, filename, policy = constant_1.ScanPolicy.All } = opts; | ||
// require file for find loader | ||
const targetClazz = await (0, compatible_require_1.default)(path.join(root, filename)); | ||
if (!(0, is_1.isClass)(targetClazz)) { | ||
// The file is not export with default class | ||
return null; | ||
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)}`); | ||
} | ||
// 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) { | ||
return loaderMd.loader; | ||
} | ||
// default loder with @Injectable | ||
const injectableMd = (0, injection_1.isInjectable)(targetClazz); | ||
if (injectableMd) { | ||
return constant_1.DEFAULT_LOADER; | ||
} | ||
return null; | ||
return { loader: (_b = loaders[0]) !== null && _b !== void 0 ? _b : null, exportNames }; | ||
} | ||
@@ -121,0 +139,0 @@ } |
@@ -0,1 +1,2 @@ | ||
import { Constructable } from '@artus/injection'; | ||
import { ManifestItem, Loader } from '../types'; | ||
@@ -5,4 +6,4 @@ declare class ModuleLoader implements Loader { | ||
constructor(container: any); | ||
load(item: ManifestItem): Promise<any>; | ||
load(item: ManifestItem): Promise<Constructable[]>; | ||
} | ||
export default ModuleLoader; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const tslib_1 = require("tslib"); | ||
const injection_1 = require("@artus/injection"); | ||
const decorator_1 = require("../decorator"); | ||
@@ -12,15 +13,23 @@ const compatible_require_1 = tslib_1.__importDefault(require("../../utils/compatible_require")); | ||
async load(item) { | ||
const moduleClazz = await (0, compatible_require_1.default)(item.path); | ||
const opts = { | ||
path: item.path, | ||
type: moduleClazz, | ||
}; | ||
if (item.id) { | ||
opts.id = item.id; | ||
const origin = await (0, compatible_require_1.default)(item.path, true); | ||
item._loaderState = Object.assign({ exportNames: ['default'] }, item._loaderState); | ||
const { _loaderState: state } = item; | ||
const modules = []; | ||
for (const name of state.exportNames) { | ||
const moduleClazz = origin[name]; | ||
const opts = { | ||
path: item.path, | ||
type: moduleClazz, | ||
scope: injection_1.ScopeEnum.EXECUTION, // The class used with @artus/core will have default scope EXECUTION, can be overwritten by Injectable decorator | ||
}; | ||
if (item.id) { | ||
opts.id = item.id; | ||
} | ||
const shouldOverwriteValue = Reflect.getMetadata(constant_1.SHOULD_OVERWRITE_VALUE, moduleClazz); | ||
if (shouldOverwriteValue || !this.container.hasValue(opts)) { | ||
this.container.set(opts); | ||
} | ||
modules.push(moduleClazz); | ||
} | ||
const shouldOverwriteValue = Reflect.getMetadata(constant_1.SHOULD_OVERWRITE_VALUE, moduleClazz); | ||
if (shouldOverwriteValue || !this.container.hasValue(opts)) { | ||
this.container.set(opts); | ||
} | ||
return moduleClazz; | ||
return modules; | ||
} | ||
@@ -27,0 +36,0 @@ }; |
import { Container } from '@artus/injection'; | ||
import { ScanPolicy } from '../constant'; | ||
interface Manifest { | ||
@@ -20,2 +21,3 @@ items: ManifestItem[]; | ||
configDir: string; | ||
policy?: ScanPolicy; | ||
} | ||
@@ -22,0 +24,0 @@ interface LoaderFindResult { |
@@ -25,3 +25,3 @@ "use strict"; | ||
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), { exclude: constant_1.DEFAULT_EXCLUDES.concat((_a = options.exclude) !== null && _a !== void 0 ? _a : []), extensions: [...new Set(this.moduleExtensions.concat((_b = options.extensions) !== null && _b !== void 0 ? _b : []))] }); | ||
this.options = Object.assign(Object.assign({ appName: 'app', needWriteFile: true, useRelativePath: true, configDir: constant_1.DEFAULT_CONFIG_DIR, loaderListGenerator: (defaultLoaderList) => defaultLoaderList, policy: constant_1.ScanPolicy.All }, options), { exclude: constant_1.DEFAULT_EXCLUDES.concat((_a = options.exclude) !== null && _a !== void 0 ? _a : []), extensions: [...new Set(this.moduleExtensions.concat((_b = options.extensions) !== null && _b !== void 0 ? _b : []))] }); | ||
this.app = (_c = options.app) !== null && _c !== void 0 ? _c : new application_1.ArtusApplication(); | ||
@@ -138,3 +138,3 @@ } | ||
} | ||
let loader = await loaderFactory.findLoaderName({ | ||
let { loader } = await loaderFactory.findLoaderName({ | ||
filename, | ||
@@ -144,2 +144,3 @@ baseDir, | ||
configDir, | ||
policy: this.options.policy, | ||
}); | ||
@@ -202,2 +203,3 @@ if (loader === 'framework-config') { | ||
configDir: this.options.configDir, | ||
policy: this.options.policy, | ||
}; | ||
@@ -204,0 +206,0 @@ if (source === 'plugin') { |
@@ -5,2 +5,3 @@ import { BaseLoader, ManifestItem } from "../loader"; | ||
import { Application } from "../types"; | ||
import { ScanPolicy } from "../constant"; | ||
export interface ScannerOptions { | ||
@@ -13,2 +14,3 @@ appName: string; | ||
configDir: string; | ||
policy: ScanPolicy; | ||
envs?: string[]; | ||
@@ -24,2 +26,3 @@ framework?: FrameworkConfig; | ||
configDir: string; | ||
policy: ScanPolicy; | ||
extensions: string[]; | ||
@@ -26,0 +29,0 @@ exclude: string[]; |
@@ -54,2 +54,3 @@ "use strict"; | ||
configDir, | ||
policy: options.policy, | ||
}); | ||
@@ -56,0 +57,0 @@ if (!loaderFindResult) { |
@@ -5,2 +5,2 @@ /** | ||
*/ | ||
export default function compatibleRequire(path: string): Promise<any>; | ||
export default function compatibleRequire(path: string, origin?: boolean): Promise<any>; |
@@ -9,8 +9,8 @@ "use strict"; | ||
*/ | ||
async function compatibleRequire(path) { | ||
async function compatibleRequire(path, origin = false) { | ||
const requiredModule = await Promise.resolve().then(() => tslib_1.__importStar(require(path))); | ||
(0, assert_1.default)(requiredModule, `module '${path}' exports is undefined`); | ||
return requiredModule.default || requiredModule; | ||
return origin ? requiredModule : (requiredModule.default || requiredModule); | ||
} | ||
exports.default = compatibleRequire; | ||
//# sourceMappingURL=compatible_require.js.map |
{ | ||
"name": "@artus/core", | ||
"version": "1.0.0-beta.21", | ||
"version": "1.0.0-beta.22", | ||
"description": "Core package of Artus", | ||
@@ -31,3 +31,3 @@ "main": "./lib/index.js", | ||
"build": "tsc -p ./tsconfig.build.json", | ||
"test": "jest --coverage --detectOpenHandles", | ||
"test": "jest --coverage --detectOpenHandles --testTimeout=15000", | ||
"ci": "npm run lint && npm run test", | ||
@@ -34,0 +34,0 @@ "lint:fix": "eslint . --ext .ts --fix", |
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
166776
2549