Skip to content

Commit

Permalink
chore: fix ng update
Browse files Browse the repository at this point in the history
  • Loading branch information
cipchk committed Nov 10, 2023
1 parent 21375f4 commit efe1e8e
Showing 1 changed file with 25 additions and 7 deletions.
32 changes: 25 additions & 7 deletions schematics/ng-update/upgrade-rules/v17/preloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
};
}
Expand All @@ -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 = '<app-root />';
if (!indexContent.includes(selfClose)) return;

tree.overwrite(indexPath, indexContent.replace(selfClose, '<app-root></app-root>'));
}

function run(tree: Tree, name: string, sourceRoot: string, context: SchematicContext): void {
Expand All @@ -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'));

Expand Down

0 comments on commit efe1e8e

Please sign in to comment.