UNPKG

@nestjs-mod/common

Version:

A collection of utilities for unifying NestJS applications and modules

137 lines 5.92 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.NxProjectJsonService = void 0; const tslib_1 = require("tslib"); const common_1 = require("@nestjs/common"); const fs_1 = require("fs"); const path_1 = require("path"); const is_infrastructure_1 = require("../../../../utils/is-infrastructure"); const project_utils_configuration_1 = require("../project-utils.configuration"); const project_utils_constants_1 = require("../project-utils.constants"); const application_package_json_service_1 = require("./application-package-json.service"); const dot_env_service_1 = require("./dot-env.service"); const package_json_service_1 = require("./package-json.service"); let NxProjectJsonService = class NxProjectJsonService { constructor(projectUtilsConfiguration, applicationPackageJsonService, packageJsonService, dotEnvService) { this.projectUtilsConfiguration = projectUtilsConfiguration; this.applicationPackageJsonService = applicationPackageJsonService; this.packageJsonService = packageJsonService; this.dotEnvService = dotEnvService; } getNxProjectJsonFilePath() { if (this.projectUtilsConfiguration.nxProjectJsonFile) { return this.projectUtilsConfiguration.nxProjectJsonFile; } const applicationPackageJsonFilePath = this.applicationPackageJsonService.getPackageJsonFilePath(); if (applicationPackageJsonFilePath) { return (0, path_1.join)((0, path_1.dirname)(applicationPackageJsonFilePath), project_utils_constants_1.PROJECT_JSON_FILE); } return undefined; } addRunCommands( /** * Command lines for append */ lines, /** * Name of the target where you need to add it; if it does not exist, it will be created automatically */ targetName = project_utils_constants_1.GENERATE_TARGET_NAME, /** * A line to check whether to add, by default it searches for each command to add */ searchCommand, customNxProjectJsonFile) { const projectJson = this.read(customNxProjectJsonFile) || {}; if (!projectJson?.targets) { projectJson.targets = {}; } if (!projectJson.targets[targetName]) { projectJson.targets[targetName] = {}; } if (!projectJson.targets[targetName]) { projectJson.targets[targetName] = {}; } if (!projectJson.targets[targetName].executor) { projectJson.targets[targetName].executor = 'nx:run-commands'; } if (!projectJson.targets[targetName].options) { projectJson.targets[targetName].options = {}; } if (!projectJson.targets[targetName].options['commands']) { projectJson.targets[targetName].options['commands'] = []; } for (const line of lines) { if (!lines.find((line) => projectJson.targets[targetName].options['commands'].find((c) => c === line))) { projectJson.targets[targetName].options['commands'].push(line); } } if (!projectJson.targets[targetName].options['parallel']) { projectJson.targets[targetName].options['parallel'] = false; } const dotEnvFile = this.dotEnvService.getEnvFilePath(); const packageJson = this.packageJsonService.getPackageJsonFilePath(); if (dotEnvFile && (0, fs_1.existsSync)(dotEnvFile) && packageJson && (0, fs_1.existsSync)(packageJson) && !projectJson.targets[targetName].options['envFile']) { projectJson.targets[targetName].options['envFile'] = `.${dotEnvFile.replace((0, path_1.dirname)(packageJson), '')}`; } if (!projectJson.targets[targetName].options['color']) { projectJson.targets[targetName].options['color'] = true; } this.write(projectJson, customNxProjectJsonFile); } readFile(nxProjectJsonFile) { try { return JSON.parse((0, fs_1.readFileSync)(nxProjectJsonFile).toString()); } catch (err) { return undefined; } } writeFile(nxProjectJsonFile, data) { try { if (!nxProjectJsonFile) { return; } const fileDir = (0, path_1.dirname)(nxProjectJsonFile); if (!(0, fs_1.existsSync)(fileDir)) { (0, fs_1.mkdirSync)(fileDir, { recursive: true }); } (0, fs_1.writeFileSync)(nxProjectJsonFile, JSON.stringify(data, null, 2)); } catch (err) { // } } read(customNxProjectJsonFile) { const nxProjectJsonFile = customNxProjectJsonFile || this.getNxProjectJsonFilePath(); if (!nxProjectJsonFile) { return undefined; } return this.readFile(nxProjectJsonFile); } write(data, customNxProjectJsonFile) { const nxProjectJsonFile = customNxProjectJsonFile || this.getNxProjectJsonFilePath(); if (!nxProjectJsonFile) { return; } if ((0, is_infrastructure_1.isInfrastructureMode)()) { const fileDir = (0, path_1.dirname)(nxProjectJsonFile); if (!(0, fs_1.existsSync)(fileDir)) { (0, fs_1.mkdirSync)(fileDir, { recursive: true }); } this.writeFile(nxProjectJsonFile, data); } } }; exports.NxProjectJsonService = NxProjectJsonService; exports.NxProjectJsonService = NxProjectJsonService = tslib_1.__decorate([ (0, common_1.Injectable)(), tslib_1.__metadata("design:paramtypes", [project_utils_configuration_1.ProjectUtilsConfiguration, application_package_json_service_1.ApplicationPackageJsonService, package_json_service_1.PackageJsonService, dot_env_service_1.DotEnvService]) ], NxProjectJsonService); //# sourceMappingURL=nx-project-json.service.js.map