Skip to content

Commit

Permalink
chore(cli): Add @fern-api/source-resolver (#5295)
Browse files Browse the repository at this point in the history
  • Loading branch information
amckinney authored Dec 1, 2024
1 parent ad8197e commit edc2457
Show file tree
Hide file tree
Showing 66 changed files with 554 additions and 127 deletions.
10 changes: 10 additions & 0 deletions packages/cli/cli-source-resolver/.depcheckrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"ignores": [
"@types/jest",
"globals",
"@types/node"
],
"ignore-patterns": [
"lib"
]
}
1 change: 1 addition & 0 deletions packages/cli/cli-source-resolver/.prettierrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require("../../../.prettierrc.json");
46 changes: 46 additions & 0 deletions packages/cli/cli-source-resolver/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"name": "@fern-api/cli-source-resolver",
"version": "0.0.0",
"repository": {
"type": "git",
"url": "https://github.com/fern-api/fern.git",
"directory": "packages/cli/cli-source-resolver"
},
"private": true,
"files": [
"lib"
],
"type": "module",
"source": "src/index.ts",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"sideEffects": false,
"scripts": {
"clean": "rm -rf ./lib && tsc --build --clean",
"compile": "tsc --build",
"test": "vitest --passWithNoTests --run",
"test:update": "vitest --passWithNoTests --run -u",
"lint:eslint": "eslint --max-warnings 0 . --ignore-path=../../../.eslintignore",
"lint:eslint:fix": "yarn lint:eslint --fix",
"format": "prettier --write --ignore-unknown --ignore-path ../../../shared/.prettierignore \"**\"",
"format:check": "prettier --check --ignore-unknown --ignore-path ../../../shared/.prettierignore \"**\"",
"organize-imports": "organize-imports-cli tsconfig.json",
"depcheck": "depcheck"
},
"dependencies": {
"@fern-api/api-workspace-commons": "workspace:*",
"@fern-api/fern-definition-schema": "workspace:*",
"@fern-api/fs-utils": "workspace:*",
"@fern-api/source-resolver": "workspace:*",
"@fern-api/task-context": "workspace:*"
},
"devDependencies": {
"@types/jest": "^29.5.12",
"depcheck": "^1.4.6",
"eslint": "^8.56.0",
"vitest": "^2.1.4",
"organize-imports-cli": "^0.10.0",
"prettier": "^2.7.1",
"typescript": "4.6.4"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,8 @@ import { AbsoluteFilePath, doesPathExist, join, RelativeFilePath } from "@fern-a
import { TaskContext } from "@fern-api/task-context";
import { FernWorkspace } from "@fern-api/api-workspace-commons";
import { isRawProtobufSourceSchema, RawSchemas } from "@fern-api/fern-definition-schema";
import { FernFileContext } from "../FernFileContext";
import { ProtobufParser } from "../parsers/ProtobufParser";
import { ResolvedSource } from "./ResolvedSource";

export interface SourceResolver {
resolveSource: (args: {
source: RawSchemas.SourceSchema;
file: FernFileContext;
}) => Promise<ResolvedSource | undefined>;
resolveSourceOrThrow: (args: {
source: RawSchemas.SourceSchema;
file: FernFileContext;
}) => Promise<ResolvedSource | undefined>;
}
import { ProtobufParser } from "./parsers/ProtobufParser";
import { ResolvedSource, SourceResolver } from "@fern-api/source-resolver";

export class SourceResolverImpl implements SourceResolver {
private readonly context: TaskContext;
Expand All @@ -30,41 +18,33 @@ export class SourceResolverImpl implements SourceResolver {

public async resolveSourceOrThrow({
source,
file
relativeFilepath
}: {
source: RawSchemas.SourceSchema;
file: FernFileContext;
relativeFilepath: RelativeFilePath;
}): Promise<ResolvedSource | undefined> {
const resolvedType = await this.resolveSource({ source, file });
const resolvedType = await this.resolveSource({ source });
if (resolvedType == null) {
if (isRawProtobufSourceSchema(source)) {
throw new Error(`Cannot resolve source ${source.proto} from file ${file.relativeFilepath}`);
throw new Error(`Cannot resolve source ${source.proto} from file ${relativeFilepath}`);
}
// Do not throw if OpenAPI since the source is not actually required.
this.context.logger.warn(`Cannot resolve source ${source.openapi} from file ${file.relativeFilepath}`);
this.context.logger.warn(`Cannot resolve source ${source.openapi} from file ${relativeFilepath}`);
}
return resolvedType;
}

public async resolveSource({
source,
file
}: {
source: RawSchemas.SourceSchema;
file: FernFileContext;
}): Promise<ResolvedSource | undefined> {
public async resolveSource({ source }: { source: RawSchemas.SourceSchema }): Promise<ResolvedSource | undefined> {
if (isRawProtobufSourceSchema(source)) {
return await this.resolveProtobufSource({ source, file });
return await this.resolveProtobufSource({ source });
}
return await this.resolveOpenAPISource({ source, file });
return await this.resolveOpenAPISource({ source });
}

private async resolveProtobufSource({
source,
file
source
}: {
source: RawSchemas.ProtobufSourceSchema;
file: FernFileContext;
}): Promise<ResolvedSource | undefined> {
const absoluteFilepath = join(this.workspace.absoluteFilePath, RelativeFilePath.of(source.proto));
if (this.sourceCache.has(absoluteFilepath)) {
Expand All @@ -88,11 +68,9 @@ export class SourceResolverImpl implements SourceResolver {
}

private async resolveOpenAPISource({
source,
file
source
}: {
source: RawSchemas.OpenApiSourceSchema;
file: FernFileContext;
}): Promise<ResolvedSource | undefined> {
const absoluteFilepath = join(this.workspace.absoluteFilePath, RelativeFilePath.of(source.openapi));
if (this.sourceCache.has(absoluteFilepath)) {
Expand Down
1 change: 1 addition & 0 deletions packages/cli/cli-source-resolver/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { SourceResolverImpl } from "./SourceResolverImpl";
31 changes: 31 additions & 0 deletions packages/cli/cli-source-resolver/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"extends": "../../../shared/tsconfig.shared.json",
"compilerOptions": {
"composite": true,
"outDir": "lib",
"rootDir": "src"
},
"include": [
"./src/**/*"
],
"references": [
{
"path": "../../commons/core-utils"
},
{
"path": "../../commons/fs-utils"
},
{
"path": "../fern-definition/schema"
},
{
"path": "../source-resolver"
},
{
"path": "../task-context"
},
{
"path": "../workspace-commons"
}
]
}
1 change: 1 addition & 0 deletions packages/cli/cli-source-resolver/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from "../../../shared/vitest.config";
3 changes: 2 additions & 1 deletion packages/cli/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,16 @@
"publish:cli:prod": "cd dist/prod && npm publish"
},
"devDependencies": {
"@fern-api/docs-resolver": "workspace:*",
"@fern-api/api-workspace-commons": "workspace:*",
"@fern-api/auth": "workspace:*",
"@fern-api/cli-logger": "workspace:*",
"@fern-api/cli-migrations": "workspace:*",
"@fern-api/cli-source-resolver": "workspace:*",
"@fern-api/configuration-loader": "workspace:*",
"@fern-api/core": "workspace:*",
"@fern-api/core-utils": "workspace:*",
"@fern-api/docs-preview": "workspace:*",
"@fern-api/docs-resolver": "workspace:*",
"@fern-api/docs-validator": "workspace:*",
"@fern-api/fern-definition-formatter": "workspace:*",
"@fern-api/fern-definition-schema": "workspace:*",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { IntermediateRepresentation } from "@fern-api/ir-sdk";
import { TaskContext } from "@fern-api/task-context";
import { FernWorkspace } from "@fern-api/api-workspace-commons";
import { validateAPIWorkspaceAndLogIssues } from "../validate/validateAPIWorkspaceAndLogIssues";
import { SourceResolverImpl } from "@fern-api/cli-source-resolver";

export async function generateIrForFernWorkspace({
workspace,
Expand Down Expand Up @@ -35,6 +36,7 @@ export async function generateIrForFernWorkspace({
readme,
version: undefined,
packageName: undefined,
context
context,
sourceResolver: new SourceResolverImpl(context, workspace)
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { serialization as IrSerialization } from "@fern-api/ir-sdk";
import { Project } from "@fern-api/project-loader";
import { TaskContext } from "@fern-api/task-context";
import { FernWorkspace } from "@fern-api/api-workspace-commons";
import { writeFile } from "fs/promises";
import path from "path";
import { CliContext } from "../../cli-context/CliContext";
import { generateIrForFernWorkspace } from "./generateIrForFernWorkspace";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
} from "@fern-api/ir-generator";
import { mkdir, writeFile } from "fs/promises";
import chalk from "chalk";
import { SourceResolverImpl } from "@fern-api/cli-source-resolver";

export async function generateJsonschemaForWorkspaces({
typeLocator,
Expand Down Expand Up @@ -39,7 +40,8 @@ export async function generateJsonschemaForWorkspaces({
disableExamples: true,
version: undefined,
packageName: undefined,
readme: undefined
readme: undefined,
sourceResolver: new SourceResolverImpl(context, fernWorkspace)
});

const splitTypeLocator = typeLocator.split(".");
Expand Down
4 changes: 3 additions & 1 deletion packages/cli/cli/src/commands/mock/mockServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { AbstractAPIWorkspace, FernWorkspace } from "@fern-api/workspace-loader"
import { CliContext } from "../../cli-context/CliContext";
import { API_CLI_OPTION } from "../../constants";
import { validateAPIWorkspaceAndLogIssues } from "../validate/validateAPIWorkspaceAndLogIssues";
import { SourceResolverImpl } from "@fern-api/cli-source-resolver";

export async function mockServer({
cliContext,
Expand Down Expand Up @@ -45,7 +46,8 @@ export async function mockServer({
readme: undefined,
version: undefined,
packageName: undefined,
context
context,
sourceResolver: new SourceResolverImpl(context, fernWorkspace)
});

const mockServer = new MockServer({
Expand Down
4 changes: 3 additions & 1 deletion packages/cli/cli/src/commands/test/testOutput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { AbstractAPIWorkspace, FernWorkspace } from "@fern-api/workspace-loader"
import { CliContext } from "../../cli-context/CliContext";
import { API_CLI_OPTION } from "../../constants";
import { validateAPIWorkspaceAndLogIssues } from "../validate/validateAPIWorkspaceAndLogIssues";
import { SourceResolverImpl } from "@fern-api/cli-source-resolver";

export async function testOutput({
cliContext,
Expand Down Expand Up @@ -53,7 +54,8 @@ export async function testOutput({
readme: undefined,
version: undefined,
packageName: undefined,
context
context,
sourceResolver: new SourceResolverImpl(context, fernWorkspace)
});

const mockServer = new MockServer({
Expand Down
3 changes: 3 additions & 0 deletions packages/cli/cli/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
{
"path": "../auth"
},
{
"path": "../cli-source-resolver"
},
{
"path": "../configuration-loader"
},
Expand Down
1 change: 1 addition & 0 deletions packages/cli/docs-resolver/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"depcheck": "depcheck"
},
"dependencies": {
"@fern-api/cli-source-resolver": "workspace:*",
"@fern-api/configuration-loader": "workspace:*",
"@fern-api/core-utils": "workspace:*",
"@fern-api/docs-markdown-utils": "workspace:*",
Expand Down
4 changes: 3 additions & 1 deletion packages/cli/docs-resolver/src/DocsDefinitionResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { convertDocsSnippetsConfigToFdr } from "./utils/convertDocsSnippetsConfi
import { convertIrToApiDefinition } from "./utils/convertIrToApiDefinition";
import { collectFilesFromDocsConfig } from "./utils/getImageFilepathsToUpload";
import { wrapWithHttps } from "./wrapWithHttps";
import { SourceResolverImpl } from "@fern-api/cli-source-resolver";

dayjs.extend(utc);

Expand Down Expand Up @@ -553,7 +554,8 @@ export class DocsDefinitionResolver {
readme: undefined,
version: undefined,
packageName: undefined,
context: this.taskContext
context: this.taskContext,
sourceResolver: new SourceResolverImpl(this.taskContext, workspace)
});
const apiDefinitionId = await this.registerApi({
ir,
Expand Down
4 changes: 3 additions & 1 deletion packages/cli/docs-resolver/src/__test__/api-resolver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { ApiDefinitionHolder } from "../ApiDefinitionHolder";
import { ApiReferenceNodeConverter } from "../ApiReferenceNodeConverter";
import { NodeIdGenerator } from "../NodeIdGenerator";
import { convertIrToApiDefinition } from "../utils/convertIrToApiDefinition";
import { SourceResolverImpl } from "@fern-api/cli-source-resolver";

const context = createMockTaskContext();

Expand Down Expand Up @@ -66,7 +67,8 @@ it.skip("converts to api reference node", async () => {
readme: undefined,
version: undefined,
packageName: undefined,
context
context,
sourceResolver: new SourceResolverImpl(context, apiWorkspace)
});

const apiDefinition = convertIrToApiDefinition(ir, apiDefinitionId);
Expand Down
4 changes: 3 additions & 1 deletion packages/cli/docs-resolver/src/__test__/stream.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { ApiDefinitionHolder } from "../ApiDefinitionHolder";
import { ApiReferenceNodeConverter } from "../ApiReferenceNodeConverter";
import { NodeIdGenerator } from "../NodeIdGenerator";
import { convertIrToApiDefinition } from "../utils/convertIrToApiDefinition";
import { SourceResolverImpl } from "@fern-api/cli-source-resolver";

const context = createMockTaskContext();

Expand Down Expand Up @@ -70,7 +71,8 @@ it.skip("converts to api reference node", async () => {
readme: undefined,
version: undefined,
packageName: undefined,
context
context,
sourceResolver: new SourceResolverImpl(context, apiWorkspace)
});

const apiDefinition = convertIrToApiDefinition(ir, apiDefinitionId);
Expand Down
3 changes: 3 additions & 0 deletions packages/cli/docs-resolver/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
{
"path": "../../ir-sdk"
},
{
"path": "../cli-source-resolver"
},
{
"path": "../configuration-loader"
},
Expand Down
1 change: 1 addition & 0 deletions packages/cli/fern-definition/ir-to-jsonschema/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"depcheck": "depcheck"
},
"dependencies": {
"@fern-api/cli-source-resolver": "workspace:*",
"@fern-api/core-utils": "workspace:*",
"@fern-api/fs-utils": "workspace:*",
"@fern-api/ir-generator": "workspace:*",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { AbsoluteFilePath, RelativeFilePath, join } from "@fern-api/fs-utils";
import { generateIntermediateRepresentation } from "@fern-api/ir-generator";
import Ajv from "ajv";
import addFormats from "ajv-formats";
import { SourceResolverImpl } from "@fern-api/cli-source-resolver";

describe("convertIRtoJsonSchema", async () => {
const TEST_DEFINITIONS_DIR = join(
Expand Down Expand Up @@ -39,7 +40,8 @@ describe("convertIRtoJsonSchema", async () => {
readme: undefined,
version: undefined,
packageName: undefined,
context
context,
sourceResolver: new SourceResolverImpl(context, fernWorkspace)
});

for (const [typeId, _] of Object.entries(intermediateRepresentation.types)) {
Expand Down
Loading

0 comments on commit edc2457

Please sign in to comment.