diff --git a/schematics/ng-update/upgrade-rules/v17/preloader.ts b/schematics/ng-update/upgrade-rules/v17/preloader.ts index fa7dee6e93..18b1db033f 100644 --- a/schematics/ng-update/upgrade-rules/v17/preloader.ts +++ b/schematics/ng-update/upgrade-rules/v17/preloader.ts @@ -9,7 +9,9 @@ export function updatePreloader(): Rule { const angularJson = readJSON(tree, DEFAULT_WORKSPACE_PATH); const projectNames = Object.keys(angularJson.projects); for (const name of projectNames) { - run(tree, name, angularJson.projects[name].sourceRoot, context); + const sourceRoot = angularJson.projects[name].sourceRoot; + fixIndexHtml(tree, name, sourceRoot, context); + run(tree, name, sourceRoot, context); } }; } @@ -18,7 +20,21 @@ function addESLintIgnore(tree: Tree): void { const filePath = '/.eslintignore'; if (!tree.exists(filePath)) return; const content = tree.readText(filePath); - tree.overwrite(filePath, `${content}\n**/src/index.html`); + if (!content.includes('**/src/index.html')) { + tree.overwrite(filePath, `${content}\n**/src/index.html`); + } +} + +function fixIndexHtml(tree: Tree, _: string, sourceRoot: string, __: SchematicContext): void { + const indexPath = `${sourceRoot}/index.html`; + if (!tree.exists(indexPath)) return; + + let indexContent = tree.readText(indexPath); + + const selfClose = ''; + if (!indexContent.includes(selfClose)) return; + + tree.overwrite(indexPath, indexContent.replace(selfClose, '')); } function run(tree: Tree, name: string, sourceRoot: string, context: SchematicContext): void { @@ -36,18 +52,20 @@ function run(tree: Tree, name: string, sourceRoot: string, context: SchematicCon const appPath = `${sourceRoot}/app/app.component.ts`; if (!tree.exists(appPath)) return; const appContent = tree.readText(appPath); + if (appContent.includes(', stepPreloader')) return; + const appContentLines = appContent.split('\n'); const importIndex = appContentLines.findIndex(line => line.includes(', VERSION as VERSION_ALAIN')); - const eventIndex = appContentLines.findIndex(line => line.includes('this.router.events.subscribe(')); - const eventEndIndex = appContentLines.findIndex(line => line.includes('if (ev instanceof NavigationEnd) {')); - if (importIndex === -1 || eventIndex === -1 || eventEndIndex === -1) return; + const addIndex = appContentLines.findIndex(line => line.includes('export class AppComponent')); + const callDoneIndex = appContentLines.findIndex(line => line.includes('if (ev instanceof NavigationEnd) {')); + if (importIndex === -1 || addIndex === -1 || callDoneIndex === -1) return; appContentLines[importIndex] = appContentLines[importIndex].replace( ', VERSION as VERSION_ALAIN', ', VERSION as VERSION_ALAIN, stepPreloader' ); - appContentLines.splice(eventIndex, 0, 'const done = stepPreloader();'); - appContentLines.splice(eventEndIndex + 2, 0, 'done();'); + appContentLines.splice(addIndex + 1, 0, 'private donePreloader = stepPreloader();'); + appContentLines.splice(callDoneIndex + 2, 0, 'this.donePreloader();'); tree.overwrite(appPath, appContentLines.join('\n'));