-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathmigrate-controller.ts
426 lines (368 loc) · 21.2 KB
/
migrate-controller.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
import * as path from "path";
import * as semver from "semver";
import * as constants from "../constants";
import * as glob from "glob";
import { UpdateControllerBase } from "./update-controller-base";
import { fromWindowsRelativePathToUnix } from "../common/helpers";
export class MigrateController extends UpdateControllerBase implements IMigrateController {
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";
private static UNABLE_TO_MIGRATE_APP_ERROR = `The current application is not compatible with NativeScript CLI 6.0.
Use the \`tns migrate\` command to migrate the app dependencies to a form compatible with NativeScript 6.0.
Running this command will ${MigrateController.COMMON_MIGRATE_MESSAGE}`;
private static MIGRATE_FINISH_MESSAGE = `The \`tns migrate\` command does ${MigrateController.COMMON_MIGRATE_MESSAGE}`;
constructor(
protected $fs: IFileSystem,
protected $platformCommandHelper: IPlatformCommandHelper,
protected $platformsDataService: IPlatformsDataService,
protected $packageInstallationManager: IPackageInstallationManager,
protected $packageManager: IPackageManager,
protected $pacoteService: IPacoteService,
private $androidResourcesMigrationService: IAndroidResourcesMigrationService,
private $devicePlatformsConstants: Mobile.IDevicePlatformsConstants,
private $logger: ILogger,
private $errors: IErrors,
private $addPlatformService: IAddPlatformService,
private $pluginsService: IPluginsService,
private $projectDataService: IProjectDataService,
private $platformValidationService: IPlatformValidationService,
private $resources: IResourceLoader) {
super($fs, $platformCommandHelper, $platformsDataService, $packageInstallationManager, $packageManager, $pacoteService);
}
static readonly typescriptPackageName: string = "typescript";
static readonly backupFolder: string = ".migration_backup";
static readonly migrateFailMessage: string = "Could not migrate the project!";
static readonly backupFailMessage: string = "Could not backup project folders!";
static readonly folders: string[] = [
constants.LIB_DIR_NAME,
constants.HOOKS_DIR_NAME,
constants.WEBPACK_CONFIG_NAME,
constants.PACKAGE_JSON_FILE_NAME,
constants.PACKAGE_LOCK_JSON_FILE_NAME,
constants.TSCCONFIG_TNS_JSON_NAME,
constants.KARMA_CONFIG_NAME
];
private migrationDependencies: IMigrationDependency[] = [
{ packageName: constants.TNS_CORE_MODULES_NAME, verifiedVersion: "6.0.1" },
{ packageName: constants.TNS_CORE_MODULES_WIDGETS_NAME, verifiedVersion: "6.0.1" },
{ packageName: "tns-platform-declarations", isDev: true, verifiedVersion: "6.0.1" },
{ packageName: "node-sass", isDev: true, verifiedVersion: "4.12.0" },
{ packageName: "nativescript-dev-sass", isDev: true, replaceWith: "node-sass" },
{ packageName: "nativescript-dev-typescript", isDev: true, replaceWith: MigrateController.typescriptPackageName },
{ packageName: "nativescript-dev-less", isDev: true, shouldRemove: true, warning: "LESS CSS is not supported out of the box. In order to enable it, follow the steps in this feature request: https://github.com/NativeScript/nativescript-dev-webpack/issues/967" },
{ packageName: constants.WEBPACK_PLUGIN_NAME, isDev: true, shouldAddIfMissing: true, verifiedVersion: "1.0.1" },
{ packageName: "nativescript-camera", verifiedVersion: "4.5.0" },
{ packageName: "nativescript-geolocation", verifiedVersion: "5.1.0" },
{ packageName: "nativescript-imagepicker", verifiedVersion: "6.2.0" },
{ packageName: "nativescript-social-share", verifiedVersion: "1.5.2" },
{ packageName: "nativescript-ui-chart", verifiedVersion: "5.0.0" },
{ packageName: "nativescript-ui-dataform", verifiedVersion: "5.0.0" },
{ packageName: "nativescript-ui-gauge", verifiedVersion: "5.0.0" },
{ packageName: "nativescript-ui-listview", verifiedVersion: "7.0.0" },
{ packageName: "nativescript-ui-sidedrawer", verifiedVersion: "7.0.0" },
{ packageName: "nativescript-ui-calendar", verifiedVersion: "5.0.0" },
{ packageName: "nativescript-ui-autocomplete", verifiedVersion: "5.0.0" },
{ packageName: "nativescript-datetimepicker", verifiedVersion: "1.1.0" },
{ packageName: "kinvey-nativescript-sdk", verifiedVersion: "4.2.1" },
{ packageName: "nativescript-plugin-firebase", verifiedVersion: "9.0.2" },
{
packageName: "nativescript-vue", verifiedVersion: "2.3.0",
shouldMigrateAction: async (projectData: IProjectData, allowInvalidVersions: boolean) => {
const dependency = { packageName: "nativescript-vue", verifiedVersion: "2.3.0", isDev: false };
const result = this.hasDependency(dependency, projectData) && await this.shouldMigrateDependencyVersion(dependency, projectData, allowInvalidVersions);
return result;
},
migrateAction: this.migrateNativeScriptVue.bind(this)
},
{
packageName: "nativescript-angular", verifiedVersion: "8.0.2",
shouldMigrateAction: async (projectData: IProjectData, allowInvalidVersions: boolean) => {
const dependency = { packageName: "nativescript-angular", verifiedVersion: "8.0.2", isDev: false };
const result = this.hasDependency(dependency, projectData) && await this.shouldMigrateDependencyVersion(dependency, projectData, allowInvalidVersions);
return result;
},
migrateAction: this.migrateNativeScriptAngular.bind(this)
},
{ packageName: "nativescript-permissions", verifiedVersion: "1.3.0" },
{ packageName: "nativescript-cardview", verifiedVersion: "3.2.0" },
{
packageName: "nativescript-unit-test-runner", verifiedVersion: "0.7.0",
shouldMigrateAction: async (projectData: IProjectData, allowInvalidVersions: boolean) => {
const dependency = { packageName: "nativescript-unit-test-runner", verifiedVersion: "0.7.0", isDev: false };
const result = this.hasDependency(dependency, projectData) && await this.shouldMigrateDependencyVersion(dependency, projectData, allowInvalidVersions);
return result;
},
migrateAction: this.migrateUnitTestRunner.bind(this)
},
{ packageName: MigrateController.typescriptPackageName, isDev: true, verifiedVersion: "3.4.5" },
{ packageName: "nativescript-localize", verifiedVersion: "4.2.0" },
{ packageName: "nativescript-dev-babel", verifiedVersion: "0.2.1" },
{ packageName: "nativescript-nfc", verifiedVersion: "4.0.1" }
];
get verifiedPlatformVersions(): IDictionary<string> {
return {
[this.$devicePlatformsConstants.Android.toLowerCase()]: "6.0.0",
[this.$devicePlatformsConstants.iOS.toLowerCase()]: "6.0.1"
};
}
public async migrate({ projectDir, platforms, allowInvalidVersions = false }: IMigrationData): Promise<void> {
const projectData = this.$projectDataService.getProjectData(projectDir);
const backupDir = path.join(projectDir, MigrateController.backupFolder);
try {
this.$logger.info("Backup project configuration.");
this.backup(MigrateController.folders, backupDir, projectData.projectDir);
this.$logger.info("Backup project configuration complete.");
} catch (error) {
this.$logger.error(MigrateController.backupFailMessage);
this.$fs.deleteDirectory(backupDir);
return;
}
try {
this.$logger.info("Clean auto-generated files.");
this.handleAutoGeneratedFiles(backupDir, projectData);
this.$logger.info("Clean auto-generated files complete.");
} catch (error) {
this.$logger.trace(`Error during auto-generated files handling. ${(error && error.message) || error}`);
}
await this.migrateOldAndroidAppResources(projectData, backupDir);
try {
await this.cleanUpProject(projectData);
await this.migrateDependencies(projectData, platforms, allowInvalidVersions);
} catch (error) {
this.restoreBackup(MigrateController.folders, backupDir, projectData.projectDir);
this.$errors.failWithoutHelp(`${MigrateController.migrateFailMessage} The error is: ${error}`);
}
this.$logger.info(MigrateController.MIGRATE_FINISH_MESSAGE);
}
public async shouldMigrate({ projectDir, platforms, allowInvalidVersions = false }: IMigrationData): Promise<boolean> {
const projectData = this.$projectDataService.getProjectData(projectDir);
const shouldMigrateCommonMessage = "The app is not compatible with this CLI version and it should be migrated. Reason: ";
for (let i = 0; i < this.migrationDependencies.length; i++) {
const dependency = this.migrationDependencies[i];
const hasDependency = this.hasDependency(dependency, projectData);
if (hasDependency && dependency.shouldMigrateAction && await dependency.shouldMigrateAction(projectData, allowInvalidVersions)) {
this.$logger.trace(`${shouldMigrateCommonMessage}'${dependency.packageName}' requires an update.`);
return true;
}
if (hasDependency && (dependency.replaceWith || dependency.shouldRemove)) {
this.$logger.trace(`${shouldMigrateCommonMessage}'${dependency.packageName}' is deprecated.`);
return true;
}
if (hasDependency && await this.shouldMigrateDependencyVersion(dependency, projectData, allowInvalidVersions)) {
this.$logger.trace(`${shouldMigrateCommonMessage}'${dependency.packageName}' should be updated.`);
return true;
}
if (!hasDependency && dependency.shouldAddIfMissing) {
this.$logger.trace(`${shouldMigrateCommonMessage}'${dependency.packageName}' is missing.`);
return true;
}
}
for (let platform of platforms) {
platform = platform && platform.toLowerCase();
if (!this.$platformValidationService.isValidPlatform(platform, projectData)) {
continue;
}
const hasRuntimeDependency = this.hasRuntimeDependency({ platform, projectData });
if (hasRuntimeDependency && await this.shouldUpdateRuntimeVersion(this.verifiedPlatformVersions[platform.toLowerCase()], platform, projectData, allowInvalidVersions)) {
this.$logger.trace(`${shouldMigrateCommonMessage}Platform '${platform}' should be updated.`);
return true;
}
}
}
public async validate({ projectDir, platforms, allowInvalidVersions = true }: IMigrationData): Promise<void> {
const shouldMigrate = await this.shouldMigrate({ projectDir, platforms, allowInvalidVersions });
if (shouldMigrate) {
this.$errors.failWithoutHelp(MigrateController.UNABLE_TO_MIGRATE_APP_ERROR);
}
}
private async migrateOldAndroidAppResources(projectData: IProjectData, backupDir: string) {
const appResourcesPath = projectData.getAppResourcesDirectoryPath();
if (!this.$androidResourcesMigrationService.hasMigrated(appResourcesPath)) {
this.$logger.info("Migrate old Android App_Resources structure.");
try {
await this.$androidResourcesMigrationService.migrate(appResourcesPath, backupDir);
} catch (error) {
this.$logger.warn("Migrate old Android App_Resources structure failed: ", error.message);
}
}
}
private async cleanUpProject(projectData: IProjectData): Promise<void> {
this.$logger.info("Clean old project artefacts.");
this.$projectDataService.removeNSConfigProperty(projectData.projectDir, "useLegacyWorkflow");
this.$fs.deleteDirectory(path.join(projectData.projectDir, constants.HOOKS_DIR_NAME));
this.$fs.deleteDirectory(path.join(projectData.projectDir, constants.PLATFORMS_DIR_NAME));
this.$fs.deleteDirectory(path.join(projectData.projectDir, constants.NODE_MODULES_FOLDER_NAME));
this.$fs.deleteFile(path.join(projectData.projectDir, constants.WEBPACK_CONFIG_NAME));
this.$fs.deleteFile(path.join(projectData.projectDir, constants.PACKAGE_LOCK_JSON_FILE_NAME));
if (!projectData.isShared) {
this.$fs.deleteFile(path.join(projectData.projectDir, constants.TSCCONFIG_TNS_JSON_NAME));
}
this.$logger.info("Clean old project artefacts complete.");
}
private handleAutoGeneratedFiles(backupDir: string, projectData: IProjectData): void {
const globOptions: glob.IOptions = {
silent: true,
nocase: true,
matchBase: true,
nodir: true,
absolute: false,
cwd: projectData.appDirectoryPath
};
const jsFiles = glob.sync("*.@(js|ts|js.map)", globOptions);
const autoGeneratedJsFiles = this.getGeneratedFiles(jsFiles, [".js"], [".ts"]);
const autoGeneratedJsMapFiles = this.getGeneratedFiles(jsFiles, [".map"], [""]);
const cssFiles = glob.sync("*.@(le|sa|sc|c)ss", globOptions);
const autoGeneratedCssFiles = this.getGeneratedFiles(cssFiles, [".css"], [".scss", ".sass", ".less"]);
const allGeneratedFiles = autoGeneratedJsFiles.concat(autoGeneratedJsMapFiles).concat(autoGeneratedCssFiles);
for (const generatedFile of allGeneratedFiles) {
const sourceFile = path.join(projectData.appDirectoryPath, generatedFile);
const destinationFile = path.join(backupDir, generatedFile);
const destinationFileDir = path.dirname(destinationFile);
this.$fs.ensureDirectoryExists(destinationFileDir);
this.$fs.rename(sourceFile, destinationFile);
}
}
private getGeneratedFiles(allFiles: string[], generatedFileExts: string[], sourceFileExts: string[]): string[] {
const autoGeneratedFiles = allFiles.filter(file => {
let isGenerated = false;
const { dir, name, ext } = path.parse(file);
if (generatedFileExts.indexOf(ext) > -1) {
for (const sourceExt of sourceFileExts) {
const possibleSourceFile = path.format({ dir, name, ext: sourceExt });
isGenerated = allFiles.indexOf(possibleSourceFile) > -1;
if (isGenerated) {
break;
}
}
}
return isGenerated;
});
return autoGeneratedFiles;
}
private async migrateDependencies(projectData: IProjectData, platforms: string[], allowInvalidVersions: boolean): Promise<void> {
this.$logger.info("Start dependencies migration.");
for (let i = 0; i < this.migrationDependencies.length; i++) {
const dependency = this.migrationDependencies[i];
const hasDependency = this.hasDependency(dependency, projectData);
if (hasDependency && dependency.migrateAction && await dependency.shouldMigrateAction(projectData, allowInvalidVersions)) {
const newDependencies = await dependency.migrateAction(projectData, path.join(projectData.projectDir, MigrateController.backupFolder));
for (const newDependency of newDependencies) {
await this.migrateDependency(newDependency, projectData, allowInvalidVersions);
}
}
await this.migrateDependency(dependency, projectData, allowInvalidVersions);
}
for (const platform of platforms) {
const lowercasePlatform = platform.toLowerCase();
const hasRuntimeDependency = this.hasRuntimeDependency({ platform, projectData });
if (hasRuntimeDependency && await this.shouldUpdateRuntimeVersion(this.verifiedPlatformVersions[lowercasePlatform], platform, projectData, allowInvalidVersions)) {
const verifiedPlatformVersion = this.verifiedPlatformVersions[lowercasePlatform];
const platformData = this.$platformsDataService.getPlatformData(lowercasePlatform, projectData);
this.$logger.info(`Updating ${platform} platform to version '${verifiedPlatformVersion}'.`);
await this.$addPlatformService.setPlatformVersion(platformData, projectData, verifiedPlatformVersion);
}
}
this.$logger.info("Install packages.");
await this.$packageManager.install(projectData.projectDir, projectData.projectDir, {
disableNpmInstall: false,
frameworkPath: null,
ignoreScripts: false,
path: projectData.projectDir
});
this.$logger.info("Migration complete.");
}
private async migrateDependency(dependency: IMigrationDependency, projectData: IProjectData, allowInvalidVersions: boolean): Promise<void> {
const hasDependency = this.hasDependency(dependency, projectData);
if (hasDependency && dependency.warning) {
this.$logger.warn(dependency.warning);
}
if (hasDependency && (dependency.replaceWith || dependency.shouldRemove)) {
this.$pluginsService.removeFromPackageJson(dependency.packageName, projectData.projectDir);
if (dependency.replaceWith) {
const replacementDep = _.find(this.migrationDependencies, migrationPackage => migrationPackage.packageName === dependency.replaceWith);
if (!replacementDep) {
this.$errors.failWithoutHelp("Failed to find replacement dependency.");
}
this.$logger.info(`Replacing '${dependency.packageName}' with '${replacementDep.packageName}'.`);
this.$pluginsService.addToPackageJson(replacementDep.packageName, replacementDep.verifiedVersion, replacementDep.isDev, projectData.projectDir);
}
return;
}
if (hasDependency && await this.shouldMigrateDependencyVersion(dependency, projectData, allowInvalidVersions)) {
this.$logger.info(`Updating '${dependency.packageName}' to compatible version '${dependency.verifiedVersion}'`);
this.$pluginsService.addToPackageJson(dependency.packageName, dependency.verifiedVersion, dependency.isDev, projectData.projectDir);
return;
}
if (!hasDependency && dependency.shouldAddIfMissing) {
this.$logger.info(`Adding '${dependency.packageName}' with version '${dependency.verifiedVersion}'`);
this.$pluginsService.addToPackageJson(dependency.packageName, dependency.verifiedVersion, dependency.isDev, projectData.projectDir);
}
}
private async shouldMigrateDependencyVersion(dependency: IMigrationDependency, projectData: IProjectData, allowInvalidVersions: boolean): Promise<boolean> {
const devDependencies = projectData.devDependencies || {};
const dependencies = projectData.dependencies || {};
const packageName = dependency.packageName;
const referencedVersion = dependencies[packageName] || devDependencies[packageName];
const installedVersion = await this.getMaxDependencyVersion(dependency.packageName, referencedVersion);
const requiredVersion = dependency.verifiedVersion;
return this.isOutdatedVersion(installedVersion, requiredVersion, allowInvalidVersions);
}
private async shouldUpdateRuntimeVersion(targetVersion: string, platform: string, projectData: IProjectData, allowInvalidVersions: boolean): Promise<boolean> {
const installedVersion = await this.getMaxRuntimeVersion({ platform, projectData });
return this.isOutdatedVersion(installedVersion, targetVersion, allowInvalidVersions);
}
private isOutdatedVersion(version: string, targetVersion: string, allowInvalidVersions: boolean): boolean {
return !!version ? semver.lt(version, targetVersion) : !allowInvalidVersions;
}
private async migrateUnitTestRunner(projectData: IProjectData, migrationBackupDirPath: string): Promise<IMigrationDependency[]> {
// Migrate karma.conf.js
const pathToKarmaConfig = path.join(migrationBackupDirPath, constants.KARMA_CONFIG_NAME);
if (this.$fs.exists(pathToKarmaConfig)) {
const oldKarmaContent = this.$fs.readText(pathToKarmaConfig);
const regExp = /frameworks:\s+\[([\S\s]*?)\]/g;
const matches = regExp.exec(oldKarmaContent);
const frameworks = (matches && matches[1] && matches[1].trim()) || '["jasmine"]';
const testsDir = path.join(projectData.appDirectoryPath, 'tests');
const relativeTestsDir = path.relative(projectData.projectDir, testsDir);
const testFiles = `'${fromWindowsRelativePathToUnix(relativeTestsDir)}/**/*.*'`;
const karmaConfTemplate = this.$resources.readText('test/karma.conf.js');
const karmaConf = _.template(karmaConfTemplate)({ frameworks, testFiles });
this.$fs.writeFile(path.join(projectData.projectDir, constants.KARMA_CONFIG_NAME), karmaConf);
}
// Dependencies to migrate
const dependencies = [
{ packageName: "karma-webpack", verifiedVersion: "3.0.5", isDev: true, shouldAddIfMissing: true },
{ packageName: "karma-jasmine", verifiedVersion: "2.0.1", isDev: true },
{ packageName: "karma-mocha", verifiedVersion: "1.3.0", isDev: true },
{ packageName: "karma-chai", verifiedVersion: "0.1.0", isDev: true },
{ packageName: "karma-qunit", verifiedVersion: "3.1.2", isDev: true },
{ packageName: "karma", verifiedVersion: "4.1.0", isDev: true },
];
return dependencies;
}
private async migrateNativeScriptAngular(): Promise<IMigrationDependency[]> {
const dependencies = [
{ packageName: "@angular/platform-browser-dynamic", verifiedVersion: "8.0.0", shouldAddIfMissing: true },
{ packageName: "@angular/common", verifiedVersion: "8.0.0", shouldAddIfMissing: true },
{ packageName: "@angular/compiler", verifiedVersion: "8.0.0", shouldAddIfMissing: true },
{ packageName: "@angular/core", verifiedVersion: "8.0.0", shouldAddIfMissing: true },
{ packageName: "@angular/forms", verifiedVersion: "8.0.0", shouldAddIfMissing: true },
{ packageName: "@angular/http", verifiedVersion: "8.0.0-beta.10", shouldAddIfMissing: true },
{ packageName: "@angular/platform-browser", verifiedVersion: "8.0.0", shouldAddIfMissing: true },
{ packageName: "@angular/router", verifiedVersion: "8.0.0", shouldAddIfMissing: true },
{ packageName: "rxjs", verifiedVersion: "6.3.3", shouldAddIfMissing: true },
{ packageName: "zone.js", verifiedVersion: "0.9.1", shouldAddIfMissing: true },
{ packageName: "@angular/animations", verifiedVersion: "8.0.0" },
{ packageName: "@angular/compiler-cli", verifiedVersion: "8.0.0", isDev: true },
{ packageName: "@ngtools/webpack", verifiedVersion: "8.0.0", isDev: true },
{ packageName: "@angular-devkit/build-angular", verifiedVersion: "0.800.3", isDev: true }
];
return dependencies;
}
private async migrateNativeScriptVue(): Promise<IMigrationDependency[]> {
const dependencies = [
{ packageName: "nativescript-vue-template-compiler", verifiedVersion: "2.3.0", isDev: true }
];
return dependencies;
}
}
$injector.register("migrateController", MigrateController);