Skip to content

Commit 8728335

Browse files
authored
fix(migrate): create polyfills.ts if missing for angular projects (#5593)
1 parent 3c03579 commit 8728335

File tree

1 file changed

+88
-2
lines changed

1 file changed

+88
-2
lines changed

lib/controllers/migrate-controller.ts

+88-2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import { IInjector } from "../common/definitions/yok";
4040
import { injector } from "../common/yok";
4141
import { IJsonFileSettingsService } from "../common/definitions/json-file-settings-service";
4242
import { SupportedConfigValues } from "../tools/config-manipulation/config-transformer";
43+
import * as temp from "temp";
4344

4445
// const wait: (ms: number) => Promise<void> = (ms: number = 1000) =>
4546
// new Promise((resolve) => setTimeout(resolve, ms));
@@ -432,12 +433,29 @@ export class MigrateController
432433

433434
this.spinner.succeed("Project dependencies have been updated");
434435

436+
const isAngular = this.hasDependency(
437+
{
438+
packageName: "@nativescript/angular",
439+
},
440+
projectData
441+
);
442+
443+
// ensure polyfills.ts exists in angular projects
444+
let polyfillsPath;
445+
if (isAngular) {
446+
polyfillsPath = await this.checkOrCreatePolyfillsTS(projectData);
447+
}
448+
435449
// update tsconfig
436450
const tsConfigPath = path.resolve(projectDir, "tsconfig.json");
437451
if (this.$fs.exists(tsConfigPath)) {
438452
this.spinner.info(`Updating ${"tsconfig.json".yellow}`);
439453

440-
await this.migrateTSConfig(tsConfigPath);
454+
await this.migrateTSConfig({
455+
tsConfigPath,
456+
isAngular,
457+
polyfillsPath,
458+
});
441459

442460
this.spinner.succeed(`Updated ${"tsconfig.json".yellow}`);
443461
}
@@ -1189,7 +1207,15 @@ export class MigrateController
11891207
return dependencies;
11901208
}
11911209

1192-
private async migrateTSConfig(tsConfigPath: string): Promise<boolean> {
1210+
private async migrateTSConfig({
1211+
tsConfigPath,
1212+
isAngular,
1213+
polyfillsPath,
1214+
}: {
1215+
tsConfigPath: string;
1216+
isAngular: boolean;
1217+
polyfillsPath?: string;
1218+
}): Promise<boolean> {
11931219
try {
11941220
const configContents = this.$fs.readJson(tsConfigPath);
11951221

@@ -1205,6 +1231,18 @@ export class MigrateController
12051231
...new Set([...(configContents.compilerOptions.lib || []), "es2017"]),
12061232
];
12071233

1234+
if (isAngular) {
1235+
// make sure polyfills.ts is in files
1236+
if (configContents.files) {
1237+
configContents.files = [
1238+
...new Set([
1239+
...(configContents.files || []),
1240+
polyfillsPath ?? "./src/polyfills.ts",
1241+
]),
1242+
];
1243+
}
1244+
}
1245+
12081246
this.$fs.writeJson(tsConfigPath, configContents);
12091247

12101248
return true;
@@ -1214,6 +1252,48 @@ export class MigrateController
12141252
}
12151253
}
12161254

1255+
private async checkOrCreatePolyfillsTS(
1256+
projectData: IProjectData
1257+
): Promise<string> {
1258+
const { projectDir, appDirectoryPath } = projectData;
1259+
1260+
const possiblePaths = [
1261+
`${appDirectoryPath}/polyfills.ts`,
1262+
`./src/polyfills.ts`,
1263+
`./app/polyfills.ts`,
1264+
].map((possiblePath) => path.resolve(projectDir, possiblePath));
1265+
1266+
let polyfillsPath = possiblePaths.find((possiblePath) => {
1267+
return this.$fs.exists(possiblePath);
1268+
});
1269+
1270+
if (polyfillsPath) {
1271+
return "./" + path.relative(projectDir, polyfillsPath);
1272+
}
1273+
1274+
const tempDir = temp.mkdirSync({
1275+
prefix: "migrate-angular-polyfills",
1276+
});
1277+
1278+
// get from default angular template
1279+
await this.$pacoteService.extractPackage(
1280+
constants.RESERVED_TEMPLATE_NAMES["angular"],
1281+
tempDir
1282+
);
1283+
1284+
this.$fs.copyFile(
1285+
path.resolve(tempDir, "src/polyfills.ts"),
1286+
possiblePaths[0]
1287+
);
1288+
1289+
// clean up temp project
1290+
this.$fs.deleteDirectory(tempDir);
1291+
1292+
this.spinner.succeed(`Created fresh ${"polyfills.ts".cyan}`);
1293+
1294+
return "./" + path.relative(projectDir, possiblePaths[0]);
1295+
}
1296+
12171297
private async migrateNativeScriptAngular(): Promise<IMigrationDependency[]> {
12181298
const minVersion = "10.0.0";
12191299
const desiredVersion = "^12.2.5";
@@ -1281,6 +1361,12 @@ export class MigrateController
12811361
},
12821362

12831363
// devDependencies
1364+
{
1365+
packageName: "@angular/cli",
1366+
minVersion,
1367+
desiredVersion,
1368+
isDev: true,
1369+
},
12841370
{
12851371
packageName: "@angular/compiler-cli",
12861372
minVersion,

0 commit comments

Comments
 (0)