fighting-konoha/tools/build_precache.py

22 lines
2.0 KiB
Python

# Rebuilds precache.js and files.json. Run from anywhere: python3 tools/build_precache.py
import os,json,hashlib,subprocess
os.chdir(os.path.join(os.path.dirname(os.path.abspath(__file__)),'..'))
# every top-level page, style and script (new files like feel.js/meta.js are picked up automatically)
root=sorted(f for f in os.listdir('.') if f.endswith(('.html','.css','.js')) and f not in ('sw.js','precache.js'))
# data files under content/ (kutipan, dst.) are runtime files too
content=sorted('content/'+f for f in os.listdir('content') if f.endswith(('.js','.json'))) if os.path.isdir('content') else []
tracked=subprocess.run(['git','ls-files','assets'],capture_output=True,text=True).stdout.split('\n')
others=subprocess.run(['git','ls-files','--others','--exclude-standard','assets'],capture_output=True,text=True).stdout.split('\n')
assets=sorted({f for f in tracked+others if f and os.path.isfile(f)})
# PWA: manifest (ekstensi .webmanifest tidak ikut daftar di atas) dan ikon assets/pwa; ikon dipastikan ikut meski git tidak tersedia. offline.html, pwa.js, touchlayout.* ikut lewat daftar root.
extra=['manifest.webmanifest']
pwa=sorted('assets/pwa/'+f for f in os.listdir('assets/pwa') if not f.startswith('.')) if os.path.isdir('assets/pwa') else []
files=[f for f in dict.fromkeys(root+extra+content+assets+pwa) if os.path.isfile(f)]
entries=[]
for f in files:
b=open(f,'rb').read(); entries.append([f,len(b),hashlib.sha1(b).hexdigest()[:12]])
total=sum(e[1] for e in entries); ver=hashlib.sha1(''.join(e[0]+e[2] for e in entries).encode()).hexdigest()[:12]
open('precache.js','w').write('/* Generated by tools/build_dist.mjs (regenerated by hand for Konoha Fighting): runtime files [path, bytes, sha1] for preload.js and sw.js. Do not edit. */\nself.AETHER_PRECACHE = '+json.dumps({'version':ver,'total':total,'files':entries},separators=(',',':'))+';\n')
open('files.json','w').write(json.dumps({'files':len(files),'bytes':total,'list':[{'file':f,'bytes':b} for f,b,_ in entries]},indent=2)+'\n')
print(len(files),'files',round(total/1e6,1),'MB; ver',ver)