From 574c81a22978ad13abe3701a6a6f0a439e66599d Mon Sep 17 00:00:00 2001 From: Charly Chevalier Date: Thu, 9 Jan 2025 15:22:50 +0100 Subject: [PATCH] fix(scripts): fix create-package path check --- scripts/create-package/utils.ts | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/scripts/create-package/utils.ts b/scripts/create-package/utils.ts index 85c24d10a8d..d74df02a33a 100644 --- a/scripts/create-package/utils.ts +++ b/scripts/create-package/utils.ts @@ -82,6 +82,24 @@ export async function readMonorepoFiles(): Promise { }; } +/** + * Checks if a package path already exists. + * + * @param packagePath - The package path. + * @returns True if the path exists, false otherwise. + */ +async function packagePathExists(packagePath: string) { + try { + // We use `access`, cause no matter if `packagePath` is a directory or a file, we won't + // be able to write anything there if it already exists. + await fs.access(packagePath); + + return true; + } catch { + return false; + } +} + /** * Finalizes package and repo files, writes them to disk, and performs necessary * postprocessing (e.g. running `yarn install`). @@ -94,7 +112,7 @@ export async function finalizeAndWriteData( monorepoFileData: MonorepoFileData, ) { const packagePath = path.join(PACKAGES_PATH, packageData.directoryName); - if ((await fs.stat(packagePath)).isDirectory()) { + if (await packagePathExists(packagePath)) { throw new Error(`The package directory already exists: ${packagePath}`); }