Skip to content

Commit

Permalink
chore(typescript): generate a file
Browse files Browse the repository at this point in the history
  • Loading branch information
dsinghvi committed Sep 9, 2024
1 parent 3941bc0 commit a182616
Show file tree
Hide file tree
Showing 19 changed files with 272 additions and 5 deletions.
11 changes: 11 additions & 0 deletions generators/typescript/codegen/.depcheckrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"ignores": [
"@types/jest",
"globals",
"@types/node",
"@fern-fern/ir-sdk"
],
"ignore-patterns": [
"lib"
]
}
1 change: 1 addition & 0 deletions generators/typescript/codegen/.prettierrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require("../../../.prettierrc.json");
43 changes: 43 additions & 0 deletions generators/typescript/codegen/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"name": "@fern-api/typescript-codegen",
"version": "0.0.0",
"repository": {
"type": "git",
"url": "https://github.com/fern-api/fern.git"
},
"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 --passWithNoTests",
"test:update": "vitest --run --passWithNoTests -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"
},
"devDependencies": {
"@fern-api/fs-utils": "workspace:*",
"@fern-api/core-utils": "workspace:*",
"@fern-api/generator-commons": "workspace:*",
"@fern-fern/ir-sdk": "53.8.0",
"zod": "^3.22.3",
"@types/jest": "^29.5.12",
"@types/node": "^18.7.18",
"depcheck": "^1.4.6",
"eslint": "^8.56.0",
"organize-imports-cli": "^0.10.0",
"prettier": "^2.7.1",
"typescript": "4.6.4",
"vitest": "^2.0.5"
}
}
21 changes: 21 additions & 0 deletions generators/typescript/codegen/src/ast/CodeBlock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { CodeBlock as CommonCodeBlock } from "@fern-api/generator-commons";
import { AstNode, Writer } from "../typescript";

export declare namespace CodeBlock {
/* Write arbitrary code */
type Args = CommonCodeBlock.Arg<Writer>;
}

export class CodeBlock extends AstNode {
private args: CodeBlock.Args;

public constructor(args: CodeBlock.Args) {
super();
this.args = args;
}

public write(writer: Writer): void {
const commonCodeBlock = new CommonCodeBlock(this.args);
return commonCodeBlock.write(writer);
}
}
33 changes: 33 additions & 0 deletions generators/typescript/codegen/src/ast/Type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { assertNever } from "@fern-api/core-utils";
import { AstNode } from "./core/AstNode";
import { Writer } from "./core/Writer";

type InternalType = String_;

interface String_ {
type: "string";
}

/* A C# parameter to a method */
export class Type extends AstNode {
private constructor(public readonly internalType: InternalType) {
super();
}

public write(writer: Writer): void {
switch (this.internalType.type) {
case "string":
writer.write("string");
break;
default:
assertNever(this.internalType.type);
}
}

/* Static factory methods for creating a Type */
public static string(): Type {
return new this({
type: "string"
});
}
}
32 changes: 32 additions & 0 deletions generators/typescript/codegen/src/ast/Variable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { CodeBlock } from "./CodeBlock";
import { AstNode, Writer } from "./core";

export declare namespace Variable {
interface Args {
/* Whether to export */
export?: boolean;
/* Whether to label as const */
const?: boolean;
/* The name of the variable */
name: string;
/* The initializer for the variable */
initializer: CodeBlock;
}
}

export class Variable extends AstNode {
public constructor(private readonly args: Variable.Args) {
super();
}

public write(writer: Writer): void {
if (this.args.export) {
writer.write("export ");
}
if (this.args.const) {
writer.write("const ");
}
writer.write(`${this.args.name} = `);
writer.writeNodeStatement(this.args.initializer);
}
}
3 changes: 3 additions & 0 deletions generators/typescript/codegen/src/ast/core/AstNode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { AbstractAstNode } from "@fern-api/generator-commons";

export abstract class AstNode extends AbstractAstNode {}
7 changes: 7 additions & 0 deletions generators/typescript/codegen/src/ast/core/Writer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { AbstractWriter } from "@fern-api/generator-commons";

export class Writer extends AbstractWriter {
public toString(): string {
return this.buffer;
}
}
2 changes: 2 additions & 0 deletions generators/typescript/codegen/src/ast/core/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { AstNode } from "./AstNode";
export { Writer } from "./Writer";
4 changes: 4 additions & 0 deletions generators/typescript/codegen/src/ast/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from "./core";
export { CodeBlock } from "./CodeBlock";
export { Type } from "./Type";
export { Variable } from "./Variable";
2 changes: 2 additions & 0 deletions generators/typescript/codegen/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * as ts from "./typescript";
export * from "./ast/core";
13 changes: 13 additions & 0 deletions generators/typescript/codegen/src/typescript.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import * as AST from "./ast";

export function codeblock(arg: AST.CodeBlock.Args): AST.CodeBlock {
return new AST.CodeBlock(arg);
}

export function variable(arg: AST.Variable.Args): AST.Variable {
return new AST.Variable(arg);
}

export * from "./ast";
export { Type as Types } from "./ast";
export * from "./ast/core";
11 changes: 11 additions & 0 deletions generators/typescript/codegen/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "../../../shared/tsconfig.shared.json",
"compilerOptions": { "composite": true, "outDir": "lib", "rootDir": "src" },
"include": ["./src/**/*"],
"references": [
{ "path": "../../../packages/commons/core-utils" },
{ "path": "../../../packages/commons/fs-utils" },
{ "path": "../../../packages/commons/logging-execa" },
{ "path": "../../commons" }
]
}
1 change: 1 addition & 0 deletions generators/typescript/codegen/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from "../../../shared/vitest.config";
1 change: 1 addition & 0 deletions generators/typescript/sdk/generator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"dependencies": {
"@fern-api/core-utils": "workspace:*",
"@fern-api/fs-utils": "workspace:*",
"@fern-api/typescript-codegen": "workspace:*",
"@fern-api/generator-commons": "workspace:*",
"@fern-api/logger": "workspace:*",
"@fern-fern/generator-cli-sdk": "0.0.56",
Expand Down
7 changes: 7 additions & 0 deletions generators/typescript/sdk/generator/src/SdkGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ import { TypeScriptGeneratorAgent } from "./TypeScriptGeneratorAgent";
import { TemplateGenerator } from "./TemplateGenerator";
import { JestTestGenerator } from "./test-generator/JestTestGenerator";
import { VersionGenerator } from "./version/VersionGenerator";
import { VersionFileGenerator } from "./version/VersionFileGenerator";

const FILE_HEADER = `/**
* This file was auto-generated by Fern from our API Definition.
Expand Down Expand Up @@ -423,6 +424,12 @@ export class SdkGenerator {
}
}

const versionFileGenerator = new VersionFileGenerator({
ir: this.intermediateRepresentation,
rootDirectory: this.rootDirectory
});
versionFileGenerator.generate();

this.coreUtilitiesManager.finalize(this.exportsManager, this.dependencyManager);
this.exportsManager.writeExportsToProject(this.rootDirectory);
this.context.logger.debug("Generated exports");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { IntermediateRepresentation } from "@fern-fern/ir-sdk/api";
import { Directory } from "ts-morph";
import { Writer, ts } from "@fern-api/typescript-codegen";

export declare namespace VersionFileGenerator {
interface Args {
ir: IntermediateRepresentation;
rootDirectory: Directory;
}
}

export class VersionFileGenerator {
private ir: IntermediateRepresentation;
private rootDirectory: Directory;

constructor({ ir, rootDirectory }: VersionFileGenerator.Args) {
this.ir = ir;
this.rootDirectory = rootDirectory;
}

public generate(): void {
const writer = new Writer();
writer.writeNodeStatement(
ts.variable({
const: true,
export: true,
initializer: ts.codeblock(`"${this.ir.sdkConfig.platformHeaders.sdkVersion}"`),
name: "SdkVersion"
})
);
this.rootDirectory.createSourceFile("version.ts", writer.toString());
}
}
3 changes: 2 additions & 1 deletion generators/typescript/sdk/generator/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
{ "path": "../sdk-endpoint-type-schemas-generator" },
{ "path": "../sdk-error-generator" },
{ "path": "../sdk-error-schema-generator" },
{ "path": "../sdk-inlined-request-body-schema-generator" }
{ "path": "../sdk-inlined-request-body-schema-generator" },
{ "path": "../../codegen" },
]
}
49 changes: 45 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a182616

Please sign in to comment.