Skip to content
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
5 changes: 5 additions & 0 deletions .changeset/fuzzy-dodos-prune.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"leadtype": patch
---

Fix `convertAllMdx()` directory pruning on Windows by normalizing globbed paths before comparing them with the output directory.
13 changes: 13 additions & 0 deletions packages/leadtype/src/convert/convert.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
convertAllMdx,
convertMdxFile,
resolveMdxFrontmatter,
resolvePruneParentDirectories,
} from "./convert";

const execFileAsync = promisify(execFile);
Expand Down Expand Up @@ -82,6 +83,18 @@ afterEach(async () => {
});

describe("convertAllMdx", () => {
it("normalizes glob paths before pruning directories on Windows", () => {
const parents = resolvePruneParentDirectories(
[
"C:/repo/public/guides/orphan.md",
"C:\\repo\\public\\guides\\another-orphan.md",
],
path.win32
);

expect([...parents]).toEqual(["C:\\repo\\public\\guides"]);
});

it("defaults to the framework-neutral docs directory", async () => {
const projectDir = await createTempProject();
const previousCwd = process.cwd();
Expand Down
17 changes: 16 additions & 1 deletion packages/leadtype/src/convert/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1272,7 +1272,7 @@ async function pruneOrphanedOutputs(
// Deleting the last page of a section leaves an empty directory behind;
// sweep upward until a non-empty parent (or outDir) stops the walk.
const resolvedOutDir = resolve(outDir);
const parents = new Set(orphans.map((filePath) => dirname(filePath)));
const parents = resolvePruneParentDirectories(orphans);
for (let dir of parents) {
while (dir !== resolvedOutDir && dir.startsWith(resolvedOutDir + sep)) {
try {
Expand All @@ -1287,6 +1287,21 @@ async function pruneOrphanedOutputs(
return orphans;
}

interface PrunePathApi {
dirname: (filePath: string) => string;
resolve: (...paths: string[]) => string;
}

/** Normalize glob output before comparing it with native filesystem paths. */
export function resolvePruneParentDirectories(
filePaths: readonly string[],
pathApi: PrunePathApi = { dirname, resolve }
): Set<string> {
return new Set(
filePaths.map((filePath) => pathApi.resolve(pathApi.dirname(filePath)))
);
}

/**
* Convert every .mdx file under srcDir to .md under outDir (preserving the
* relative directory structure).
Expand Down
Loading