Skip to content

Commit

Permalink
refactor: next version manifest & scanner (#245)
Browse files Browse the repository at this point in the history
* feat(scanner): refactor scanner workflow by task-queue (#242)

* refactor: support load manifest version 2 (#243)

* refactor(loader): strip findLoader

* refactor(loader): support manifest version 2

* refactor: remove framework (#244)

* feat: scanner typings & config handler

* feat: support env list

* refactor: not support manifest version 1

* test: better windows support

* refactor: remove trigger & pipeline

* refactor: fixed order for walkDir

* feat: scanner support manifest filePath

* refactor: optimize scanner to speed up

* fix: scanner resolve plugin root

* fix: scanner exclude path

* refactor: use IoC to manage env

* refactor: use IoC to manage all runtime class

* refactor(scanner): use ScanTaskRunner instead of fns

* test: add plugin preset case

* chore(plugin): typo

* fix(scanner): order of plugin task for correct behavior of deepmerge

* fix: loader/lifecycle default class scope

* fix(loader): side effect of metadata

* feat(scanner): multipie version check

* fix(scanner): order of app

* feat(scanner): add scan check for different path with same version

* refactor(scanner): manage task queue by ScanTaskRunner

* test: handle new relativedPath field for windows
  • Loading branch information
noahziheng authored May 18, 2023
1 parent e0e233e commit ba0649d
Show file tree
Hide file tree
Showing 178 changed files with 1,912 additions and 3,041 deletions.
7 changes: 1 addition & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@artus/core",
"version": "1.0.9",
"version": "2.0.0",
"description": "Core package of Artus",
"main": "./lib/index.js",
"types": "./lib/index.d.ts",
Expand All @@ -13,10 +13,6 @@
"types": "./lib/injection.d.ts",
"default": "./lib/injection.js"
},
"./pipeline": {
"types": "./lib/pipeline.d.ts",
"default": "./lib/pipeline.js"
},
"./utils/*": {
"types": "./lib/utils/*.d.ts",
"default": "./lib/utils/*.js"
Expand Down Expand Up @@ -66,7 +62,6 @@
},
"dependencies": {
"@artus/injection": "^0.5.1",
"@artus/pipeline": "^0.2.2",
"deepmerge": "^4.2.2",
"minimatch": "^5.0.1"
},
Expand Down
59 changes: 25 additions & 34 deletions src/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,22 @@ import { ArtusStdError } from './exception';
import { HookFunction, LifecycleManager } from './lifecycle';
import { LoaderFactory, Manifest } from './loader';
import { Application, ApplicationInitOptions } from './types';
import Trigger from './trigger';
import ConfigurationHandler from './configuration';
import { Logger, LoggerType } from './logger';

export class ArtusApplication implements Application {
public manifest?: Manifest;
public container: Container;

protected lifecycleManager: LifecycleManager;
protected loaderFactory: LoaderFactory;

constructor(opts?: ApplicationInitOptions) {
this.container = new Container(opts?.containerName ?? ArtusInjectEnum.DefaultContainerName);
this.lifecycleManager = new LifecycleManager(this, this.container);
this.loaderFactory = LoaderFactory.create(this.container);

this.addLoaderListener();
if (opts?.env) {
const envList = [].concat(opts.env);
this.container.set({ id: ArtusInjectEnum.EnvList, value: envList });
}
this.loadDefaultClass();
this.addLoaderListener();

process.on('SIGINT', () => this.close(true));
process.on('SIGTERM', () => this.close(true));
Expand All @@ -32,16 +30,16 @@ export class ArtusApplication implements Application {
return this.container.get(ArtusInjectEnum.Config);
}

get frameworks(): Record<string, any> {
return this.container.get(ArtusInjectEnum.Frameworks);
get configurationHandler(): ConfigurationHandler {
return this.container.get(ConfigurationHandler);
}

get packages(): Record<string, any> {
return this.container.get(ArtusInjectEnum.Packages);
get lifecycleManager(): LifecycleManager {
return this.container.get(LifecycleManager);
}

get configurationHandler(): ConfigurationHandler {
return this.container.get(ConfigurationHandler);
get loaderFactory(): LoaderFactory {
return this.container.get(LoaderFactory);
}

get logger(): LoggerType {
Expand All @@ -52,18 +50,19 @@ export class ArtusApplication implements Application {
// load Artus default clazz
this.container.set({ id: Container, value: this.container });
this.container.set({ id: ArtusInjectEnum.Application, value: this });
this.container.set({ id: ArtusInjectEnum.LifecycleManager, value: this.lifecycleManager });
this.container.set({ id: ArtusInjectEnum.Config, value: {} });

this.container.set({ type: ConfigurationHandler });
this.container.set({ type: LoaderFactory });
this.container.set({ type: LifecycleManager });
this.container.set({ type: Logger });
this.container.set({ type: Trigger });
}

async load(manifest: Manifest, root: string = process.cwd()) {
// Load user manifest
this.manifest = manifest;

await this.loaderFactory.loadManifest(manifest, manifest.relative ? root : undefined);
await this.loaderFactory.loadManifest(manifest, root);

await this.lifecycleManager.emitHook('didLoad');

Expand Down Expand Up @@ -103,26 +102,18 @@ export class ArtusApplication implements Application {
.addLoaderListener('config', {
before: () => this.lifecycleManager.emitHook('configWillLoad'),
after: () => {
this.container.set({
id: ArtusInjectEnum.Config,
value: this.configurationHandler.getAllConfig(),
});
this.updateConfig();
return this.lifecycleManager.emitHook('configDidLoad');
},
})
.addLoaderListener('framework-config', {
after: () =>
this.container.set({
id: ArtusInjectEnum.Frameworks,
value: this.configurationHandler.getFrameworkConfig(),
}),
})
.addLoaderListener('package-json', {
after: () =>
this.container.set({
id: ArtusInjectEnum.Packages,
value: this.configurationHandler.getPackages(),
}),
});
}

protected updateConfig() {
const oldConfig = this.container.get(ArtusInjectEnum.Config, { noThrow: true }) ?? {};
const newConfig = this.configurationHandler.getMergedConfig() ?? {};
this.container.set({
id: ArtusInjectEnum.Config,
value: Object.assign(oldConfig, newConfig),
});
}
}
10 changes: 0 additions & 10 deletions src/configuration/decorator.ts

This file was deleted.

68 changes: 17 additions & 51 deletions src/configuration/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { Injectable } from '@artus/injection';
import { ARTUS_DEFAULT_CONFIG_ENV, ARTUS_SERVER_ENV } from '../constant';
import { Container, Inject, Injectable } from '@artus/injection';
import { ArtusInjectEnum, ARTUS_DEFAULT_CONFIG_ENV, ARTUS_SERVER_ENV } from '../constant';
import { ManifestItem } from '../loader';
import { mergeConfig } from '../loader/utils/merge';
import compatibleRequire from '../utils/compatible_require';
import { DefineConfigHandle } from './decorator';

export type ConfigObject = Record<string, any>;
export type FrameworkObject = { path: string; env: string };
Expand All @@ -20,15 +19,19 @@ export default class ConfigurationHandler {
return env;
}

private configStore: Map<string, ConfigObject> = new Map();
private frameworks: Map<string, FrameworkObject[]> = new Map();
private packages: Map<string, PackageObject[]> = new Map();
public configStore: Map<string, ConfigObject> = new Map();

getMergedConfig(env?: string): ConfigObject {
const currentEnv = env ?? process.env[ARTUS_SERVER_ENV] ?? ARTUS_DEFAULT_CONFIG_ENV.DEV;
@Inject()
private container: Container;

getMergedConfig(): ConfigObject {
let envList: string[] = this.container.get(ArtusInjectEnum.EnvList, { noThrow: true });
if (!envList) {
envList = process.env[ARTUS_SERVER_ENV] ? [process.env[ARTUS_SERVER_ENV]] : [ARTUS_DEFAULT_CONFIG_ENV.DEV];
}
const defaultConfig = this.configStore.get(ARTUS_DEFAULT_CONFIG_ENV.DEFAULT) ?? {};
const envConfig = this.configStore.get(currentEnv) ?? {};
return mergeConfig(defaultConfig, envConfig);
const envConfigList = envList.map(currentEnv => (this.configStore.get(currentEnv) ?? {}));
return mergeConfig(defaultConfig, ...envConfigList);
}

getAllConfig(): ConfigObject {
Expand All @@ -39,6 +42,10 @@ export default class ConfigurationHandler {
return mergeConfig(defaultConfig, ...keys.map(key => this.configStore.get(key) ?? {}));
}

clearStore(): void {
this.configStore.clear();
}

setConfig(env: string, config: ConfigObject) {
const storedConfig = this.configStore.get(env) ?? {};
this.configStore.set(env, mergeConfig(storedConfig, config));
Expand All @@ -51,45 +58,4 @@ export default class ConfigurationHandler {
this.setConfig(env, configContent);
}
}

@DefineConfigHandle('framework-config')
getFrameworkConfig(
env?: string,
key = 'app',
frameworkMap = new Map<string, FrameworkObject>(),
): Map<string, FrameworkObject> {
if (!this.frameworks.has(key)) {
return frameworkMap;
}
const currentEnv = env ?? process.env[ARTUS_SERVER_ENV] ?? ARTUS_DEFAULT_CONFIG_ENV.DEV;
const list = this.frameworks.get(key) as unknown as FrameworkObject[];
const defaultConfig =
list.filter(item => item.env === ARTUS_DEFAULT_CONFIG_ENV.DEFAULT)[0] ?? {};
const envConfig = list.filter(item => item.env === currentEnv)[0] ?? {};
const config = mergeConfig(defaultConfig, envConfig) as unknown as FrameworkObject;
frameworkMap.set(key, config);

if (config.path) {
this.getFrameworkConfig(env, config.path, frameworkMap);
}
return frameworkMap;
}

addFramework(source: string, framework: FrameworkObject, options: FrameworkOptions) {
const key = options.unitName || source;
const list = this.frameworks.get(key) ?? [];
framework.env = options.env;
list.push(framework);
this.frameworks.set(key, list);
}

getPackages(): Map<string, PackageObject[]> {
return this.packages;
}

addPackage(source: string, pkg: PackageObject) {
const list = this.packages.get(source) ?? [];
list.push(pkg);
this.packages.set(source, list);
}
}
8 changes: 4 additions & 4 deletions src/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ export enum ArtusInjectEnum {
Application = 'artus#application',
Config = 'artus#config',
DefaultContainerName = 'artus#default_container',
Frameworks = 'artus#framework-config',
LifecycleManager = 'artus#lifecycle-manager',
Packages = 'artus#packages',
EnvList = 'artus#env_list',
}

export enum ARTUS_DEFAULT_CONFIG_ENV {
Expand All @@ -30,7 +28,6 @@ export const ARTUS_SERVER_ENV = 'ARTUS_SERVER_ENV';

export const HOOK_NAME_META_PREFIX = 'hookName:';
export const HOOK_FILE_LOADER = 'appHook:fileLoader';
export const HOOK_CONFIG_HANDLE = 'appHook:configHandle::';

export const DEFAULT_EXCLUDES = [
'test',
Expand All @@ -43,6 +40,8 @@ export const DEFAULT_EXCLUDES = [
'LICENSE',
'pnpm-lock.yaml',
];
export const DEFAULT_MODULE_EXTENSIONS = ['.js', '.json', '.node'];
export const DEFAULT_APP_REF = '_app';

export const FRAMEWORK_PATTERN = 'framework.*';
export const PLUGIN_CONFIG_PATTERN = 'plugin.*';
Expand All @@ -62,5 +61,6 @@ export const DEFAULT_LOADER_LIST_WITH_ORDER = [
];

export const DEFAULT_CONFIG_DIR = 'src/config';
export const DEFAULT_MANIFEST_FILENAME = 'manifest.json';

export const SHOULD_OVERWRITE_VALUE = 'shouldOverwrite';
14 changes: 0 additions & 14 deletions src/exception/impl.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,5 @@
import { Middleware } from '@artus/pipeline';
import { ARTUS_EXCEPTION_DEFAULT_LOCALE } from '../constant';
import { ExceptionItem } from './types';
import { matchExceptionFilter } from './utils';

export const exceptionFilterMiddleware: Middleware = async (ctx, next) => {
try {
await next();
} catch (err) {
const filter = matchExceptionFilter(err, ctx.container);
if (filter) {
await filter.catch(err);
}
throw err;
}
};

export class ArtusStdError extends Error {
name = 'ArtusStdError';
Expand Down
36 changes: 0 additions & 36 deletions src/framework/handler.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/framework/index.ts

This file was deleted.

2 changes: 0 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,3 @@ export * from './constant';
import ConfigurationHandler from './configuration';
export { ConfigurationHandler };

import Trigger from './trigger';
export { Trigger };
Loading

0 comments on commit ba0649d

Please sign in to comment.