Files
zyt/identity-admin/.test-runtime/kf-batch-inline-20261105/package_frontend.py
T
2026-11-11 14:22:02 +08:00

48 lines
2.3 KiB
Python

import hashlib
import json
import shutil
import tarfile
from pathlib import Path
work = Path(__file__).resolve().parent
frontend = work / 'frontend'
build = json.loads((frontend / 'build-result.json').read_text('utf-8-sig'))
manifest = json.loads((frontend / 'frontend-manifest.json').read_text('utf-8-sig'))
assert all(build[name] == 0 for name in ('typecheck_exit', 'build_exit', 'token_check_exit'))
dist = Path(build['output']).resolve()
assert dist == Path(manifest['build_output']).resolve()
assert dist == Path('C:/zyt/identity-admin/.test-runtime/kefu-admin-web/apps/web-antd/dist-batch-inline-20261105')
def sha(path):
return hashlib.sha256(path.read_bytes()).hexdigest()
assert sha(dist / 'index.html') == build['index_sha256'].lower()
payload = work / 'release-payload'
assert not payload.exists()
shutil.copytree(dist, payload / 'frontend')
static = payload / 'frontend'
configs = list(static.glob('_app-config-*.js'))
assert len(configs) == 1
previous_config = configs[0]
config = 'window._VBEN_ADMIN_PRO_APP_CONF_={"VITE_GLOB_API_URL":"/api/v2"};Object.freeze(window._VBEN_ADMIN_PRO_APP_CONF_);Object.defineProperty(window,"_VBEN_ADMIN_PRO_APP_CONF_",{configurable:false,writable:false});\n'
digest = hashlib.sha256(config.encode()).hexdigest()
public_config = previous_config.with_name(previous_config.stem + '-batch-inline-' + digest[:12] + '.js')
public_config.write_text(config, encoding='utf-8', newline='\n')
index = static / 'index.html'
entry = index.read_text('utf-8')
assert entry.count(previous_config.name) == 1
index.write_text(entry.replace(previous_config.name, public_config.name), encoding='utf-8', newline='\n')
previous_config.unlink()
files = [dict(path=p.relative_to(static).as_posix(), sha256=sha(p)) for p in sorted(static.rglob('*')) if p.is_file()]
assert len(files) == build['output_files']
(payload / 'manifest.json').write_text(json.dumps(files, indent=2), encoding='utf-8')
archive = work / 'batch-inline-20261105.tgz'
with tarfile.open(archive, 'w:gz') as tar:
for p in sorted(payload.rglob('*')):
if p.is_file():
tar.add(p, arcname=p.relative_to(payload).as_posix(), recursive=False)
release = {'sha256': sha(archive), 'frontend_files': len(files), 'entry_sha256': sha(index), 'public_config': public_config.name}
(work / 'release.json').write_text(json.dumps(release, indent=2), encoding='utf-8')
print(json.dumps(release))