@nestjs-mod/pm2
Version:
Production process manager for Node.JS applications with a built-in load balancer for NestJS-mod (Generator ecosystem.config.json for https://www.npmjs.com/package/pm2)
81 lines • 4.07 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Pm2Service = void 0;
const tslib_1 = require("tslib");
const common_1 = require("@nestjs-mod/common");
const common_2 = require("@nestjs/common");
const path_1 = require("path");
const pm2_ecosystem_config_file_service_1 = require("./pm2-ecosystem-config-file.service");
const pm2_configuration_1 = require("./pm2.configuration");
const pm2_constants_1 = require("./pm2.constants");
let Pm2Service = class Pm2Service {
constructor(pm2Configuration, pm2EcosystemConfigFileService, applicationPackageJsonService, packageJsonService, wrapApplicationOptionsService, nxProjectJsonService) {
this.pm2Configuration = pm2Configuration;
this.pm2EcosystemConfigFileService = pm2EcosystemConfigFileService;
this.applicationPackageJsonService = applicationPackageJsonService;
this.packageJsonService = packageJsonService;
this.wrapApplicationOptionsService = wrapApplicationOptionsService;
this.nxProjectJsonService = nxProjectJsonService;
}
onApplicationBootstrap() {
this.updatePm2EcosystemConfigFile();
this.updatePackageJson();
}
updatePackageJson() {
const packageJson = this.packageJsonService.read();
const packageJsonFilePath = this.packageJsonService.getPackageJsonFilePath();
if (packageJson && packageJsonFilePath) {
const ecosystemConfigFilePath = this.pm2Configuration.ecosystemConfigFile.replace((0, path_1.dirname)(packageJsonFilePath), '');
if (packageJson.scripts) {
this.packageJsonService.addScripts(pm2_constants_1.PM2_PROD_INFRA_CATEGORY_NAME, {
'pm2:start': {
commands: [`./node_modules/.bin/pm2 start .${ecosystemConfigFilePath}`],
comments: ['Launch all applications in PM2 mode'],
},
'pm2:stop': {
commands: [`./node_modules/.bin/pm2 delete all`],
comments: ['Stop all applications in PM2 mode'],
},
}, packageJson);
}
this.packageJsonService.write(packageJson);
}
}
updatePm2EcosystemConfigFile() {
const currentConfig = this.pm2EcosystemConfigFileService.read();
const packageJsonFilePath = this.packageJsonService.getPackageJsonFilePath();
if (!packageJsonFilePath) {
return;
}
const appName = this.wrapApplicationOptionsService?.project?.name ?? this.applicationPackageJsonService.read()?.name;
const currentApp = {
...Object.entries(this.pm2Configuration)
.filter(([key]) => key !== 'ecosystemConfigFile' && key !== 'applicationScriptFile')
.reduce((all, [key, value]) => ({ ...all, [key]: value }), {}),
name: appName,
script: `./node_modules/.bin/nx serve ${this.nxProjectJsonService.read()?.name} --skip-nx-cache=true`,
// `node ./${this.pm2Configuration.applicationScriptFile.replace(dirname(packageJsonFilePath), '')}`,
};
currentConfig.apps = currentConfig.apps.map((app) => {
if (app.name === appName) {
return { ...currentApp, ...app };
}
return app;
});
if (!currentConfig.apps.some((app) => app.name === appName)) {
currentConfig.apps.push(currentApp);
}
this.pm2EcosystemConfigFileService.write(currentConfig);
}
};
exports.Pm2Service = Pm2Service;
exports.Pm2Service = Pm2Service = tslib_1.__decorate([
(0, common_2.Injectable)(),
tslib_1.__metadata("design:paramtypes", [pm2_configuration_1.Pm2Configuration,
pm2_ecosystem_config_file_service_1.Pm2EcosystemConfigFileService,
common_1.ApplicationPackageJsonService,
common_1.PackageJsonService,
common_1.WrapApplicationOptionsService,
common_1.NxProjectJsonService])
], Pm2Service);
//# sourceMappingURL=pm2.service.js.map