UNPKG

nativescript

Version:

Command-line interface for building NativeScript projects

179 lines 9.42 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.VersionInformationType = void 0; const constants = require("../constants"); const helpers = require("../common/helpers"); const semver = require("semver"); const path = require("path"); const _ = require("lodash"); const yok_1 = require("../common/yok"); var VersionInformationType; (function (VersionInformationType) { VersionInformationType["UpToDate"] = "UpToDate"; VersionInformationType["UpdateAvailable"] = "UpdateAvailable"; VersionInformationType["NotInstalled"] = "NotInstalled"; })(VersionInformationType || (exports.VersionInformationType = VersionInformationType = {})); class VersionsService { constructor($fs, $packageInstallationManager, $injector, $logger, $staticConfig, $pluginsService, $projectDataService, $terminalSpinnerService) { this.$fs = $fs; this.$packageInstallationManager = $packageInstallationManager; this.$injector = $injector; this.$logger = $logger; this.$staticConfig = $staticConfig; this.$pluginsService = $pluginsService; this.$projectDataService = $projectDataService; this.$terminalSpinnerService = $terminalSpinnerService; this.projectData = this.getProjectData(); } async getNativescriptCliVersion() { const currentCliVersion = this.$staticConfig.version; const latestCliVersion = await this.$packageInstallationManager.getLatestVersion(constants.NATIVESCRIPT_KEY_NAME); return { componentName: constants.NATIVESCRIPT_KEY_NAME, currentVersion: currentCliVersion, latestVersion: latestCliVersion, }; } async getTnsCoreModulesVersion() { const latestTnsCoreModulesVersion = await this.$packageInstallationManager.getLatestVersion(constants.TNS_CORE_MODULES_NAME); const nativescriptCoreModulesInfo = { componentName: constants.TNS_CORE_MODULES_NAME, latestVersion: latestTnsCoreModulesVersion, }; const versionInformations = []; if (this.projectData) { const nodeModulesPath = path.join(this.projectData.projectDir, constants.NODE_MODULES_FOLDER_NAME); const scopedPackagePath = path.join(nodeModulesPath, constants.SCOPED_TNS_CORE_MODULES); const tnsCoreModulesPath = path.join(nodeModulesPath, constants.TNS_CORE_MODULES_NAME); const dependsOnNonScopedPackage = !!this.projectData.dependencies[constants.TNS_CORE_MODULES_NAME]; const dependsOnScopedPackage = !!this.projectData.dependencies[constants.SCOPED_TNS_CORE_MODULES]; // ensure the dependencies are installed, so we can get their actual versions from node_modules if (!this.$fs.exists(nodeModulesPath) || (dependsOnNonScopedPackage && !this.$fs.exists(tnsCoreModulesPath)) || (dependsOnScopedPackage && !this.$fs.exists(scopedPackagePath))) { await this.$pluginsService.ensureAllDependenciesAreInstalled(this.projectData); } if (dependsOnNonScopedPackage && this.$fs.exists(tnsCoreModulesPath)) { const currentTnsCoreModulesVersion = this.$fs.readJson(path.join(tnsCoreModulesPath, constants.PACKAGE_JSON_FILE_NAME)).version; nativescriptCoreModulesInfo.currentVersion = currentTnsCoreModulesVersion; versionInformations.push(nativescriptCoreModulesInfo); } if (dependsOnScopedPackage && this.$fs.exists(scopedPackagePath)) { const scopedModulesInformation = { componentName: constants.SCOPED_TNS_CORE_MODULES, latestVersion: await this.$packageInstallationManager.getLatestVersion(constants.SCOPED_TNS_CORE_MODULES), }; const currentScopedPackageVersion = this.$fs.readJson(path.join(scopedPackagePath, constants.PACKAGE_JSON_FILE_NAME)).version; scopedModulesInformation.currentVersion = currentScopedPackageVersion; versionInformations.push(scopedModulesInformation); } } else { versionInformations.push(nativescriptCoreModulesInfo); } return versionInformations; } async getRuntimesVersions(platform) { const iosRuntime = this.$projectDataService.getRuntimePackage(this.projectData.projectDir, "ios" /* constants.PlatformTypes.ios */); const androidRuntime = this.$projectDataService.getRuntimePackage(this.projectData.projectDir, "android" /* constants.PlatformTypes.android */); let runtimes = []; if (!platform) { runtimes = [iosRuntime, androidRuntime]; } else if (platform === "ios" /* PlatformTypes.ios */) { runtimes.push(iosRuntime); } else if (platform === "android" /* PlatformTypes.android */) { runtimes.push(androidRuntime); } const runtimesVersions = await Promise.all(runtimes.map(async (runtime) => { const latestVersion = await this.$packageInstallationManager.getLatestVersion(runtime.name); const runtimeInformation = { componentName: runtime.name, currentVersion: runtime.version, latestVersion, }; return runtimeInformation; })); return runtimesVersions; } async getAllComponentsVersions(platform) { try { let allComponents = []; const nativescriptCliInformation = await this.getNativescriptCliVersion(); if (nativescriptCliInformation) { allComponents.push(nativescriptCliInformation); } if (this.projectData) { const nativescriptCoreModulesInformation = await this.getTnsCoreModulesVersion(); if (nativescriptCoreModulesInformation) { allComponents.push(...nativescriptCoreModulesInformation); } const runtimesVersions = await this.getRuntimesVersions(platform); allComponents = allComponents.concat(runtimesVersions); } return allComponents.map((componentInformation) => { if (componentInformation.currentVersion) { if (this.hasUpdate(componentInformation)) { componentInformation.type = VersionInformationType.UpdateAvailable; componentInformation.message = `${VersionsService.UPDATE_AVAILABLE_MESSAGE} for component ${componentInformation.componentName}. Your current version is ${componentInformation.currentVersion} and the latest available version is ${componentInformation.latestVersion}.`; } else { componentInformation.type = VersionInformationType.UpToDate; componentInformation.message = `Component ${componentInformation.componentName} has ${componentInformation.currentVersion} version and is ${VersionsService.UP_TO_DATE_MESSAGE}.`; } } else { componentInformation.type = VersionInformationType.NotInstalled; componentInformation.message = `Component ${componentInformation.componentName} is ${VersionsService.NOT_INSTALLED_MESSAGE}.`; } return componentInformation; }); } catch (error) { this.$logger.trace("Error while trying to get component information. Error is: ", error); return []; } } async printVersionsInformation(platform) { const versionsInformation = await this.$terminalSpinnerService.execute({ text: `Getting NativeScript components versions information...`, }, () => this.getAllComponentsVersions(platform)); if (!helpers.isInteractive()) { versionsInformation.map((componentInformation) => this.$logger.info(componentInformation.message)); } _.forEach(versionsInformation, (componentInformation) => { const spinner = this.$terminalSpinnerService.createSpinner(); spinner.text = componentInformation.message; switch (componentInformation.type) { case VersionInformationType.UpToDate: spinner.succeed(); break; case VersionInformationType.UpdateAvailable: spinner.warn(); break; case VersionInformationType.NotInstalled: spinner.fail(); break; } }); } getProjectData() { try { const projectData = this.$injector.resolve("projectData"); projectData.initializeProjectData(); return projectData; } catch (error) { return null; } } hasUpdate(component) { return !semver.satisfies(component.latestVersion, semver.validRange(component.currentVersion)); } } VersionsService.UP_TO_DATE_MESSAGE = "up to date"; VersionsService.UPDATE_AVAILABLE_MESSAGE = "Update available"; VersionsService.NOT_INSTALLED_MESSAGE = "not installed"; yok_1.injector.register("versionsService", VersionsService); //# sourceMappingURL=versions-service.js.map