From da7b82610dd1839414cc969385d4b75da59e0543 Mon Sep 17 00:00:00 2001
From: darthmaim <git@darthmaim.de>
Date: Wed, 21 Feb 2024 11:06:28 +0100
Subject: [PATCH] Publish packages

---
 .github/workflows/ci.yml    | 58 ++++++++++++++++++++++++++++++++++++-
 package-lock.json           | 11 +++++++
 packages/fetch/package.json |  7 ++++-
 packages/types/package.json |  8 ++++-
 4 files changed, 81 insertions(+), 3 deletions(-)

diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 4102c6c..1bc25f3 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -15,7 +15,7 @@ jobs:
   build:
     name: Build / Node ${{ matrix.node-version }}
     runs-on: ubuntu-latest
-    
+
     strategy:
       matrix:
         node-version: [ 18, 20, 21 ]
@@ -38,3 +38,59 @@ jobs:
     steps:
     - run: jq --exit-status 'all(.result == "success")' <<< '${{ toJson(needs) }}'
     - run: exit 0
+
+  publish-npm:
+    name: Publish / npm / ${{ matrix.target }}
+    runs-on: ubuntu-latest
+    needs: [success]
+    permissions:
+      contents: read
+      id-token: write
+      pull-requests: write
+    strategy:
+      matrix:
+        target: [ "@gw2api/types", "@gw2api/fetch" ]
+    steps:
+    - uses: actions/checkout@v4
+    - name: Setup Node.js
+      uses: actions/setup-node@v4
+      with:
+        node-version: 21
+        cache: 'npm'
+    - name: Build package
+      run: npm run build -w ${{ matrix.target }}
+    - name: Run Publish
+      run: npm run publish-package -w ${{ matrix.target }}
+      id: publish
+      env:
+        NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
+        NPM_CONFIG_PROVENANCE: true
+        GITHUB_EVENT_NAME: ${{ github.event_name }}
+    - name: Find Comment
+      if: github.event_name == 'pull_request'
+      uses: peter-evans/find-comment@v3
+      id: fc
+      with:
+        issue-number: ${{ github.event.pull_request.number }}
+        comment-author: 'github-actions[bot]'
+        body-includes: Publish / npm / ${{ matrix.target }}
+    - name: Delete comment if found but nothing will be published
+      if: github.event_name == 'pull_request' && steps.fc.outputs.comment-id && !steps.publish.outputs.publish
+      uses: actions/github-script@v7
+      with:
+        script: |
+          github.rest.issues.deleteComment({
+            owner: context.repo.owner,
+            repo: context.repo.repo,
+            comment_id: ${{ steps.fc.outputs.comment-id }}
+          })
+    - name: Add comment with packages to be published
+      if: github.event_name == 'pull_request' && steps.publish.outputs.publish
+      uses: peter-evans/create-or-update-comment@v4
+      with:
+        comment-id: ${{ steps.fc.outputs.comment-id }}
+        issue-number: ${{ github.event.pull_request.number }}
+        edit-mode: replace
+        body: |
+          **Publish / npm / ${{ matrix.target }}**
+          Merging this PR will publish the npm package `${{ steps.publish.outputs.publish }}`
diff --git a/package-lock.json b/package-lock.json
index 3868d48..66db93c 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -18,6 +18,15 @@
       "resolved": "packages/types",
       "link": true
     },
+    "node_modules/@gw2treasures/publish-package": {
+      "version": "0.0.1",
+      "resolved": "https://registry.npmjs.org/@gw2treasures/publish-package/-/publish-package-0.0.1.tgz",
+      "integrity": "sha512-5SzHU6ure63ScZyhyi+9wpWKGGbsw5ZjlPoYwNGxYu1EugV4BcSE2uYFBTWrvQ5eyzTLai9bY8JA6AP2VRyNXQ==",
+      "dev": true,
+      "bin": {
+        "gw2treasures-publish-package": "dist/index.js"
+      }
+    },
     "node_modules/typescript": {
       "version": "5.3.3",
       "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.3.3.tgz",
@@ -39,6 +48,7 @@
         "@gw2api/types": "0.0.1"
       },
       "devDependencies": {
+        "@gw2treasures/publish-package": "0.0.1",
         "typescript": "^5.2.2"
       }
     },
@@ -47,6 +57,7 @@
       "version": "0.0.1",
       "license": "MIT",
       "devDependencies": {
+        "@gw2treasures/publish-package": "0.0.1",
         "typescript": "^5.2.2"
       }
     }
diff --git a/packages/fetch/package.json b/packages/fetch/package.json
index b6fdbcf..bed1b7c 100644
--- a/packages/fetch/package.json
+++ b/packages/fetch/package.json
@@ -18,12 +18,17 @@
   ],
   "scripts": {
     "test": "tsc --noEmit",
-    "build": "tsc"
+    "build": "tsc",
+    "publish-package": "gw2treasures-publish-package"
   },
   "dependencies": {
     "@gw2api/types": "0.0.1"
   },
   "devDependencies": {
+    "@gw2treasures/publish-package": "0.0.1",
     "typescript": "^5.2.2"
+  },
+  "publishConfig": {
+    "access": "public"
   }
 }
diff --git a/packages/types/package.json b/packages/types/package.json
index c0e5329..f650d06 100644
--- a/packages/types/package.json
+++ b/packages/types/package.json
@@ -27,9 +27,15 @@
     "typescript"
   ],
   "scripts": {
-    "test": "tsc"
+    "test": "tsc",
+    "build": "tsc",
+    "publish-package": "gw2treasures-publish-package"
   },
   "devDependencies": {
+    "@gw2treasures/publish-package": "0.0.1",
     "typescript": "^5.2.2"
+  },
+  "publishConfig": {
+    "access": "public"
   }
 }