Skip to content

Commit

Permalink
fix(scripts): fix create-package path check
Browse files Browse the repository at this point in the history
  • Loading branch information
ccharly committed Jan 9, 2025
1 parent dd040e5 commit 574c81a
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion scripts/create-package/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,24 @@ export async function readMonorepoFiles(): Promise<MonorepoFileData> {
};
}

/**
* 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`).
Expand All @@ -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}`);
}

Expand Down

0 comments on commit 574c81a

Please sign in to comment.