From e94fa816533ce7709655303bba7e56dbd95efbec Mon Sep 17 00:00:00 2001 From: rosen-vladimirov Date: Mon, 6 Jan 2020 16:17:14 +0200 Subject: [PATCH] fix: `tns info` should work with scoped modules Currently the `tns info` command fails when executed inside application in which `tns-core-modules` package is not available as dependency. This may happen in case you use only `@nativescript/core` package. The problem is that CLI is trying to read the package.json of the `/node_modules/tns-core-modules` and this operation fails. To fix this, add checks for both `tns-core-modules` and `@nativescript/core` packages. Warnings/information will be shown only for the package used in package.json, i.e. in case you use `tns-core-modules`, we'll show information only for it. --- lib/declarations.d.ts | 6 ++--- lib/services/versions-service.ts | 39 ++++++++++++++++++++++++++------ 2 files changed, 35 insertions(+), 10 deletions(-) diff --git a/lib/declarations.d.ts b/lib/declarations.d.ts index 71c137847e..7afd8d6e9b 100644 --- a/lib/declarations.d.ts +++ b/lib/declarations.d.ts @@ -798,10 +798,10 @@ interface IVersionsService { getNativescriptCliVersion(): Promise; /** - * Gets version information about tns-core-modules. - * @return {Promise} The version information. + * Gets version information about tns-core-modules and @nativescript/core packages. + * @return {Promise} The version information. */ - getTnsCoreModulesVersion(): Promise; + getTnsCoreModulesVersion(): Promise; /** * Gets versions information about nativescript runtimes. diff --git a/lib/services/versions-service.ts b/lib/services/versions-service.ts index 9f5ef644a2..865e4a8652 100644 --- a/lib/services/versions-service.ts +++ b/lib/services/versions-service.ts @@ -37,26 +37,51 @@ class VersionsService implements IVersionsService { }; } - public async getTnsCoreModulesVersion(): Promise { + public async getTnsCoreModulesVersion(): Promise { const latestTnsCoreModulesVersion = await this.$packageInstallationManager.getLatestVersion(constants.TNS_CORE_MODULES_NAME); const nativescriptCoreModulesInfo: IVersionInformation = { componentName: constants.TNS_CORE_MODULES_NAME, latestVersion: latestTnsCoreModulesVersion }; + const versionInformations: IVersionInformation[] = []; + 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) || - !this.$fs.exists(tnsCoreModulesPath)) { + (dependsOnNonScopedPackage && !this.$fs.exists(tnsCoreModulesPath)) || + (dependsOnScopedPackage && !this.$fs.exists(scopedPackagePath))) { await this.$pluginsService.ensureAllDependenciesAreInstalled(this.projectData); } - const currentTnsCoreModulesVersion = this.$fs.readJson(path.join(tnsCoreModulesPath, constants.PACKAGE_JSON_FILE_NAME)).version; - nativescriptCoreModulesInfo.currentVersion = currentTnsCoreModulesVersion; + 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: IVersionInformation = { + 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 nativescriptCoreModulesInfo; + return versionInformations; } public async getRuntimesVersions(): Promise { @@ -101,9 +126,9 @@ class VersionsService implements IVersionsService { } if (this.projectData) { - const nativescriptCoreModulesInformation: IVersionInformation = await this.getTnsCoreModulesVersion(); + const nativescriptCoreModulesInformation: IVersionInformation[] = await this.getTnsCoreModulesVersion(); if (nativescriptCoreModulesInformation) { - allComponents.push(nativescriptCoreModulesInformation); + allComponents.push(...nativescriptCoreModulesInformation); } const runtimesVersions: IVersionInformation[] = await this.getRuntimesVersions();