Skip to content

Commit 1ab4b77

Browse files
devversionjelbourn
authored andcommitted
refactor(ng-update): run package manager upon migration completion (#17455)
We cannot run the package-manager to refresh the lock file synchronously. This is because the changes made to the schematic tree are not applied until the actual task completes. Hence we need to run the task asynchronously upon migration completion. This ensures that the lock file is properly updated if something changed the `package.json` file.
1 parent 2f197cd commit 1ab4b77

File tree

3 files changed

+25
-8
lines changed

3 files changed

+25
-8
lines changed

src/cdk/schematics/ng-update/upgrade-rules/index.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88

99
import {Rule, SchematicContext, Tree} from '@angular-devkit/schematics';
10+
import {NodePackageInstallTask} from '@angular-devkit/schematics/tasks';
1011

1112
import {MigrationRuleType, runMigrationRules} from '../../update-tool';
1213
import {TargetVersion} from '../../update-tool/target-version';
@@ -79,12 +80,23 @@ export function createUpgradeRule(
7980
buildPaths.forEach(p => runMigration(p, false));
8081
testPaths.forEach(p => runMigration(p, true));
8182

83+
let runPackageManager = false;
84+
8285
// Run the global post migration static members for all migration rules.
83-
rules.forEach(r => r.globalPostMigration(tree, context));
86+
rules.forEach(rule => {
87+
const actionResult = rule.globalPostMigration(tree, context);
88+
if (actionResult) {
89+
runPackageManager = runPackageManager || actionResult.runPackageManager;
90+
}
91+
});
8492

85-
// Execute all asynchronous tasks and await them here. We want to run
86-
// the "onMigrationCompleteFn" after all work is done.
87-
await context.engine.executePostTasks().toPromise();
93+
// If a rule requested the package manager to run, we run it as an
94+
// asynchronous post migration task. We cannot run it synchronously,
95+
// as file changes from the current migration task are not applied to
96+
// the file system yet.
97+
if (runPackageManager) {
98+
context.addTask(new NodePackageInstallTask({quiet: false}));
99+
}
88100

89101
if (onMigrationCompleteFn) {
90102
onMigrationCompleteFn(targetVersion, hasRuleFailures);

src/cdk/schematics/update-tool/migration-rule.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ export interface MigrationFailure {
1919
position?: LineAndCharacter;
2020
}
2121

22+
export type PostMigrationAction = void | {
23+
/** Whether the package manager should run upon migration completion. */
24+
runPackageManager: boolean;
25+
};
26+
2227
export class MigrationRule<T> {
2328
/** List of migration failures that need to be reported. */
2429
failures: MigrationFailure[] = [];
@@ -92,5 +97,5 @@ export class MigrationRule<T> {
9297
* migration result of all individual targets. e.g. removing HammerJS if it
9398
* is not needed in any project target.
9499
*/
95-
static globalPostMigration(tree: Tree, context: SchematicContext) {}
100+
static globalPostMigration(tree: Tree, context: SchematicContext): PostMigrationAction {}
96101
}

src/material/schematics/ng-update/upgrade-rules/hammer-gestures-v9/hammer-gestures-rule.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ import {
1212
Path as DevkitPath
1313
} from '@angular-devkit/core';
1414
import {SchematicContext, SchematicsException, Tree} from '@angular-devkit/schematics';
15-
import {NodePackageInstallTask} from '@angular-devkit/schematics/tasks';
1615
import {
1716
getProjectIndexFiles,
1817
getProjectMainFile,
1918
MigrationFailure,
2019
MigrationRule,
20+
PostMigrationAction,
2121
ResolvedResource,
2222
TargetVersion
2323
} from '@angular/cdk/schematics';
@@ -762,11 +762,11 @@ export class HammerGesturesRule extends MigrationRule<null> {
762762
* on the analysis of the individual targets. For example: we only remove Hammer
763763
* from the "package.json" if it is not used in *any* project target.
764764
*/
765-
static globalPostMigration(tree: Tree, context: SchematicContext) {
765+
static globalPostMigration(tree: Tree, context: SchematicContext): PostMigrationAction {
766766
if (!this.globalUsesHammer && this._removeHammerFromPackageJson(tree)) {
767767
// Since Hammer has been removed from the workspace "package.json" file,
768768
// we schedule a node package install task to refresh the lock file.
769-
context.addTask(new NodePackageInstallTask({quiet: false}));
769+
return {runPackageManager: true};
770770
}
771771

772772
context.logger.info(chalk.yellow(

0 commit comments

Comments
 (0)