From 8e52c9c56ddbc9c627508fd41c5b3693b7515197 Mon Sep 17 00:00:00 2001 From: Alan Agius Date: Mon, 28 Jun 2021 14:57:45 +0200 Subject: [PATCH] fix(@angular-devkit/build-angular): handle `ENOENT` and `ENOTDIR` errors when deleting outputs Closes #21202 (cherry picked from commit 9a751f0f81919d67f5eeeaecbe807d5c216f6a7a) --- .../src/utils/delete-output-dir.ts | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/packages/angular_devkit/build_angular/src/utils/delete-output-dir.ts b/packages/angular_devkit/build_angular/src/utils/delete-output-dir.ts index dd783462e853..2bb58977b9c0 100644 --- a/packages/angular_devkit/build_angular/src/utils/delete-output-dir.ts +++ b/packages/angular_devkit/build_angular/src/utils/delete-output-dir.ts @@ -6,7 +6,7 @@ * found in the LICENSE file at https://angular.io/license */ -import { rmdirSync } from 'fs'; +import * as fs from 'fs'; import { resolve } from 'path'; /** @@ -18,5 +18,22 @@ export function deleteOutputDir(root: string, outputPath: string): void { throw new Error('Output path MUST not be project root directory!'); } - rmdirSync(resolvedOutputPath, { recursive: true, maxRetries: 3 }); + // The below should be removed and replace with just `rmSync` when support for Node.Js 12 is removed. + const { rmSync, rmdirSync } = fs as typeof fs & { + rmSync?: ( + path: fs.PathLike, + options?: { + force?: boolean; + maxRetries?: number; + recursive?: boolean; + retryDelay?: number; + }, + ) => void; + }; + + if (rmSync) { + rmSync(resolvedOutputPath, { force: true, recursive: true, maxRetries: 3 }); + } else { + rmdirSync(resolvedOutputPath, { recursive: true, maxRetries: 3 }); + } }