Skip to content

Commit

Permalink
fix(@angular/cli): update the update command to fully support Node.js…
Browse files Browse the repository at this point in the history
… v16

Node.js v16's `fs.rmdir` will now throw an `ENOENT` error if the path does not exist. `fs.rm` is now the preferred option when using Node.js v16 but since this function is not available on Node.js v12 both are tried with `fs.rm` given preference.
In the case of the update command, the usage was guarded by a try/catch block with the downside of leaving the `fs.rmdir` being that a deprecation warning would be shown when running the command which is not ideal.
  • Loading branch information
clydin authored and filipesilva committed Oct 6, 2021
1 parent b7416d5 commit d8c9f6e
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions packages/angular/cli/commands/update-impl.ts
Expand Up @@ -656,10 +656,32 @@ export class UpdateCommand extends Command<UpdateCommandSchema> {
try {
// Remove existing node modules directory to provide a stronger guarantee that packages
// will be hoisted into the correct locations.
await fs.promises.rmdir(path.join(this.context.root, 'node_modules'), {
recursive: true,
maxRetries: 3,
});

// The below should be removed and replaced with just `rm` when support for Node.Js 12 is removed.
const { rm, rmdir } = fs.promises as typeof fs.promises & {
rm?: (
path: fs.PathLike,
options?: {
force?: boolean;
maxRetries?: number;
recursive?: boolean;
retryDelay?: number;
},
) => Promise<void>;
};

if (rm) {
await rm(path.join(this.context.root, 'node_modules'), {
force: true,
recursive: true,
maxRetries: 3,
});
} else {
await rmdir(path.join(this.context.root, 'node_modules'), {
recursive: true,
maxRetries: 3,
});
}
} catch {}

const result = await installAllPackages(
Expand Down

0 comments on commit d8c9f6e

Please sign in to comment.