Skip to content

Commit

Permalink
(fix, docs): validate api workspaces as in addition to docs workspaces (
Browse files Browse the repository at this point in the history
  • Loading branch information
dsinghvi authored Aug 1, 2024
1 parent bc1bbd5 commit 0be71ad
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 6 deletions.
13 changes: 12 additions & 1 deletion packages/cli/cli/src/commands/docs-dev/devDocsWorkspace.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { runPreviewServer } from "@fern-api/docs-preview";
import { Project } from "@fern-api/project-loader";
import { CliContext } from "../../cli-context/CliContext";
import { validateAPIWorkspaceWithoutExiting } from "../validate/validateAPIWorkspaceAndLogIssues";
import { validateDocsWorkspaceWithoutExiting } from "../validate/validateDocsWorkspaceAndLogIssues";

export async function previewDocsWorkspace({
Expand Down Expand Up @@ -37,9 +38,19 @@ export async function previewDocsWorkspace({
await validateDocsWorkspaceWithoutExiting({
workspace: docsWorkspace,
context,
logWarnings: false,
logWarnings: true,
logSummary: false
});
for (const apiWorkspace of project.apiWorkspaces) {
await cliContext.runTaskForWorkspace(apiWorkspace, async (apiWorkspaceContext) => {
await validateAPIWorkspaceWithoutExiting({
workspace: await apiWorkspace.toFernWorkspace({ context }),
context: apiWorkspaceContext,
logWarnings: false,
logSummary: false
});
});
}
},
context,
port
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,23 @@ import { FernWorkspace } from "@fern-api/workspace-loader";
import validatePackageName from "validate-npm-package-name";
import { logViolations } from "./logViolations";

export async function validateAPIWorkspaceWithoutExiting({
workspace,
context,
logWarnings,
logSummary = true
}: {
workspace: FernWorkspace;
context: TaskContext;
logWarnings: boolean;
logSummary?: boolean;
}): Promise<{ hasErrors: boolean }> {
const violations = await validateFernWorkspace(workspace, context.logger);
const { hasErrors } = logViolations({ violations, context, logWarnings, logSummary });

return { hasErrors };
}

export async function validateAPIWorkspaceAndLogIssues({
workspace,
context,
Expand All @@ -17,8 +34,7 @@ export async function validateAPIWorkspaceAndLogIssues({
context.failAndThrow("API name is not valid.");
}

const violations = await validateFernWorkspace(workspace, context.logger);
const { hasErrors } = logViolations({ violations, context, logWarnings });
const { hasErrors } = await validateAPIWorkspaceWithoutExiting({ workspace, context, logWarnings });

if (hasErrors) {
context.failAndThrow();
Expand Down
11 changes: 8 additions & 3 deletions packages/cli/docs-preview/src/runPreviewServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,12 @@ export async function runPreviewServer({
let docsDefinition: DocsV1Read.DocsDefinition | undefined;

const reloadDocsDefinition = async () => {
context.logger.info("Reloading docs");
context.logger.info("Reloading docs...");
const startTime = Date.now();
try {
project = await reloadProject();
context.logger.info("Validating docs...");
await validateProject(project);
const newDocsDefinition = await getPreviewDocsDefinition({
domain: instance.host,
project,
Expand All @@ -100,8 +102,11 @@ export async function runPreviewServer({
context.logger.info(`Reload completed in ${Date.now() - startTime}ms`);
return newDocsDefinition;
} catch (err) {
context.logger.error("Failed to reload because of validation errors: ");
await validateProject(project);
if (docsDefinition == null) {
context.logger.error("Failed to read docs configuration. Rendering blank page.");
} else {
context.logger.error("Failed to read docs configuration. Rendering last successful configuration.");
}
return docsDefinition;
}
};
Expand Down
1 change: 1 addition & 0 deletions packages/cli/workspace-loader/src/loadDocsWorkspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { readFile } from "fs/promises";
import yaml from "js-yaml";
import path from "path";
import { DocsWorkspace } from "./types/Workspace";
import { APIWorkspace } from "./workspaces";

export async function loadDocsWorkspace({
fernDirectory,
Expand Down
2 changes: 2 additions & 0 deletions packages/cli/workspace-loader/src/workspaces/FernWorkspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ export class LazyFernWorkspace extends AbstractAPIWorkspace<OSSWorkspace.Setting

if (!this.downloaded) {
this.downloaded = true;
} else {
context?.logger.enable();
}

return new FernWorkspace({
Expand Down

0 comments on commit 0be71ad

Please sign in to comment.