Skip to content

Commit

Permalink
Merge pull request #310 from NorskHelsenett/feature/ror-typescript-re…
Browse files Browse the repository at this point in the history
…source-models

Generating typescript interfaces and publishing npm package
  • Loading branch information
havardelnan authored Jan 27, 2025
2 parents f8d68c5 + 5a49cdb commit 70ea775
Show file tree
Hide file tree
Showing 16 changed files with 511 additions and 92 deletions.
61 changes: 61 additions & 0 deletions .github/workflows/release-npm-package.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
name: Release ROR resource models to npm

on:
push:
tags:
- 'v*'

env:
PACKAGE_NAME: ror-resources
PACKAGE_ORG: "@rork8s"

jobs:
setenv:
permissions:
contents: read
runs-on: ubuntu-latest
outputs:
rorversion: ${{ steps.env.outputs.ROR_VERSION }}
version: ${{ steps.env.outputs.VERSION }}
shortsha: ${{ steps.env.outputs.SHA_SHORT }}
steps:
- uses: actions/checkout@v3
- id: env
name: Set env
run: |
echo "ROR_VERSION=${GITHUB_REF#refs/*/}" >> "$GITHUB_OUTPUT"
echo "VERSION=$(echo $ROR_VERSION | cut -d'v' -f 2)"
echo "SHA_SHORT=$(git rev-parse --short HEAD)" >> "$GITHUB_OUTPUT"
publish-ror-resource-models:
if: github.ref_type == 'tag'
runs-on: ubuntu-latest
needs: setenv

permissions:
contents: read
packages: write
id-token: write

steps:
- uses: actions/checkout@v3
- name: Install jq
uses: dcarbone/[email protected]

- name: Use node.js
uses: actions/setup-node@v3
with:
node-version: '22.x'
- name: Build package
run: |
echo "Building ${{ env.PACKAGE_ORG }}/${{ env.PACKAGE_NAME }} ${{ needs.setenv.outputs.rorversion}} (${{ needs.setenv.outputs.shortsha}})"
- run: mv package.json package-backup.json
- run: jq --arg rorversion ${{ needs.setenv.outputs.version}} --arg shortsha ${{ needs.setenv.outputs.shortsha}} '.version = $rorversion | .commit = $shortsha' package-backup.json > package.json
- run: npm ci
- run: npm run build
- run: npm pack
- name: Publish package
run: |
echo "//registry.npmjs.org/:_authToken=${{ secrets.NPMPUBLISHTOKEN }}" > ~/.npmrc
npm publish --access public
65 changes: 65 additions & 0 deletions cmd/generator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
package main

