From 1281fe32daa066d1bb5db5cebdb654f88cfb0342 Mon Sep 17 00:00:00 2001
From: Shibo Lyu <github@of.sb>
Date: Thu, 26 Sep 2024 01:48:16 +0800
Subject: [PATCH] chore: publish on npm

---
 .github/workflows/jsr-publish.yml | 16 --------------
 .github/workflows/publish.yml     | 36 +++++++++++++++++++++++++++++++
 .gitignore                        |  1 +
 deno.json                         |  2 +-
 scripts/build_npm.ts              | 19 ++++++++++++----
 5 files changed, 53 insertions(+), 21 deletions(-)
 delete mode 100644 .github/workflows/jsr-publish.yml
 create mode 100644 .github/workflows/publish.yml
 create mode 100644 .gitignore

diff --git a/.github/workflows/jsr-publish.yml b/.github/workflows/jsr-publish.yml
deleted file mode 100644
index a52f8b4..0000000
--- a/.github/workflows/jsr-publish.yml
+++ /dev/null
@@ -1,16 +0,0 @@
-name: Publish on JSR
-
-on:
-  push:
-    branches:
-      - main
-
-jobs:
-  publish:
-    runs-on: ubuntu-latest
-    permissions:
-      contents: read
-      id-token: write # The OIDC ID token is used for authentication with JSR.
-    steps:
-      - uses: actions/checkout@v4
-      - run: npx jsr publish
diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml
new file mode 100644
index 0000000..26c77a9
--- /dev/null
+++ b/.github/workflows/publish.yml
@@ -0,0 +1,36 @@
+name: Publish on JSR & npm
+
+on:
+  workflow_run:
+    workflows: [Deno]
+    types: [completed]
+    branches: [main]
+jobs:
+  publish:
+    runs-on: ubuntu-latest
+    if: ${{ github.event.workflow_run.conclusion == 'success' }}
+    permissions:
+      contents: read
+      id-token: write # The OIDC ID token is used for authentication with JSR.
+    steps:
+      - name: Setup Deno
+        uses: denoland/setup-deno@61fe2df320078202e33d7d5ad347e7dcfa0e8f31 # v1.1.2
+        with:
+          deno-version: v1.x
+      - name: Setup Node.js
+        uses: actions/setup-node@v4
+        with:
+          node-version: "20.x"
+          registry-url: "https://registry.npmjs.org"
+      - name: Checkout repo
+        uses: actions/checkout@v4
+      - name: Build & test npm package
+        run: deno task build:npm
+      - name: Publish to JSR
+        run: npx jsr publish
+      - name: Publish to npm
+        run: |
+          cd npm
+          npx is-published@0.2.0 || npm publish --provenance --access public
+        env:
+          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..b235581
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+npm
diff --git a/deno.json b/deno.json
index ae506d3..80537d1 100644
--- a/deno.json
+++ b/deno.json
@@ -12,7 +12,7 @@
     "zod": "npm:zod@^3.23.8"
   },
   "tasks": {
-    "build:npm": "deno run ./scripts/build_npm.ts"
+    "build:npm": "deno run -A ./scripts/build_npm.ts"
   },
   "publish": {
     "include": ["LICENSE", "README.md", "crypto", "identity"],
diff --git a/scripts/build_npm.ts b/scripts/build_npm.ts
index 30fbad6..7804aae 100644
--- a/scripts/build_npm.ts
+++ b/scripts/build_npm.ts
@@ -1,18 +1,29 @@
 import { build, emptyDir } from "@deno/dnt";
+import denoJson from "../deno.json" with { type: "json" };
 
 await emptyDir("./npm");
 
 await build({
-  entryPoints: ["./mod.ts"],
+  entryPoints: [
+    { name: "./crypto", path: "crypto/mod.ts" },
+    { name: "./identity", path: "identity/mod.ts" },
+  ],
   outDir: "./npm",
+  importMap: "deno.json",
+  compilerOptions: {
+    lib: ["ESNext", "DOM"],
+  },
   shims: {
-    // see JS docs for overview and more options
-    deno: true,
+    deno: {
+      test: "dev",
+    },
+    crypto: true,
+    webSocket: true,
   },
   package: {
     // package.json properties
     name: "@blah-im/core",
-    version: Deno.args[0],
+    version: denoJson.version,
     description: "Core logic & types for Blah IM.",
     license: "GPL-3.0-only",
     repository: {