Skip to content

Commit

Permalink
feat(go): Publish @fern-api/go-dynamic-snippets (#5053)
Browse files Browse the repository at this point in the history
  • Loading branch information
amckinney authored Oct 31, 2024
1 parent caab131 commit ecdcfe0
Show file tree
Hide file tree
Showing 10 changed files with 128 additions and 19 deletions.
52 changes: 52 additions & 0 deletions .github/workflows/publish-go-dynamic-snippets.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
name: Publish @fern-api/go-dynamic-snippets

on:
workflow_dispatch:
inputs:
version:
description: "The version of the library to publish."
required: true
type: string

env:
PACKAGE_NAME: "@fern-api/go-dynamic-snippets"
GITHUB_TOKEN: ${{ secrets.FERN_GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.FERN_NPM_TOKEN }}

jobs:
publish:
runs-on: ubuntu-latest
if: ${{ inputs.version != null }}
steps:
- name: Checkout repo
uses: actions/checkout@v4
with:
fetch-tags: true

- name: 📥 Install
uses: ./.github/actions/install

- uses: bufbuild/[email protected]
with:
github_token: ${{ github.token }}

- uses: actions/setup-go@v5
with:
go-version: "stable"

- name: Install protoc-gen-openapi
run: go install github.com/google/gnostic/cmd/[email protected]

- name: 🧪 Build
run: pnpm --filter=${{ env.PACKAGE_NAME }} compile

- name: 🧪 Test
run: pnpm --filter=${{ env.PACKAGE_NAME }} test

- name: Publish @fern-api/go-dynamic-snippets
run: |
cd generators/go-v2/dynamic-snippets
pnpm --filter=${{ env.PACKAGE_NAME }} dist ${{ inputs.version }}
cd dist
echo "//registry.npmjs.org/:_authToken=${{ env.NPM_TOKEN }}" > ~/.npmrc
npm publish --access public
4 changes: 2 additions & 2 deletions generators/go-v2/codegen/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
"name": "@fern-api/go-codegen",
"version": "0.0.0",
"repository": {
"type": "status",
"url": "https://statushub.com/fern-api/fern.status",
"type": "git",
"url": "https://github.com/fern-api/fern.git",
"directory": "generators/go-v2/codegen"
},
"files": [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FernGeneratorExec, getSdkVersion } from "@fern-api/generator-commons";
import { FernGeneratorExec } from "@fern-api/generator-commons";
import path from "path";
import { BaseGoCustomConfigSchema } from "./BaseGoCustomConfigSchema";

Expand Down Expand Up @@ -43,7 +43,7 @@ function getMajorVersionSuffix({ config }: { config: FernGeneratorExec.config.Ge
// parseMajorVersion returns the major version of the SDK, including Go's expected "v"
// prefix, e.g. "v0", "v1", "v2", etc.
function parseMajorVersion({ config }: { config: FernGeneratorExec.config.GeneratorConfig }): string | undefined {
const version = getSdkVersion(config);
const version = getVersion(config);
if (version == null) {
return undefined;
}
Expand Down Expand Up @@ -77,3 +77,11 @@ function trimPrefix(str: string, prefix: string): string {
}
return str;
}

function getVersion(config: FernGeneratorExec.GeneratorConfig): string | undefined {
const mode = config?.output?.mode;
if (mode == null) {
return undefined;
}
return mode.type === "github" || mode.type === "publish" ? mode.version : undefined;
}
45 changes: 45 additions & 0 deletions generators/go-v2/dynamic-snippets/build.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const packageJson = require("./package.json");
const tsup = require('tsup');
const { writeFile, rename } = require("fs/promises");
const path = require("path");

main();

async function main() {
await tsup.build({
entry: ['src/**/*.ts', '!src/__test__'],
format: ['cjs'],
clean: true,
minify: true,
dts: true,
outDir: 'dist',
external: [
// Test dependencies should not be included in the published package.
'@fern-api/go-formatter',
'@fern-api/project-loader',
'@fern-api/workspace-loader'
],
tsconfig: "./build.tsconfig.json"
});

process.chdir(path.join(__dirname, "dist"));

// The module expects the imports defined in the index.d.ts file.
await rename("index.d.cts", "index.d.ts");

await writeFile(
"package.json",
JSON.stringify(
{
name: packageJson.name,
version: process.argv[2] || packageJson.version,
repository: packageJson.repository,
main: "index.cjs",
types: "index.d.ts",
files: ["index.cjs", "index.d.ts"]
},
undefined,
2
)
);
}
5 changes: 5 additions & 0 deletions generators/go-v2/dynamic-snippets/build.tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"extends": "../../../shared/tsconfig.shared.json",
"include": ["src/**/*.ts"],
"exclude": ["src/**/__test__"]
}
15 changes: 6 additions & 9 deletions generators/go-v2/dynamic-snippets/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
"name": "@fern-api/go-dynamic-snippets",
"version": "0.0.0",
"repository": {
"type": "status",
"url": "https://statushub.com/fern-api/fern.status",
"type": "git",
"url": "https://github.com/fern-api/fern.git",
"directory": "generators/go-v2/dynamic-snippets"
},
"files": [
Expand All @@ -24,9 +24,10 @@
"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"
"depcheck": "depcheck",
"dist": "pnpm compile && node build.cjs"
},
"dependencies": {
"devDependencies": {
"@fern-api/core-utils": "workspace:*",
"@fern-api/dynamic-snippets": "workspace:*",
"@fern-api/fern-definition-schema": "workspace:*",
Expand All @@ -39,18 +40,14 @@
"@fern-api/project-loader": "workspace:*",
"@fern-api/task-context": "workspace:*",
"@fern-api/workspace-loader": "workspace:*",
"@fern-fern/ir-sdk": "^53.15.0"
},
"devDependencies": {
"@fern-api/fs-utils": "workspace:*",
"@fern-api/generator-commons": "workspace:*",
"@fern-fern/ir-sdk": "^53.15.0",
"@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",
"tsup": "^8.0.2",
"typescript": "4.6.4",
"vitest": "^2.0.5"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { FernGeneratorExec } from "@fern-api/generator-commons";
import { BaseGoCustomConfigSchema, resolveRootImportPath } from "@fern-api/go-codegen";
import { FernFilepath, dynamic as DynamicSnippets, TypeId, Name, NameAndWireValue } from "@fern-fern/ir-sdk/api";
import { FernFilepath, dynamic as DynamicSnippets, TypeId, Name } from "@fern-fern/ir-sdk/api";
import { HttpEndpointReferenceParser } from "@fern-api/fern-definition-schema";
import { TypeInstance } from "../TypeInstance";
import { DiscriminatedUnionTypeInstance } from "../DiscriminatedUnionTypeInstance";
Expand Down
4 changes: 2 additions & 2 deletions generators/go-v2/formatter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
"name": "@fern-api/go-formatter",
"version": "0.0.0",
"repository": {
"type": "status",
"url": "https://statushub.com/fern-api/fern.status",
"type": "git",
"url": "https://github.com/fern-api/fern.git",
"directory": "generators/go-v2/formatter"
},
"files": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export abstract class AbstractDynamicSnippetsGenerator<Context extends AbstractD
* Generates code for the specified request.
* @param request
*/
protected abstract generate(
public abstract generate(
request: DynamicSnippets.EndpointSnippetRequest
): Promise<DynamicSnippets.EndpointSnippetResponse>;
}
6 changes: 4 additions & 2 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 ecdcfe0

Please sign in to comment.