import (
"errors"
"fmt"
"log"
"os"
Expand All @@ -32,7 +33,9 @@ import (
"strings"
"text/template"

"github.com/NorskHelsenett/ror/pkg/rorresources"
"github.com/NorskHelsenett/ror/pkg/rorresources/rordefs"
"github.com/tkrajina/typescriptify-golang-structs/typescriptify"
)

func main() {
Expand Down Expand Up @@ -60,6 +63,7 @@ func main() {
templateFileOnce(filepath, "pkg/rorresources/rortypes/resource_models_input_filter.go.tmpl", res)
}

generateTypescriptModels()
}

func templateFileOnce(filepath string, templatePath string, data any) {
Expand Down Expand Up @@ -125,3 +129,64 @@ func templateToFile(filepath string, templatePath string, data any) {
}
fmt.Println("Generated file: ", filepath)
}

func touchFile(filePath string) error {
file, err := os.OpenFile(filePath, os.O_RDONLY|os.O_CREATE, 0644)
if err != nil {
return err
}
return file.Close()
}

func generateTypescriptModels() {
workingDirectory, err := os.Getwd()
if err != nil {
panic(err.Error())
}
resourceV2TypescriptFilePath := fmt.Sprintf("%s/typescript/models/src/resources.ts", workingDirectory)
if _, err = os.Stat(resourceV2TypescriptFilePath); errors.Is(err, os.ErrNotExist) {
err = touchFile(resourceV2TypescriptFilePath)
if err != nil {
panic(err.Error())
}
}

converter := typescriptify.New()
converter.CreateInterface = true
converter.CreateConstructor = true

converter.Add(rorresources.ResourceSet{})
converter.Add(rorresources.ResourceQuery{})

err = converter.ConvertToFile(resourceV2TypescriptFilePath)
if err != nil {
panic(err.Error())
}

formatTypescript()
}

func formatTypescript() {
workingDirectory, err := os.Getwd()
if err != nil {
panic(err.Error())
}

resourceV2TypescriptFilePath := fmt.Sprintf("%s/typescript/models", workingDirectory)

getNodeDependenciesCmd := exec.Command("npm", "install")
getNodeDependenciesCmd.Dir = resourceV2TypescriptFilePath
_, err = getNodeDependenciesCmd.CombinedOutput()
if err != nil {
_, _ = fmt.Println("npm install failed with err: ", err.Error())
fmt.Println(err)
}

formatCmd := exec.Command("npm", "run", "format")
formatCmd.Dir = resourceV2TypescriptFilePath
_, err = formatCmd.CombinedOutput()
if err != nil {
_, _ = fmt.Println("prettier failed with err: ", err.Error())
fmt.Println(err)
}
}
26 changes: 14 additions & 12 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/NorskHelsenett/ror
go 1.23.0

require (
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.8.0
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.8.1
github.com/dotse/go-health v0.2.6
github.com/evanphx/json-patch/v5 v5.9.0
github.com/gin-gonic/gin v1.10.0
Expand All @@ -23,9 +23,10 @@ require (
github.com/redis/go-redis/v9 v9.7.0
github.com/spf13/cobra v1.8.1
github.com/spf13/viper v1.19.0
github.com/tkrajina/typescriptify-golang-structs v0.2.0
go.mongodb.org/mongo-driver v1.17.2
go.opentelemetry.io/contrib/instrumentation/go.mongodb.org/mongo-driver/mongo/otelmongo v0.59.0
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.58.0
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.59.0
go.opentelemetry.io/otel v1.34.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.34.0
go.opentelemetry.io/otel/sdk v1.34.0
Expand All @@ -50,12 +51,12 @@ require (
github.com/AzureAD/microsoft-authentication-library-for-go v1.3.2 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/blang/semver/v4 v4.0.0 // indirect
github.com/bytedance/sonic v1.12.7 // indirect
github.com/bytedance/sonic/loader v0.2.2 // indirect
github.com/bytedance/sonic v1.12.8 // indirect
github.com/bytedance/sonic/loader v0.2.3 // indirect
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/cjlapao/common-go v0.0.41 // indirect
github.com/cloudwego/base64x v0.1.4 // indirect
github.com/cloudwego/base64x v0.1.5 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/emicklei/go-restful/v3 v3.12.1 // indirect
Expand Down Expand Up @@ -88,7 +89,7 @@ require (
github.com/google/uuid v1.6.0 // indirect
github.com/gorilla/websocket v1.5.3 // indirect
github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.25.1 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.26.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-retryablehttp v0.7.7 // indirect
github.com/hashicorp/go-rootcerts v1.0.2 // indirect
Expand Down Expand Up @@ -128,7 +129,7 @@ require (
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.61.0 // indirect
github.com/prometheus/common v0.62.0 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
github.com/redis/go-redis/extra/rediscmd/v9 v9.7.0 // indirect
github.com/robfig/cron/v3 v3.0.1 // indirect
Expand All @@ -142,6 +143,7 @@ require (
github.com/std-uritemplate/std-uritemplate/go/v2 v2.0.1 // indirect
github.com/stretchr/testify v1.10.0 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/tkrajina/go-reflector v0.5.8 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect
github.com/x448/float16 v0.8.4 // indirect
Expand All @@ -164,9 +166,9 @@ require (
golang.org/x/sys v0.29.0 // indirect
golang.org/x/term v0.28.0 // indirect
golang.org/x/time v0.9.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20250115164207-1a7da9e5054f // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20250115164207-1a7da9e5054f // indirect
google.golang.org/protobuf v1.36.3 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20250124145028-65684f501c47 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20250124145028-65684f501c47 // indirect
google.golang.org/protobuf v1.36.4 // indirect
gopkg.in/evanphx/json-patch.v4 v4.12.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
Expand All @@ -175,8 +177,8 @@ require (
k8s.io/kube-openapi v0.0.0-20241212222426-2c72e554b1e7 // indirect
k8s.io/utils v0.0.0-20241210054802-24370beab758 // indirect
sigs.k8s.io/json v0.0.0-20241014173422-cfa47c3a1cc8 // indirect
sigs.k8s.io/kustomize/api v0.18.0 // indirect
sigs.k8s.io/kustomize/kyaml v0.18.1 // indirect
sigs.k8s.io/kustomize/api v0.19.0 // indirect
sigs.k8s.io/kustomize/kyaml v0.19.0 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.5.0 // indirect
sigs.k8s.io/yaml v1.4.0 // indirect
)
Loading

0 comments on commit 70ea775

Please sign in to comment.