Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading