From d090a37560185e5a64bf2df860676df0372f7292 Mon Sep 17 00:00:00 2001
From: Matei Adriel <rafaeladriel11@gmail.com>
Date: Sun, 29 Oct 2023 05:46:53 +0100
Subject: [PATCH] Update CI

---
 .github/workflows/build.yaml  | 22 ++++++++++++++++
 .github/workflows/deploy.yaml | 39 ++++++++++++++--------------
 .gitignore                    |  1 +
 .prettierrc.json              |  2 +-
 build.js                      | 16 +++++++-----
 flake.nix                     | 49 +++++++++++++++++++++++++++--------
 public/robots.txt             |  4 ---
 7 files changed, 91 insertions(+), 42 deletions(-)
 create mode 100644 .github/workflows/build.yaml
 delete mode 100644 public/robots.txt

diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml
new file mode 100644
index 0000000..ae047f1
--- /dev/null
+++ b/.github/workflows/build.yaml
@@ -0,0 +1,22 @@
+name: Build
+
+on:
+  push:
+  pull_request:
+
+jobs:
+  build:
+    runs-on: ubuntu-latest
+
+    steps:
+      - uses: actions/checkout@v2
+      - uses: DeterminateSystems/nix-installer-action@main
+      - uses: DeterminateSystems/magic-nix-cache-action@main
+
+      - name: Build project
+        run: nix build
+
+      - name: Upload artifact
+        uses: actions/upload-pages-artifact@v2
+        with:
+          path: ./result/www
diff --git a/.github/workflows/deploy.yaml b/.github/workflows/deploy.yaml
index 242534a..462db72 100644
--- a/.github/workflows/deploy.yaml
+++ b/.github/workflows/deploy.yaml
@@ -1,25 +1,24 @@
-name: "Deploy"
+# Inspired by https://github.com/actions/starter-workflows/blob/main/pages/mdbook.yml
+name: Deploy
+
 on:
-  pull_request:
   push:
+    branches: [$default-branch]
+  workflow_dispatch:
+
+permissions:
+  contents: read
+  pages: write
+  id-token: write
+
 jobs:
-  tests:
+  deploy:
+    environment:
+      name: github-pages
+      url: ${{ steps.deployment.outputs.page_url }}
     runs-on: ubuntu-latest
+    needs: build
     steps:
-    - uses: actions/checkout@v3
-    - uses: cachix/install-nix-action@v19
-      with:
-        github_access_token: ${{ secrets.GITHUB_TOKEN }}
-    - uses: cachix/cachix-action@v12
-      with:
-        name: erratic-gate
-        authToken: '${{ secrets.CACHIX_AUTH_TOKEN }}'
-
-    - run: nix build
-    - run: nix flake check
-
-    - name: Deploy
-      uses: peaceiris/actions-gh-pages@v3
-      with:
-        github_token: ${{ secrets.GITHUB_TOKEN }}
-        publish_dir: ./dist
+      - name: Deploy to GitHub Pages
+        id: deployment
+        uses: actions/deploy-pages@v2
diff --git a/.gitignore b/.gitignore
index d1a3d45..7a95014 100644
--- a/.gitignore
+++ b/.gitignore
@@ -3,3 +3,4 @@ dist
 coverege
 idea
 .direnv
+result
diff --git a/.prettierrc.json b/.prettierrc.json
index 54856a2..184f937 100644
--- a/.prettierrc.json
+++ b/.prettierrc.json
@@ -3,5 +3,5 @@
     "trailingComma": "none",
     "singleQuote": true,
     "printWidth": 80,
-    "tabWidth": 4
+    "tabWidth": 2
 }
diff --git a/build.js b/build.js
index 9e64509..47c8690 100644
--- a/build.js
+++ b/build.js
@@ -1,19 +1,20 @@
 import * as esbuild from 'esbuild'
 import { htmlPlugin } from '@craftamap/esbuild-plugin-html'
 import { sassPlugin } from 'esbuild-sass-plugin'
+import * as fs from 'fs'
 
-const production = process.env.NODE_ENV === 'production'
 const serve = process.env.ESBUILD_SERVE === '1'
 
 const ctx = await esbuild.context({
     entryPoints: ['src/index.ts'],
-    bundle: production,
-    // minify: production,
-    outdir: 'dist',
+    minify: !serve,
+    bundle: true,
     metafile: true,
     splitting: true,
+    outdir: 'dist',
     format: 'esm',
-    target: ['chrome100', 'firefox100'],
+    target: ['es2020'],
+    assetNames: 'assets/[name]-[hash]',
     loader: {
         '.svg': 'file'
     },
@@ -26,7 +27,6 @@ const ctx = await esbuild.context({
                     favicon: 'public/favicon.ico',
                     htmlTemplate: 'public/index.html',
                     scriptLoading: 'module'
-                    // inline: { js: true }
                 }
             ]
         }),
@@ -37,4 +37,8 @@ const ctx = await esbuild.context({
 if (serve) {
     const { port, host } = await ctx.serve({ servedir: 'dist' })
     console.log(`Serving on ${host}:${port}`)
+} else {
+    await ctx.rebuild()
+    await ctx.dispose()
+    fs.cpSync('./dist/index.html', './dist/404.html') // Needed to please github pages
 }
diff --git a/flake.nix b/flake.nix
index 4828c98..c683c2a 100644
--- a/flake.nix
+++ b/flake.nix
@@ -3,19 +3,45 @@
   inputs.flake-utils.url = "github:numtide/flake-utils";
 
   outputs = { nixpkgs, flake-utils, ... }:
-    flake-utils.lib.eachDefaultSystem
-      (system:
-        let pkgs = nixpkgs.legacyPackages.${system};
-        in
-        {
-          devShells.default =
-            pkgs.mkShell {
-              buildInputs = with pkgs; with nodePackages_latest; [
+    flake-utils.lib.eachDefaultSystem (system:
+      let pkgs = nixpkgs.legacyPackages.${system};
+      in
+      rec {
+        packages.erratic-gate = pkgs.buildNpmPackage {
+          name = "erratic-gate";
+
+          buildInputs = [ pkgs.nodejs_18 ];
+
+          src = pkgs.lib.cleanSource ./.;
+          npmDepsHash = "sha256-f5mw6IjkhZgsIuzCz9d7DvoAdceY1y+yWXn1BOonsVI=";
+          npmBuild = "npm run build";
+
+          installPhase = ''
+            mkdir $out
+            cp -r dist $out/www
+          '';
+        };
+
+        packages.default = packages.erratic-gate;
+
+        devShells.default =
+          pkgs.mkShell {
+            buildInputs = with pkgs;
+              with nodePackages_latest; [
                 nodejs
               ];
-            };
-        }
-      );
+          };
+
+        apps.compute-npm-dep-hash = {
+          type = "app";
+          program = pkgs.lib.getExe (pkgs.writeShellApplication {
+            name = "generate-layout-previes";
+            runtimeInputs = [ pkgs.prefetch-npm-deps ];
+            text = "prefetch-npm-deps ./package-lock.json";
+          });
+        };
+      }
+    );
 
   # {{{ Caching and whatnot
   nixConfig = {
@@ -29,3 +55,4 @@
   };
   # }}}
 }
+
diff --git a/public/robots.txt b/public/robots.txt
deleted file mode 100644
index 3914151..0000000
--- a/public/robots.txt
+++ /dev/null
@@ -1,4 +0,0 @@
-User-agent: *
-Disallow: /server.js
-Disallow: /js
-Disallow: /css
\ No newline at end of file