Skip to content

Commit

Permalink
Generating typescript interfaces and publishing npm package
Browse files Browse the repository at this point in the history
  • Loading branch information
westerbo-nhn committed Jan 27, 2025
1 parent da5f44b commit 52dc425
Show file tree
Hide file tree
Showing 14 changed files with 466 additions and 54 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"

Check failure on line 38 in cmd/generator/main.go

View workflow job for this annotation

GitHub Actions / build-app

no required module provides package github.com/tkrajina/typescriptify-golang-structs/typescriptify; to add it:
)

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)
}
}
13 changes: 13 additions & 0 deletions hacks/assets/mongodb queries/delete_cluster.mongodb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Select the database to use.
use('nhn-ror');

//delete cluster
let clusterId = '';
var result = db.clusters.deleteOne({ clusterid: clusterId });
printjson(result);
result = db.resources.deleteMany({ 'owner.subject': clusterId });
printjson(result);
result = db.resources2.deleteMany({ 'rormeta.ownerref.subject': clusterId });
printjson(result);
result = db.apikeys.deleteMany({ displayname: clusterId });
printjson(result);
2 changes: 1 addition & 1 deletion pkg/rorresources/rortypes/resource_common_models.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ type ResourceRorMeta struct {
type ResourceTag struct {
Key string `json:"key"`
Value string `json:"value"`
Properties map[ResourceTagProperties]string `json:"properties"`
Properties map[ResourceTagProperties]string `json:"properties" ts_type:"{ [key: string]: string }"`
}

// The RorResourceOwnerReference or ownereref references the owner og a resource.
Expand Down
2 changes: 2 additions & 0 deletions typescript/models/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
dist
3 changes: 3 additions & 0 deletions typescript/models/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
dist
.prettier*
2 changes: 2 additions & 0 deletions typescript/models/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.yaml
*.template.*
6 changes: 6 additions & 0 deletions typescript/models/.prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"singleQuote": true,
"trailingComma": "all",
"endOfLine": "auto",
"printWidth": 150
}
41 changes: 41 additions & 0 deletions typescript/models/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# ROR NPM

Npm package for ROR (Release Operate Report) project: https://github.com/norskHelsenett/ror

## Prerequisite

Npm publish token in environment variables

## Build

```bash
bun i
```

## Test locally

- Update version number in `package.json`
- Run

```bash
npm pack
```

A new file will appear beside ``package.json`. named something like: rork8s-ror-resources-<version number>.tgz

To use in another `package.json`
Replace dependency url, while testing to example this:

```json
"@rork8s/ror-resources": "file:../../../ror-typescript-resource-models/rork8s-ror-resources-0.0.6.tgz",
```

## Publish

- Update version number in `package.json`
- Remember to add npm token before publishing
- Run

```bash
npm publish
```
127 changes: 127 additions & 0 deletions typescript/models/package-lock.json

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

Loading

0 comments on commit 52dc425

Please sign in to comment.