Open-source gym & body-weight tracker, now in the App Store (iPhone + Apple Watch) and as an Android APK. Plan routines, log workouts, see which muscles are trained, import from Strong/Hevy/FitNotes. Self-host it or use it on the phone alone.
JavaScript apple-watchbodyweightdockerfitnessfitness-trackergymhealthiosmcpnodejspasskeysprogressive-web-applicationspwareactself-hostedself-hostingvitewebauthnweightliftingworkout-tracker
Mobile build: fail when the synced bundle is not the phone build
Duarte Santos50e92c13ba83
diff --git a/frontend/package.json b/frontend/package.jsonindex 91e463b40..9eb6ead2d 100644--- a/frontend/package.json+++ b/frontend/package.json@@ -6,7 +6,7 @@"scripts": {"dev": "vite","build": "vite build",- "build:mobile": "VITE_MOBILE=1 VITE_IMG_BASE=https://cdn.jsdelivr.net/gh/hasaneyldrm/exercises-dataset@7455efae41b330c265e7cd4b78dfa848e7ce5ebd/images/ VITE_GIF_BASE=https://cdn.jsdelivr.net/gh/hasaneyldrm/exercises-dataset@7455efae41b330c265e7cd4b78dfa848e7ce5ebd/videos/ vite build && cap sync",+ "build:mobile": "VITE_MOBILE=1 VITE_IMG_BASE=https://cdn.jsdelivr.net/gh/hasaneyldrm/exercises-dataset@7455efae41b330c265e7cd4b78dfa848e7ce5ebd/images/ VITE_GIF_BASE=https://cdn.jsdelivr.net/gh/hasaneyldrm/exercises-dataset@7455efae41b330c265e7cd4b78dfa848e7ce5ebd/videos/ vite build && cap sync && node scripts/check-mobile-bundle.mjs","preview": "vite preview","test": "vitest run","test:watch": "vitest",diff --git a/frontend/scripts/check-mobile-bundle.mjs b/frontend/scripts/check-mobile-bundle.mjsnew file mode 100644index 000000000..7d7fa3772--- /dev/null+++ b/frontend/scripts/check-mobile-bundle.mjs@@ -0,0 +1,52 @@+#!/usr/bin/env node+/* Run after `npm run build:mobile`'s `cap sync`: is what went into the native shells the phone+ * build, whole?+ *+ * A web `vite build` that runs into the same dist/ between the mobile build and `cap sync` (two+ * builds in one worktree) leaves the web index.html beside the mobile chunks, and the APK then+ * opens the web app: no Android-only settings, a service worker, the passkey login screen. Each+ * copy is checked for+ * - the flavour tag the mobile build writes into index.html (vite.config.js, `opengym-flavor`),+ * - every script and stylesheet index.html loads, present beside it,+ * - a stamped service worker (no `__BUILD__` left in sw.js).+ *+ * Usage: node scripts/check-mobile-bundle.mjs [frontend dir] — exit code 1 on any problem. */+import { existsSync, readFileSync } from 'node:fs'+import { dirname, join, resolve } from 'node:path'+import { fileURLToPath } from 'node:url'++const COPIES = [+ 'dist',+ 'android/app/src/main/assets/public',+ 'ios/App/App/public',+]++export function checkCopy(dir) {+ const problems = []+ const html = join(dir, 'index.html')+ if (!existsSync(html)) return [`${dir}: no index.html`]+ const text = readFileSync(html, 'utf8')+ if (!/<meta\s+name="opengym-flavor"\s+content="mobile"/.test(text))+ problems.push(`${dir}/index.html is not the mobile build (no opengym-flavor meta) — another build ran into dist/?`)+ for (const [, ref] of text.matchAll(/(?:src|href)="\.?\/?(assets\/[^"]+\.(?:js|css))"/g))+ if (!existsSync(join(dir, ref))) problems.push(`${dir}/index.html loads ${ref}, which is missing`)+ const sw = join(dir, 'sw.js')+ if (existsSync(sw) && readFileSync(sw, 'utf8').includes('__BUILD__'))+ problems.push(`${dir}/sw.js is not stamped (__BUILD__ left in it)`)+ return problems+}++export function checkMobileBundle(root) {+ const copies = COPIES.map(rel => join(root, rel)).filter((dir, i) => i === 0 || existsSync(dir))+ return copies.flatMap(checkCopy)+}++if (process.argv[1] && resolve(process.argv[1]) === fileURLToPath(import.meta.url)) {+ const root = resolve(process.argv[2] || join(dirname(fileURLToPath(import.meta.url)), '..'))+ const problems = checkMobileBundle(root)+ if (problems.length) {+ for (const p of problems) console.error('✗ ' + p)+ process.exit(1)+ }+ console.log('Mobile bundle OK in ' + COPIES.filter(rel => existsSync(join(root, rel))).join(', ') + '.')+}diff --git a/frontend/scripts/check-mobile-bundle.test.mjs b/frontend/scripts/check-mobile-bundle.test.mjsnew file mode 100644index 000000000..ede977b6d--- /dev/null+++ b/frontend/scripts/check-mobile-bundle.test.mjs@@ -0,0 +1,57 @@+/* check-mobile-bundle.mjs: the guard `npm run build:mobile` runs after `cap sync`. The broken APK+ * it exists for had the web index.html (a second build into the same dist/) beside the mobile+ * chunks; these fixtures are that tree and the good one. */+import { describe, expect, it } from 'vitest'+import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'+import { tmpdir } from 'node:os'+import { join } from 'node:path'+import { checkMobileBundle } from './check-mobile-bundle.mjs'++const MOBILE_HTML = '<head><meta name="opengym-flavor" content="mobile"><script type="module" crossorigin src="./assets/index-BKpgfSzf.js"></script><link rel="stylesheet" href="./assets/index-x.css"></head>'+const WEB_HTML = '<head><script type="module" crossorigin src="./assets/index-C_XIl0Rk.js"></script><link rel="stylesheet" href="./assets/index-x.css"></head>'++const tree = (files) => {+ const dir = mkdtempSync(join(tmpdir(), 'check-mobile-'))+ for (const [path, body] of Object.entries(files)) {+ mkdirSync(join(dir, path, '..'), { recursive: true })+ writeFileSync(join(dir, path), body)+ }+ return dir+}+const copy = (prefix, html, { sw = 'const CACHE = "og-1a2b3c"' } = {}) => ({+ [`${prefix}/index.html`]: html,+ [`${prefix}/assets/index-BKpgfSzf.js`]: '',+ [`${prefix}/assets/index-C_XIl0Rk.js`]: '',+ [`${prefix}/assets/index-x.css`]: '',+ [`${prefix}/sw.js`]: sw,+})+const check = files => {+ const dir = tree(files)+ try { return checkMobileBundle(dir).map(p => p.replace(dir + '/', '')) }+ finally { rmSync(dir, { recursive: true, force: true }) }+}++describe('check-mobile-bundle', () => {+ it('passes a mobile build synced into android', () => {+ expect(check({ ...copy('dist', MOBILE_HTML), ...copy('android/app/src/main/assets/public', MOBILE_HTML) })).toEqual([])+ })++ it('fails when the synced index.html is the web build (two builds into one dist/)', () => {+ const problems = check({ ...copy('dist', WEB_HTML), ...copy('android/app/src/main/assets/public', WEB_HTML) })+ expect(problems.some(p => p.startsWith('android/app/src/main/assets/public/index.html is not the mobile build'))).toBe(true)+ })++ it('fails when index.html loads a chunk that is not there', () => {+ const files = copy('dist', MOBILE_HTML)+ delete files['dist/assets/index-BKpgfSzf.js']+ expect(check(files)).toEqual(['dist/index.html loads assets/index-BKpgfSzf.js, which is missing'])+ })++ it('fails on an unstamped service worker', () => {+ expect(check(copy('dist', MOBILE_HTML, { sw: 'const CACHE = "og-__BUILD__"' }))).toEqual(['dist/sw.js is not stamped (__BUILD__ left in it)'])+ })++ it('fails when there is no build at all', () => {+ expect(check({})).toEqual(['dist: no index.html'])+ })+})diff --git a/frontend/vite.config.js b/frontend/vite.config.jsindex 8da9c724a..1469f7fec 100644--- a/frontend/vite.config.js+++ b/frontend/vite.config.js@@ -1,5 +1,7 @@import { readFileSync, writeFileSync, existsSync } from 'node:fs'import { createHash } from 'node:crypto'+import { join, resolve } from 'node:path'+import { fileURLToPath } from 'node:url'import { defineConfig } from 'vite'import react from '@vitejs/plugin-react'@@ -34,18 +36,33 @@ const umami = {// placeholder): a deploy is then a new worker with its own cache, and the previous build's// shell and chunks are dropped on activate instead of piling up under one fixed name. The// stamp is a hash of the built index.html — it changes exactly when the bundle does.+// The directory is the one this build wrote to (`--outDir` included), not a fixed ./dist/: a+// build into a private outDir left `__BUILD__` unstamped in its sw.js.+let outDir = fileURLToPath(new URL('./dist/', import.meta.url))const swStamp = {name: 'opengym-sw-stamp',apply: 'build',+ configResolved(config) { outDir = resolve(config.root, config.build.outDir) },closeBundle() {- const dir = new URL('./dist/', import.meta.url)- const html = new URL('index.html', dir), sw = new URL('sw.js', dir)+ const html = join(outDir, 'index.html'), sw = join(outDir, 'sw.js')if (!existsSync(html) || !existsSync(sw)) returnconst stamp = createHash('sha256').update(readFileSync(html)).digest('hex').slice(0, 10)writeFileSync(sw, readFileSync(sw, 'utf8').replace('__BUILD__', stamp))}}+// The phone flavour says so in its index.html. A web build that ran into the same dist/ between+// the mobile build and `cap sync` (two builds in one worktree) shipped the web shell inside an+// APK: no Android-only settings, a service worker, the passkey login. scripts/check-mobile-bundle.mjs+// reads this tag in the synced copy after `cap sync` and fails the build without it.+const flavor = {+ name: 'opengym-flavor',+ transformIndexHtml() {+ if (process.env.VITE_MOBILE !== '1') return+ return [{ tag: 'meta', attrs: { name: 'opengym-flavor', content: 'mobile' }, injectTo: 'head' }]+ }+}+// The version people are asked for in #install-help and on every bug report. Read from// package.json so it cannot drift from the release it was built in, and inlined at build// time so no runtime fetch is involved.@@ -60,7 +77,7 @@ const appVersion = process.env.APP_BUILD ? `${pkgVersion}+${process.env.APP_BUILexport default defineConfig({define: { __APP_VERSION__: JSON.stringify(appVersion) },- plugins: [react(), umami, swStamp],+ plugins: [react(), umami, flavor, swStamp],base: './',server: {// The Coach's core (payload, validator, prompts, HTTP adapters) lives in ../api/coach/core