From 4c69aa914933fb2bcc538c08914e8c78f584cf67 Mon Sep 17 00:00:00 2001 From: rosen-vladimirov Date: Tue, 9 May 2017 12:41:20 +0300 Subject: [PATCH] Fix installation of tns-core-modules during project creation CLI tries to install `tns-core-modules` during project creation. In case the template does not have `tns-core-modules` in it, CLI will install latest version of `tns-core-modules` which may (and most probably is) be incompatible with the runtime and other dependencies of the project. In such cases ensure the CLI installs version of tns-core-modules that matches its own version, i.e. when CLI is 2.5.2, it should install latest 2.5.x core modules. Also ensure exact versions are installed and persisted by `npmInstallationManager`. --- lib/npm-installation-manager.ts | 2 +- lib/services/project-service.ts | 11 +++++++---- test/project-service.ts | 4 +++- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/lib/npm-installation-manager.ts b/lib/npm-installation-manager.ts index d295cec3db..1fc8015910 100644 --- a/lib/npm-installation-manager.ts +++ b/lib/npm-installation-manager.ts @@ -108,7 +108,7 @@ export class NpmInstallationManager implements INpmInstallationManager { packageName = packageName + (version ? `@${version}` : ""); - let npmOptions: any = { silent: true }; + let npmOptions: any = { silent: true, "save-exact": true }; if (dependencyType) { npmOptions[dependencyType] = true; diff --git a/lib/services/project-service.ts b/lib/services/project-service.ts index 150430e873..9bc4b1ba25 100644 --- a/lib/services/project-service.ts +++ b/lib/services/project-service.ts @@ -14,7 +14,8 @@ export class ProjectService implements IProjectService { private $projectHelper: IProjectHelper, private $projectNameService: IProjectNameService, private $projectTemplatesService: IProjectTemplatesService, - private $staticConfig: IStaticConfig) { } + private $staticConfig: IStaticConfig, + private $npmInstallationManager: INpmInstallationManager) { } @exported("projectService") public async createProject(projectOptions: IProjectSettings): Promise { @@ -50,10 +51,12 @@ export class ProjectService implements IProjectService { await this.ensureAppResourcesExist(projectDir); - let packageName = constants.TNS_CORE_MODULES_NAME; - await this.$npm.install(packageName, projectDir, { save: true, "save-exact": true }); - let templatePackageJsonData = this.getDataFromJson(templatePath); + + if (!(templatePackageJsonData && templatePackageJsonData.dependencies && templatePackageJsonData.dependencies[constants.TNS_CORE_MODULES_NAME])) { + await this.$npmInstallationManager.install(constants.TNS_CORE_MODULES_NAME, projectDir, { dependencyType: "save" }); + } + this.mergeProjectAndTemplateProperties(projectDir, templatePackageJsonData); //merging dependencies from template (dev && prod) this.removeMergedDependencies(projectDir, templatePackageJsonData); diff --git a/test/project-service.ts b/test/project-service.ts index a4bac957e1..8a1c7b0ad4 100644 --- a/test/project-service.ts +++ b/test/project-service.ts @@ -114,7 +114,7 @@ class ProjectIntegrationTest { this.testInjector.register("fs", FileSystem); this.testInjector.register("projectDataService", ProjectDataServiceLib.ProjectDataService); this.testInjector.register("staticConfig", StaticConfig); - this.testInjector.register("analyticsService", { track: async () => undefined }); + this.testInjector.register("analyticsService", { track: async (): Promise => undefined }); this.testInjector.register("npmInstallationManager", NpmInstallationManager); this.testInjector.register("npm", NpmLib.NodePackageManager); @@ -130,6 +130,7 @@ class ProjectIntegrationTest { return dummyString; } }); + this.testInjector.register("npmInstallationManager", NpmInstallationManager); } } @@ -471,6 +472,7 @@ describe("Project Service Tests", () => { testInjector.register("projectTemplatesService", {}); testInjector.register("staticConfig", {}); testInjector.register("projectHelper", {}); + testInjector.register("npmInstallationManager", {}); return testInjector; };