@tsdi/platform-server-boot
Version:
boot application on node server, AOP Ioc container, via typescript decorator
107 lines (104 loc) • 3.34 kB
JavaScript
import { __awaiter, __decorate, __metadata } from 'tslib';
import { Injectable } from '@tsdi/ioc';
import { ConfigureLoaderToken, DIModule, ProcessRunRootToken, ApplicationExit } from '@tsdi/boot';
import { runMainPath, syncRequire, ServerModule } from '@tsdi/platform-server';
import { join } from 'path';
/**
* configure file loader.
*
* @export
* @class ConfigureFileLoader
* @implements {IConfigureLoader<Configure>}
*/
let ConfigureFileLoader = class ConfigureFileLoader {
constructor(baseURL, container) {
this.baseURL = baseURL;
this.container = container;
this.baseURL = this.baseURL || runMainPath();
}
load(uri) {
return __awaiter(this, void 0, void 0, function* () {
const fs = syncRequire('fs');
if (uri) {
if (fs.existsSync(uri)) {
return syncRequire(uri);
}
else if (fs.existsSync(join(this.baseURL, uri))) {
return syncRequire(join(this.baseURL, uri));
}
else {
console.log(`config file: ${uri} not exists.`);
return null;
}
}
else {
let cfgmodeles;
let cfgpath = join(this.baseURL, './config');
['.js', '.ts', '.json'].some(ext => {
if (fs.existsSync(cfgpath + ext)) {
cfgmodeles = syncRequire(cfgpath + ext);
}
return !!cfgmodeles;
});
return cfgmodeles;
}
});
}
static ρAnn() {
return { "name": "ConfigureFileLoader", "params": { "constructor": ["baseURL", "container"], "load": ["uri"] } };
}
};
ConfigureFileLoader = __decorate([
Injectable(ConfigureLoaderToken),
__metadata("design:paramtypes", [String, Object])
], ConfigureFileLoader);
class ServerApplicationExit extends ApplicationExit {
constructor() {
super(...arguments);
this.enable = true;
}
exit(context, err) {
var _a;
const logger = (_a = context.getLogManager()) === null || _a === void 0 ? void 0 : _a.getLogger();
if (err) {
logger ? logger.error(err) : console.error(err);
}
context.destroy();
if (logger) {
logger.log('SIGINT: app destoryed.');
}
else {
console.log('SIGINT: app destoryed.');
}
process.exit(err ? 1 : 0);
}
static ρAnn() {
return { "name": "ServerApplicationExit" };
}
}
/**
* server boot module
*
* @export
* @class ServerBootstrapModule
*/
let ServerBootstrapModule = class ServerBootstrapModule {
static ρAnn() {
return { "name": "ServerBootstrapModule", "params": {} };
}
};
ServerBootstrapModule = __decorate([
DIModule({
regIn: 'root',
imports: [
ServerModule
],
providers: [
ConfigureFileLoader,
{ provide: ProcessRunRootToken, useValue: runMainPath() },
{ provide: ApplicationExit, useValue: new ServerApplicationExit() }
]
})
], ServerBootstrapModule);
export { ConfigureFileLoader, ServerBootstrapModule };
//# sourceMappingURL=platform-server-boot.js.map