Skip to content

Commit 3663253

Browse files
committed
copy fonts to source and serve local fonts
1 parent 5d23a61 commit 3663253

4 files changed

Lines changed: 62 additions & 2 deletions

File tree

freshen.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@
5555
},
5656
{
5757
"name": "piped",
58+
"update_scripts": [{
59+
"attr_path": "piped-frontend.passthru.fonts"
60+
}],
5861
"tests": [{
5962
"attr_path": "piped-test",
6063
"disable_sandbox": true

piped-frontend/default.nix

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
, lib
33
, buildNpmPackage
44
, pnpm
5+
, fetchurl
6+
, writeScript
57
}:
68

79
let
@@ -16,6 +18,17 @@ let
1618

1719
pnpmDeps = doPnpmDeps (builtins.fromJSON (builtins.readFile ./npmDepsHash.json));
1820

21+
22+
fonts = builtins.mapAttrs (url: hash: fetchurl {inherit url hash;}) (builtins.fromJSON (builtins.readFile ./fonts.json));
23+
copyFonts = builtins.concatStringsSep "\n" (lib.mapAttrsToList (
24+
url: font: let
25+
fontPath = "dist/fonts/" + (builtins.substring (builtins.stringLength "https://fonts.gstatic.com/") (-1) url);
26+
in ''
27+
mkdir -p $(dirname ${fontPath})
28+
cp ${font} ${fontPath}
29+
''
30+
)
31+
fonts);
1932
in
2033

2134
buildNpmPackage {
@@ -25,12 +38,38 @@ buildNpmPackage {
2538
npmConfigHook = pnpm.configHook;
2639

2740
npmDeps = pnpmDeps;
41+
42+
postBuild = ''
43+
mkdir -p dist/fonts
44+
45+
${copyFonts}
46+
47+
sed -i "s#https://fonts.gstatic.com#/fonts#g" dist/assets/*
48+
'';
2849

2950
installPhase = ''
3051
cp dist $out -r
3152
'';
32-
33-
passthru.hashUpdate = doPnpmDeps "";
53+
54+
passthru = {
55+
hashUpdate = doPnpmDeps "";
56+
fonts = writeScript "update-font-hashes" ''
57+
#!/usr/bin/env bash
58+
59+
tmp=$(mktemp -d)
60+
cp -r "${src}" "$tmp/src"
61+
chmod -R 750 $tmp/src
62+
(
63+
cd "$tmp/src"
64+
${pnpm}/bin/pnpm install
65+
echo "extracting"
66+
bash ${./extract-font-hashes.sh} > fonts.json
67+
cat fonts.json
68+
)
69+
cp "$tmp/src/fonts.json" "piped-frontend/fonts.json"
70+
rm -fr "$tmp"
71+
'';
72+
};
3473

3574
meta = {
3675
homepage = "https://github.com/TeamPiped/piped";
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env nix-shell
2+
#!nix-shell -i bash -p curl nix
3+
4+
fonts=($(grep -hPo "https://fonts\.gstatic\.com/s[^)]*" -r node_modules/ | sort | uniq))
5+
echo '{'
6+
for font in "${fonts[@]}"; do
7+
hash=$(nix-hash --to-sri --type sha256 $(nix-prefetch-url "$font" 2>/dev/null))
8+
if [ "$font" = "${fonts[-1]}" ]; then
9+
echo ' "'"$font"'"': '"'"$hash"'"'
10+
else
11+
echo ' "'"$font"'"': '"'"$hash"'",'
12+
fi
13+
done
14+
echo '}'

piped-frontend/fonts.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"https://fonts.gstatic.com/s/materialiconsround/v108/LDItaoyNOAY6Uewc665JcIzCKsKc_M9flwmM.otf": "sha256-utheVFS2KIEEzgOAbDcyO82PFF4wlOcnhgFzrIyRBi4=",
3+
"https://fonts.gstatic.com/s/roboto/v47/KFOMCnqEu92Fr1ME7kSn66aGLdTylUAMQXC89YmC2DPNWubEbVmUiA8.ttf": "sha256-Z6CSYilO1dB4PF8qsg2Wc2VMLePQOHBHN+v+noJu1hM="
4+
}

0 commit comments

Comments
 (0)