@@ -5,7 +5,11 @@ import * as glob from "glob";
5
5
import * as _ from "lodash";
6
6
import { UpdateControllerBase } from "./update-controller-base";
7
7
import { fromWindowsRelativePathToUnix, getHash } from "../common/helpers";
8
- import { IProjectDataService, IProjectData } from "../definitions/project";
8
+ import {
9
+ IProjectDataService,
10
+ IProjectData,
11
+ IProjectConfigService,
12
+ } from "../definitions/project";
9
13
import {
10
14
IMigrateController,
11
15
IMigrationDependency,
@@ -15,8 +19,9 @@ import {
15
19
IPlatformCommandHelper,
16
20
IPackageInstallationManager,
17
21
IPackageManager,
18
- IAndroidResourcesMigrationService,
22
+ // IAndroidResourcesMigrationService,
19
23
IPlatformValidationService,
24
+ IOptions,
20
25
} from "../declarations";
21
26
import {
22
27
IPlatformsDataService,
@@ -33,6 +38,7 @@ import {
33
38
import { IInjector } from "../common/definitions/yok";
34
39
import { injector } from "../common/yok";
35
40
import { IJsonFileSettingsService } from "../common/definitions/json-file-settings-service";
41
+ // import { project } from "nativescript-dev-xcode";
36
42
37
43
export class MigrateController
38
44
extends UpdateControllerBase
@@ -51,13 +57,15 @@ Running this command will ${MigrateController.COMMON_MIGRATE_MESSAGE}`;
51
57
protected $packageInstallationManager: IPackageInstallationManager,
52
58
protected $packageManager: IPackageManager,
53
59
protected $pacoteService: IPacoteService,
54
- private $androidResourcesMigrationService: IAndroidResourcesMigrationService,
60
+ // private $androidResourcesMigrationService: IAndroidResourcesMigrationService,
55
61
private $devicePlatformsConstants: Mobile.IDevicePlatformsConstants,
56
62
private $logger: ILogger,
57
63
private $errors: IErrors,
58
64
private $addPlatformService: IAddPlatformService,
59
65
private $pluginsService: IPluginsService,
60
66
private $projectDataService: IProjectDataService,
67
+ private $projectConfigService: IProjectConfigService,
68
+ private $options: IOptions,
61
69
private $platformValidationService: IPlatformValidationService,
62
70
private $resources: IResourceLoader,
63
71
private $injector: IInjector,
@@ -88,6 +96,7 @@ Running this command will ${MigrateController.COMMON_MIGRATE_MESSAGE}`;
88
96
constants.PACKAGE_LOCK_JSON_FILE_NAME,
89
97
constants.TSCCONFIG_TNS_JSON_NAME,
90
98
constants.KARMA_CONFIG_NAME,
99
+ constants.CONFIG_NS_FILE_NAME,
91
100
];
92
101
93
102
private get $jsonFileSettingsService(): IJsonFileSettingsService {
@@ -234,7 +243,13 @@ Running this command will ${MigrateController.COMMON_MIGRATE_MESSAGE}`;
234
243
235
244
try {
236
245
this.$logger.info("Backup project configuration.");
237
- this.backup(MigrateController.folders, backupDir, projectData.projectDir);
246
+ const backupFolders = MigrateController.folders;
247
+ const embeddedPackagePath = path.join(
248
+ projectData.getAppDirectoryRelativePath(),
249
+ "package.json"
250
+ );
251
+ backupFolders.push(embeddedPackagePath);
252
+ this.backup(backupFolders, backupDir, projectData.projectDir);
238
253
this.$logger.info("Backup project configuration complete.");
239
254
} catch (error) {
240
255
this.$logger.error(MigrateController.backupFailMessage);
@@ -254,21 +269,24 @@ Running this command will ${MigrateController.COMMON_MIGRATE_MESSAGE}`;
254
269
);
255
270
}
256
271
257
- await this.migrateOldAndroidAppResources(projectData, backupDir);
272
+ // await this.migrateOldAndroidAppResources(projectData, backupDir);
258
273
259
274
try {
260
275
await this.cleanUpProject(projectData);
276
+ await this.migrateConfig(projectData);
261
277
await this.migrateDependencies(
262
278
projectData,
263
279
platforms,
264
280
allowInvalidVersions
265
281
);
266
282
} catch (error) {
267
- this.restoreBackup(
268
- MigrateController.folders,
269
- backupDir ,
270
- projectData.projectDir
283
+ const backupFolders = MigrateController.folders;
284
+ const embeddedPackagePath = path.join(
285
+ projectData.getAppDirectoryRelativePath() ,
286
+ "package.json"
271
287
);
288
+ backupFolders.push(embeddedPackagePath);
289
+ this.restoreBackup(backupFolders, backupDir, projectData.projectDir);
272
290
this.$errors.fail(
273
291
`${MigrateController.migrateFailMessage} The error is: ${error}`
274
292
);
@@ -325,6 +343,14 @@ Running this command will ${MigrateController.COMMON_MIGRATE_MESSAGE}`;
325
343
allowInvalidVersions,
326
344
}: IMigrationData): Promise<boolean> {
327
345
const projectData = this.$projectDataService.getProjectData(projectDir);
346
+ const isMigrate = this.$options.argv._[0] === "migrate";
347
+ const projectInfo = this.$projectConfigService.detectInfo(
348
+ projectData.projectDir
349
+ );
350
+ if (!isMigrate && projectInfo.usesLegacyConfig) {
351
+ return;
352
+ }
353
+
328
354
const shouldMigrateCommonMessage =
329
355
"The app is not compatible with this CLI version and it should be migrated. Reason: ";
330
356
@@ -458,26 +484,26 @@ Running this command will ${MigrateController.COMMON_MIGRATE_MESSAGE}`;
458
484
return await this.$fs.getFileShasum(projectPackageJsonFilePath);
459
485
}
460
486
461
- private async migrateOldAndroidAppResources(
462
- projectData: IProjectData,
463
- backupDir: string
464
- ) {
465
- const appResourcesPath = projectData.getAppResourcesDirectoryPath();
466
- if (!this.$androidResourcesMigrationService.hasMigrated(appResourcesPath)) {
467
- this.$logger.info("Migrate old Android App_Resources structure.");
468
- try {
469
- await this.$androidResourcesMigrationService.migrate(
470
- appResourcesPath,
471
- backupDir
472
- );
473
- } catch (error) {
474
- this.$logger.warn(
475
- "Migrate old Android App_Resources structure failed: ",
476
- error.message
477
- );
478
- }
479
- }
480
- }
487
+ // private async migrateOldAndroidAppResources(
488
+ // projectData: IProjectData,
489
+ // backupDir: string
490
+ // ) {
491
+ // const appResourcesPath = projectData.getAppResourcesDirectoryPath();
492
+ // if (!this.$androidResourcesMigrationService.hasMigrated(appResourcesPath)) {
493
+ // this.$logger.info("Migrate old Android App_Resources structure.");
494
+ // try {
495
+ // await this.$androidResourcesMigrationService.migrate(
496
+ // appResourcesPath,
497
+ // backupDir
498
+ // );
499
+ // } catch (error) {
500
+ // this.$logger.warn(
501
+ // "Migrate old Android App_Resources structure failed: ",
502
+ // error.message
503
+ // );
504
+ // }
505
+ // }
506
+ // }
481
507
482
508
private async cleanUpProject(projectData: IProjectData): Promise<void> {
483
509
this.$logger.info("Clean old project artifacts.");
@@ -776,6 +802,79 @@ Running this command will ${MigrateController.COMMON_MIGRATE_MESSAGE}`;
776
802
: !allowInvalidVersions;
777
803
}
778
804
805
+ private async migrateConfig(projectData: IProjectData) {
806
+ const embeddedPackagePath = path.resolve(
807
+ projectData.projectDir,
808
+ projectData.getAppDirectoryRelativePath(),
809
+ constants.PACKAGE_JSON_FILE_NAME
810
+ );
811
+ const legacyNsConfigPath = path.resolve(
812
+ projectData.projectDir,
813
+ constants.CONFIG_NS_FILE_NAME
814
+ );
815
+ let embeddedPackageData: any = {};
816
+ if (this.$fs.exists(embeddedPackagePath)) {
817
+ embeddedPackageData = this.$fs.readJson(embeddedPackagePath);
818
+ }
819
+ let legacyNsConfigData: any = {};
820
+ if (this.$fs.exists(legacyNsConfigPath)) {
821
+ legacyNsConfigData = this.$fs.readJson(legacyNsConfigPath);
822
+ }
823
+ const legacyData: any = {
824
+ ...embeddedPackageData,
825
+ ...legacyNsConfigData,
826
+ };
827
+ const packageJsonPath: any = path.resolve(
828
+ projectData.projectDir,
829
+ constants.PACKAGE_JSON_FILE_NAME
830
+ );
831
+ const packageJsonData: any = this.$fs.readFile(packageJsonPath);
832
+ if (legacyData.main) {
833
+ packageJsonPath.main = legacyData.main;
834
+ delete legacyData.main;
835
+ }
836
+ if (
837
+ legacyData &&
838
+ legacyData.android &&
839
+ typeof legacyData.android.codeCache === "string"
840
+ ) {
841
+ legacyData.android.codeCache = legacyData.android.codeCache === "true";
842
+ }
843
+ const flattenObjectToPaths = (obj: any, basePath?: string): any => {
844
+ const toPath = (key: any) => [basePath, key].filter(Boolean).join(".");
845
+ return Object.keys(obj).reduce((all: any, key) => {
846
+ if (typeof obj[key] === "object") {
847
+ return [...all, ...flattenObjectToPaths(obj[key], toPath(key))];
848
+ }
849
+ return [
850
+ ...all,
851
+ {
852
+ key: toPath(key),
853
+ value: obj[key],
854
+ },
855
+ ];
856
+ }, []);
857
+ };
858
+ const dotNotationPaths = flattenObjectToPaths(legacyData);
859
+ dotNotationPaths.forEach((p: any) => {
860
+ // this.$logger.info(p.key, p.value);
861
+ this.$projectConfigService.setValue(p.key, p.value);
862
+ });
863
+ if (
864
+ packageJsonData &&
865
+ packageJsonData.nativescript &&
866
+ packageJsonData.nativescript.id
867
+ ) {
868
+ this.$projectConfigService.setValue(
869
+ "id",
870
+ packageJsonData.nativescript.id
871
+ );
872
+ delete packageJsonData.nativescript;
873
+ }
874
+ this.$fs.writeJson(packageJsonPath, packageJsonData);
875
+ this.$logger.info(`Migrated to ${constants.CONFIG_FILE_NAME_TS}`);
876
+ }
877
+
779
878
private async migrateUnitTestRunner(
780
879
projectData: IProjectData,
781
880
migrationBackupDirPath: string
0 commit comments