Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix API docs script to tolerate empty docs folder #1316

Merged
merged 4 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
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
11 changes: 5 additions & 6 deletions scripts/commands/updateApiDocs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,9 @@ async function prepareSphinxFolder(pkg: Pkg, args: Arguments): Promise<string> {

async function deleteExistingMarkdown(pkg: Pkg): Promise<void> {
const markdownDir = pkg.outputDir("docs");
if (pkg.isLatest() || (await pathExists(markdownDir))) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's necessary to check if pkg.isLatest() - it's already covered by the pathExists check. If the path does not exist, then there is nothing to delete, regardless of latest or not.

console.log(
`Deleting existing markdown for ${pkg.name}:${pkg.versionWithoutPatch}`,
);
await rmFilesInFolder(markdownDir);
}
if (!(await pathExists(markdownDir))) return;
console.log(
`Deleting existing markdown for ${pkg.name}:${pkg.versionWithoutPatch}`,
);
await rmFilesInFolder(markdownDir);
}
12 changes: 7 additions & 5 deletions scripts/lib/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@
// copyright notice, and modified files need to carry a notice indicating
// that they have been altered from the originals.

import { stat } from "fs/promises";
import { normalize } from "path";
import fs from "fs/promises";
import path from "path";

import { $ } from "zx/core";

export function getRoot() {
return normalize(`${__dirname}/../../`);
return path.normalize(`${__dirname}/../../`);
}

export async function pathExists(path: string) {
try {
await stat(path);
await fs.stat(path);
return true;
} catch (err: any) {
if (err && err.code === "ENOENT") return false;
Expand All @@ -31,7 +31,9 @@ export async function pathExists(path: string) {

/**
* Deletes all the files in the folder, but preserves subfolders.
*
* Assumes the folder exists, but it's fine if it's empty.
*/
export async function rmFilesInFolder(dir: string): Promise<void> {
await $`find ${dir}/* -maxdepth 0 -type f | xargs rm -f {}`.quiet();
await $`find ${dir} -maxdepth 1 -type f -path "${dir}/*" | xargs rm -f {}`.quiet();
}
Loading