Skip to content

Commit 06c1ecc

Browse files
NathanWalkerrigor789
authored andcommitted
chore: config migration
1 parent 9160870 commit 06c1ecc

File tree

9 files changed

+407
-191
lines changed

9 files changed

+407
-191
lines changed

lib/controllers/migrate-controller.ts

Lines changed: 128 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ import * as glob from "glob";
55
import * as _ from "lodash";
66
import { UpdateControllerBase } from "./update-controller-base";
77
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";
913
import {
1014
IMigrateController,
1115
IMigrationDependency,
@@ -15,8 +19,9 @@ import {
1519
IPlatformCommandHelper,
1620
IPackageInstallationManager,
1721
IPackageManager,
18-
IAndroidResourcesMigrationService,
22+
// IAndroidResourcesMigrationService,
1923
IPlatformValidationService,
24+
IOptions,
2025
} from "../declarations";
2126
import {
2227
IPlatformsDataService,
@@ -33,6 +38,7 @@ import {
3338
import { IInjector } from "../common/definitions/yok";
3439
import { injector } from "../common/yok";
3540
import { IJsonFileSettingsService } from "../common/definitions/json-file-settings-service";
41+
// import { project } from "nativescript-dev-xcode";
3642

3743
export class MigrateController
3844
extends UpdateControllerBase
@@ -51,13 +57,15 @@ Running this command will ${MigrateController.COMMON_MIGRATE_MESSAGE}`;
5157
protected $packageInstallationManager: IPackageInstallationManager,
5258
protected $packageManager: IPackageManager,
5359
protected $pacoteService: IPacoteService,
54-
private $androidResourcesMigrationService: IAndroidResourcesMigrationService,
60+
// private $androidResourcesMigrationService: IAndroidResourcesMigrationService,
5561
private $devicePlatformsConstants: Mobile.IDevicePlatformsConstants,
5662
private $logger: ILogger,
5763
private $errors: IErrors,
5864
private $addPlatformService: IAddPlatformService,
5965
private $pluginsService: IPluginsService,
6066
private $projectDataService: IProjectDataService,
67+
private $projectConfigService: IProjectConfigService,
68+
private $options: IOptions,
6169
private $platformValidationService: IPlatformValidationService,
6270
private $resources: IResourceLoader,
6371
private $injector: IInjector,
@@ -88,6 +96,7 @@ Running this command will ${MigrateController.COMMON_MIGRATE_MESSAGE}`;
8896
constants.PACKAGE_LOCK_JSON_FILE_NAME,
8997
constants.TSCCONFIG_TNS_JSON_NAME,
9098
constants.KARMA_CONFIG_NAME,
99+
constants.CONFIG_NS_FILE_NAME,
91100
];
92101

93102
private get $jsonFileSettingsService(): IJsonFileSettingsService {
@@ -234,7 +243,13 @@ Running this command will ${MigrateController.COMMON_MIGRATE_MESSAGE}`;
234243

235244
try {
236245
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);
238253
this.$logger.info("Backup project configuration complete.");
239254
} catch (error) {
240255
this.$logger.error(MigrateController.backupFailMessage);
@@ -254,21 +269,24 @@ Running this command will ${MigrateController.COMMON_MIGRATE_MESSAGE}`;
254269
);
255270
}
256271

257-
await this.migrateOldAndroidAppResources(projectData, backupDir);
272+
// await this.migrateOldAndroidAppResources(projectData, backupDir);
258273

259274
try {
260275
await this.cleanUpProject(projectData);
276+
await this.migrateConfig(projectData);
261277
await this.migrateDependencies(
262278
projectData,
263279
platforms,
264280
allowInvalidVersions
265281
);
266282
} 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"
271287
);
288+
backupFolders.push(embeddedPackagePath);
289+
this.restoreBackup(backupFolders, backupDir, projectData.projectDir);
272290
this.$errors.fail(
273291
`${MigrateController.migrateFailMessage} The error is: ${error}`
274292
);
@@ -325,6 +343,14 @@ Running this command will ${MigrateController.COMMON_MIGRATE_MESSAGE}`;
325343
allowInvalidVersions,
326344
}: IMigrationData): Promise<boolean> {
327345
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+
328354
const shouldMigrateCommonMessage =
329355
"The app is not compatible with this CLI version and it should be migrated. Reason: ";
330356

@@ -458,26 +484,26 @@ Running this command will ${MigrateController.COMMON_MIGRATE_MESSAGE}`;
458484
return await this.$fs.getFileShasum(projectPackageJsonFilePath);
459485
}
460486

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+
// }
481507

482508
private async cleanUpProject(projectData: IProjectData): Promise<void> {
483509
this.$logger.info("Clean old project artifacts.");
@@ -776,6 +802,79 @@ Running this command will ${MigrateController.COMMON_MIGRATE_MESSAGE}`;
776802
: !allowInvalidVersions;
777803
}
778804

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+
779878
private async migrateUnitTestRunner(
780879
projectData: IProjectData,
781880
migrationBackupDirPath: string

lib/controllers/prepare-controller.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,13 @@ export class PrepareController extends EventEmitter {
396396
projectData: IProjectData,
397397
platformData: IPlatformData
398398
) {
399+
const projectInfo = this.$projectConfigService.detectInfo(
400+
projectData.projectDir
401+
);
402+
if (projectInfo.usesLegacyConfig) {
403+
return;
404+
}
405+
399406
this.$logger.info(
400407
"Updating runtime package.json with configuration values..."
401408
);

lib/controllers/update-controller-base.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ export class UpdateControllerBase {
5151
this.$fs.deleteDirectory(backupDir);
5252
this.$fs.createDirectory(backupDir);
5353
for (const folder of folders) {
54-
const folderToCopy = path.join(projectDir, folder);
54+
const fileName = folder.replace(path.sep, "_");
55+
const folderToCopy = path.join(projectDir, fileName);
5556
if (this.$fs.exists(folderToCopy)) {
5657
this.$fs.copyFile(folderToCopy, backupDir);
5758
}

lib/definitions/project.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,8 +315,10 @@ interface IProjectConfigService {
315315
): {
316316
hasTS: boolean;
317317
hasJS: boolean;
318+
usesLegacyConfig: boolean;
318319
configJSFilePath: string;
319320
configTSFilePath: string;
321+
configNSConfigFilePath: string;
320322
};
321323

322324
getDefaultTSConfig(appId: string): string;

0 commit comments

Comments
 (0)