From ecdcfe0c883f930ad5e079c44a8f8d9420685201 Mon Sep 17 00:00:00 2001 From: Alex McKinney Date: Thu, 31 Oct 2024 16:45:58 -0400 Subject: [PATCH] feat(go): Publish @fern-api/go-dynamic-snippets (#5053) --- .../workflows/publish-go-dynamic-snippets.yml | 52 +++++++++++++++++++ generators/go-v2/codegen/package.json | 4 +- .../custom-config/resolveRootImportPath.ts | 12 ++++- generators/go-v2/dynamic-snippets/build.cjs | 45 ++++++++++++++++ .../dynamic-snippets/build.tsconfig.json | 5 ++ .../go-v2/dynamic-snippets/package.json | 15 +++--- .../DynamicSnippetsGeneratorContext.ts | 2 +- generators/go-v2/formatter/package.json | 4 +- .../src/AbstractDynamicSnippetsGenerator.ts | 2 +- pnpm-lock.yaml | 6 ++- 10 files changed, 128 insertions(+), 19 deletions(-) create mode 100644 .github/workflows/publish-go-dynamic-snippets.yml create mode 100644 generators/go-v2/dynamic-snippets/build.cjs create mode 100644 generators/go-v2/dynamic-snippets/build.tsconfig.json diff --git a/.github/workflows/publish-go-dynamic-snippets.yml b/.github/workflows/publish-go-dynamic-snippets.yml new file mode 100644 index 00000000000..733783ce93d --- /dev/null +++ b/.github/workflows/publish-go-dynamic-snippets.yml @@ -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/buf-setup-action@v1.34.0 + 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/protoc-gen-openapi@v0.7.0 + + - 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 diff --git a/generators/go-v2/codegen/package.json b/generators/go-v2/codegen/package.json index 2a160fdf7f5..f4989e86f46 100644 --- a/generators/go-v2/codegen/package.json +++ b/generators/go-v2/codegen/package.json @@ -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": [ diff --git a/generators/go-v2/codegen/src/custom-config/resolveRootImportPath.ts b/generators/go-v2/codegen/src/custom-config/resolveRootImportPath.ts index e084048c1ef..d7e380c5359 100644 --- a/generators/go-v2/codegen/src/custom-config/resolveRootImportPath.ts +++ b/generators/go-v2/codegen/src/custom-config/resolveRootImportPath.ts @@ -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"; @@ -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; } @@ -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; +} diff --git a/generators/go-v2/dynamic-snippets/build.cjs b/generators/go-v2/dynamic-snippets/build.cjs new file mode 100644 index 00000000000..cc12e9cb640 --- /dev/null +++ b/generators/go-v2/dynamic-snippets/build.cjs @@ -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 + ) + ); +} \ No newline at end of file diff --git a/generators/go-v2/dynamic-snippets/build.tsconfig.json b/generators/go-v2/dynamic-snippets/build.tsconfig.json new file mode 100644 index 00000000000..6a461575e21 --- /dev/null +++ b/generators/go-v2/dynamic-snippets/build.tsconfig.json @@ -0,0 +1,5 @@ +{ + "extends": "../../../shared/tsconfig.shared.json", + "include": ["src/**/*.ts"], + "exclude": ["src/**/__test__"] +} diff --git a/generators/go-v2/dynamic-snippets/package.json b/generators/go-v2/dynamic-snippets/package.json index 1ba558228ff..9a0fd54574a 100644 --- a/generators/go-v2/dynamic-snippets/package.json +++ b/generators/go-v2/dynamic-snippets/package.json @@ -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": [ @@ -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:*", @@ -39,11 +40,6 @@ "@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", @@ -51,6 +47,7 @@ "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" } diff --git a/generators/go-v2/dynamic-snippets/src/context/DynamicSnippetsGeneratorContext.ts b/generators/go-v2/dynamic-snippets/src/context/DynamicSnippetsGeneratorContext.ts index e5bdf42ec64..871201518b1 100644 --- a/generators/go-v2/dynamic-snippets/src/context/DynamicSnippetsGeneratorContext.ts +++ b/generators/go-v2/dynamic-snippets/src/context/DynamicSnippetsGeneratorContext.ts @@ -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"; diff --git a/generators/go-v2/formatter/package.json b/generators/go-v2/formatter/package.json index 408763f619d..3867bbb0f28 100644 --- a/generators/go-v2/formatter/package.json +++ b/generators/go-v2/formatter/package.json @@ -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": [ diff --git a/packages/cli/dynamic-snippets/src/AbstractDynamicSnippetsGenerator.ts b/packages/cli/dynamic-snippets/src/AbstractDynamicSnippetsGenerator.ts index 9e4e2b6d82f..a938c1b26c0 100644 --- a/packages/cli/dynamic-snippets/src/AbstractDynamicSnippetsGenerator.ts +++ b/packages/cli/dynamic-snippets/src/AbstractDynamicSnippetsGenerator.ts @@ -8,7 +8,7 @@ export abstract class AbstractDynamicSnippetsGenerator; } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 8fda620200b..eaa88d04b6f 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -431,7 +431,7 @@ importers: version: 2.0.5(@types/node@18.7.18)(jsdom@20.0.3)(sass@1.72.0)(terser@5.31.5) generators/go-v2/dynamic-snippets: - dependencies: + devDependencies: '@fern-api/core-utils': specifier: workspace:* version: link:../../../packages/commons/core-utils @@ -471,7 +471,6 @@ importers: '@fern-fern/ir-sdk': specifier: ^53.15.0 version: 53.15.0 - devDependencies: '@types/jest': specifier: ^29.5.12 version: 29.5.12 @@ -490,6 +489,9 @@ importers: prettier: specifier: ^2.7.1 version: 2.7.1 + tsup: + specifier: ^8.0.2 + version: 8.0.2(postcss@8.4.47)(ts-node@10.9.2(@types/node@18.7.18)(typescript@4.6.4))(typescript@4.6.4) typescript: specifier: 4.6.4 version: 4.6.4