@@ -3,7 +3,7 @@ import * as semver from "semver";
3
3
import * as constants from "../constants" ;
4
4
import * as glob from "glob" ;
5
5
import { UpdateControllerBase } from "./update-controller-base" ;
6
- import { fromWindowsRelativePathToUnix } from "../common/helpers" ;
6
+ import { fromWindowsRelativePathToUnix , getHash } from "../common/helpers" ;
7
7
8
8
export class MigrateController extends UpdateControllerBase implements IMigrateController {
9
9
private static COMMON_MIGRATE_MESSAGE = "not affect the codebase of the application and you might need to do additional changes manually – for more information, refer to the instructions in the following blog post: https://www.nativescript.org/blog/nativescript-6.0-application-migration" ;
@@ -27,7 +27,10 @@ Running this command will ${MigrateController.COMMON_MIGRATE_MESSAGE}`;
27
27
private $pluginsService : IPluginsService ,
28
28
private $projectDataService : IProjectDataService ,
29
29
private $platformValidationService : IPlatformValidationService ,
30
- private $resources : IResourceLoader ) {
30
+ private $resources : IResourceLoader ,
31
+ private $injector : IInjector ,
32
+ private $settingsService : ISettingsService ,
33
+ private $staticConfig : Config . IStaticConfig ) {
31
34
super ( $fs , $platformCommandHelper , $platformsDataService , $packageInstallationManager , $packageManager , $pacoteService ) ;
32
35
}
33
36
@@ -46,6 +49,12 @@ Running this command will ${MigrateController.COMMON_MIGRATE_MESSAGE}`;
46
49
constants . KARMA_CONFIG_NAME
47
50
] ;
48
51
52
+ private get $jsonFileSettingsService ( ) : IJsonFileSettingsService {
53
+ const cliVersion = semver . coerce ( this . $staticConfig . version ) ;
54
+ const shouldMigrateCacheFilePath = path . join ( this . $settingsService . getProfileDir ( ) , `should-migrate-cache-${ cliVersion } .json` ) ;
55
+ return this . $injector . resolve ( "jsonFileSettingsService" , { jsonFileSettingsPath : shouldMigrateCacheFilePath } ) ;
56
+ }
57
+
49
58
private migrationDependencies : IMigrationDependency [ ] = [
50
59
{ packageName : constants . TNS_CORE_MODULES_NAME , verifiedVersion : "6.0.1" } ,
51
60
{ packageName : constants . TNS_CORE_MODULES_WIDGETS_NAME , verifiedVersion : "6.0.1" } ,
@@ -147,6 +156,30 @@ Running this command will ${MigrateController.COMMON_MIGRATE_MESSAGE}`;
147
156
}
148
157
149
158
public async shouldMigrate ( { projectDir, platforms, allowInvalidVersions = false } : IMigrationData ) : Promise < boolean > {
159
+ const remainingPlatforms = [ ] ;
160
+ let shouldMigrate = false ;
161
+
162
+ for ( const platform of platforms ) {
163
+ const cachedResult = await this . getCachedShouldMigrate ( projectDir , platform ) ;
164
+ if ( cachedResult !== false ) {
165
+ remainingPlatforms . push ( platform ) ;
166
+ }
167
+ }
168
+
169
+ if ( remainingPlatforms . length > 0 ) {
170
+ shouldMigrate = await this . _shouldMigrate ( { projectDir, platforms : remainingPlatforms , allowInvalidVersions } ) ;
171
+
172
+ if ( ! shouldMigrate ) {
173
+ for ( const remainingPlatform of remainingPlatforms ) {
174
+ await this . setCachedShouldMigrate ( projectDir , remainingPlatform ) ;
175
+ }
176
+ }
177
+ }
178
+
179
+ return shouldMigrate ;
180
+ }
181
+
182
+ private async _shouldMigrate ( { projectDir, platforms, allowInvalidVersions } : IMigrationData ) : Promise < boolean > {
150
183
const projectData = this . $projectDataService . getProjectData ( projectDir ) ;
151
184
const shouldMigrateCommonMessage = "The app is not compatible with this CLI version and it should be migrated. Reason: " ;
152
185
@@ -196,6 +229,28 @@ Running this command will ${MigrateController.COMMON_MIGRATE_MESSAGE}`;
196
229
}
197
230
}
198
231
232
+ private async getCachedShouldMigrate ( projectDir : string , platform : string ) : Promise < boolean > {
233
+ let cachedShouldMigrateValue = null ;
234
+
235
+ const cachedHash = await this . $jsonFileSettingsService . getSettingValue ( getHash ( `${ projectDir } ${ platform . toLowerCase ( ) } ` ) ) ;
236
+ const packageJsonHash = await this . getPachageJsonHash ( projectDir ) ;
237
+ if ( cachedHash === packageJsonHash ) {
238
+ cachedShouldMigrateValue = false ;
239
+ }
240
+
241
+ return cachedShouldMigrateValue ;
242
+ }
243
+
244
+ private async setCachedShouldMigrate ( projectDir : string , platform : string ) : Promise < void > {
245
+ const packageJsonHash = await this . getPachageJsonHash ( projectDir ) ;
246
+ await this . $jsonFileSettingsService . saveSetting ( getHash ( `${ projectDir } ${ platform . toLowerCase ( ) } ` ) , packageJsonHash ) ;
247
+ }
248
+
249
+ private async getPachageJsonHash ( projectDir : string ) {
250
+ const projectPackageJsonFilePath = path . join ( projectDir , constants . PACKAGE_JSON_FILE_NAME ) ;
251
+ return await this . $fs . getFileShasum ( projectPackageJsonFilePath ) ;
252
+ }
253
+
199
254
private async migrateOldAndroidAppResources ( projectData : IProjectData , backupDir : string ) {
200
255
const appResourcesPath = projectData . getAppResourcesDirectoryPath ( ) ;
201
256
if ( ! this . $androidResourcesMigrationService . hasMigrated ( appResourcesPath ) ) {
0 commit comments