From fbbec7fef8efd6f7f2c26a0a158be78dedda7d05 Mon Sep 17 00:00:00 2001 From: Joshua Chen Date: Thu, 10 Feb 2022 12:54:32 +0800 Subject: [PATCH] fix(cli): make docusaurus clear also remove .yarn/.cache folder (#6646) * fix(cli): make clear also remove .yarn/.cache folder * refactor --- packages/docusaurus/src/commands/clear.ts | 34 +++++++++++++++++------ 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/packages/docusaurus/src/commands/clear.ts b/packages/docusaurus/src/commands/clear.ts index 327db8c767..f58129bf63 100644 --- a/packages/docusaurus/src/commands/clear.ts +++ b/packages/docusaurus/src/commands/clear.ts @@ -13,20 +13,36 @@ import { GENERATED_FILES_DIR_NAME, } from '@docusaurus/utils'; -async function removePath(fsPath: string) { +async function removePath(entry: {path: string; description: string}) { + if (!(await fs.pathExists(entry.path))) { + return; + } try { - fs.remove(path.join(fsPath)); - logger.success`Removed the path=${fsPath} directory.`; + await fs.remove(entry.path); + logger.success`Removed the ${entry.description} at path=${entry.path}.`; } catch (e) { - logger.error`Could not remove path=${fsPath} directory. + logger.error`Could not remove the ${entry.description} at path=${ + entry.path + }. ${e as string}`; } } export default async function clear(siteDir: string): Promise { - return Promise.all([ - removePath(path.join(siteDir, GENERATED_FILES_DIR_NAME)), - removePath(path.join(siteDir, DEFAULT_BUILD_DIR_NAME)), - removePath(path.join(siteDir, 'node_modules', '.cache')), - ]); + const generatedFolder = { + path: path.join(siteDir, GENERATED_FILES_DIR_NAME), + description: 'generated folder', + }; + const buildFolder = { + path: path.join(siteDir, DEFAULT_BUILD_DIR_NAME), + description: 'build output folder', + }; + // In Yarn PnP, cache is stored in `.yarn/.cache` because n_m doesn't exist + const cacheFolders = ['node_modules', '.yarn'].map((p) => ({ + path: path.join(siteDir, p, '.cache'), + description: 'Webpack persistent cache folder', + })); + return Promise.all( + [generatedFolder, buildFolder, ...cacheFolders].map(removePath), + ); }