Skip to content

Commit 54b093f

Browse files
committed
fix: do not allow invalid version during tns migrate
1 parent 3358280 commit 54b093f

File tree

2 files changed

+28
-23
lines changed

2 files changed

+28
-23
lines changed

lib/controllers/migrate-controller.ts

+26-22
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@ Running this command will ${MigrateController.COMMON_MIGRATE_MESSAGE}`;
7575
{ packageName: "nativescript-cardview", verifiedVersion: "3.2.0" },
7676
{
7777
packageName: "nativescript-unit-test-runner", verifiedVersion: "0.6.4",
78-
shouldMigrateAction: async (projectData: IProjectData) => {
78+
shouldMigrateAction: async (projectData: IProjectData, allowInvalidVersions: boolean) => {
7979
const dependency = { packageName: "nativescript-unit-test-runner", verifiedVersion: "0.6.4", isDev: false };
80-
const result = this.hasDependency(dependency, projectData) && await this.shouldMigrateDependencyVersion(dependency, projectData);
80+
const result = this.hasDependency(dependency, projectData) && await this.shouldMigrateDependencyVersion(dependency, projectData, allowInvalidVersions);
8181
return result;
8282
},
8383
migrateAction: this.migrateUnitTestRunner.bind(this)
@@ -95,7 +95,7 @@ Running this command will ${MigrateController.COMMON_MIGRATE_MESSAGE}`;
9595
};
9696
}
9797

