Skip to content

Commit

Permalink
feat(cli): Add IR for dynamic snippets (#4977)
Browse files Browse the repository at this point in the history
  • Loading branch information
amckinney authored Oct 23, 2024
1 parent e3abb3c commit f51a94a
Show file tree
Hide file tree
Showing 207 changed files with 111,407 additions and 3 deletions.
1 change: 1 addition & 0 deletions packages/cli/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"@fern-api/core-utils": "workspace:*",
"@fern-api/docs-preview": "workspace:*",
"@fern-api/docs-validator": "workspace:*",
"@fern-api/dynamic-snippets": "workspace:*",
"@fern-api/fern-definition-formatter": "workspace:*",
"@fern-api/fern-definition-schema": "workspace:*",
"@fern-api/fern-definition-validator": "workspace:*",
Expand Down
50 changes: 50 additions & 0 deletions packages/cli/cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import { FERN_CWD_ENV_VAR } from "./cwd";
import { rerunFernCliAtVersion } from "./rerunFernCliAtVersion";
import { isURL } from "./utils/isUrl";
import { generateJsonschemaForWorkspaces } from "./commands/jsonschema/generateJsonschemaForWorkspace";
import { generateDynamicIrForWorkspaces } from "./commands/generate-dynamic-ir/generateDynamicIrForWorkspaces";
import { setGlobalDispatcher, Agent } from "undici";

setGlobalDispatcher(new Agent({ connect: { timeout: 5_000 } }));
Expand Down Expand Up @@ -142,6 +143,7 @@ async function tryRunCli(cliContext: CliContext) {
addIrCommand(cli, cliContext);
addFdrCommand(cli, cliContext);
addOpenAPIIrCommand(cli, cliContext);
addDynamicIrCommand(cli, cliContext);
addValidateCommand(cli, cliContext);
addRegisterCommand(cli, cliContext);
addRegisterV2Command(cli, cliContext);
Expand Down Expand Up @@ -514,6 +516,54 @@ function addOpenAPIIrCommand(cli: Argv<GlobalCliOptions>, cliContext: CliContext
);
}

function addDynamicIrCommand(cli: Argv<GlobalCliOptions>, cliContext: CliContext) {
cli.command(
"dynamic-ir <path-to-output>",
false,
(yargs) =>
yargs
.positional("path-to-output", {
type: "string",
description: "Path to write intermediate representation (IR)",
demandOption: true
})
.option("api", {
string: true,
description: "Only run the command on the provided API"
})
.option("version", {
string: true,
description: "The version of IR to produce"
})
.option("language", {
choices: Object.values(generatorsYml.GenerationLanguage),
description: "Generate IR for a particular language"
})
.option("audience", {
type: "array",
string: true,
default: new Array<string>(),
description: "Filter the IR for certain audiences"
}),
async (argv) => {
await generateDynamicIrForWorkspaces({
project: await loadProjectAndRegisterWorkspacesWithContext(cliContext, {
commandLineApiWorkspace: argv.api,
defaultToAllApiWorkspaces: false,
sdkLanguage: argv.language
}),
irFilepath: resolve(cwd(), argv.pathToOutput),
cliContext,
generationLanguage: argv.language,
audiences: { type: "all" },
version: argv.version,
keywords: undefined,
smartCasing: false
});
}
);
}

function addFdrCommand(cli: Argv<GlobalCliOptions>, cliContext: CliContext) {
cli.command(
"fdr <path-to-output>",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { Audiences, generatorsYml } from "@fern-api/configuration";
import { AbsoluteFilePath, streamObjectToFile, stringifyLargeObject } from "@fern-api/fs-utils";
import { Project } from "@fern-api/project-loader";
import path from "path";
import { CliContext } from "../../cli-context/CliContext";
import { generateIrForFernWorkspace } from "../generate-ir/generateIrForFernWorkspace";
import { convertIrToDynamicSnippetsIr } from "@fern-api/dynamic-snippets";

export async function generateDynamicIrForWorkspaces({
project,
irFilepath,
cliContext,
generationLanguage,
audiences,
version,
keywords,
smartCasing
}: {
project: Project;
irFilepath: AbsoluteFilePath;
cliContext: CliContext;
generationLanguage: generatorsYml.GenerationLanguage | undefined;
audiences: Audiences;
version: string | undefined;
keywords: string[] | undefined;
smartCasing: boolean;
}): Promise<void> {
await Promise.all(
project.apiWorkspaces.map(async (workspace) => {
await cliContext.runTaskForWorkspace(workspace, async (context) => {
cliContext.logger.info(`Generating IR for workspace ${workspace.workspaceName ?? "api"}`);
const fernWorkspace = await workspace.toFernWorkspace({ context });

const intermediateRepresentation = await generateIrForFernWorkspace({
workspace: fernWorkspace,
context,
generationLanguage,
keywords,
smartCasing,
disableExamples: false,
audiences,
readme: undefined
});

const dynamicIntermediateRepresentation = await convertIrToDynamicSnippetsIr(
intermediateRepresentation
);

const irOutputFilePath = path.resolve(irFilepath);
await streamObjectToFile(AbsoluteFilePath.of(irOutputFilePath), dynamicIntermediateRepresentation);
context.logger.info(`Wrote IR to ${irOutputFilePath}`);
});
})
);
}
1 change: 1 addition & 0 deletions packages/cli/cli/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
{ "path": "../auth" },
{ "path": "../configuration" },
{ "path": "../docs-preview" },
{ "path": "../dynamic-snippets" },
{ "path": "../generation/ir-generator" },
{ "path": "../generation/ir-migrations" },
{ "path": "../generation/local-generation/local-workspace-runner" },
Expand Down
10 changes: 10 additions & 0 deletions packages/cli/dynamic-snippets/.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/dynamic-snippets/.prettierrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require("../../../.prettierrc.json");
50 changes: 50 additions & 0 deletions packages/cli/dynamic-snippets/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"name": "@fern-api/dynamic-snippets",
"version": "0.0.0",
"repository": {
"type": "git",
"url": "https://github.com/fern-api/fern.git",
"directory": "packages/cli/dynamic-snippets"
},
"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 --run",
"test:update": "vitest --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/configuration": "workspace:*",
"@fern-api/core-utils": "workspace:*",
"@fern-api/fs-utils": "workspace:*",
"@fern-api/ir-generator": "workspace:*",
"@fern-api/ir-sdk": "workspace:*",
"@fern-api/project-loader": "workspace:*",
"@fern-api/task-context": "workspace:*",
"@fern-api/workspace-loader": "workspace:*",
"url-join": "^5.0.0"
},
"devDependencies": {
"@types/node": "18.7.18",
"depcheck": "^1.4.6",
"eslint": "^8.56.0",
"globals": "link:@types/vitest/globals",
"organize-imports-cli": "^0.10.0",
"typescript": "4.6.4",
"vitest": "^2.0.5"
}
}
Loading

0 comments on commit f51a94a

Please sign in to comment.