From 8ea1945f35a4496cff218827a03cb8bc6e74768c Mon Sep 17 00:00:00 2001 From: thedevwonder <113778748+thedevwonder@users.noreply.github.com> Date: Fri, 30 Jun 2023 21:28:24 +0530 Subject: [PATCH] fix(core): throw error if build folder already exists on initial clean (#9112) Co-authored-by: Joshua Chen --- .../src/webpack/plugins/CleanWebpackPlugin.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/packages/docusaurus/src/webpack/plugins/CleanWebpackPlugin.ts b/packages/docusaurus/src/webpack/plugins/CleanWebpackPlugin.ts index a87d2dcfda..a619b4b0e0 100644 --- a/packages/docusaurus/src/webpack/plugins/CleanWebpackPlugin.ts +++ b/packages/docusaurus/src/webpack/plugins/CleanWebpackPlugin.ts @@ -30,6 +30,7 @@ // More context: https://github.com/facebook/docusaurus/pull/1839 import path from 'path'; +import fs from 'fs-extra'; import {sync as delSync} from 'del'; import type {Compiler, Stats} from 'webpack'; @@ -152,6 +153,17 @@ export default class CleanWebpackPlugin { return; } + if ( + // eslint-disable-next-line no-restricted-properties + fs.pathExistsSync(this.outputPath) && + // eslint-disable-next-line no-restricted-properties + fs.statSync(this.outputPath).isFile() + ) { + throw new Error( + `A file '${this.outputPath}' already exists. Docusaurus needs this directory to save the build output. Either remove/change the file or choose a different build directory via '--out-dir'.`, + ); + } + this.initialClean = true; this.removeFiles(this.cleanOnceBeforeBuildPatterns);