98-
public async migrate({ projectDir, platforms }: IMigrationData): Promise<void> {
98+
public async migrate({ projectDir, platforms, allowInvalidVersions = false }: IMigrationData): Promise<void> {
9999
const projectData = this.$projectDataService.getProjectData(projectDir);
100100
const backupDir = path.join(projectDir, MigrateController.backupFolder);
101101

@@ -121,7 +121,7 @@ Running this command will ${MigrateController.COMMON_MIGRATE_MESSAGE}`;
121121

122122
try {
123123
await this.cleanUpProject(projectData);
124-
await this.migrateDependencies(projectData, platforms);
124+
await this.migrateDependencies(projectData, platforms, allowInvalidVersions);
125125
} catch (error) {
126126
this.restoreBackup(MigrateController.folders, backupDir, projectData.projectDir);
127127
this.$errors.failWithoutHelp(`${MigrateController.migrateFailMessage} The error is: ${error}`);
@@ -130,15 +130,15 @@ Running this command will ${MigrateController.COMMON_MIGRATE_MESSAGE}`;
130130
this.$logger.info(MigrateController.MIGRATE_FINISH_MESSAGE);
131131
}
132132

133-
public async shouldMigrate({ projectDir, platforms }: IMigrationData): Promise<boolean> {
133+
public async shouldMigrate({ projectDir, platforms, allowInvalidVersions = false }: IMigrationData): Promise<boolean> {
134134
const projectData = this.$projectDataService.getProjectData(projectDir);
135135
const shouldMigrateCommonMessage = "The app is not compatible with this CLI version and it should be migrated. Reason: ";
136136

137137
for (let i = 0; i < this.migrationDependencies.length; i++) {
138138
const dependency = this.migrationDependencies[i];
139139
const hasDependency = this.hasDependency(dependency, projectData);
140140

141-
if (hasDependency && dependency.shouldMigrateAction && await dependency.shouldMigrateAction(projectData)) {
141+
if (hasDependency && dependency.shouldMigrateAction && await dependency.shouldMigrateAction(projectData, allowInvalidVersions)) {
142142
this.$logger.trace(`${shouldMigrateCommonMessage}'${dependency.packageName}' requires an update.`);
143143
return true;
144144
}
@@ -148,7 +148,7 @@ Running this command will ${MigrateController.COMMON_MIGRATE_MESSAGE}`;
148148
return true;
149149
}
150150

151-
if (hasDependency && await this.shouldMigrateDependencyVersion(dependency, projectData)) {
151+
if (hasDependency && await this.shouldMigrateDependencyVersion(dependency, projectData, allowInvalidVersions)) {
152152
this.$logger.trace(`${shouldMigrateCommonMessage}'${dependency.packageName}' should be updated.`);
153153
return true;
154154
}
@@ -163,15 +163,15 @@ Running this command will ${MigrateController.COMMON_MIGRATE_MESSAGE}`;
163163
platform = platform && platform.toLowerCase();
164164

165165
const hasRuntimeDependency = this.hasRuntimeDependency({ platform, projectData });
166-
if (hasRuntimeDependency && await this.shouldUpdateRuntimeVersion({ targetVersion: this.verifiedPlatformVersions[platform.toLowerCase()], platform, projectData })) {
166+
if (hasRuntimeDependency && await this.shouldUpdateRuntimeVersion(this.verifiedPlatformVersions[platform.toLowerCase()], platform, projectData, allowInvalidVersions)) {
167167
this.$logger.trace(`${shouldMigrateCommonMessage}Platform '${platform}' should be updated.`);
168168
return true;
169169
}
170170
}
171171
}
172172

173-
public async validate({ projectDir, platforms }: IMigrationData): Promise<void> {
174-
const shouldMigrate = await this.shouldMigrate({ projectDir, platforms });
173+
public async validate({ projectDir, platforms, allowInvalidVersions = true }: IMigrationData): Promise<void> {
174+
const shouldMigrate = await this.shouldMigrate({ projectDir, platforms, allowInvalidVersions });
175175
if (shouldMigrate) {
176176
this.$errors.failWithoutHelp(MigrateController.UNABLE_TO_MIGRATE_APP_ERROR);
177177
}
@@ -275,26 +275,26 @@ Running this command will ${MigrateController.COMMON_MIGRATE_MESSAGE}`;
275275
return autoGeneratedFiles;
276276
}
277277

278-
private async migrateDependencies(projectData: IProjectData, platforms: string[]): Promise<void> {
278+
private async migrateDependencies(projectData: IProjectData, platforms: string[], allowInvalidVersions: boolean): Promise<void> {
279279
this.$logger.info("Start dependencies migration.");
280280
for (let i = 0; i < this.migrationDependencies.length; i++) {
281281
const dependency = this.migrationDependencies[i];
282282
const hasDependency = this.hasDependency(dependency, projectData);
283283

284-
if (hasDependency && dependency.migrateAction && await dependency.shouldMigrateAction(projectData)) {
284+
if (hasDependency && dependency.migrateAction && await dependency.shouldMigrateAction(projectData, allowInvalidVersions)) {
285285
const newDependencies = await dependency.migrateAction(projectData, path.join(projectData.projectDir, MigrateController.backupFolder));
286286
for (const newDependency of newDependencies) {
287-
await this.migrateDependency(newDependency, projectData);
287+
await this.migrateDependency(newDependency, projectData, allowInvalidVersions);
288288
}
289289
}
290290

291-
await this.migrateDependency(dependency, projectData);
291+
await this.migrateDependency(dependency, projectData, allowInvalidVersions);
292292
}
293293

294294
for (const platform of platforms) {
295295
const lowercasePlatform = platform.toLowerCase();
296296
const hasRuntimeDependency = this.hasRuntimeDependency({ platform, projectData });
297-
if (hasRuntimeDependency && await this.shouldUpdateRuntimeVersion({ targetVersion: this.verifiedPlatformVersions[lowercasePlatform], platform, projectData })) {
297+
if (hasRuntimeDependency && await this.shouldUpdateRuntimeVersion(this.verifiedPlatformVersions[lowercasePlatform], platform, projectData, allowInvalidVersions)) {
298298
const verifiedPlatformVersion = this.verifiedPlatformVersions[lowercasePlatform];
299299
const platformData = this.$platformsDataService.getPlatformData(lowercasePlatform, projectData);
300300
this.$logger.info(`Updating ${platform} platform to version '${verifiedPlatformVersion}'.`);
@@ -313,7 +313,7 @@ Running this command will ${MigrateController.COMMON_MIGRATE_MESSAGE}`;
313313
this.$logger.info("Migration complete.");
314314
}
315315

316-
private async migrateDependency(dependency: IMigrationDependency, projectData: IProjectData): Promise<void> {
316+
private async migrateDependency(dependency: IMigrationDependency, projectData: IProjectData, allowInvalidVersions: boolean): Promise<void> {
317317
const hasDependency = this.hasDependency(dependency, projectData);
318318
if (hasDependency && dependency.warning) {
319319
this.$logger.warn(dependency.warning);
@@ -336,7 +336,7 @@ Running this command will ${MigrateController.COMMON_MIGRATE_MESSAGE}`;
336336
}
337337

338338
const dependencyVersion = await this.getDependencyVerifiedVersion(dependency, projectData);
339-
if (hasDependency && await this.shouldMigrateDependencyVersion(dependency, projectData)) {
339+
if (hasDependency && await this.shouldMigrateDependencyVersion(dependency, projectData, allowInvalidVersions)) {
340340
this.$logger.info(`Updating '${dependency.packageName}' to compatible version '${dependencyVersion}'`);
341341
this.$pluginsService.addToPackageJson(dependency.packageName, dependencyVersion, dependency.isDev, projectData.projectDir);
342342
return;
@@ -356,21 +356,25 @@ Running this command will ${MigrateController.COMMON_MIGRATE_MESSAGE}`;
356356
return dependency.verifiedVersion;
357357
}
358358

359-
private async shouldMigrateDependencyVersion(dependency: IMigrationDependency, projectData: IProjectData): Promise<boolean> {
359+
private async shouldMigrateDependencyVersion(dependency: IMigrationDependency, projectData: IProjectData, allowInvalidVersions: boolean): Promise<boolean> {
360360
const devDependencies = projectData.devDependencies || {};
361361
const dependencies = projectData.dependencies || {};
362362
const packageName = dependency.packageName;
363363
const referencedVersion = dependencies[packageName] || devDependencies[packageName];
364364
const installedVersion = await this.getMaxDependencyVersion(dependency.packageName, referencedVersion);
365365
const requiredVersion = await this.getDependencyVerifiedVersion(dependency, projectData);
366366

367-
return !!installedVersion && semver.lt(installedVersion, requiredVersion);
367+
return this.isOutdatedVersion(installedVersion, requiredVersion, allowInvalidVersions);
368368
}
369369

370-
protected async shouldUpdateRuntimeVersion({ targetVersion, platform, projectData }: { targetVersion: string, platform: string, projectData: IProjectData }): Promise<boolean> {
371-
const maxRuntimeVersion = await this.getMaxRuntimeVersion({ platform, projectData });
370+
private async shouldUpdateRuntimeVersion(targetVersion: string, platform: string, projectData: IProjectData, allowInvalidVersions: boolean): Promise<boolean> {
371+
const installedVersion = await this.getMaxRuntimeVersion({ platform, projectData });
372372

373-
return !(maxRuntimeVersion && semver.gte(maxRuntimeVersion, targetVersion));
373+
return this.isOutdatedVersion(installedVersion, targetVersion, allowInvalidVersions);
374+
}
375+
376+
private isOutdatedVersion(version: string, targetVersion: string, allowInvalidVersions: boolean): boolean {
377+
return !!version ? semver.lt(version, targetVersion) : !allowInvalidVersions;
374378
}
375379

376380
private async migrateUnitTestRunner(projectData: IProjectData, migrationBackupDirPath: string): Promise<IMigrationDependency[]> {

lib/definitions/migrate.d.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ interface IMigrateController {
66

77
interface IMigrationData extends IProjectDir {
88
platforms: string[];
9+
allowInvalidVersions?: boolean;
910
}
1011

1112
interface IDependency {
@@ -20,6 +21,6 @@ interface IMigrationDependency extends IDependency {
2021
verifiedVersion?: string;
2122
getVerifiedVersion?: (projectData: IProjectData) => Promise<string>;
2223
shouldAddIfMissing?: boolean;
23-
shouldMigrateAction?: (projectData: IProjectData) => Promise<boolean>;
24+
shouldMigrateAction?: (projectData: IProjectData, allowInvalidVersions: boolean) => Promise<boolean>;
2425
migrateAction?: (projectData: IProjectData, migrationBackupDirPath: string) => Promise<IMigrationDependency[]>;
2526
}

0 commit comments

Comments
 (0)