aboutsummaryrefslogtreecommitdiff
path: root/.github/workflows/publish.yml
blob: 44a604e6a6126c83401ff4c05ac1a578d4fbbd47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
name: Publish to npm

on:
  push:
    branches:
      - master

concurrency:
  group: npm-publish
  cancel-in-progress: false

permissions:
  contents: read
  id-token: write

jobs:
  publish:
    runs-on: ubuntu-latest

    steps:
      - name: Check out repository
        uses: actions/checkout@v4

      - name: Set up Bun
        uses: oven-sh/setup-bun@v2

      - name: Set up Node for npm
        uses: actions/setup-node@v4
        with:
          node-version: 24
          registry-url: https://registry.npmjs.org

      - name: Build optimized package
        run: bun run build

      - name: Choose daily version
        id: release
        shell: bash
        run: |
          version="$(node --input-type=module <<'NODE'
          import { readFile } from "node:fs/promises";

          const packageJson = JSON.parse(await readFile("package.json", "utf8"));
          const date = new Date().toISOString().slice(0, 10).replaceAll("-", "");
          const escapedName = packageJson.name.replace("/", "%2f");
          const response = await fetch(`https://registry.npmjs.org/${escapedName}`);

          let versions = [];
          if (response.status !== 404) {
            if (!response.ok) {
              throw new Error(`npm registry returned ${response.status} ${response.statusText}`);
            }
            const metadata = await response.json();
            versions = Object.keys(metadata.versions ?? {});
          }

          const pattern = new RegExp(`^${date}\\.0\\.(\\d+)$`);
          const previous = versions
            .map((version) => pattern.exec(version)?.[1])
            .filter((increment) => increment !== undefined)
            .map(Number);
          const increment = previous.length === 0 ? 0 : Math.max(...previous) + 1;

          process.stdout.write(`${date}.0.${increment}`);
          NODE
          )"

          npm pkg set version="$version"
          echo "version=$version" >> "$GITHUB_OUTPUT"

      - name: Verify package contents
        run: npm pack --dry-run

      - name: Publish ${{ steps.release.outputs.version }}
        run: npm publish --access public --provenance