|
| 1 | +import * as path from "path"; |
| 2 | +import * as semver from "semver"; |
| 3 | +import * as constants from "../constants"; |
| 4 | +import { BaseUpdateController } from "./base-update-controller"; |
| 5 | + |
| 6 | +export class UpdateController extends BaseUpdateController implements IUpdateController { |
| 7 | + constructor( |
| 8 | + protected $fs: IFileSystem, |
| 9 | + protected $platformsDataService: IPlatformsDataService, |
| 10 | + protected $platformCommandHelper: IPlatformCommandHelper, |
| 11 | + protected $packageInstallationManager: IPackageInstallationManager, |
| 12 | + protected $packageManager: IPackageManager, |
| 13 | + private $devicePlatformsConstants: Mobile.IDevicePlatformsConstants, |
| 14 | + private $addPlatformService: IAddPlatformService, |
| 15 | + private $logger: ILogger, |
| 16 | + private $pluginsService: IPluginsService, |
| 17 | + private $pacoteService: IPacoteService, |
| 18 | + private $projectDataService: IProjectDataService) { |
| 19 | + super($fs, $platformCommandHelper, $platformsDataService, $packageInstallationManager, $packageManager); |
| 20 | + this.getTemplateManifest = _.memoize(this._getTemplateManifest, (...args) => { |
| 21 | + return args.join("@"); |
| 22 | + }); |
| 23 | + } |
| 24 | + private getTemplateManifest: Function; |
| 25 | + static readonly folders: string[] = [ |
| 26 | + constants.LIB_DIR_NAME, |
| 27 | + constants.HOOKS_DIR_NAME, |
| 28 | + //constants.PLATFORMS_DIR_NAME, |
| 29 | + //constants.NODE_MODULES_FOLDER_NAME, |
| 30 | + constants.WEBPACK_CONFIG_NAME, |
| 31 | + constants.PACKAGE_JSON_FILE_NAME, |
| 32 | + constants.PACKAGE_LOCK_JSON_FILE_NAME |
| 33 | + ]; |
| 34 | + |
| 35 | + static readonly tempFolder: string = ".update_backup"; |
| 36 | + static readonly updateFailMessage: string = "Could not update the project!"; |
| 37 | + static readonly backupFailMessage: string = "Could not backup project folders!"; |
| 38 | + |
| 39 | + public async update(projectDir: string, version?: string): Promise<void> { |
| 40 | + const projectData = this.$projectDataService.getProjectData(projectDir); |
| 41 | + const tmpDir = path.join(projectDir, UpdateController.tempFolder); |
| 42 | + |
| 43 | + try { |
| 44 | + this.backup(UpdateController.folders, tmpDir, projectData); |
| 45 | + } catch (error) { |
| 46 | + this.$logger.error(UpdateController.backupFailMessage); |
| 47 | + this.$fs.deleteDirectory(tmpDir); |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + try { |
| 52 | + await this.cleanUpProject(projectData); |
| 53 | + await this.updateProject(projectData, version); |
| 54 | + } catch (error) { |
| 55 | + this.restoreBackup(UpdateController.folders, tmpDir, projectData); |
| 56 | + this.$logger.error(UpdateController.updateFailMessage); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + public async shouldUpdate({projectDir, version}: {projectDir: string, version?: string}): Promise<boolean> { |
| 61 | + const projectData = this.$projectDataService.getProjectData(projectDir); |
| 62 | + const templateName = this.getTemplateName(projectData); |
| 63 | + const templateManifest = await this.getTemplateManifest(templateName, version); |
| 64 | + const dependencies = templateManifest.dependencies; |
| 65 | + const devDependencies = templateManifest.devDependencies; |
| 66 | + |
| 67 | + if ( |
| 68 | + await this.hasDependenciesToUpdate({dependencies, areDev: false, projectData}) || |
| 69 | + await this.hasDependenciesToUpdate({dependencies: devDependencies, areDev: true, projectData}) |
| 70 | + ) { |
| 71 | + return true; |
| 72 | + } |
| 73 | + |
| 74 | + for (const platform in this.$devicePlatformsConstants) { |
| 75 | + const lowercasePlatform = platform.toLowerCase(); |
| 76 | + const platformData = this.$platformsDataService.getPlatformData(lowercasePlatform, projectData); |
| 77 | + const currentPlatformData = this.$projectDataService.getNSValueFromContent(templateManifest, platformData.frameworkPackageName); |
| 78 | + const runtimeVersion = currentPlatformData && currentPlatformData.version; |
| 79 | + if (runtimeVersion && await this.shouldUpdateRuntimeVersion({targetVersion: runtimeVersion, platform, projectData, shouldAdd: false})) { |
| 80 | + return true; |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + private async cleanUpProject(projectData: IProjectData) { |
| 86 | + this.$logger.info("Clean old project artefacts."); |
| 87 | + this.$fs.deleteDirectory(path.join(projectData.projectDir, constants.HOOKS_DIR_NAME)); |
| 88 | + this.$fs.deleteDirectory(path.join(projectData.projectDir, constants.PLATFORMS_DIR_NAME)); |
| 89 | + this.$fs.deleteDirectory(path.join(projectData.projectDir, constants.NODE_MODULES_FOLDER_NAME)); |
| 90 | + this.$fs.deleteFile(path.join(projectData.projectDir, constants.WEBPACK_CONFIG_NAME)); |
| 91 | + this.$fs.deleteFile(path.join(projectData.projectDir, constants.PACKAGE_LOCK_JSON_FILE_NAME)); |
| 92 | + this.$logger.info("Clean old project artefacts complete."); |
| 93 | + } |
| 94 | + |
| 95 | + private async updateProject(projectData: IProjectData, version: string): Promise<void> { |
| 96 | + const templateName = this.getTemplateName(projectData); |
| 97 | + const templateManifest = await this.getTemplateManifest(templateName, version); |
| 98 | + |
| 99 | + this.$logger.info("Start updating dependencies."); |
| 100 | + await this.updateDependencies({ dependencies: templateManifest.dependencies, areDev: false, projectData}); |
| 101 | + this.$logger.info("Finished updating dependencies."); |
| 102 | + this.$logger.info("Start updating devDependencies."); |
| 103 | + await this.updateDependencies({ dependencies: templateManifest.devDependencies, areDev: true, projectData}); |
| 104 | + this.$logger.info("Finished updating devDependencies."); |
| 105 | + |
| 106 | + this.$logger.info("Start updating runtimes."); |
| 107 | + await this.updateRuntimes(templateManifest, projectData); |
| 108 | + this.$logger.info("Finished updating runtimes."); |
| 109 | + |
| 110 | + this.$logger.info("Install packages."); |
| 111 | + await this.$packageManager.install(projectData.projectDir, projectData.projectDir, { |
| 112 | + disableNpmInstall: false, |
| 113 | + frameworkPath: null, |
| 114 | + ignoreScripts: false, |
| 115 | + path: projectData.projectDir |
| 116 | + }); |
| 117 | + } |
| 118 | + |
| 119 | + private async updateDependencies( {dependencies, areDev, projectData} : {dependencies: IDictionary<string>, areDev: boolean, projectData: IProjectData}) { |
| 120 | + for (const dependency in dependencies) { |
| 121 | + const templateVersion = dependencies[dependency]; |
| 122 | + if (this.shouldSkipDependency({ packageName: dependency, isDev: areDev }, projectData)) { |
| 123 | + continue; |
| 124 | + } |
| 125 | + |
| 126 | + if (await this.shouldUpdateDependency(dependency, templateVersion, areDev, projectData)) { |
| 127 | + this.$logger.info(`Updating '${dependency}' to version '${templateVersion}'.`); |
| 128 | + this.$pluginsService.addToPackageJson(dependency, templateVersion, areDev, projectData.projectDir); |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + private async shouldUpdateDependency(dependency: string, targetVersion: string, isDev: boolean, projectData: IProjectData) { |
| 134 | + const collection = isDev ? projectData.devDependencies : projectData.dependencies; |
| 135 | + const projectVersion = collection[dependency]; |
| 136 | + const maxSatisfyingTargetVersion = await this.$packageInstallationManager.maxSatisfyingVersion(dependency, targetVersion); |
| 137 | + const maxSatisfyingProjectVersion = await this.getMaxDependencyVersion(dependency, projectVersion); |
| 138 | + |
| 139 | + if (maxSatisfyingProjectVersion && semver.gt(maxSatisfyingTargetVersion, maxSatisfyingProjectVersion)) { |
| 140 | + return true; |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + private async hasDependenciesToUpdate({dependencies, areDev, projectData}: {dependencies: IDictionary<string>, areDev: boolean, projectData:IProjectData}) { |
| 145 | + for (const dependency in dependencies) { |
| 146 | + const templateVersion = dependencies[dependency]; |
| 147 | + if (this.shouldSkipDependency({ packageName: dependency, isDev: areDev }, projectData)) { |
| 148 | + continue; |
| 149 | + } |
| 150 | + |
| 151 | + if (await this.shouldUpdateDependency(dependency, templateVersion, areDev, projectData)) { |
| 152 | + return true; |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + private async updateRuntimes(templateData: Object, projectData: IProjectData) { |
| 158 | + for (const platform in this.$devicePlatformsConstants) { |
| 159 | + const lowercasePlatform = platform.toLowerCase(); |
| 160 | + const platformData = this.$platformsDataService.getPlatformData(lowercasePlatform, projectData); |
| 161 | + const currentPlatformData = this.$projectDataService.getNSValueFromContent(templateData, platformData.frameworkPackageName); |
| 162 | + const runtimeVersion = currentPlatformData && currentPlatformData.version; |
| 163 | + if (runtimeVersion && await this.shouldUpdateRuntimeVersion({targetVersion: runtimeVersion, platform, projectData, shouldAdd: false})) { |
| 164 | + this.$logger.info(`Updating ${platform} platform to version '${runtimeVersion}'.`); |
| 165 | + await this.$addPlatformService.setPlatformVersion(platformData, projectData, runtimeVersion); |
| 166 | + } |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + private async _getTemplateManifest(templateName: string, version: string) { |
| 171 | + let packageVersion = version ? version : await this.$packageInstallationManager.getLatestCompatibleVersionSafe(templateName); |
| 172 | + packageVersion = semver.valid(version) ? version : await this.$packageManager.getTagVersion(templateName, packageVersion); |
| 173 | + packageVersion = packageVersion ? packageVersion : await this.$packageInstallationManager.getLatestCompatibleVersionSafe(templateName); |
| 174 | + |
| 175 | + return await this.$pacoteService.manifest(`${templateName}@${packageVersion}`, { fullMetadata: true }); |
| 176 | + } |
| 177 | + |
| 178 | + private getTemplateName(projectData: IProjectData) { |
| 179 | + let template; |
| 180 | + switch (projectData.projectType) { |
| 181 | + case constants.ProjectTypes.NgFlavorName: { |
| 182 | + template = constants.RESERVED_TEMPLATE_NAMES.angular; |
| 183 | + break; |
| 184 | + } |
| 185 | + case constants.ProjectTypes.VueFlavorName: { |
| 186 | + template = constants.RESERVED_TEMPLATE_NAMES.vue; |
| 187 | + break; |
| 188 | + } |
| 189 | + case constants.ProjectTypes.TsFlavorName: { |
| 190 | + template = constants.RESERVED_TEMPLATE_NAMES.typescript; |
| 191 | + break; |
| 192 | + } |
| 193 | + case constants.ProjectTypes.JsFlavorName: { |
| 194 | + template = constants.RESERVED_TEMPLATE_NAMES.javascript; |
| 195 | + break; |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | + return template; |
| 200 | + } |
| 201 | +} |
| 202 | + |
| 203 | +$injector.register("updateController", UpdateController); |
0 commit comments