Merge remote-tracking branch 'origin/main' into flaky-console

# Conflicts:
#	tests/site-overflow.test.ts
This commit is contained in:
CRTkafa 2026-10-12 04:54:13 +08:00
commit a7569deb26
68 changed files with 3422 additions and 385 deletions

View file

@ -2,6 +2,73 @@
## Unreleased ## Unreleased
- Local font files in `shots.fonts`: `title`, `body` and `fallback[]` take, besides a Google Fonts family, a path
(relative to the config) or `{ family, file, weight?, style?, license? }` (TTF / OTF / WOFF / WOFF2, validated by
extension and signature; the family comes from the name table when not given). `shots fonts` checks the file's cmap
per language, tries the listed fallbacks in order before the automatic Noto ones (`pl title: ę ł ą Ł not in ER
Display -> Noto Sans Display`), errors when nothing covers a character, and records the file in fonts.json with its
path, sha256 and license. Render and `shots video` serve it in place at `/fonts/local/...` (never copied); the hash
in fonts.css invalidates the capture cache, and render refuses a font file changed since `shots fonts`. `shots check`
treats the local font and listed fallbacks as configured and warns when a local font has no license note.
- Idempotency: every store write is an upsert; running a command twice, or again after a crash or a lost answer,
leaves exactly one of each thing (table of every write path: `docs/idempotency.md`).
- New `cli/lib/upsert.ts`: `findOrCreate` looks again with a fresh GET right before a create and asks a journal of
recent creates (`.storebound/created.json`, a direct GET by id) so a re-run during the store's list lag reuses what
the last run made; `createOnce` treats 409 / ALREADY_EXISTS as found and never sends a create blindly again after
a timeout or 5xx (it looks first; only keys the store keeps unique get one more try).
- The App Store Connect, Google Play, RevenueCat and `storebound api` clients retry 5xx / network errors only for
requests that are safe twice (GET, PUT, PATCH, DELETE, relationship adds, state actions, price schedules); a POST
create without a clear answer throws `AmbiguousWriteError`. Plan runners treat a 409 duplicate as done and stop on
an unclear create instead of retrying.
- Uses it in `apple iap`, `apple subscriptions` (groups, subscriptions, localizations, promotional offers, offer code
campaigns, custom codes, review screenshot by MD5), `apple offer-codes`, `apple cpp create`, `apple events create`,
`apple ppo create`, `apple testflight groups`, `apple bundle`, `apple review-details` (only changed fields),
`apple release --phased on`, `apple adhoc register` / `profile`, `apple signing ensure` (a lost certificate answer
keeps the key and finds the certificate by its public key), `apple upload` (a COMPLETE build upload is not sent
again, an unfinished one is resumed), `analytics setup`, `reviews send`, `revenuecat sync`, `play subscriptions`
(subscriptions and offers).
- Review submissions (`apple iap-submit`, `cpp submit`, `events submit`): the draft is reused or created once, an
item already in it is not added again.
- Screenshot and preview sets keep the leading files that already match (MD5, position, delivered), replace the rest
in order and fix the order if Apple shows another; a crash midway converges on the next run without duplicates.
- Play edits: one open at a time per run, deleted on every error path, committed only on success, deleted instead of
committed when nothing changed (`ctl.unchanged()`); redone only when nothing went live, never after a commit
without a clear answer. `play images` compares sha256 first and replaces only changed image types; `play release`
reuses a versionCode Play already has and skips identical track releases; `play rollout` commits nothing when the
track is already like that; `play upload` is done when the versionCode is on the track already.
- One writer per app and store: write runs take `.storebound/locks/<store>-<app>.lock` (dry runs and reads do not);
a second run stops with who holds it; a lock of a process that is gone is taken over; `--force-unlock`.
- `tests/idempotency.test.ts` with stateful store doubles (`tests/helpers/fake-stores.ts`): write commands run twice
with zero writes on the second run, a crash in the middle of a screenshot set, IAP and subscription creates whose
answer is lost, 409 duplicates, the list lag after a crash, Play edit error paths and an unclear commit, the lock.
Pitfall 9 in `docs/pitfalls.md` and the store-pitfalls skill.
- The last open rows are closed (`docs/idempotency.md` has none left):
- `apple listing`: the App Store version is created through `findOrCreate` (a fresh look right before the create,
the journal for the list lag, a lost answer looks first); localizations through `createOnce` (409 = found, then
only the differing fields are PATCHed); unchanged localizations and an attached build are left alone.
- `play listing`: compares again inside the write edit and PUTs only differing details and languages; nothing left
to change = the edit is deleted, not committed.
- `apple appinfo`: categories, age rating, content rights compared with the live values, only differences PATCHed.
- `play data-safety`: Play cannot read the form back, so the sha256 of the last uploaded CSV is kept in
`.storebound/data-safety.sha256`; the same CSV is not sent again unless `--force` (MCP `force`).
- Twice-run tests for `apple listing` (plus a lost version create and a 409 localization), `play listing` (plus a
store that became current before the edit), `apple appinfo`, `play data-safety`, `apple promoted`,
`apple win-back`, `apple preorder`, `apple testflight testers`, `play upload` and `prices import` (Play).
- Faster `shots capture` (web) and `shots render` (a 22-language game took about 50 min to capture, serially, with a
new context and a full game boot per frame):
- `--concurrency N` (and `shots.capture.concurrency`): parallel browser contexts in one Chromium; defaults from the
CPU count (capture min(3, cores / 4), render min(4, cores / 2)). Output files do not depend on the order; progress
lines carry language / device / frame, n/total and an ETA. Render runs ffmpeg asynchronously and makes the contact
sheets after the images.
- Per-frame timing (goto, waitFor, eval, delay, screenshot, total) in each progress line and in
`.storebound/capture-report.json`, with time per step and the slowest frames at the end.
- `shots.capture.session: 'per-language'`: one page per language and device, booted once; shots switch with
`navigate: 'spa'` (pushState + popstate, then waitFor / eval) or `'reload'`; a failed precondition falls back to a
fresh page (`reuseTimeoutMs`).
- Input-hash cache (`.storebound/capture-cache.json`: shot entry, URL, frame size, session, copy text, app build from
`shots.capture.build` or the served HTML + scripts, fonts): unchanged frames are skipped; `--only-missing`,
`--force`. MCP `shots_capture` (`concurrency`, `mode`) and `shots_render` (`concurrency`).
- `apple listing --dry`, `play listing --dry` and `apple appinfo --dry` print a real plan (also `--json`), read-only: - `apple listing --dry`, `play listing --dry` and `apple appinfo --dry` print a real plan (also `--json`), read-only:
the version decision of `apple listing` (keep, update the editable version's string while PREPARE_FOR_SUBMISSION, the version decision of `apple listing` (keep, update the editable version's string while PREPARE_FOR_SUBMISSION,
create, or refuse: number taken, not higher than a reviewed version, another version in review / pending release), create, or refuse: number taken, not higher than a reviewed version, another version in review / pending release),

View file

@ -430,9 +430,9 @@ run in the background with a log (`storebound shots render > .storebound/render.
| command | kind | what | | command | kind | what |
|---|---|---| |---|---|---|
| `shots capture [--source web\|ios-sim\|android-emu\|folder]` | local | raw frames from your running app or game | | `shots capture [--source web\|ios-sim\|android-emu\|folder] [--concurrency N] [--only-missing \| --force]` | local | raw frames from your running app or game; web: parallel contexts, optional one boot per language (`capture.session`), skips unchanged frames, timing per step |
| `shots fonts` | network | font subsets for your copy (Noto for CJK / Arabic / Cyrillic / Devanagari ...) and the glyph coverage check per language | | `shots fonts` | network | font subsets for your copy (Noto for CJK / Arabic / Cyrillic / Devanagari ...), your own local font files, and the glyph coverage check per language |
| `shots render [--variant b \| --variants]` | local | every store size and language in the game or app style, feature graphic, contact sheet, overflow report | | `shots render [--variant b \| --variants] [--concurrency N]` | local | every store size and language in the game or app style, feature graphic, contact sheet, overflow report |
| `shots jpeg` | local | JPEG q92 copies of the Play renders (about 4x faster uploads) | | `shots jpeg` | local | JPEG q92 copies of the Play renders (about 4x faster uploads) |
| `shots check [--variants] [--json]` | local, read-only | store rules: sizes, alpha, format, bytes, counts, overflow, fallback / missing glyphs, videos, folder frames | | `shots check [--variants] [--json]` | local, read-only | store rules: sizes, alpha, format, bytes, counts, overflow, fallback / missing glyphs, videos, folder frames |
| `shots gallery` | local | `<renders>/index.html` review page | | `shots gallery` | local | `<renders>/index.html` review page |
@ -727,6 +727,23 @@ languages, every store size, a review gallery and an app preview video. No store
Some display fonts lack Latin Extended: Lilita One has no ş ğ İ ą ę ń ś ź ż ć ă ș ț č ř ě ů ő ű or Vietnamese, Some display fonts lack Latin Extended: Lilita One has no ş ğ İ ą ę ń ś ź ż ć ă ș ț č ř ě ů ő ű or Vietnamese,
so Turkish, Polish, Czech, Romanian, Hungarian or Vietnamese titles need another title font (Paytone One, Baloo 2, so Turkish, Polish, Czech, Romanian, Hungarian or Vietnamese titles need another title font (Paytone One, Baloo 2,
Rowdies) or `fonts: { ..., fallback: ['Noto Sans Display'] }` to accept the mixed glyphs. Rowdies) or `fonts: { ..., fallback: ['Noto Sans Display'] }` to accept the mixed glyphs.
**Your own brand font.** `shots.fonts.title` / `body` / `fallback[]` also take a local file (relative to the
config; TTF, OTF, WOFF or WOFF2, checked by extension and signature) or `{ family, file, weight?, style?, license? }`:
```ts
fonts: {
title: { family: 'ER Display', file: 'apps/game/public/assets/fonts/er-display.woff2', license: 'OFL-1.1' },
body: 'Montserrat',
fallback: ['Noto Sans Display'], // tried in order, before the automatic Noto fallbacks
},
```
`shots fonts` runs the same cmap check on the file (`pl title: ę ł ą Ł not in ER Display -> Noto Sans Display`),
prints its size, glyph count, sha256 and license, and records it in `fonts.json`. The file is never copied: render
(and `shots video` captions) serve it from where it is, and a changed file changes `fonts.css` (the capture cache
misses) and makes `shots render` ask for `shots fonts` again. `shots check` counts the local font and listed
fallbacks as configured, not as fallbacks, and warns when a local font has no `license` note.
3. **Render.** Every store size at exact pixels: iPhone 6.9" and 6.5", 13" iPad (if your app runs on iPad), Play 3. **Render.** Every store size at exact pixels: iPhone 6.9" and 6.5", 13" iPad (if your app runs on iPad), Play
phone, 7" and 10" tablets, the 1024 x 500 feature graphic. 24-bit RGB, no alpha (both stores reject it). phone, 7" and 10" tablets, the 1024 x 500 feature graphic. 24-bit RGB, no alpha (both stores reject it).
4. **Check and review.** `shots check` applies the store rules locally (sources cited in `cli/shots/spec.ts`); 4. **Check and review.** `shots check` applies the store rules locally (sources cited in `cli/shots/spec.ts`);

2
bunfig.toml Normal file
View file

@ -0,0 +1,2 @@
[test]
preload = ["./tests/helpers/preload.ts"]

View file

@ -10,6 +10,7 @@
// text, screenshots, config): the days before vs after each change. Read-only on both stores except `setup`. // text, screenshots, config): the days before vs after each change. Read-only on both stores except `setup`.
import { existsSync, mkdirSync, readdirSync, readFileSync, writeFileSync } from 'node:fs'; import { existsSync, mkdirSync, readdirSync, readFileSync, writeFileSync } from 'node:fs';
import { join } from 'node:path'; import { join } from 'node:path';
import { createOnce } from './lib/upsert';
import { ascClient, type Asc, type J } from './apple/asc'; import { ascClient, type Asc, type J } from './apple/asc';
import type { Ctx } from './lib/ctx'; import type { Ctx } from './lib/ctx';
import { import {
@ -102,7 +103,9 @@ export async function run(ctx: Ctx): Promise<void> {
if (cur) { done(TAG, `apple: ONGOING report request exists (${cur.id})`); return; } if (cur) { done(TAG, `apple: ONGOING report request exists (${cur.id})`); return; }
const body = { data: { type: 'analyticsReportRequests', attributes: { accessType: 'ONGOING' }, relationships: { app: { data: { type: 'apps', id: appId } } } } }; const body = { data: { type: 'analyticsReportRequests', attributes: { accessType: 'ONGOING' }, relationships: { app: { data: { type: 'apps', id: appId } } } } };
if (dry) { log(TAG, 'would POST /v1/analyticsReportRequests {accessType=ONGOING} (dry)'); return; } if (dry) { log(TAG, 'would POST /v1/analyticsReportRequests {accessType=ONGOING} (dry)'); return; }
await c.asc('POST', '/v1/analyticsReportRequests', body); // one ONGOING request per app: a 409 or an unclear answer is checked by reading it back, never re-sent blindly
await createOnce({ what: 'ONGOING analytics report request', tag: TAG, unique: true, find: () => ongoingRequest(c, appId),
create: async () => (await c.asc('POST', '/v1/analyticsReportRequests', body)).data });
done(TAG, 'apple: ONGOING report request created; the first reports appear after 1-2 days, then daily'); done(TAG, 'apple: ONGOING report request created; the first reports appear after 1-2 days, then daily');
return; return;
} }

View file

@ -1,8 +1,10 @@
// Generic client for every App Store Connect and Google Play operation: the generated rows (apple.gen.ts, play.gen.ts) // Generic client for every App Store Connect and Google Play operation: the generated rows (apple.gen.ts, play.gen.ts)
// plus a few helpers instead of a function per endpoint. Request building is pure (tests cover URLs and parameters); // plus a few helpers instead of a function per endpoint. Request building is pure (tests cover URLs and parameters);
// sending retries 429 / 5xx with backoff (Retry-After honoured) and follows pages. // sending retries 429 (and 5xx / network errors of requests that are safe to send twice: not a POST create) with
// backoff (Retry-After honoured) and follows pages.
// Auth: the App Store Connect team key JWT from cli/apple/asc.ts, the Play service account token from // Auth: the App Store Connect team key JWT from cli/apple/asc.ts, the Play service account token from
// cli/lib/google-auth.ts (one scope per Google API). Credentials only come from env or .storebound/. // cli/lib/google-auth.ts (one scope per Google API). Credentials only come from env or .storebound/.
import { AmbiguousWriteError, retrySafe } from '../lib/upsert';
import { APPLE_OPS, APPLE_SPEC } from './apple.gen'; import { APPLE_OPS, APPLE_SPEC } from './apple.gen';
import { PLAY_APIS, PLAY_OPS } from './play.gen'; import { PLAY_APIS, PLAY_OPS } from './play.gen';
import type { AppleBody, AppleOp, AppleParams, AppleResponse, ParamValue, PlayBody, PlayOp, PlayParams, PlayResponse } from './types'; import type { AppleBody, AppleOp, AppleParams, AppleResponse, ParamValue, PlayBody, PlayOp, PlayParams, PlayResponse } from './types';
@ -207,15 +209,20 @@ export async function send(req: BuiltRequest, auth: Auth, o: SendOptions = {}):
const headers: Record<string, string> = { Authorization: await auth(req) }; const headers: Record<string, string> = { Authorization: await auth(req) };
if (req.contentType) headers['Content-Type'] = req.contentType; if (req.contentType) headers['Content-Type'] = req.contentType;
if (req.accept) headers.Accept = req.accept; if (req.accept) headers.Accept = req.accept;
// 5xx and network errors are retried only for requests that end the same when sent twice: a POST create without
// a clear answer may have been applied, so it is reported (AmbiguousWriteError) and never sent again by itself
const safe = retrySafe(req.method, req.url);
let r: Response; let r: Response;
try { try {
r = await doFetch(req.url, { method: req.method, headers, body: req.body as BodyInit | undefined }); r = await doFetch(req.url, { method: req.method, headers, body: req.body as BodyInit | undefined });
} catch (e) { } catch (e) {
if (!safe) throw new AmbiguousWriteError(`${req.method} ${short(req.url)}: no answer (${(e as Error).message}); not sent again: it may have been applied, check with a GET before you run it again`);
if (attempt >= attempts) throw e; if (attempt >= attempts) throw e;
o.onRetry?.(`${req.method} ${short(req.url)}: ${(e as Error).message}, retry ${attempt}/${attempts - 1}`); o.onRetry?.(`${req.method} ${short(req.url)}: ${(e as Error).message}, retry ${attempt}/${attempts - 1}`);
await sleep(backoffMs(attempt)); await sleep(backoffMs(attempt));
continue; continue;
} }
if (r.status >= 500 && !safe) throw new AmbiguousWriteError(`${req.method} ${short(req.url)} -> ${r.status} ${errorBody(await r.text())}; not sent again: it may have been applied, check with a GET before you run it again`, r.status);
if ((r.status === 429 || r.status >= 500) && attempt < attempts) { if ((r.status === 429 || r.status >= 500) && attempt < attempts) {
const wait = backoffMs(attempt, r.headers.get('retry-after')); const wait = backoffMs(attempt, r.headers.get('retry-after'));
await r.body?.cancel(); await r.body?.cancel();

View file

@ -16,6 +16,7 @@ export const WRAPPED: { apple: Record<string, string>; play: Record<string, stri
'appCustomProductPageVersions_updateInstance': 'apple/cpp', 'appCustomProductPageVersions_updateInstance': 'apple/cpp',
'appCustomProductPages_appCustomProductPageVersions_getToManyRelated': 'apple/cpp', 'appCustomProductPages_appCustomProductPageVersions_getToManyRelated': 'apple/cpp',
'appCustomProductPages_createInstance': 'apple/cpp', 'appCustomProductPages_createInstance': 'apple/cpp',
'appCustomProductPages_getInstance': 'apple/cpp',
'appCustomProductPages_updateInstance': 'apple/cpp', 'appCustomProductPages_updateInstance': 'apple/cpp',
'appEncryptionDeclarations_getCollection': 'apple/encryption', 'appEncryptionDeclarations_getCollection': 'apple/encryption',
'appEventLocalizations_appEventScreenshots_getToManyRelated': 'apple/events', 'appEventLocalizations_appEventScreenshots_getToManyRelated': 'apple/events',
@ -25,6 +26,7 @@ export const WRAPPED: { apple: Record<string, string>; play: Record<string, stri
'appEventScreenshots_deleteInstance': 'apple/events', 'appEventScreenshots_deleteInstance': 'apple/events',
'appEventScreenshots_updateInstance': 'apple/events', 'appEventScreenshots_updateInstance': 'apple/events',
'appEvents_createInstance': 'apple/events', 'appEvents_createInstance': 'apple/events',
'appEvents_getInstance': 'apple/events',
'appEvents_localizations_getToManyRelated': 'apple/events', 'appEvents_localizations_getToManyRelated': 'apple/events',
'appInfos_ageRatingDeclaration_getToOneRelated': 'apple/appinfo validate', 'appInfos_ageRatingDeclaration_getToOneRelated': 'apple/appinfo validate',
'appInfos_appInfoLocalizations_getToManyRelated': 'apple/listing validate', 'appInfos_appInfoLocalizations_getToManyRelated': 'apple/listing validate',
@ -32,23 +34,20 @@ export const WRAPPED: { apple: Record<string, string>; play: Record<string, stri
'appInfos_updateInstance': 'apple/appinfo', 'appInfos_updateInstance': 'apple/appinfo',
'appPreviewSets_appPreviews_getToManyRelated': 'apple/previews', 'appPreviewSets_appPreviews_getToManyRelated': 'apple/previews',
'appPreviewSets_createInstance': 'apple/previews', 'appPreviewSets_createInstance': 'apple/previews',
'appPreviews_createInstance': 'apple/previews',
'appPreviews_deleteInstance': 'apple/previews',
'appPreviews_updateInstance': 'apple/previews', 'appPreviews_updateInstance': 'apple/previews',
'appPriceSchedules_createInstance': 'apple/appinfo', 'appPriceSchedules_createInstance': 'apple/appinfo',
'appScreenshotSets_appScreenshots_getToManyRelated': 'apple/ppo apple/screenshots validate', 'appScreenshotSets_appScreenshots_getToManyRelated': 'apple/ppo apple/screenshots validate',
'appScreenshotSets_createInstance': 'apple/screenshots', 'appScreenshotSets_createInstance': 'apple/screenshots',
'appScreenshots_createInstance': 'apple/screenshots',
'appScreenshots_deleteInstance': 'apple/screenshots',
'appScreenshots_updateInstance': 'apple/screenshots',
'appStoreReviewDetails_createInstance': 'apple/review-details', 'appStoreReviewDetails_createInstance': 'apple/review-details',
'appStoreReviewDetails_updateInstance': 'apple/review-details', 'appStoreReviewDetails_updateInstance': 'apple/review-details',
'appStoreVersionExperimentTreatmentLocalizations_appScreenshotSets_getToManyRelated': 'apple/ppo', 'appStoreVersionExperimentTreatmentLocalizations_appScreenshotSets_getToManyRelated': 'apple/ppo',
'appStoreVersionExperimentTreatmentLocalizations_createInstance': 'lib/ppo', 'appStoreVersionExperimentTreatmentLocalizations_createInstance': 'lib/ppo',
'appStoreVersionExperimentTreatments_appStoreVersionExperimentTreatmentLocalizations_getToManyRelated': 'apple/ppo', 'appStoreVersionExperimentTreatments_appStoreVersionExperimentTreatmentLocalizations_getToManyRelated': 'apple/ppo',
'appStoreVersionExperimentTreatments_createInstance': 'lib/ppo', 'appStoreVersionExperimentTreatments_createInstance': 'lib/ppo',
'appStoreVersionExperimentTreatments_getInstance': 'apple/ppo',
'appStoreVersionExperimentsV2_appStoreVersionExperimentTreatments_getToManyRelated': 'apple/ppo', 'appStoreVersionExperimentsV2_appStoreVersionExperimentTreatments_getToManyRelated': 'apple/ppo',
'appStoreVersionExperimentsV2_createInstance': 'lib/ppo', 'appStoreVersionExperimentsV2_createInstance': 'lib/ppo',
'appStoreVersionExperimentsV2_getInstance': 'apple/ppo',
'appStoreVersionExperimentsV2_updateInstance': 'lib/ppo', 'appStoreVersionExperimentsV2_updateInstance': 'lib/ppo',
'appStoreVersionLocalizations_appPreviewSets_getToManyRelated': 'apple/previews', 'appStoreVersionLocalizations_appPreviewSets_getToManyRelated': 'apple/previews',
'appStoreVersionLocalizations_appScreenshotSets_getToManyRelated': 'apple/screenshots validate', 'appStoreVersionLocalizations_appScreenshotSets_getToManyRelated': 'apple/screenshots validate',
@ -61,6 +60,7 @@ export const WRAPPED: { apple: Record<string, string>; play: Record<string, stri
'appStoreVersions_build_getToOneRelated': 'apple/listing validate', 'appStoreVersions_build_getToOneRelated': 'apple/listing validate',
'appStoreVersions_build_updateToOneRelationship': 'apple/listing', 'appStoreVersions_build_updateToOneRelationship': 'apple/listing',
'appStoreVersions_createInstance': 'apple/listing', 'appStoreVersions_createInstance': 'apple/listing',
'appStoreVersions_getInstance': 'apple/listing',
'appStoreVersions_updateInstance': 'apple/listing', 'appStoreVersions_updateInstance': 'apple/listing',
'apps_analyticsReportRequests_getToManyRelated': 'analytics', 'apps_analyticsReportRequests_getToManyRelated': 'analytics',
'apps_appAvailabilityV2_getToOneRelated': 'apple/appinfo apple/preorder validate', 'apps_appAvailabilityV2_getToOneRelated': 'apple/appinfo apple/preorder validate',
@ -75,6 +75,7 @@ export const WRAPPED: { apple: Record<string, string>; play: Record<string, stri
'apps_betaAppReviewDetail_getToOneRelated': 'apple/testflight', 'apps_betaAppReviewDetail_getToOneRelated': 'apple/testflight',
'apps_betaFeedbackCrashSubmissions_getToManyRelated': 'health', 'apps_betaFeedbackCrashSubmissions_getToManyRelated': 'health',
'apps_betaGroups_getToManyRelated': 'apple/testflight', 'apps_betaGroups_getToManyRelated': 'apple/testflight',
'apps_buildUploads_getToManyRelated': 'apple/upload',
'apps_customerReviews_getToManyRelated': 'reviews', 'apps_customerReviews_getToManyRelated': 'reviews',
'apps_getCollection': 'apple/asc apple/bundle apps lib/store-apps', 'apps_getCollection': 'apple/asc apple/bundle apps lib/store-apps',
'apps_getInstance': 'apple/appinfo apple/status', 'apps_getInstance': 'apple/appinfo apple/status',
@ -98,11 +99,13 @@ export const WRAPPED: { apple: Record<string, string>; play: Record<string, stri
'betaGroups_builds_getToManyRelated': 'apple/testflight', 'betaGroups_builds_getToManyRelated': 'apple/testflight',
'betaGroups_createInstance': 'lib/testflight', 'betaGroups_createInstance': 'lib/testflight',
'betaGroups_getCollection': 'apple/testflight', 'betaGroups_getCollection': 'apple/testflight',
'betaGroups_getInstance': 'apple/testflight',
'betaTesterInvitations_createInstance': 'lib/testflight', 'betaTesterInvitations_createInstance': 'lib/testflight',
'betaTesters_createInstance': 'lib/testflight', 'betaTesters_createInstance': 'lib/testflight',
'betaTesters_getCollection': 'apple/testflight', 'betaTesters_getCollection': 'apple/testflight',
'buildUploadFiles_createInstance': 'lib/xcode', 'buildUploadFiles_createInstance': 'lib/xcode',
'buildUploadFiles_updateInstance': 'lib/xcode', 'buildUploadFiles_updateInstance': 'lib/xcode',
'buildUploads_buildUploadFiles_getToManyRelated': 'apple/upload',
'buildUploads_createInstance': 'lib/xcode', 'buildUploads_createInstance': 'lib/xcode',
'buildUploads_getInstance': 'apple/upload', 'buildUploads_getInstance': 'apple/upload',
'builds_appEncryptionDeclaration_getToOneRelated': 'validate', 'builds_appEncryptionDeclaration_getToOneRelated': 'validate',
@ -132,6 +135,7 @@ export const WRAPPED: { apple: Record<string, string>; play: Record<string, stri
'inAppPurchaseOfferCodeOneTimeUseCodes_values_getToOneRelated': 'apple/offer-codes', 'inAppPurchaseOfferCodeOneTimeUseCodes_values_getToOneRelated': 'apple/offer-codes',
'inAppPurchaseOfferCodes_createInstance': 'apple/offer-codes', 'inAppPurchaseOfferCodes_createInstance': 'apple/offer-codes',
'inAppPurchaseOfferCodes_customCodes_getToManyRelated': 'apple/offer-codes', 'inAppPurchaseOfferCodes_customCodes_getToManyRelated': 'apple/offer-codes',
'inAppPurchaseOfferCodes_getInstance': 'apple/offer-codes',
'inAppPurchaseOfferCodes_oneTimeUseCodes_getToManyRelated': 'apple/offer-codes', 'inAppPurchaseOfferCodes_oneTimeUseCodes_getToManyRelated': 'apple/offer-codes',
'inAppPurchasePricePoints_equalizations_getToManyRelated': 'apple/offer-codes', 'inAppPurchasePricePoints_equalizations_getToManyRelated': 'apple/offer-codes',
'inAppPurchasePriceSchedules_automaticPrices_getToManyRelated': 'prices', 'inAppPurchasePriceSchedules_automaticPrices_getToManyRelated': 'prices',
@ -141,6 +145,7 @@ export const WRAPPED: { apple: Record<string, string>; play: Record<string, stri
'inAppPurchaseVersions_createInstance': 'lib/review-submit', 'inAppPurchaseVersions_createInstance': 'lib/review-submit',
'inAppPurchasesV2_appStoreReviewScreenshot_getToOneRelated': 'apple/iap-review validate', 'inAppPurchasesV2_appStoreReviewScreenshot_getToOneRelated': 'apple/iap-review validate',
'inAppPurchasesV2_createInstance': 'apple/iap', 'inAppPurchasesV2_createInstance': 'apple/iap',
'inAppPurchasesV2_getInstance': 'apple/iap',
'inAppPurchasesV2_iapPriceSchedule_getToOneRelated': 'apple/iap apple/iap-review catalog', 'inAppPurchasesV2_iapPriceSchedule_getToOneRelated': 'apple/iap apple/iap-review catalog',
'inAppPurchasesV2_inAppPurchaseAvailability_getToOneRelated': 'apple/iap apple/iap-review', 'inAppPurchasesV2_inAppPurchaseAvailability_getToOneRelated': 'apple/iap apple/iap-review',
'inAppPurchasesV2_inAppPurchaseLocalizations_getToManyRelated': 'apple/iap apple/iap-review catalog', 'inAppPurchasesV2_inAppPurchaseLocalizations_getToManyRelated': 'apple/iap apple/iap-review catalog',
@ -153,17 +158,20 @@ export const WRAPPED: { apple: Record<string, string>; play: Record<string, stri
'profiles_getInstance': 'apple/signing', 'profiles_getInstance': 'apple/signing',
'promotedPurchases_createInstance': 'lib/offers', 'promotedPurchases_createInstance': 'lib/offers',
'promotedPurchases_updateInstance': 'lib/offers', 'promotedPurchases_updateInstance': 'lib/offers',
'reviewSubmissionItems_createInstance': 'apple/events lib/cpp-events lib/review-submit', 'reviewSubmissionItems_createInstance': 'apple/submission lib/cpp-events lib/review-submit',
'reviewSubmissions_createInstance': 'apple/cpp apple/events lib/review-submit', 'reviewSubmissions_createInstance': 'apple/cpp apple/events apple/submission lib/review-submit',
'reviewSubmissions_getCollection': 'apple/cpp apple/events apple/iap-submit apple/status', 'reviewSubmissions_getCollection': 'apple/events apple/iap-submit apple/status apple/submission',
'reviewSubmissions_items_getToManyRelated': 'apple/iap-submit', 'reviewSubmissions_getInstance': 'apple/submission',
'reviewSubmissions_items_getToManyRelated': 'apple/iap-submit apple/submission',
'reviewSubmissions_updateInstance': 'apple/cpp apple/events apple/iap-submit apple/status lib/review-submit', 'reviewSubmissions_updateInstance': 'apple/cpp apple/events apple/iap-submit apple/status lib/review-submit',
'salesReports_getCollection': 'lib/reports', 'salesReports_getCollection': 'lib/reports',
'subscriptionAppStoreReviewScreenshots_createInstance': 'apple/subscriptions', 'subscriptionAppStoreReviewScreenshots_createInstance': 'apple/subscriptions',
'subscriptionAppStoreReviewScreenshots_deleteInstance': 'apple/subscriptions',
'subscriptionAppStoreReviewScreenshots_updateInstance': 'apple/subscriptions', 'subscriptionAppStoreReviewScreenshots_updateInstance': 'apple/subscriptions',
'subscriptionAvailabilities_createInstance': 'apple/subscriptions', 'subscriptionAvailabilities_createInstance': 'apple/subscriptions',
'subscriptionGroupLocalizations_createInstance': 'apple/subscriptions', 'subscriptionGroupLocalizations_createInstance': 'apple/subscriptions',
'subscriptionGroups_createInstance': 'apple/subscriptions', 'subscriptionGroups_createInstance': 'apple/subscriptions',
'subscriptionGroups_getInstance': 'apple/subscriptions',
'subscriptionGroups_subscriptionGroupLocalizations_getToManyRelated': 'apple/subscriptions validate', 'subscriptionGroups_subscriptionGroupLocalizations_getToManyRelated': 'apple/subscriptions validate',
'subscriptionGroups_subscriptions_getToManyRelated': 'apple/iap-submit apple/promoted apple/subscriptions apple/win-back catalog prices validate', 'subscriptionGroups_subscriptions_getToManyRelated': 'apple/iap-submit apple/promoted apple/subscriptions apple/win-back catalog prices validate',
'subscriptionGroups_versions_getToManyRelated': 'apple/iap-submit', 'subscriptionGroups_versions_getToManyRelated': 'apple/iap-submit',
@ -171,11 +179,13 @@ export const WRAPPED: { apple: Record<string, string>; play: Record<string, stri
'subscriptionOfferCodeCustomCodes_createInstance': 'apple/subscriptions', 'subscriptionOfferCodeCustomCodes_createInstance': 'apple/subscriptions',
'subscriptionOfferCodes_createInstance': 'apple/subscriptions', 'subscriptionOfferCodes_createInstance': 'apple/subscriptions',
'subscriptionOfferCodes_customCodes_getToManyRelated': 'apple/subscriptions', 'subscriptionOfferCodes_customCodes_getToManyRelated': 'apple/subscriptions',
'subscriptionOfferCodes_getInstance': 'apple/subscriptions',
'subscriptionPricePoints_equalizations_getToManyRelated': 'apple/subscriptions apple/win-back', 'subscriptionPricePoints_equalizations_getToManyRelated': 'apple/subscriptions apple/win-back',
'subscriptionPromotionalOffers_createInstance': 'apple/subscriptions', 'subscriptionPromotionalOffers_createInstance': 'apple/subscriptions',
'subscriptionVersions_createInstance': 'lib/review-submit', 'subscriptionVersions_createInstance': 'lib/review-submit',
'subscriptions_appStoreReviewScreenshot_getToOneRelated': 'apple/subscriptions validate', 'subscriptions_appStoreReviewScreenshot_getToOneRelated': 'apple/subscriptions validate',
'subscriptions_createInstance': 'apple/subscriptions', 'subscriptions_createInstance': 'apple/subscriptions',
'subscriptions_getInstance': 'apple/subscriptions',
'subscriptions_introductoryOffers_getToManyRelated': 'apple/subscriptions catalog', 'subscriptions_introductoryOffers_getToManyRelated': 'apple/subscriptions catalog',
'subscriptions_offerCodes_getToManyRelated': 'apple/subscriptions catalog', 'subscriptions_offerCodes_getToManyRelated': 'apple/subscriptions catalog',
'subscriptions_pricePoints_getToManyRelated': 'apple/subscriptions apple/win-back', 'subscriptions_pricePoints_getToManyRelated': 'apple/subscriptions apple/win-back',
@ -205,7 +215,7 @@ export const WRAPPED: { apple: Record<string, string>; play: Record<string, stri
'androidpublisher.edits.insert': 'apps play/client validate', 'androidpublisher.edits.insert': 'apps play/client validate',
'androidpublisher.edits.listings.list': 'play/listing play/status validate', 'androidpublisher.edits.listings.list': 'play/listing play/status validate',
'androidpublisher.edits.listings.update': 'play/listing', 'androidpublisher.edits.listings.update': 'play/listing',
'androidpublisher.edits.tracks.get': 'play/rollout play/upload', 'androidpublisher.edits.tracks.get': 'play/release play/rollout play/upload',
'androidpublisher.edits.tracks.list': 'play/next-version-code play/status validate', 'androidpublisher.edits.tracks.list': 'play/next-version-code play/status validate',
'androidpublisher.edits.tracks.update': 'play/release play/rollout play/upload', 'androidpublisher.edits.tracks.update': 'play/release play/rollout play/upload',
'androidpublisher.edits.validate': 'play/client', 'androidpublisher.edits.validate': 'play/client',
@ -216,11 +226,9 @@ export const WRAPPED: { apple: Record<string, string>; play: Record<string, stri
'androidpublisher.monetization.onetimeproducts.patch': 'play/products prices', 'androidpublisher.monetization.onetimeproducts.patch': 'play/products prices',
'androidpublisher.monetization.onetimeproducts.purchaseOptions.batchUpdateStates': 'play/products', 'androidpublisher.monetization.onetimeproducts.purchaseOptions.batchUpdateStates': 'play/products',
'androidpublisher.monetization.subscriptions.basePlans.activate': 'play/subscriptions', 'androidpublisher.monetization.subscriptions.basePlans.activate': 'play/subscriptions',
'androidpublisher.monetization.subscriptions.basePlans.offers.activate': 'play/subscriptions',
'androidpublisher.monetization.subscriptions.basePlans.offers.create': 'play/subscriptions',
'androidpublisher.monetization.subscriptions.basePlans.offers.list': 'catalog play/subscriptions', 'androidpublisher.monetization.subscriptions.basePlans.offers.list': 'catalog play/subscriptions',
'androidpublisher.monetization.subscriptions.create': 'play/subscriptions', 'androidpublisher.monetization.subscriptions.create': 'play/subscriptions',
'androidpublisher.monetization.subscriptions.get': 'prices', 'androidpublisher.monetization.subscriptions.get': 'play/subscriptions prices',
'androidpublisher.monetization.subscriptions.list': 'catalog play/subscriptions validate', 'androidpublisher.monetization.subscriptions.list': 'catalog play/subscriptions validate',
'androidpublisher.monetization.subscriptions.patch': 'play/subscriptions', 'androidpublisher.monetization.subscriptions.patch': 'play/subscriptions',
'androidpublisher.reviews.list': 'reviews', 'androidpublisher.reviews.list': 'reviews',

View file

@ -32,6 +32,7 @@ import type { Ctx } from '../lib/ctx';
import { confirm, need } from '../lib/ctx'; import { confirm, need } from '../lib/ctx';
import { done, log, printJson, warn } from '../lib/log'; import { done, log, printJson, warn } from '../lib/log';
import { textTable } from '../lib/table'; import { textTable } from '../lib/table';
import { createOnce } from '../lib/upsert';
import { type Asc, ascClient, type J } from './asc'; import { type Asc, ascClient, type J } from './asc';
const TAG = 'adhoc'; const TAG = 'adhoc';
@ -86,8 +87,11 @@ async function register(ctx: Ctx, c: () => Promise<Asc>): Promise<void> {
let failed = 0; let failed = 0;
for (const d of plan.add) { for (const d of plan.add) {
try { try {
await api.asc('POST', '/v1/devices', deviceCreateReq(d)); // a UDID is registered once per team: a 409 or an unclear answer is checked by a direct lookup, never re-sent blindly
log(TAG, `registered ${d.name} (${maskUdid(d.udid)})`); const r = await createOnce({ what: `device ${d.name} (${maskUdid(d.udid)})`, tag: TAG, unique: true,
find: async () => (await api.all(`/v1/devices?filter[udid]=${encodeURIComponent(d.udid)}&limit=10`)).find((x: J) => String(x.attributes?.udid ?? '').toLowerCase() === d.udid.toLowerCase()) ?? null,
create: async () => (await api.asc('POST', '/v1/devices', deviceCreateReq(d))).data });
log(TAG, `${r.created ? 'registered' : 'already registered:'} ${d.name} (${maskUdid(d.udid)})`);
} catch (e) { } catch (e) {
failed++; failed++;
warn(TAG, `${d.name} (${maskUdid(d.udid)}) not registered: ${scrubUdids((e as Error).message, udids)}`); warn(TAG, `${d.name} (${maskUdid(d.udid)}) not registered: ${scrubUdids((e as Error).message, udids)}`);
@ -110,13 +114,16 @@ async function profile(ctx: Ctx, c: () => Promise<Asc>): Promise<void> {
const devices = profileDevices(await registered(api, 'IOS')); const devices = profileDevices(await registered(api, 'IOS'));
if (!devices.length) throw new Error('no enabled iOS devices: register them first (storebound apple adhoc register)'); if (!devices.length) throw new Error('no enabled iOS devices: register them first (storebound apple adhoc register)');
const want = devices.map(d => d.id); const want = devices.map(d => d.id);
const existing = (await api.all(`/v1/bundleIds/${bid.id}/profiles?limit=200`)).filter((p: J) => p.attributes?.profileType === 'IOS_APP_ADHOC' && p.attributes?.name === name); // the profile and the dated ones an earlier run made when it had to replace it ("<name> YYYY-MM-DD"), newest first
const dated = new RegExp(`^${name.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}( \\d{4}-\\d{2}-\\d{2}( \\d{2}-\\d{2})?)?$`);
const existing = (await api.all(`/v1/bundleIds/${bid.id}/profiles?limit=200`)).filter((p: J) => p.attributes?.profileType === 'IOS_APP_ADHOC' && dated.test(p.attributes?.name ?? ''))
.sort((x: J, y: J) => String(y.attributes.name).localeCompare(String(x.attributes.name)));
let newName = name; let newName = name;
for (const p of existing) { for (const p of existing) {
const has = (await api.all(`/v1/profiles/${p.id}/devices?limit=200`)).map((d: J) => d.id as string); const has = (await api.all(`/v1/profiles/${p.id}/devices?limit=200`)).map((d: J) => d.id as string);
const st = profileCurrent(p.attributes.profileState, has, want); const st = profileCurrent(p.attributes.profileState, has, want);
if (st.current) { done(TAG, `profile "${name}" is current (${has.length} devices, expires ${String(p.attributes.expirationDate ?? '').slice(0, 10)})`); return; } if (st.current) { done(TAG, `profile "${p.attributes.name}" is current (${has.length} devices, expires ${String(p.attributes.expirationDate ?? '').slice(0, 10)})`); return; }
log(TAG, `profile "${name}" is ${p.attributes.profileState === 'ACTIVE' ? `missing ${st.missing} device(s)` : 'invalid'}; profiles cannot be changed, so a new one is created`); log(TAG, `profile "${p.attributes.name}" is ${p.attributes.profileState === 'ACTIVE' ? `missing ${st.missing} device(s)` : 'invalid'}; profiles cannot be changed, so a new one is created`);
newName = `${name} ${new Date().toISOString().slice(0, 10)}`; newName = `${name} ${new Date().toISOString().slice(0, 10)}`;
} }
const classes = new Map<string, number>(); const classes = new Map<string, number>();
@ -124,7 +131,10 @@ async function profile(ctx: Ctx, c: () => Promise<Asc>): Promise<void> {
log(TAG, `plan: create IOS_APP_ADHOC "${newName}" for ${bundleId}: ${devices.length} devices (${[...classes].map(([k, v]) => `${v} ${k}`).join(', ')}), ${certs.length} distribution certificate(s)`); log(TAG, `plan: create IOS_APP_ADHOC "${newName}" for ${bundleId}: ${devices.length} devices (${[...classes].map(([k, v]) => `${v} ${k}`).join(', ')}), ${certs.length} distribution certificate(s)`);
if (ctx.args.has('dry')) { log(TAG, 'dry run: nothing created'); return; } if (ctx.args.has('dry')) { log(TAG, 'dry run: nothing created'); return; }
if (!(await confirm(ctx, `Create the ad-hoc provisioning profile "${newName}"?`))) { log(TAG, 'not confirmed, nothing created'); return; } if (!(await confirm(ctx, `Create the ad-hoc provisioning profile "${newName}"?`))) { log(TAG, 'not confirmed, nothing created'); return; }
const res = (await api.asc('POST', '/v1/profiles', profileCreateReq(newName, bid.id, certs, want))).data; if (existing.some((p: J) => p.attributes.name === newName)) newName = `${name} ${new Date().toISOString().slice(0, 16).replace('T', ' ').replace(':', '-')}`;
const res = (await createOnce<J>({ what: `profile "${newName}"`, tag: TAG,
find: async () => (await api.all(`/v1/bundleIds/${bid.id}/profiles?limit=200`)).find((p: J) => p.attributes?.name === newName) ?? null,
create: async () => (await api.asc('POST', '/v1/profiles', profileCreateReq(newName, bid.id, certs, want))).data })).value;
const content = res?.attributes?.profileContent as string | undefined; const content = res?.attributes?.profileContent as string | undefined;
if (content) { if (content) {
const out = resolve(`build/adhoc/${newName.replace(/[^\w.-]+/g, '_')}.mobileprovision`); const out = resolve(`build/adhoc/${newName.replace(/[^\w.-]+/g, '_')}.mobileprovision`);

View file

@ -4,6 +4,7 @@ import { createHash, createPrivateKey, sign } from 'node:crypto';
import { readFileSync } from 'node:fs'; import { readFileSync } from 'node:fs';
import { ascKey } from '../lib/credentials'; import { ascKey } from '../lib/credentials';
import { errorBody } from '../lib/http'; import { errorBody } from '../lib/http';
import { AmbiguousWriteError, retrySafe } from '../lib/upsert';
const b64 = (b: Buffer | string): string => Buffer.from(b).toString('base64url'); const b64 = (b: Buffer | string): string => Buffer.from(b).toString('base64url');
export const md5 = (b: Buffer | Uint8Array): string => createHash('md5').update(b).digest('hex'); export const md5 = (b: Buffer | Uint8Array): string => createHash('md5').update(b).digest('hex');
@ -24,6 +25,14 @@ export function setUpToDate(existing: { checksum?: string | null; state?: string
return existing.length === want.length && existing.every((e, i) => e.checksum === want[i] && DELIVERED.includes(e.state ?? '')); return existing.length === want.length && existing.every((e, i) => e.checksum === want[i] && DELIVERED.includes(e.state ?? ''));
} }
/** How many leading assets of a set already hold the wanted files (same MD5 at the same position, delivered): those
* stay when the set is synced. Pure. */
export function keptPrefix(existing: { checksum?: string | null; state?: string | null }[], want: string[]): number {
let k = 0;
while (k < existing.length && k < want.length && existing[k]!.checksum === want[k] && DELIVERED.includes(existing[k]!.state ?? '')) k++;
return k;
}
/** Cached ES256 token getter for the team API key (env or .storebound/): valid 18 minutes, renewed after 15. Shared by /** Cached ES256 token getter for the team API key (env or .storebound/): valid 18 minutes, renewed after 15. Shared by
* ascClient and the generic `storebound api apple` client (cli/api/client.ts). */ * ascClient and the generic `storebound api apple` client (cli/api/client.ts). */
export function ascJwt(root: string): { jwt: () => string; keyId: string } { export function ascJwt(root: string): { jwt: () => string; keyId: string } {
@ -45,12 +54,28 @@ export async function ascClient(root: string, bundleId: string, appIdHint?: stri
const k = ascJwt(root); const k = ascJwt(root);
const jwt = k.jwt; const jwt = k.jwt;
/**
* One API request. 429 is retried (the request was refused, not applied). 5xx and network errors are retried only
* for requests that end the same when sent twice (retrySafe: GET, PATCH, DELETE, relationship adds, the "create or
* replace" singletons); a POST create then throws AmbiguousWriteError instead, so callers look before they create
* again (cli/lib/upsert.ts). A DELETE answered 404 after a retry was applied by the first attempt.
*/
async function asc(method: string, path: string, body?: unknown, attempt = 1): Promise<J> { async function asc(method: string, path: string, body?: unknown, attempt = 1): Promise<J> {
const r = await fetch(path.startsWith('http') ? path : 'https://api.appstoreconnect.apple.com' + path, { const safe = retrySafe(method, path);
method, headers: { Authorization: 'Bearer ' + jwt(), 'Content-Type': 'application/json' }, body: body ? JSON.stringify(body) : undefined, let r: Response;
}); try {
r = await fetch(path.startsWith('http') ? path : 'https://api.appstoreconnect.apple.com' + path, {
method, headers: { Authorization: 'Bearer ' + jwt(), 'Content-Type': 'application/json' }, body: body ? JSON.stringify(body) : undefined,
});
} catch (e) {
if (safe && attempt < 5) { await Bun.sleep(2000 * attempt); return asc(method, path, body, attempt + 1); }
if (!safe) throw new AmbiguousWriteError(`${method} ${path} -> no answer (${(e as Error).message})`);
throw e;
}
const t = await r.text(); const t = await r.text();
if ((r.status >= 500 || r.status === 429) && attempt < 5) { await Bun.sleep(2000 * attempt); return asc(method, path, body, attempt + 1); } if ((r.status === 429 || (r.status >= 500 && safe)) && attempt < 5) { await Bun.sleep(2000 * attempt); return asc(method, path, body, attempt + 1); }
if (r.status >= 500 && !safe) throw new AmbiguousWriteError(`${method} ${path} -> ${r.status} ${errorBody(t)}`, r.status);
if (r.status === 404 && method === 'DELETE' && attempt > 1) return {};
if (!r.ok) throw new Error(`${method} ${path} -> ${r.status} ${errorBody(t)}`); if (!r.ok) throw new Error(`${method} ${path} -> ${r.status} ${errorBody(t)}`);
return t ? JSON.parse(t) : {}; return t ? JSON.parse(t) : {};
} }
@ -127,5 +152,38 @@ export async function ascClient(root: string, bundleId: string, appIdHint?: stri
return res.id; return res.id;
} }
return { asc, all, maybe, getAs, bytes, app, editableVersion, editableAppInfo, uploadAsset, keyId: k.keyId }; /**
* Makes a screenshot or preview set hold exactly `files`, in this order, touching as little as possible: the leading
* assets that already match stay, everything after them is deleted, the rest is uploaded in order. A crash midway
* leaves a prefix of the wanted files (plus maybe one half-uploaded asset), which the next run keeps or removes, so
* re-runs converge and never duplicate. A reservation with an unknown outcome is cleaned up (the set is read again and
* any asset this run does not know is deleted) and sent once more. At the end the order is read back and, if Apple
* shows another one, set through the set's relationship.
*/
async function syncAssets(o: { set: { type: 'appScreenshotSets' | 'appPreviewSets'; id: string }; existing: J[]; files: string[]; want: string[];
state: (x: J) => string | undefined; attrs?: (file: string) => Record<string, unknown>; onUpload?: (file: string) => void }): Promise<{ uploaded: number; removed: number; kept: number }> {
const asset = o.set.type === 'appScreenshotSets' ? 'appScreenshots' : 'appPreviews';
const rel = { [o.set.type === 'appScreenshotSets' ? 'appScreenshotSet' : 'appPreviewSet']: { data: { type: o.set.type, id: o.set.id } } };
const k = keptPrefix(o.existing.map(x => ({ checksum: x.attributes?.sourceFileChecksum, state: o.state(x) })), o.want);
const ids: string[] = o.existing.slice(0, k).map(x => x.id);
const extra = o.existing.slice(k);
for (const x of extra) await asc('DELETE', `/v1/${asset}/${x.id}`);
for (const f of o.files.slice(k)) {
o.onUpload?.(f);
try { ids.push(await uploadAsset(asset, rel, f, o.attrs?.(f) ?? {})); } catch (e) {
if (!(e as { ambiguous?: boolean }).ambiguous) throw e;
// the reservation may exist: remove whatever this run does not know, then upload this file once more
for (const x of await all(`/v1/${o.set.type}/${o.set.id}/${asset}?limit=50`)) if (!ids.includes(x.id)) await asc('DELETE', `/v1/${asset}/${x.id}`);
ids.push(await uploadAsset(asset, rel, f, o.attrs?.(f) ?? {}));
}
}
if (o.files.length > k) {
const now = (await all(`/v1/${o.set.type}/${o.set.id}/${asset}?limit=50`)).map(x => x.id as string);
if (now.join() !== ids.join() && ids.every(id => now.includes(id)))
await asc('PATCH', `/v1/${o.set.type}/${o.set.id}/relationships/${asset}`, { data: ids.map(id => ({ type: asset, id })) });
}
return { uploaded: o.files.length - k, removed: extra.length, kept: k };
}
return { asc, all, maybe, getAs, bytes, app, editableVersion, editableAppInfo, uploadAsset, syncAssets, keyId: k.keyId };
} }

View file

@ -5,6 +5,7 @@
import type { Ctx } from '../lib/ctx'; import type { Ctx } from '../lib/ctx';
import { need } from '../lib/ctx'; import { need } from '../lib/ctx';
import { done, log } from '../lib/log'; import { done, log } from '../lib/log';
import { createOnce } from '../lib/upsert';
import { ascClient } from './asc'; import { ascClient } from './asc';
export async function run(ctx: Ctx): Promise<void> { export async function run(ctx: Ctx): Promise<void> {
@ -16,8 +17,11 @@ export async function run(ctx: Ctx): Promise<void> {
if (found.length) { log('apple', `bundle id ${bundleId} already registered (${found[0].attributes.platform})`); } if (found.length) { log('apple', `bundle id ${bundleId} already registered (${found[0].attributes.platform})`); }
else if (ctx.args.has('dry')) { log('apple', `bundle id ${bundleId} missing (dry run, not registering)`); return; } else if (ctx.args.has('dry')) { log('apple', `bundle id ${bundleId} missing (dry run, not registering)`); return; }
else { else {
await c.asc('POST', '/v1/bundleIds', { data: { type: 'bundleIds', attributes: { identifier: bundleId, name: ctx.args.str('name', config.app.name).replace(/[^A-Za-z0-9 ]/g, ' ').trim(), platform: 'IOS' } } }); // identifiers are unique per team: a 409 (or an unclear answer) is checked by a direct lookup, never re-sent blindly
log('apple', `bundle id ${bundleId} registered`); const r = await createOnce({ what: `bundle id ${bundleId}`, tag: 'apple', unique: true,
find: async () => (await c.asc('GET', `/v1/bundleIds?filter[identifier]=${encodeURIComponent(bundleId)}`)).data?.find((b: { attributes: { identifier: string } }) => b.attributes.identifier === bundleId) ?? null,
create: async () => (await c.asc('POST', '/v1/bundleIds', { data: { type: 'bundleIds', attributes: { identifier: bundleId, name: ctx.args.str('name', config.app.name).replace(/[^A-Za-z0-9 ]/g, ' ').trim(), platform: 'IOS' } } })).data });
log('apple', `bundle id ${bundleId} ${r.created ? 'registered' : 'already registered'}`);
} }
const apps = (await c.asc('GET', `/v1/apps?filter[bundleId]=${encodeURIComponent(bundleId)}`)).data; const apps = (await c.asc('GET', `/v1/apps?filter[bundleId]=${encodeURIComponent(bundleId)}`)).data;
if (apps.length) log('apple', `app record: ${apps[0].attributes.name} (Apple ID ${apps[0].id}); put it in apple.appId`); if (apps.length) log('apple', `app record: ${apps[0].attributes.name} (Apple ID ${apps[0].id}); put it in apple.appId`);

View file

@ -18,6 +18,8 @@ import { numberedImages } from '../play/images';
import { variantRoot } from '../shots/lib'; import { variantRoot } from '../shots/lib';
import { type Asc, ascClient, type J } from './asc'; import { type Asc, ascClient, type J } from './asc';
import { describeReq, execute, one, type Req } from './jsonapi'; import { describeReq, execute, one, type Req } from './jsonapi';
import { addItem, draftSubmission, findDraft } from './submission';
import { findOrCreate, getById } from '../lib/upsert';
import { displaySets, syncScreenshotSet } from './screenshots'; import { displaySets, syncScreenshotSet } from './screenshots';
const TAG = 'cpp'; const TAG = 'cpp';
@ -35,11 +37,6 @@ async function pagesOf(c: Asc, appId: string): Promise<Page[]> {
return out; return out;
} }
/** A draft review submission of the app (READY_FOR_REVIEW), or the request that creates one. */
async function draftSubmission(c: Asc, appId: string): Promise<string | null> {
return (await c.all(`/v1/reviewSubmissions?filter[app]=${appId}&filter[platform]=IOS&filter[state]=READY_FOR_REVIEW&limit=10`))[0]?.id ?? null;
}
export async function run(ctx: Ctx): Promise<void> { export async function run(ctx: Ctx): Promise<void> {
const { config, root, resolve } = ctx.loaded; const { config, root, resolve } = ctx.loaded;
const a = ctx.args; const a = ctx.args;
@ -82,7 +79,18 @@ export async function run(ctx: Ctx): Promise<void> {
if (pages.length + reqs.filter(r => r.path === '/v1/appCustomProductPages').length > CPP_LIMITS.pages) throw new Error(`more than ${CPP_LIMITS.pages} custom product pages`); if (pages.length + reqs.filter(r => r.path === '/v1/appCustomProductPages').length > CPP_LIMITS.pages) throw new Error(`more than ${CPP_LIMITS.pages} custom product pages`);
if (a.has('json')) printJson({ dryRun: dry, requests: reqs.map(r => describeReq(r)) }); if (a.has('json')) printJson({ dryRun: dry, requests: reqs.map(r => describeReq(r)) });
if (!reqs.length) { done(TAG, 'custom product pages up to date'); return; } if (!reqs.length) { done(TAG, 'custom product pages up to date'); return; }
await execute(c.asc, reqs, { dry, tag: TAG }); if (dry) { await execute(c.asc, reqs, { dry, tag: TAG }); done(TAG, `dry run: ${reqs.length} change(s)`); return; }
// page names are not known to be unique on Apple's side: each new page is looked up right before it is created
// (fresh list, then the journal of recent creates for the list lag) and never sent twice
for (const r of reqs) {
if (r.path !== '/v1/appCustomProductPages') { await execute(c.asc, [r], { dry, tag: TAG }); continue; }
const name = (r.body as J).data.attributes.name as string;
const made = await findOrCreate<J>({ what: `custom product page "${name}"`, tag: TAG,
find: async () => (await c.all(`/v1/apps/${appId}/appCustomProductPages?limit=200`)).find((p: J) => p.attributes.name === name) ?? null,
journal: { root, key: `apple:${appId}:cpp:${name}`, byId: id => getById(c.asc, `/v1/appCustomProductPages/${id}`) as Promise<J | null>, idOf: v => v.id },
create: async () => (await c.asc('POST', r.path, r.body)).data });
log(TAG, `${describeReq(r)}: ${made.created ? 'done' : 'exists already'}`);
}
done(TAG, dry ? `dry run: ${reqs.length} change(s)` : `${reqs.length} change(s); next: storebound apple cpp upload --name "<page>"`); done(TAG, dry ? `dry run: ${reqs.length} change(s)` : `${reqs.length} change(s); next: storebound apple cpp upload --name "<page>"`);
return; return;
} }
@ -117,13 +125,13 @@ export async function run(ctx: Ctx): Promise<void> {
const { def, page } = pick(); const { def, page } = pick();
if (!page?.version) throw new Error(`"${def.name}" is not on App Store Connect yet (storebound apple cpp create)`); if (!page?.version) throw new Error(`"${def.name}" is not on App Store Connect yet (storebound apple cpp create)`);
if (!['PREPARE_FOR_SUBMISSION', 'REJECTED'].includes(page.version.state)) { done(TAG, `"${def.name}": version is ${page.version.state}, nothing to submit`); return; } if (!['PREPARE_FOR_SUBMISSION', 'REJECTED'].includes(page.version.state)) { done(TAG, `"${def.name}": version is ${page.version.state}, nothing to submit`); return; }
const draft = await draftSubmission(c, appId); const draft = await findDraft(c, appId);
const create: Req = { method: 'POST', path: '/v1/reviewSubmissions', note: 'new draft review submission', body: { data: { type: 'reviewSubmissions', attributes: { platform: 'IOS' }, relationships: { app: one('apps', appId) } } } }; const create: Req = { method: 'POST', path: '/v1/reviewSubmissions', note: 'new draft review submission', body: { data: { type: 'reviewSubmissions', attributes: { platform: 'IOS' }, relationships: { app: one('apps', appId) } } } };
const item = reviewItemReq(draft ?? '${submission}', 'appCustomProductPageVersion', page.version.id, `custom product page "${def.name}"`); const item = reviewItemReq(draft ?? '${submission}', 'appCustomProductPageVersion', page.version.id, `custom product page "${def.name}"`);
if (dry) { await execute(c.asc, [...(draft ? [] : [create]), item, { method: 'PATCH', path: `/v1/reviewSubmissions/${draft ?? '${submission}'}`, note: 'submit for App Review' }], { dry, tag: TAG }); return; } if (dry) { await execute(c.asc, [...(draft ? [] : [create]), item, { method: 'PATCH', path: `/v1/reviewSubmissions/${draft ?? '${submission}'}`, note: 'submit for App Review' }], { dry, tag: TAG }); return; }
if (!(await confirm(ctx, `Send custom product page "${def.name}" to App Review now?`))) return; if (!(await confirm(ctx, `Send custom product page "${def.name}" to App Review now?`))) return;
const sid = draft ?? ((await c.asc('POST', create.path, create.body)).data.id as string); const sid = draft ?? await draftSubmission(c, appId, root, TAG);
await c.asc('POST', item.path, reviewItemReq(sid, 'appCustomProductPageVersion', page.version.id, '').body); await addItem(c, sid, 'appCustomProductPageVersion', page.version.id, `custom product page "${def.name}"`, TAG);
await c.asc('PATCH', `/v1/reviewSubmissions/${sid}`, { data: { type: 'reviewSubmissions', id: sid, attributes: { submitted: true } } }); await c.asc('PATCH', `/v1/reviewSubmissions/${sid}`, { data: { type: 'reviewSubmissions', id: sid, attributes: { submitted: true } } });
done(TAG, `"${def.name}" submitted (review submission ${sid}); after approval: storebound apple cpp visible --name "${def.name}"`); done(TAG, `"${def.name}" submitted (review submission ${sid}); after approval: storebound apple cpp visible --name "${def.name}"`);
return; return;

View file

@ -15,6 +15,8 @@ import { textTable } from '../lib/table';
import { imageInfo } from '../shots/spec'; import { imageInfo } from '../shots/spec';
import { ascClient, type J } from './asc'; import { ascClient, type J } from './asc';
import { describeReq, execute, one, type Req } from './jsonapi'; import { describeReq, execute, one, type Req } from './jsonapi';
import { addItem, draftSubmission } from './submission';
import { findOrCreate, getById } from '../lib/upsert';
const TAG = 'events'; const TAG = 'events';
const USAGE = 'usage: storebound apple events list | create [--name n] | upload --name n | submit --name n'; const USAGE = 'usage: storebound apple events list | create [--name n] | upload --name n | submit --name n';
@ -69,11 +71,21 @@ export async function run(ctx: Ctx): Promise<void> {
done(TAG, 'dry run: nothing changed'); done(TAG, 'dry run: nothing changed');
return; return;
} }
const out = await execute(c.asc, reqs, { dry: false, tag: TAG }) as J[]; // reference names are not known to be unique on Apple's side: each new event is looked up right before it is
for (const x of created) { // created (fresh list, then the journal of recent creates for the list lag) and never sent twice
const id = out[x.index].data.id as string; for (const [i, r] of reqs.entries()) {
const x = created.find(e => e.index === i);
if (!x) { await execute(c.asc, [r], { dry: false, tag: TAG }); continue; }
const name = x.def.referenceName;
const made = await findOrCreate<J>({ what: `in-app event "${name}"`, tag: TAG,
find: async () => (await c.all(`/v1/apps/${appId}/appEvents?limit=200`)).find((e: J) => e.attributes.referenceName === name) ?? null,
journal: { root, key: `apple:${appId}:event:${name}`, byId: id => getById(c.asc, `/v1/appEvents/${id}`) as Promise<J | null>, idOf: v => v.id },
create: async () => (await c.asc('POST', r.path, r.body)).data });
log(TAG, `${describeReq(r)}: ${made.created ? 'done' : 'exists already'}`);
const id = made.value.id as string;
const want = Object.entries(x.def.listings).flatMap(([lang, t]) => { const locale = localeOf(lang); return locale ? [{ locale, ...t }] : []; }); const want = Object.entries(x.def.listings).flatMap(([lang, t]) => { const locale = localeOf(lang); return locale ? [{ locale, ...t }] : []; });
await execute(c.asc, planEventLocales(id, [], want), { dry: false, tag: TAG }); const have = made.created ? [] : (await c.all(`/v1/appEvents/${id}/localizations?limit=200`)).map((l: J) => ({ id: l.id, ...l.attributes }));
await execute(c.asc, planEventLocales(id, have, want), { dry: false, tag: TAG });
} }
done(TAG, 'in-app events created / updated; next: storebound apple events upload --name "<event>"'); done(TAG, 'in-app events created / updated; next: storebound apple events upload --name "<event>"');
return; return;
@ -96,7 +108,7 @@ export async function run(ctx: Ctx): Promise<void> {
const info = imageInfo(readFileSync(file)); const info = imageInfo(readFileSync(file));
const problems = checkEventMedia(kind, info.w, info.h, info.format); const problems = checkEventMedia(kind, info.w, info.h, info.format);
if (problems.length) throw new Error(`${file}: ${problems.join('; ')}`); if (problems.length) throw new Error(`${file}: ${problems.join('; ')}`);
if (have.some((s: J) => s.attributes.appEventAssetType === kind && s.attributes.fileSize === info.bytes)) { log(TAG, `${loc.attributes.locale} ${kind}: up to date`); continue; } if (have.some((s: J) => s.attributes.appEventAssetType === kind && s.attributes.fileSize === info.bytes && ['COMPLETE', 'UPLOAD_COMPLETE'].includes(s.attributes.assetDeliveryState?.state ?? 'COMPLETE'))) { log(TAG, `${loc.attributes.locale} ${kind}: up to date`); continue; }
if (dry) { log(TAG, `${loc.attributes.locale} ${kind}: would upload ${file} (${info.w}x${info.h})`); n++; continue; } if (dry) { log(TAG, `${loc.attributes.locale} ${kind}: would upload ${file} (${info.w}x${info.h})`); n++; continue; }
for (const s of have.filter((x: J) => x.attributes.appEventAssetType === kind)) await c.asc('DELETE', `/v1/appEventScreenshots/${s.id}`); for (const s of have.filter((x: J) => x.attributes.appEventAssetType === kind)) await c.asc('DELETE', `/v1/appEventScreenshots/${s.id}`);
await c.uploadAsset('appEventScreenshots', { appEventLocalization: { data: { type: 'appEventLocalizations', id: loc.id } } }, file, { appEventAssetType: kind }); await c.uploadAsset('appEventScreenshots', { appEventLocalization: { data: { type: 'appEventLocalizations', id: loc.id } } }, file, { appEventAssetType: kind });
@ -116,8 +128,8 @@ export async function run(ctx: Ctx): Promise<void> {
const create: Req = { method: 'POST', path: '/v1/reviewSubmissions', note: 'new draft review submission', body: { data: { type: 'reviewSubmissions', attributes: { platform: 'IOS' }, relationships: { app: one('apps', appId) } } } }; const create: Req = { method: 'POST', path: '/v1/reviewSubmissions', note: 'new draft review submission', body: { data: { type: 'reviewSubmissions', attributes: { platform: 'IOS' }, relationships: { app: one('apps', appId) } } } };
if (dry) { await execute(c.asc, [...(draft ? [] : [create]), reviewItemReq(draft ?? '${submission}', 'appEvent', ev.id, `in-app event "${def.referenceName}"`), { method: 'PATCH', path: `/v1/reviewSubmissions/${draft ?? '${submission}'}`, note: 'submit for App Review' }], { dry, tag: TAG }); return; } if (dry) { await execute(c.asc, [...(draft ? [] : [create]), reviewItemReq(draft ?? '${submission}', 'appEvent', ev.id, `in-app event "${def.referenceName}"`), { method: 'PATCH', path: `/v1/reviewSubmissions/${draft ?? '${submission}'}`, note: 'submit for App Review' }], { dry, tag: TAG }); return; }
if (!(await confirm(ctx, `Send in-app event "${def.referenceName}" to App Review? Once approved it is published on ${def.schedule.publishStart} by its schedule.`))) return; if (!(await confirm(ctx, `Send in-app event "${def.referenceName}" to App Review? Once approved it is published on ${def.schedule.publishStart} by its schedule.`))) return;
const sid = draft ?? ((await c.asc('POST', create.path, create.body)).data.id as string); const sid = draft ?? await draftSubmission(c, appId, root, TAG);
await c.asc('POST', '/v1/reviewSubmissionItems', reviewItemReq(sid, 'appEvent', ev.id, '').body); await addItem(c, sid, 'appEvent', ev.id, `in-app event "${def.referenceName}"`, TAG);
await c.asc('PATCH', `/v1/reviewSubmissions/${sid}`, { data: { type: 'reviewSubmissions', id: sid, attributes: { submitted: true } } }); await c.asc('PATCH', `/v1/reviewSubmissions/${sid}`, { data: { type: 'reviewSubmissions', id: sid, attributes: { submitted: true } } });
done(TAG, `"${def.referenceName}" submitted (review submission ${sid})`); done(TAG, `"${def.referenceName}" submitted (review submission ${sid})`);
return; return;

View file

@ -14,6 +14,7 @@ import { allSubscriptions } from '../lib/subscriptions';
import { bindIds, latestVersion, newVersionReq, type PIap, planIapSubmission, type PSub, type SubmitState, versionsOf } from '../lib/review-submit'; import { bindIds, latestVersion, newVersionReq, type PIap, planIapSubmission, type PSub, type SubmitState, versionsOf } from '../lib/review-submit';
import { type Asc, ascClient, type J } from './asc'; import { type Asc, ascClient, type J } from './asc';
import { describeReq, execute } from './jsonapi'; import { describeReq, execute } from './jsonapi';
import { addItem, draftSubmission } from './submission';
const TAG = 'iap-submit'; const TAG = 'iap-submit';
@ -95,10 +96,18 @@ export async function run(ctx: Ctx): Promise<void> {
} }
const what = `${plan.items.length} item(s)${plan.appVersion ? `, including app version ${plan.appVersion.versionString}` : ''}`; const what = `${plan.items.length} item(s)${plan.appVersion ? `, including app version ${plan.appVersion.versionString}` : ''}`;
if (!(await confirm(ctx, `Add ${what} to ${state.draft ? 'the draft' : 'a new draft'} review submission?`))) return; if (!(await confirm(ctx, `Add ${what} to ${state.draft ? 'the draft' : 'a new draft'} review submission?`))) return;
let sid = state.draft?.id; // the draft is reused (or created once); the plan left out items already in it (readSubmitState read them), and an
// item the store says is in there already (409) is not added twice (cli/apple/submission.ts)
const sid = state.draft?.id ?? await draftSubmission(c, appId, root, TAG);
for (const r of prepare) { for (const r of prepare) {
const out = await c.asc(r.method, r.path, bindIds(r, { submission: sid }).body); if (r.path === '/v1/reviewSubmissions') continue;
if (r.path === '/v1/reviewSubmissions') sid = out.data.id; if (r.path === '/v1/reviewSubmissionItems') {
const rels = (r.body as J).data.relationships as Record<string, { data: { id: string } }>;
const kind = Object.keys(rels).find(k => k !== 'reviewSubmission')!;
if (await addItem(c, sid, kind, rels[kind]!.data.id, r.note ?? kind, TAG, { checked: true })) log(TAG, `${describeReq(bindIds(r, { submission: sid }))}: done`);
continue;
}
await c.asc(r.method, r.path, bindIds(r, { submission: sid }).body);
log(TAG, `${describeReq(bindIds(r, { submission: sid }))}: done`); log(TAG, `${describeReq(bindIds(r, { submission: sid }))}: done`);
} }
if (a.has('no-submit')) { done(TAG, `draft review submission ${sid} ready: press "Submit for Review" in App Store Connect (Apps > Distribution > App Review)`); return; } if (a.has('no-submit')) { done(TAG, `draft review submission ${sid} ready: press "Submit for Review" in App Store Connect (Apps > Distribution > App Review)`); return; }

View file

@ -15,6 +15,7 @@ import { dryPlan, type PlanRow } from '../lib/dry';
import { done, log, step } from '../lib/log'; import { done, log, step } from '../lib/log';
import { LAG_HINT, reportMissing } from '../lib/store-lag'; import { LAG_HINT, reportMissing } from '../lib/store-lag';
import { appleTerritory, nearestPrices, samePrice } from '../lib/money'; import { appleTerritory, nearestPrices, samePrice } from '../lib/money';
import { createOnce, findOrCreate, getById } from '../lib/upsert';
import { ascClient, type J } from './asc'; import { ascClient, type J } from './asc';
/** Product type -> InAppPurchaseType (POST /v2/inAppPurchases). */ /** Product type -> InAppPurchaseType (POST /v2/inAppPurchases). */
@ -44,26 +45,31 @@ export async function run(ctx: Ctx): Promise<void> {
const created: string[] = []; const created: string[] = [];
for (const [i, p] of products.entries()) { for (const [i, p] of products.entries()) {
const tag = step(i + 1, products.length, p.id); const tag = step(i + 1, products.length, p.id);
const found = await c.asc('GET', `/v1/apps/${appId}/inAppPurchasesV2?filter[productId]=${encodeURIComponent(p.id)}`); const lookup = async (): Promise<J | null> => (await c.asc('GET', `/v1/apps/${appId}/inAppPurchasesV2?filter[productId]=${encodeURIComponent(p.id)}`)).data
let id: string = found.data[0]?.id; ?.find((x: J) => x.attributes?.productId === p.id) ?? null;
if (dry) { rows.push(await planProduct(p, id, found.data[0]?.attributes?.state)); continue; } if (dry) { const f = await lookup(); rows.push(await planProduct(p, f?.id, f?.attributes?.state)); continue; }
if (!id) { // product ids are unique per app: a 409 finds the existing one; the journal covers the list lag after a create
const first = Object.values(p.listings)[0]!; const first = Object.values(p.listings)[0]!;
id = (await c.asc('POST', '/v2/inAppPurchases', { data: { type: 'inAppPurchases', const made = await findOrCreate<J>({ what: `in-app purchase ${p.id}`, tag: 'apple', unique: true, find: lookup,
journal: { root, key: `apple:${bundleId}:iap:${p.id}`, byId: id => getById(c.asc, `/v2/inAppPurchases/${id}`) as Promise<J | null>, idOf: v => v.id },
create: async () => (await c.asc('POST', '/v2/inAppPurchases', { data: { type: 'inAppPurchases',
attributes: { name: p.referenceName ?? first.title, productId: p.id, inAppPurchaseType: APPLE_IAP_TYPE[p.type], attributes: { name: p.referenceName ?? first.title, productId: p.id, inAppPurchaseType: APPLE_IAP_TYPE[p.type],
familySharable: p.familySharable ?? false, ...(p.reviewNote ? { reviewNote: p.reviewNote } : {}) }, familySharable: p.familySharable ?? false, ...(p.reviewNote ? { reviewNote: p.reviewNote } : {}) },
relationships: { app: { data: { type: 'apps', id: appId } } } } })).data.id; relationships: { app: { data: { type: 'apps', id: appId } } } } })).data });
created.push(p.id); const id: string = made.value.id;
log('apple', `${tag}: created`); if (made.created) created.push(p.id);
} else log('apple', `${tag}: exists`); log('apple', `${tag}: ${made.created ? 'created' : 'exists'}`);
const have = new Set((await c.all(`/v2/inAppPurchases/${id}/inAppPurchaseLocalizations`)).map((l: J) => l.attributes.locale)); const locsOf = async () => await c.all(`/v2/inAppPurchases/${id}/inAppPurchaseLocalizations`);
const have = new Set((await locsOf()).map((l: J) => l.attributes.locale));
for (const [langId, l] of Object.entries(p.listings)) { for (const [langId, l] of Object.entries(p.listings)) {
const locale = langs.get(langId)?.apple; const locale = langs.get(langId)?.apple;
if (!locale || have.has(locale)) continue; if (!locale || have.has(locale)) continue;
if ([...l.description].length > APPLE_IAP_LIMITS.description) throw new Error(`${p.id} ${locale}: description over ${APPLE_IAP_LIMITS.description} characters (Apple limit)`); if ([...l.description].length > APPLE_IAP_LIMITS.description) throw new Error(`${p.id} ${locale}: description over ${APPLE_IAP_LIMITS.description} characters (Apple limit)`);
await c.asc('POST', '/v1/inAppPurchaseLocalizations', { data: { type: 'inAppPurchaseLocalizations', attributes: { locale, name: l.title, description: l.description }, await createOnce({ what: `${p.id} localization ${locale}`, tag: 'apple', unique: true,
relationships: { inAppPurchaseV2: { data: { type: 'inAppPurchases', id } } } } }); find: async () => (await locsOf()).find((x: J) => x.attributes.locale === locale) ?? null,
create: async () => (await c.asc('POST', '/v1/inAppPurchaseLocalizations', { data: { type: 'inAppPurchaseLocalizations', attributes: { locale, name: l.title, description: l.description },
relationships: { inAppPurchaseV2: { data: { type: 'inAppPurchases', id } } } } })).data });
log('apple', ` localization ${locale}`); log('apple', ` localization ${locale}`);
} }

View file

@ -1,6 +1,7 @@
// JSON:API request builders for App Store Connect. Pure: commands build `Req`s, print them on --dry and send them // JSON:API request builders for App Store Connect. Pure: commands build `Req`s, print them on --dry and send them
// with the client otherwise. Request / response format: https://developer.apple.com/documentation/appstoreconnectapi // with the client otherwise. Request / response format: https://developer.apple.com/documentation/appstoreconnectapi
import { log } from '../lib/log'; import { log, warn } from '../lib/log';
import { isAmbiguous, isConflict } from '../lib/upsert';
export interface Req { method: 'GET' | 'POST' | 'PATCH' | 'DELETE'; path: string; body?: unknown; note?: string } export interface Req { method: 'GET' | 'POST' | 'PATCH' | 'DELETE'; path: string; body?: unknown; note?: string }
@ -16,13 +17,23 @@ export function describeReq(r: Req, hide: string[] = ['email', 'demoAccountPassw
return `${r.method} ${r.path}${keys.length ? ` {${keys.join(', ')}}` : ''}${r.note ? ` # ${r.note}` : ''}`; return `${r.method} ${r.path}${keys.length ? ` {${keys.join(', ')}}` : ''}${r.note ? ` # ${r.note}` : ''}`;
} }
/** Logs every planned request; sends them in order unless dry. Returns the responses (empty on dry). */ /**
* Logs every planned request; sends them in order unless dry. Returns the responses (empty on dry). The plans come
* from a fresh read of the store, so a create the store refuses as a duplicate (409 already exists) means an earlier
* run made it: it is logged and skipped (its slot in the result is null), never sent again. A create with an unknown
* outcome (timeout, 5xx) stops the run without a retry: running the command again reads the store and plans only
* what is still missing.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any // eslint-disable-next-line @typescript-eslint/no-explicit-any
export async function execute(asc: (m: string, p: string, b?: unknown) => Promise<any>, reqs: Req[], o: { dry: boolean; tag: string }): Promise<unknown[]> { export async function execute(asc: (m: string, p: string, b?: unknown) => Promise<any>, reqs: Req[], o: { dry: boolean; tag: string }): Promise<unknown[]> {
const out: unknown[] = []; const out: unknown[] = [];
for (const r of reqs) { for (const [i, r] of reqs.entries()) {
if (o.dry) { log(o.tag, `would ${describeReq(r)}`); continue; } if (o.dry) { log(o.tag, `would ${describeReq(r)}`); continue; }
out.push(await asc(r.method, r.path, r.body)); try { out.push(await asc(r.method, r.path, r.body)); } catch (e) {
if (r.method === 'POST' && isConflict(e)) { warn(o.tag, `${describeReq(r)}: already exists, left as it is`); out.push(null); continue; }
if (isAmbiguous(e)) throw new Error(`${describeReq(r)}: no clear answer (${(e as Error).message.slice(0, 200)}). Not sent again, so nothing is created twice; ${reqs.length - i - 1} later request(s) not sent. Run the command again: it reads the store and does only what is still missing.`);
throw e;
}
log(o.tag, `${describeReq(r)}: done`); log(o.tag, `${describeReq(r)}: done`);
} }
return out; return out;

View file

@ -16,12 +16,13 @@ import { existsSync, readFileSync, writeFileSync } from 'node:fs';
import { type AppleCopy, parseAppStoreListing } from '../lib/copy'; import { type AppleCopy, parseAppStoreListing } from '../lib/copy';
import { compactDiff } from '../lib/diff'; import { compactDiff } from '../lib/diff';
import { mergeAppleListing, overLimits } from '../lib/listing-pull'; import { mergeAppleListing, overLimits } from '../lib/listing-pull';
import { counts, draftReason, draftSections, type ItemPlan, itemLines, planLocales, planVersion, short, type Skipped, type VersionPlan } from '../lib/listing-plan'; import { counts, draftReason, draftSections, type ItemPlan, itemLines, planItem, planLocales, planVersion, short, type Skipped, type VersionPlan } from '../lib/listing-plan';
import { createOnce, findOrCreate, getById } from '../lib/upsert';
import { appleName } from '../lib/config'; import { appleName } from '../lib/config';
import type { Ctx } from '../lib/ctx'; import type { Ctx } from '../lib/ctx';
import { confirm, need } from '../lib/ctx'; import { confirm, need } from '../lib/ctx';
import { done, log, printJson, warn } from '../lib/log'; import { done, log, printJson, warn } from '../lib/log';
import { ascClient, type J } from './asc'; import { ascClient, EDITABLE, type J } from './asc';
export function readAppleCopy(ctx: Ctx) { export function readAppleCopy(ctx: Ctx) {
const { config, resolve } = ctx.loaded; const { config, resolve } = ctx.loaded;
@ -156,15 +157,21 @@ export function listingPlanLines(p: AppleListingPlan): string[] {
return out; return out;
} }
async function applyLocales(c: Asc, type: string, parentRel: string, parentType: string, parentId: string, plan: ItemPlan[]): Promise<void> { async function applyLocales(c: Asc, type: string, parentRel: string, parentType: string, parentId: string, listPath: string, plan: ItemPlan[]): Promise<void> {
const patch = async (key: string, id: string, changes: ItemPlan['changes']) => {
await c.asc('PATCH', `/v1/${type}/${id}`, { data: { type, id, attributes: Object.fromEntries(changes.map(ch => [ch.field, ch.to ?? ''])) } });
log('apple', `${type} ${key}: updated ${changes.map(ch => ch.field).join(', ')}`);
};
for (const l of plan) { for (const l of plan) {
if (l.action === 'create') { if (l.action === 'create') {
await c.asc('POST', `/v1/${type}`, { data: { type, attributes: { locale: l.key, ...l.attributes }, relationships: { [parentRel]: { data: { type: parentType, id: parentId } } } } }); // a locale is unique per parent: a 409 (or a lost answer) finds the existing one, which is then updated, never created twice
log('apple', `${type} ${l.key}: created`); const made = await createOnce<J>({ what: `${type} ${l.key}`, tag: 'apple', unique: true,
} else if (l.action === 'update') { find: async () => (await c.all(`${listPath}?limit=200`)).find((x: J) => x.attributes.locale === l.key) ?? null,
await c.asc('PATCH', `/v1/${type}/${l.id}`, { data: { type, id: l.id, attributes: Object.fromEntries(l.changes.map(ch => [ch.field, ch.to ?? ''])) } }); create: async () => (await c.asc('POST', `/v1/${type}`, { data: { type, attributes: { locale: l.key, ...l.attributes }, relationships: { [parentRel]: { data: { type: parentType, id: parentId } } } } })).data });
log('apple', `${type} ${l.key}: updated ${l.changes.map(ch => ch.field).join(', ')}`); if (made.created) { log('apple', `${type} ${l.key}: created`); continue; }
} const again = planItem(l.key, { id: made.value.id, attributes: made.value.attributes ?? {} }, l.attributes);
if (again.action === 'update') await patch(l.key, made.value.id, again.changes);
} else if (l.action === 'update') await patch(l.key, l.id!, l.changes);
} }
} }
@ -189,9 +196,21 @@ export async function run(ctx: Ctx): Promise<void> {
const vp = p.versionPlan; const vp = p.versionPlan;
let versionId = vp.id; let versionId = vp.id;
if (vp.action === 'create') { if (vp.action === 'create') {
versionId = (await c.asc('POST', '/v1/appStoreVersions', { data: { type: 'appStoreVersions', // look again right before the create (another run, or a lost answer), then the journal for the list lag
attributes: Object.fromEntries(Object.entries(vp.attributes)), relationships: { app: { data: { type: 'apps', id: appId } } } } })).data.id; const made = await findOrCreate<J>({ what: `App Store version ${version}`, tag: 'apple', unique: true,
log('apple', `version ${version} created`); find: async () => (await c.all(`/v1/apps/${appId}/appStoreVersions?filter[platform]=IOS&limit=50`))
.find((x: J) => x.attributes.versionString === version && EDITABLE.has(x.attributes.appVersionState ?? x.attributes.appStoreState)) ?? null,
journal: { root, key: `apple:${appId}:version:IOS:${version}`, byId: id => getById(c.asc, `/v1/appStoreVersions/${id}`) as Promise<J | null>, idOf: v => v.id },
create: async () => (await c.asc('POST', '/v1/appStoreVersions', { data: { type: 'appStoreVersions',
attributes: Object.fromEntries(Object.entries(vp.attributes)), relationships: { app: { data: { type: 'apps', id: appId } } } } })).data });
versionId = made.value.id;
if (made.created) log('apple', `version ${version} created`);
else {
const { platform: _p, ...want } = vp.attributes;
const again = planItem(version, { id: versionId!, attributes: made.value.attributes ?? {} }, want);
if (again.action === 'update') await c.asc('PATCH', `/v1/appStoreVersions/${versionId}`, { data: { type: 'appStoreVersions', id: versionId, attributes: Object.fromEntries(again.changes.map(ch => [ch.field, ch.to])) } });
log('apple', `version ${version} exists already (an earlier run made it): using it${again.action === 'update' ? `, updated ${again.changes.map(ch => ch.field).join(', ')}` : ''}`);
}
} else if (vp.action === 'update') { } else if (vp.action === 'update') {
await c.asc('PATCH', `/v1/appStoreVersions/${versionId}`, { data: { type: 'appStoreVersions', id: versionId, attributes: Object.fromEntries(vp.changes.map(ch => [ch.field, ch.to])) } }); await c.asc('PATCH', `/v1/appStoreVersions/${versionId}`, { data: { type: 'appStoreVersions', id: versionId, attributes: Object.fromEntries(vp.changes.map(ch => [ch.field, ch.to])) } });
log('apple', `version: ${vp.changes.map(ch => `${ch.field} ${ch.from ?? '(none)'} -> ${ch.to}`).join(', ')}`); log('apple', `version: ${vp.changes.map(ch => `${ch.field} ${ch.from ?? '(none)'} -> ${ch.to}`).join(', ')}`);
@ -200,8 +219,9 @@ export async function run(ctx: Ctx): Promise<void> {
await c.asc('PATCH', `/v1/appStoreVersions/${versionId}/relationships/build`, { data: { type: 'builds', id: p.build.id } }); await c.asc('PATCH', `/v1/appStoreVersions/${versionId}/relationships/build`, { data: { type: 'builds', id: p.build.id } });
log('apple', `build ${version} (${p.build.number}) attached`); log('apple', `build ${version} (${p.build.number}) attached`);
} }
await applyLocales(c, 'appInfoLocalizations', 'appInfo', 'appInfos', p.appInfo.id, p.appInfo.locales); await applyLocales(c, 'appInfoLocalizations', 'appInfo', 'appInfos', p.appInfo.id, `/v1/appInfos/${p.appInfo.id}/appInfoLocalizations`, p.appInfo.locales);
const vl = p.versionLocales ?? planLocales(await c.all(`/v1/appStoreVersions/${versionId}/appStoreVersionLocalizations?limit=200`), p.wanted); const vlPath = `/v1/appStoreVersions/${versionId}/appStoreVersionLocalizations`;
await applyLocales(c, 'appStoreVersionLocalizations', 'appStoreVersion', 'appStoreVersions', versionId!, vl); const vl = p.versionLocales ?? planLocales(await c.all(`${vlPath}?limit=200`), p.wanted);
await applyLocales(c, 'appStoreVersionLocalizations', 'appStoreVersion', 'appStoreVersions', versionId!, vlPath, vl);
done('apple', 'listing done'); done('apple', 'listing done');
} }

View file

@ -17,6 +17,7 @@ import { applePoints, pickPoint } from '../lib/subscriptions';
import { pages } from '../prices'; import { pages } from '../prices';
import { ascClient, type J } from './asc'; import { ascClient, type J } from './asc';
import { describeReq, execute, type Req } from './jsonapi'; import { describeReq, execute, type Req } from './jsonapi';
import { findOrCreate, getById } from '../lib/upsert';
const TAG = 'offer-codes'; const TAG = 'offer-codes';
@ -60,7 +61,16 @@ export async function run(ctx: Ctx): Promise<void> {
const body = iapOfferCodeBody(iap.id, oc, await prices(oc)); const body = iapOfferCodeBody(iap.id, oc, await prices(oc));
const req: Req = { method: 'POST', path: '/v1/inAppPurchaseOfferCodes', body, note: `${p.id}: campaign "${oc.name}" (${oc.free ? 'free' : `USD ${oc.usd}`}, ${body.included.length} territories)` }; const req: Req = { method: 'POST', path: '/v1/inAppPurchaseOfferCodes', body, note: `${p.id}: campaign "${oc.name}" (${oc.free ? 'free' : `USD ${oc.usd}`}, ${body.included.length} territories)` };
if (dry) log(TAG, `would ${describeReq(req)}`); if (dry) log(TAG, `would ${describeReq(req)}`);
else { id = ((await execute(c.asc, [req], { dry, tag: TAG }))[0] as J).data.id; active++; } else {
// campaign names are not known to be unique: looked up right before the create, never sent twice
const made = await findOrCreate<J>({ what: `${p.id} campaign "${oc.name}"`, tag: TAG,
find: async () => (await c.all(`/v2/inAppPurchases/${iap.id}/offerCodes?limit=200`)).find((x: J) => x.attributes.name === oc.name) ?? null,
journal: { root, key: `apple:${appId}:iapOfferCode:${p.id}:${oc.name}`, byId: x => getById(c.asc, `/v1/inAppPurchaseOfferCodes/${x}`) as Promise<J | null>, idOf: v => v.id },
create: async () => (await c.asc('POST', req.path, req.body)).data });
id = made.value.id;
log(TAG, `${describeReq(req)}: ${made.created ? 'done' : 'exists already'}`);
if (made.created) active++;
}
} else log(TAG, `${p.id}: campaign "${oc.name}" exists (${cur.attributes.active ? 'active' : 'inactive'}, ${cur.attributes.productionCodeCount ?? 0} production codes)`); } else log(TAG, `${p.id}: campaign "${oc.name}" exists (${cur.attributes.active ? 'active' : 'inactive'}, ${cur.attributes.productionCodeCount ?? 0} production codes)`);
const haveCustom = new Set(cur ? (await c.all(`/v1/inAppPurchaseOfferCodes/${cur.id}/customCodes?limit=200`)).map((x: J) => x.attributes.customCode) : []); const haveCustom = new Set(cur ? (await c.all(`/v1/inAppPurchaseOfferCodes/${cur.id}/customCodes?limit=200`)).map((x: J) => x.attributes.customCode) : []);
for (const cc of oc.customCodes ?? []) if (!haveCustom.has(cc.code)) codeReqs.push({ req: iapCustomCodeReq(id ?? '${new campaign}', cc), product: p.id, campaign: oc.name, oneTime: false }); for (const cc of oc.customCodes ?? []) if (!haveCustom.has(cc.code)) codeReqs.push({ req: iapCustomCodeReq(id ?? '${new campaign}', cc), product: p.id, campaign: oc.name, oneTime: false });
@ -78,8 +88,8 @@ export async function run(ctx: Ctx): Promise<void> {
if (!(await confirm(ctx, `Create ${codeReqs.length} code request(s) (${codeReqs.map(x => x.req.note).join('; ')})? Codes count against the 1 million per quarter and can be redeemed by anyone who gets them.`))) return; if (!(await confirm(ctx, `Create ${codeReqs.length} code request(s) (${codeReqs.map(x => x.req.note).join('; ')})? Codes count against the 1 million per quarter and can be redeemed by anyone who gets them.`))) return;
const dir = resolve('.storebound/offer-codes'); const dir = resolve('.storebound/offer-codes');
for (const x of codeReqs) { for (const x of codeReqs) {
const out = (await execute(c.asc, [x.req], { dry: false, tag: TAG }))[0] as J; const out = (await execute(c.asc, [x.req], { dry: false, tag: TAG }))[0] as J | null;
if (!x.oneTime) continue; if (!x.oneTime || !out) continue;
// the codes of a one-time-use batch (text/csv); saved locally, never printed // the codes of a one-time-use batch (text/csv); saved locally, never printed
const values = await c.bytes(`/v1/inAppPurchaseOfferCodeOneTimeUseCodes/${out.data.id}/values`, 'text/csv'); const values = await c.bytes(`/v1/inAppPurchaseOfferCodeOneTimeUseCodes/${out.data.id}/values`, 'text/csv');
if (!values) { warn(TAG, `${x.product} "${x.campaign}": codes not ready yet; download them in App Store Connect`); continue; } if (!values) { warn(TAG, `${x.product} "${x.campaign}": codes not ready yet; download them in App Store Connect`); continue; }

View file

@ -21,7 +21,8 @@ import { textTable } from '../lib/table';
import { numberedImages } from '../play/images'; import { numberedImages } from '../play/images';
import { variantRoot } from '../shots/lib'; import { variantRoot } from '../shots/lib';
import { type Asc, ascClient, type J } from './asc'; import { type Asc, ascClient, type J } from './asc';
import { execute } from './jsonapi'; import { describeReq, execute } from './jsonapi';
import { findOrCreate, getById } from '../lib/upsert';
import { displaySets, syncScreenshotSet } from './screenshots'; import { displaySets, syncScreenshotSet } from './screenshots';
const TAG = 'ppo'; const TAG = 'ppo';
@ -78,12 +79,29 @@ export async function run(ctx: Ctx): Promise<void> {
let exp = pickExperiment(list, name); let exp = pickExperiment(list, name);
if (!exp) { if (!exp) {
if (dry) { await execute(c.asc, [createExperimentReq(appId, name, traffic)], { dry, tag: TAG }); log(TAG, `then: ${variants.length} treatment(s) x ${locales.length} locale(s)`); done(TAG, 'create checked (dry run)'); return; } if (dry) { await execute(c.asc, [createExperimentReq(appId, name, traffic)], { dry, tag: TAG }); log(TAG, `then: ${variants.length} treatment(s) x ${locales.length} locale(s)`); done(TAG, 'create checked (dry run)'); return; }
const r = (await execute(c.asc, [createExperimentReq(appId, name, traffic)], { dry, tag: TAG }))[0] as J; // test names are not known to be unique: looked up right before the create (and in the journal), never sent twice
exp = experimentFromApi(r.data); const req = createExperimentReq(appId, name, traffic);
const made = await findOrCreate<J>({ what: `experiment "${name}"`, tag: TAG,
find: async () => (await c.all(`/v1/apps/${appId}/appStoreVersionExperimentsV2?limit=50`)).find((x: J) => x.attributes.name === name && !['COMPLETED', 'STOPPED'].includes(x.attributes.state)) ?? null,
journal: { root, key: `apple:${appId}:ppo:${name}`, byId: id => getById(c.asc, `/v2/appStoreVersionExperiments/${id}`) as Promise<J | null>, idOf: v => v.id },
create: async () => (await c.asc('POST', req.path, req.body)).data });
log(TAG, `${describeReq(req)}: ${made.created ? 'done' : 'exists already'}`);
exp = experimentFromApi(made.value);
} else log(TAG, `experiment "${name}": exists (${exp.state})`); } else log(TAG, `experiment "${name}": exists (${exp.state})`);
const ts = await treatments(c, exp.id); const ts = await treatments(c, exp.id);
const created = (await execute(c.asc, planTreatments(exp.id, ts.map((t: J) => ({ id: t.id, name: t.attributes.name })), variants), { dry, tag: TAG })) as J[]; const planned = planTreatments(exp.id, ts.map((t: J) => ({ id: t.id, name: t.attributes.name })), variants);
const all = [...ts, ...created.map(r => r.data)].filter(t => variants.includes(t.attributes.name)); const all: J[] = ts.filter((t: J) => variants.includes(t.attributes.name));
if (dry) await execute(c.asc, planned, { dry, tag: TAG });
else for (const r of planned) {
const tname = (r.body as J).data.attributes.name as string;
const expId = exp.id;
const made = await findOrCreate<J>({ what: `treatment "${tname}"`, tag: TAG,
find: async () => (await treatments(c, expId)).find((t: J) => t.attributes.name === tname) ?? null,
journal: { root, key: `apple:${appId}:ppoTreatment:${expId}:${tname}`, byId: id => getById(c.asc, `/v1/appStoreVersionExperimentTreatments/${id}`) as Promise<J | null>, idOf: v => v.id },
create: async () => (await c.asc('POST', r.path, r.body)).data });
log(TAG, `${describeReq(r)}: ${made.created ? 'done' : 'exists already'}`);
all.push(made.value);
}
for (const t of all) await execute(c.asc, planTreatmentLocales(t.id, (await treatmentLocs(c, t.id)).map((l: J) => l.attributes.locale), locales), { dry, tag: TAG }); for (const t of all) await execute(c.asc, planTreatmentLocales(t.id, (await treatmentLocs(c, t.id)).map((l: J) => l.attributes.locale), locales), { dry, tag: TAG });
done(TAG, dry ? 'create checked (dry run)' : `test "${name}" ready: storebound apple ppo upload --variant ${variants[0]}`); done(TAG, dry ? 'create checked (dry run)' : `test "${name}" ready: storebound apple ppo upload --variant ${variants[0]}`);
return; return;

View file

@ -16,7 +16,8 @@ import { done, log, warn } from '../lib/log';
import { ffprobe } from '../shots/report'; import { ffprobe } from '../shots/report';
import { checkVideo, PREVIEW_RULES, VIDEO_TARGETS } from '../shots/spec'; import { checkVideo, PREVIEW_RULES, VIDEO_TARGETS } from '../shots/spec';
import { timeCode } from '../shots/video'; import { timeCode } from '../shots/video';
import { ascClient, md5, setUpToDate, type J } from './asc'; import { findOrCreate } from '../lib/upsert';
import { ascClient, keptPrefix, md5, setUpToDate, type J } from './asc';
/** Video target folder -> ASC previewType, for this project (iPad only with apple.ipad). */ /** Video target folder -> ASC previewType, for this project (iPad only with apple.ipad). */
export const previewSets = (apple: { ipad?: boolean } | undefined): Record<string, string> => export const previewSets = (apple: { ipad?: boolean } | undefined): Record<string, string> =>
@ -100,15 +101,22 @@ export async function run(ctx: Ctx): Promise<void> {
log('apple', `${tag}: poster frame ${tc}`); log('apple', `${tag}: poster frame ${tc}`);
continue; continue;
} }
if (dry) { log('apple', `${tag}: would replace ${existing.length} with ${fs.length}`); continue; } const kept = keptPrefix(existing.map(p => ({ checksum: p.attributes.sourceFileChecksum, state: state(p) })), want);
if (!set) set = (await c.asc('POST', '/v1/appPreviewSets', { data: { type: 'appPreviewSets', attributes: { previewType: type }, if (dry) { log('apple', `${tag}: would keep ${kept}, replace ${existing.length - kept} with ${fs.length - kept}`); continue; }
relationships: { appStoreVersionLocalization: { data: { type: 'appStoreVersionLocalizations', id: loc.id } } } } })).data; if (!set) {
for (const p of existing) await c.asc('DELETE', `/v1/appPreviews/${p.id}`); const r = await findOrCreate<J>({ what: `${tag} preview set`, tag: 'apple', unique: true,
for (const f of fs) { find: async () => (await c.all(`/v1/appStoreVersionLocalizations/${loc.id}/appPreviewSets`)).find((s: J) => s.attributes.previewType === type) ?? null,
log('apple', `${tag}: uploading ${f.slice(src.length + 1)}`); create: async () => (await c.asc('POST', '/v1/appPreviewSets', { data: { type: 'appPreviewSets', attributes: { previewType: type },
await c.uploadAsset('appPreviews', { appPreviewSet: { data: { type: 'appPreviewSets', id: set.id } } }, f, previewAttrs(f, poster)); relationships: { appStoreVersionLocalization: { data: { type: 'appStoreVersionLocalizations', id: loc.id } } } } })).data });
set = r.value;
if (!r.created) existing.push(...await c.all(`/v1/appPreviewSets/${set.id}/appPreviews`));
} }
log('apple', `${tag}: uploaded ${fs.length} (removed ${existing.length})`); const r = await c.syncAssets({ set: { type: 'appPreviewSets', id: set.id }, existing, files: fs, want, state, attrs: f => previewAttrs(f, poster),
onUpload: f => log('apple', `${tag}: uploading ${f.slice(src.length + 1)}`) });
const tc = timeCode(poster);
for (const p of existing.slice(0, r.kept).filter(p => p.attributes.previewFrameTimeCode && p.attributes.previewFrameTimeCode !== tc))
await c.asc('PATCH', `/v1/appPreviews/${p.id}`, { data: { type: 'appPreviews', id: p.id, attributes: { previewFrameTimeCode: tc } } });
log('apple', `${tag}: uploaded ${r.uploaded}${r.kept ? `, kept ${r.kept}` : ''} (removed ${r.removed})`);
} }
} }
if (dry) return done('apple', 'previews checked (dry run, nothing sent)'); if (dry) return done('apple', 'previews checked (dry run, nothing sent)');

View file

@ -10,6 +10,7 @@ import type { Ctx } from '../lib/ctx';
import { confirm, need } from '../lib/ctx'; import { confirm, need } from '../lib/ctx';
import { done, log } from '../lib/log'; import { done, log } from '../lib/log';
import { type PhasedAction, phasedRequest } from '../lib/release'; import { type PhasedAction, phasedRequest } from '../lib/release';
import { createOnce } from '../lib/upsert';
import { ascClient, type J } from './asc'; import { ascClient, type J } from './asc';
export async function run(ctx: Ctx): Promise<void> { export async function run(ctx: Ctx): Promise<void> {
@ -50,8 +51,11 @@ export async function run(ctx: Ctx): Promise<void> {
if (dry) { log('apple', `${tag}: would ${req.method === 'POST' ? 'turn phased release on' : `set phased release ${req.state}`} (dry)`); return; } if (dry) { log('apple', `${tag}: would ${req.method === 'POST' ? 'turn phased release on' : `set phased release ${req.state}`} (dry)`); return; }
if ((phased === 'resume' || phased === 'complete') && !(await confirm(ctx, `${phased === 'complete' ? 'Release' : 'Resume the phased release of'} ${tag} ${phased === 'complete' ? 'to every user now' : ''}?`))) return; if ((phased === 'resume' || phased === 'complete') && !(await confirm(ctx, `${phased === 'complete' ? 'Release' : 'Resume the phased release of'} ${tag} ${phased === 'complete' ? 'to every user now' : ''}?`))) return;
if (req.method === 'POST') { if (req.method === 'POST') {
await c.asc('POST', '/v1/appStoreVersionPhasedReleases', { data: { type: 'appStoreVersionPhasedReleases', // one phased release per version: a 409 or an unclear answer is checked by reading it back, never re-sent blindly
relationships: { appStoreVersion: { data: { type: 'appStoreVersions', id: v.id } } } } }); await createOnce({ what: `phased release of ${tag}`, tag: 'apple', unique: true,
find: async () => (await c.maybe(`/v1/appStoreVersions/${v.id}/appStoreVersionPhasedRelease`))?.data ?? null,
create: async () => (await c.asc('POST', '/v1/appStoreVersionPhasedReleases', { data: { type: 'appStoreVersionPhasedReleases',
relationships: { appStoreVersion: { data: { type: 'appStoreVersions', id: v.id } } } } })).data });
} else { } else {
await c.asc('PATCH', `/v1/appStoreVersionPhasedReleases/${cur!.id}`, { data: { type: 'appStoreVersionPhasedReleases', id: cur!.id, attributes: { phasedReleaseState: req.state } } }); await c.asc('PATCH', `/v1/appStoreVersionPhasedReleases/${cur!.id}`, { data: { type: 'appStoreVersionPhasedReleases', id: cur!.id, attributes: { phasedReleaseState: req.state } } });
} }

View file

@ -10,6 +10,7 @@ import { need } from '../lib/ctx';
import { reviewContact, reviewDemoAccount } from '../lib/credentials'; import { reviewContact, reviewDemoAccount } from '../lib/credentials';
import { reviewDetailPlan } from '../lib/review-details'; import { reviewDetailPlan } from '../lib/review-details';
import { done, log } from '../lib/log'; import { done, log } from '../lib/log';
import { createOnce } from '../lib/upsert';
import { ascClient } from './asc'; import { ascClient } from './asc';
export async function run(ctx: Ctx): Promise<void> { export async function run(ctx: Ctx): Promise<void> {
@ -34,9 +35,17 @@ export async function run(ctx: Ctx): Promise<void> {
const { attributes, summary } = reviewDetailPlan(config, contact, reviewDemoAccount(root)); const { attributes, summary } = reviewDetailPlan(config, contact, reviewDemoAccount(root));
if (ctx.args.has('dry')) { log('apple', `would set review details (contact ${from ? 'copied from ' + from : 'from credentials'}, ${summary})`); return; } if (ctx.args.has('dry')) { log('apple', `would set review details (contact ${from ? 'copied from ' + from : 'from credentials'}, ${summary})`); return; }
const cur = (await c.maybe(`/v1/appStoreVersions/${version.id}/appStoreReviewDetail`))?.data; const read = async () => (await c.maybe(`/v1/appStoreVersions/${version.id}/appStoreReviewDetail`))?.data ?? null;
if (cur) await c.asc('PATCH', `/v1/appStoreReviewDetails/${cur.id}`, { data: { type: 'appStoreReviewDetails', id: cur.id, attributes } }); const cur = await read();
else await c.asc('POST', '/v1/appStoreReviewDetails', { data: { type: 'appStoreReviewDetails', attributes, // one review detail per version: only changed fields are sent; a new one is created once (a 409 finds it)
relationships: { appStoreVersion: { data: { type: 'appStoreVersions', id: version.id } } } } }); const changed = cur ? Object.fromEntries(Object.entries(attributes).filter(([k, v]) => (cur.attributes?.[k] ?? null) !== (v ?? null))) : attributes;
if (cur && !Object.keys(changed).length) { done('apple', `review details on ${version.attributes.versionString} up to date (${summary})`); return; }
if (cur) await c.asc('PATCH', `/v1/appStoreReviewDetails/${cur.id}`, { data: { type: 'appStoreReviewDetails', id: cur.id, attributes: changed } });
else {
const r = await createOnce({ what: 'App Review details', tag: 'apple', unique: true, find: read,
create: async () => (await c.asc('POST', '/v1/appStoreReviewDetails', { data: { type: 'appStoreReviewDetails', attributes,
relationships: { appStoreVersion: { data: { type: 'appStoreVersions', id: version.id } } } } })).data });
if (!r.created) await c.asc('PATCH', `/v1/appStoreReviewDetails/${r.value.id}`, { data: { type: 'appStoreReviewDetails', id: r.value.id, attributes } });
}
done('apple', `review details set on ${version.attributes.versionString} (${summary})`); done('apple', `review details set on ${version.attributes.versionString} (${summary})`);
} }

View file

@ -14,7 +14,8 @@ import { byAppleLocale, need } from '../lib/ctx';
import { done, log, warn } from '../lib/log'; import { done, log, warn } from '../lib/log';
import { numberedImages } from '../play/images'; import { numberedImages } from '../play/images';
import { variantRoot } from '../shots/lib'; import { variantRoot } from '../shots/lib';
import { type Asc, ascClient, md5, setUpToDate, type J } from './asc'; import { findOrCreate } from '../lib/upsert';
import { type Asc, ascClient, keptPrefix, md5, setUpToDate, type J } from './asc';
/** Render size key -> ASC screenshotDisplayType (iPhone sets). */ /** Render size key -> ASC screenshotDisplayType (iPhone sets). */
export const DISPLAY: Record<string, string> = { ios69: 'APP_IPHONE_67', ios65: 'APP_IPHONE_65' }; export const DISPLAY: Record<string, string> = { ios69: 'APP_IPHONE_67', ios65: 'APP_IPHONE_65' };
@ -33,19 +34,32 @@ export const displaySets = (apple: { ipad?: boolean; iphoneMedium?: boolean } |
export const requiredDisplayTypes = (apple: { ipad?: boolean; screenshotTypes?: string[] } | undefined): string[] => export const requiredDisplayTypes = (apple: { ipad?: boolean; screenshotTypes?: string[] } | undefined): string[] =>
apple?.screenshotTypes ?? ['APP_IPHONE_67', ...(apple?.ipad ? ['APP_IPAD_PRO_3GEN_129'] : [])]; apple?.screenshotTypes ?? ['APP_IPHONE_67', ...(apple?.ipad ? ['APP_IPAD_PRO_3GEN_129'] : [])];
/** Makes one screenshot set (of a version localization, or of a Product Page Optimization treatment localization) /** Makes one screenshot set (of a version localization, a custom product page localization or a Product Page
* hold exactly `files`, in order: left alone when the MD5s match, else emptied and re-uploaded. Creates the set. */ * Optimization treatment localization) hold exactly `files`, in order: left alone when the MD5s match, else the
* matching leading screenshots stay and the rest is replaced (c.syncAssets). The set is created when missing, after a
* fresh look (one set per display type: a 409 finds the existing one). */
export async function syncScreenshotSet(c: Asc, parent: { rel: string; type: string; id: string }, sets: J[], type: string, files: string[], tag: string, dry: boolean): Promise<void> { export async function syncScreenshotSet(c: Asc, parent: { rel: string; type: string; id: string }, sets: J[], type: string, files: string[], tag: string, dry: boolean): Promise<void> {
let set = sets.find(s => s.attributes.screenshotDisplayType === type); let set = sets.find(s => s.attributes.screenshotDisplayType === type);
const existing = set ? await c.all(`/v1/appScreenshotSets/${set.id}/appScreenshots`) : []; const existing = set ? await c.all(`/v1/appScreenshotSets/${set.id}/appScreenshots?limit=50`) : [];
const want = files.map(f => md5(readFileSync(f))); const want = files.map(f => md5(readFileSync(f)));
if (set && setUpToDate(existing.map(s => ({ checksum: s.attributes.sourceFileChecksum, state: s.attributes.assetDeliveryState?.state })), want)) { log('apple', `${tag}: ${files.length} up to date`); return; } const state = (s: J) => s.attributes.assetDeliveryState?.state;
if (dry) { log('apple', `${tag}: would ${set ? `replace ${existing.length}` : 'create the set and upload'} with ${files.length}`); return; } if (set && setUpToDate(existing.map(s => ({ checksum: s.attributes.sourceFileChecksum, state: state(s) })), want)) { log('apple', `${tag}: ${files.length} up to date`); return; }
if (!set) set = (await c.asc('POST', '/v1/appScreenshotSets', { data: { type: 'appScreenshotSets', attributes: { screenshotDisplayType: type }, if (dry) {
relationships: { [parent.rel]: { data: { type: parent.type, id: parent.id } } } } })).data; const k = keptPrefix(existing.map(s => ({ checksum: s.attributes.sourceFileChecksum, state: state(s) })), want);
for (const s of existing) await c.asc('DELETE', `/v1/appScreenshots/${s.id}`); log('apple', `${tag}: would ${set ? `keep ${k}, replace ${existing.length - k}` : 'create the set and upload'} with ${files.length - k} new`);
for (const f of files) await c.uploadAsset('appScreenshots', { appScreenshotSet: { data: { type: 'appScreenshotSets', id: set.id } } }, f); return;
log('apple', `${tag}: uploaded ${files.length} (removed ${existing.length})`); }
if (!set) {
const find = async () => (await c.all(`/v1/${parent.type}/${parent.id}/appScreenshotSets?limit=50`)).find((s: J) => s.attributes.screenshotDisplayType === type) ?? null;
const r = await findOrCreate<J>({ what: `${tag} screenshot set`, tag: 'apple', unique: true, find,
create: async () => (await c.asc('POST', '/v1/appScreenshotSets', { data: { type: 'appScreenshotSets', attributes: { screenshotDisplayType: type },
relationships: { [parent.rel]: { data: { type: parent.type, id: parent.id } } } } })).data });
set = r.value;
// a set found now (made by an earlier run the first read missed) may already hold screenshots
if (!r.created) existing.push(...await c.all(`/v1/appScreenshotSets/${set.id}/appScreenshots?limit=50`));
}
const r = await c.syncAssets({ set: { type: 'appScreenshotSets', id: set.id }, existing, files, want, state });
log('apple', `${tag}: uploaded ${r.uploaded}${r.kept ? `, kept ${r.kept}` : ''} (removed ${r.removed})`);
} }
export async function run(ctx: Ctx): Promise<void> { export async function run(ctx: Ctx): Promise<void> {

View file

@ -23,6 +23,7 @@ import {
import { textTable } from '../lib/table'; import { textTable } from '../lib/table';
import { type Asc, ascClient, type J } from './asc'; import { type Asc, ascClient, type J } from './asc';
import { describeReq, execute, type Req } from './jsonapi'; import { describeReq, execute, type Req } from './jsonapi';
import { createOnce, isAmbiguous, upsertTiming } from '../lib/upsert';
const TAG = 'signing'; const TAG = 'signing';
const USAGE = 'usage: storebound apple signing status | ensure [--dry] [--keychain [path]] [--no-install]'; const USAGE = 'usage: storebound apple signing status | ensure [--dry] [--keychain [path]] [--no-install]';
@ -194,9 +195,23 @@ export async function run(ctx: Ctx): Promise<void> {
const csr = buildCsr(key, { commonName: `storebound ${config.app.name}`.slice(0, 64) }); const csr = buildCsr(key, { commonName: `storebound ${config.app.name}`.slice(0, 64) });
created = (await c.asc('POST', '/v1/certificates', certificateReq(certType, csrContent(csr)).body)).data; created = (await c.asc('POST', '/v1/certificates', certificateReq(certType, csrContent(csr)).body)).data;
} catch (e) { } catch (e) {
rmSync(pending, { force: true }); if (isAmbiguous(e)) {
const why = explainCertError(e, certType); // the certificate may exist: it is the one whose public key is this pending key's. The key stays on disk either
throw new Error(why ? `${(e as Error).message}\n\n${why}` : (e as Error).message); // way (a later run finds the certificate through it), so no certificate is ever orphaned or made twice.
const mine = spkiOfKey(readFileSync(pending, 'utf8'));
let found: J | undefined;
for (let i = 0; i < 4 && !found; i++) {
if (i) await upsertTiming.sleep(upsertTiming.waitMs * i);
found = (await c.all(`/v1/certificates?filter[certificateType]=${certType}&limit=200`)).find((x: ApiCert) => !!x.attributes.certificateContent && certSpki(x.attributes.certificateContent) === mine);
}
if (!found) throw new Error(`${(e as Error).message}\n\nThe certificate request got no clear answer and App Store Connect does not list a certificate for the new key (yet). The key is kept in ${pending.replace(root + '/', '')}; run \`storebound apple signing ensure\` again in a minute: it finds the certificate through that key instead of making another one.`);
created = found;
log(TAG, 'the certificate request went through (found by its public key)');
} else {
rmSync(pending, { force: true });
const why = explainCertError(e, certType);
throw new Error(why ? `${(e as Error).message}\n\n${why}` : (e as Error).message);
}
} }
const keyFile = join(dir, `${created.id}.key`), cerFile = join(dir, `${created.id}.cer`); const keyFile = join(dir, `${created.id}.key`), cerFile = join(dir, `${created.id}.cer`);
renameSync(pending, keyFile); renameSync(pending, keyFile);
@ -212,7 +227,14 @@ export async function run(ctx: Ctx): Promise<void> {
const p = planProfile(list, { bundleId: id, bundleIdId: b.id, certId: cert!.id, type: profileType }); const p = planProfile(list, { bundleId: id, bundleIdId: b.id, certId: cert!.id, type: profileType });
let prof: ApiProfile; let prof: ApiProfile;
if (p.create) { if (p.create) {
const r = (await c.asc('POST', p.create.path, p.create.body)).data; // an unclear answer is checked by reading the bundle id's profiles again (one active profile with this
// certificate is reused), never re-sent blindly
const r = (await createOnce<J>({ what: `${id} profile`, tag: TAG,
find: async () => {
const again = planProfile(await profilesOf(c, b.id, profileType), { bundleId: id, bundleIdId: b.id, certId: cert!.id, type: profileType });
return again.reuse ? { id: again.reuse.id, attributes: (await c.asc('GET', `/v1/profiles/${again.reuse.id}`)).data.attributes } : null;
},
create: async () => (await c.asc('POST', p.create!.path, p.create!.body)).data })).value;
prof = { id: r.id, attributes: r.attributes }; prof = { id: r.id, attributes: r.attributes };
log(TAG, `${id}: profile "${prof.attributes.name}" created (${prof.attributes.uuid})`); log(TAG, `${id}: profile "${prof.attributes.name}" created (${prof.attributes.uuid})`);
} else { } else {

47
cli/apple/submission.ts Normal file
View file

@ -0,0 +1,47 @@
// The app's one draft review submission and the items in it, without duplicates: a draft (READY_FOR_REVIEW) is
// reused, a new one is created only after a fresh look (Apple keeps one open draft per app and platform), and an item
// already in the draft is not added again (a 409 or a re-read finds it). Used by iap-submit, cpp submit and events
// submit. Submitting itself (PATCH submitted: true) stays the owner's confirmed step in those commands.
import { log } from '../lib/log';
import { createOnce, findOrCreate } from '../lib/upsert';
import type { Asc, J } from './asc';
import { one } from './jsonapi';
/** The draft review submission of the app (READY_FOR_REVIEW, iOS), or null. */
export async function findDraft(c: Asc, appId: string): Promise<string | null> {
return (await c.all(`/v1/reviewSubmissions?filter[app]=${appId}&filter[platform]=IOS&filter[state]=READY_FOR_REVIEW&limit=10`))[0]?.id ?? null;
}
/** The draft's id: the existing one, else a new draft created once. */
export async function draftSubmission(c: Asc, appId: string, root: string, tag: string): Promise<string> {
const r = await findOrCreate<string>({ what: 'draft review submission', tag, unique: true,
find: () => findDraft(c, appId),
journal: { root, key: `apple:${appId}:reviewSubmission:draft`, idOf: id => id,
byId: async id => { const d = (await c.maybe(`/v1/reviewSubmissions/${id}`))?.data; return d?.attributes?.state === 'READY_FOR_REVIEW' ? id : null; } },
create: async () => (await c.asc('POST', '/v1/reviewSubmissions', { data: { type: 'reviewSubmissions', attributes: { platform: 'IOS' }, relationships: { app: one('apps', appId) } } })).data.id as string });
if (r.created) log(tag, `draft review submission ${r.value} created`);
return r.value;
}
const TYPE: Record<string, string> = {
appStoreVersion: 'appStoreVersions', inAppPurchaseVersion: 'inAppPurchaseVersions', subscriptionVersion: 'subscriptionVersions',
subscriptionGroupVersion: 'subscriptionGroupVersions', appCustomProductPageVersion: 'appCustomProductPageVersions', appEvent: 'appEvents',
appStoreVersionExperiment: 'appStoreVersionExperiments',
};
/** Whether the draft already holds `id` as `kind` (e.g. inAppPurchaseVersion). */
export async function inDraft(c: Asc, sid: string, kind: string, id: string): Promise<boolean> {
const items = await c.all(`/v1/reviewSubmissions/${sid}/items?include=${kind}&limit=200`);
return items.some((it: J) => it.relationships?.[kind]?.data?.id === id);
}
/** Adds one item to the draft unless it is in there already (`checked`: the caller read the draft's items a moment
* ago). true when it was added now. */
export async function addItem(c: Asc, sid: string, kind: string, id: string, label: string, tag: string, o: { checked?: boolean } = {}): Promise<boolean> {
if (!o.checked && await inDraft(c, sid, kind, id)) { log(tag, `${label}: already in draft ${sid}`); return false; }
const r = await createOnce({ what: `${label} in draft ${sid}`, tag, unique: true,
find: async () => ((await inDraft(c, sid, kind, id)) ? id : null),
create: async () => (await c.asc('POST', '/v1/reviewSubmissionItems', { data: { type: 'reviewSubmissionItems',
relationships: { reviewSubmission: one('reviewSubmissions', sid), [kind]: one(TYPE[kind] ?? `${kind}s`, id) } } })).data?.id ?? id });
return r.created;
}

View file

@ -25,7 +25,8 @@ import {
type ApplePoint, pickPoint, type ApplePoint, pickPoint,
} from '../lib/subscriptions'; } from '../lib/subscriptions';
import { pages } from '../prices'; import { pages } from '../prices';
import { type Asc, ascClient, type J } from './asc'; import { createOnce, findOrCreate, getById } from '../lib/upsert';
import { type Asc, ascClient, md5, type J } from './asc';
const TAG = 'apple'; const TAG = 'apple';
@ -68,7 +69,8 @@ export async function run(ctx: Ctx): Promise<void> {
.map(g => ({ ...g, subscriptions: g.subscriptions.filter(s => (!only.length || only.includes(s.id)) && sellsOn(config, s, 'apple')) })).filter(g => g.subscriptions.length); .map(g => ({ ...g, subscriptions: g.subscriptions.filter(s => (!only.length || only.includes(s.id)) && sellsOn(config, s, 'apple')) })).filter(g => g.subscriptions.length);
const dry = ctx.args.has('dry'); const dry = ctx.args.has('dry');
const csv = subscriptionCsv(ctx, 'apple'); const csv = subscriptionCsv(ctx, 'apple');
const c = await ascClient(root, need(config.apple?.bundleId, 'apple.bundleId'), config.apple?.appId); const bundleId = need(config.apple?.bundleId, 'apple.bundleId');
const c = await ascClient(root, bundleId, config.apple?.appId);
const appId = await c.app(); const appId = await c.app();
log(TAG, 'reading subscription groups, subscriptions, localizations, prices, offers'); log(TAG, 'reading subscription groups, subscriptions, localizations, prices, offers');
const state = await readState(c, appId); const state = await readState(c, appId);
@ -87,33 +89,57 @@ export async function run(ctx: Ctx): Promise<void> {
const territories: string[] = (await c.all('/v1/territories?limit=200')).map((t: J) => t.id); const territories: string[] = (await c.all('/v1/territories?limit=200')).map((t: J) => t.id);
for (const g of groups) { for (const g of groups) {
let gid = state.groups.find(x => x.referenceName === g.name)?.id; let gid = state.groups.find(x => x.referenceName === g.name)?.id;
if (!gid) { gid = (await c.asc('POST', '/v1/subscriptionGroups', appleGroupBody(appId, g))).data.id as string; log(TAG, `group "${g.name}" created`); } if (!gid) {
const gLocs = state.groups.find(x => x.id === gid)?.locales ?? []; const r = await findOrCreate<J>({ what: `subscription group "${g.name}"`, tag: TAG, unique: true,
find: async () => (await c.all(`/v1/apps/${appId}/subscriptionGroups?limit=200`)).find((x: J) => x.attributes.referenceName === g.name) ?? null,
journal: { root, key: `apple:${bundleId}:subscriptionGroup:${g.name}`, byId: id => getById(c.asc, `/v1/subscriptionGroups/${id}`) as Promise<J | null>, idOf: v => v.id },
create: async () => (await c.asc('POST', '/v1/subscriptionGroups', appleGroupBody(appId, g))).data });
gid = r.value.id as string;
log(TAG, `group "${g.name}" ${r.created ? 'created' : 'exists'}`);
}
const groupId = gid;
const gLocs = state.groups.find(x => x.id === gid)?.locales ?? (await c.all(`/v1/subscriptionGroups/${gid}/subscriptionGroupLocalizations?limit=200`)).map((l: J) => l.attributes.locale);
for (const [lang, l] of Object.entries(g.listings)) { for (const [lang, l] of Object.entries(g.listings)) {
const loc = locale.get(lang); const loc = locale.get(lang);
if (!loc || gLocs.includes(loc)) continue; if (!loc || gLocs.includes(loc)) continue;
await c.asc('POST', '/v1/subscriptionGroupLocalizations', appleGroupLocBody(gid, loc, l)); await createOnce({ what: `group "${g.name}" ${loc}`, tag: TAG, unique: true,
find: async () => (await c.all(`/v1/subscriptionGroups/${groupId}/subscriptionGroupLocalizations?limit=200`)).find((x: J) => x.attributes.locale === loc) ?? null,
create: async () => (await c.asc('POST', '/v1/subscriptionGroupLocalizations', appleGroupLocBody(groupId, loc, l))).data });
log(TAG, ` group ${loc}`); log(TAG, ` group ${loc}`);
} }
for (const [i, s] of g.subscriptions.entries()) { for (const [i, s] of g.subscriptions.entries()) {
const tag = step(i + 1, g.subscriptions.length, s.id); const tag = step(i + 1, g.subscriptions.length, s.id);
const cur = state.subscriptions.find(x => x.productId === s.id); const cur = state.subscriptions.find(x => x.productId === s.id);
let id = cur?.id; let id = cur?.id;
if (!id) { id = (await c.asc('POST', '/v1/subscriptions', appleSubscriptionBody(gid, s))).data.id as string; log(TAG, `${tag}: created`); } let subLocs = cur?.locales;
if (!id) {
const r = await findOrCreate<J>({ what: `subscription ${s.id}`, tag: TAG, unique: true,
find: async () => (await c.all(`/v1/subscriptionGroups/${groupId}/subscriptions?limit=200`)).find((x: J) => x.attributes.productId === s.id) ?? null,
journal: { root, key: `apple:${bundleId}:subscription:${s.id}`, byId: x => getById(c.asc, `/v1/subscriptions/${x}`) as Promise<J | null>, idOf: v => v.id },
create: async () => (await c.asc('POST', '/v1/subscriptions', appleSubscriptionBody(groupId, s))).data });
id = r.value.id as string;
if (!r.created) subLocs = (await c.all(`/v1/subscriptions/${id}/subscriptionLocalizations?limit=200`)).map((l: J) => l.attributes.locale);
log(TAG, `${tag}: ${r.created ? 'created' : 'exists'}`);
}
const subId = id;
for (const [lang, l] of Object.entries(s.listings)) { for (const [lang, l] of Object.entries(s.listings)) {
const loc = locale.get(lang); const loc = locale.get(lang);
if (!loc || cur?.locales.includes(loc)) continue; if (!loc || subLocs?.includes(loc)) continue;
await c.asc('POST', '/v1/subscriptionLocalizations', appleSubLocBody(id, loc, l)); await createOnce({ what: `${s.id} ${loc}`, tag: TAG, unique: true,
find: async () => (await c.all(`/v1/subscriptions/${subId}/subscriptionLocalizations?limit=200`)).find((x: J) => x.attributes.locale === loc) ?? null,
create: async () => (await c.asc('POST', '/v1/subscriptionLocalizations', appleSubLocBody(subId, loc, l))).data });
log(TAG, ` ${loc}`); log(TAG, ` ${loc}`);
} }
if (!cur?.availability) { await c.asc('POST', '/v1/subscriptionAvailabilities', appleAvailabilityBody(id, territories)); log(TAG, ` available in ${territories.length} territories + new ones`); } if (!cur?.availability && !(await c.maybe(`/v1/subscriptions/${id}/subscriptionAvailability`))?.data) { await c.asc('POST', '/v1/subscriptionAvailabilities', appleAvailabilityBody(id, territories)); log(TAG, ` available in ${territories.length} territories + new ones`); }
// prices: USA point -> equalizations -> overrides / CSV; only what differs from today's prices // prices: USA point -> equalizations -> overrides / CSV; only what differs from today's prices
const usaPoints = applePoints(await pages(c, `/v1/subscriptions/${id}/pricePoints?filter[territory]=USA&include=territory&limit=200`)); const usaPoints = applePoints(await pages(c, `/v1/subscriptions/${id}/pricePoints?filter[territory]=USA&include=territory&limit=200`));
const usa = pickPoint(usaPoints, 'USA', s.usd); const usa = pickPoint(usaPoints, 'USA', s.usd);
const equalized = applePoints(await pages(c, `/v1/subscriptionPricePoints/${usa.id}/equalizations?include=territory&limit=200`)); const equalized = applePoints(await pages(c, `/v1/subscriptionPricePoints/${usa.id}/equalizations?include=territory&limit=200`));
const known: ApplePoint[] = [usa, ...equalized]; const known: ApplePoint[] = [usa, ...equalized];
const current = cur?.hasPrices ? appleSubPrices(await pages(c, `/v1/subscriptions/${id}/prices?include=subscriptionPricePoint,territory&limit=200`)) : []; // a subscription the first read missed (list lag) is read live, so nothing is sent twice
const hasPrices = cur ? cur.hasPrices : !!(await c.asc('GET', `/v1/subscriptions/${id}/prices?limit=1`)).data?.length;
const current = hasPrices ? appleSubPrices(await pages(c, `/v1/subscriptions/${id}/prices?include=subscriptionPricePoint,territory&limit=200`)) : [];
const changes = applePriceChanges(current, appleWantedPrices(s, equalized, csv), csv.filter(r => r.product === s.id)); const changes = applePriceChanges(current, appleWantedPrices(s, equalized, csv), csv.filter(r => r.product === s.id));
if (!changes.length) log(TAG, ' prices up to date'); if (!changes.length) log(TAG, ' prices up to date');
else if (current.length && !liveOk) warn(TAG, ` ${changes.length} price change(s) skipped: run in a terminal to confirm`); else if (current.length && !liveOk) warn(TAG, ` ${changes.length} price change(s) skipped: run in a terminal to confirm`);
@ -140,27 +166,42 @@ export async function run(ctx: Ctx): Promise<void> {
const eq = applePoints(await pages(c, `/v1/subscriptionPricePoints/${ip.id}/equalizations?include=territory&limit=200`)); const eq = applePoints(await pages(c, `/v1/subscriptionPricePoints/${ip.id}/equalizations?include=territory&limit=200`));
return [{ territory: 'USA', pointId: ip.id }, ...eq.filter(p => p.territory !== 'USA').map(p => ({ territory: p.territory, pointId: p.id }))]; return [{ territory: 'USA', pointId: ip.id }, ...eq.filter(p => p.territory !== 'USA').map(p => ({ territory: p.territory, pointId: p.id }))];
}; };
if (s.intro && !cur?.introTerritories) { if (s.intro && !(cur ? cur.introTerritories : (await c.asc('GET', `/v1/subscriptions/${id}/introductoryOffers?limit=1`)).data?.length)) {
const offers = await offerPrices(s.intro); const offers = await offerPrices(s.intro);
await c.asc('PATCH', `/v1/subscriptions/${id}`, appleIntroPatch(id, s, offers)); await c.asc('PATCH', `/v1/subscriptions/${id}`, appleIntroPatch(id, s, offers));
log(TAG, ` introductory offer in ${offers.length} territories`); log(TAG, ` introductory offer in ${offers.length} territories`);
} }
for (const o of s.offers ?? []) { for (const o of s.offers ?? []) {
if (cur?.promoOffers?.includes(o.id)) continue; if (cur?.promoOffers?.includes(o.id)) continue;
await c.asc('POST', '/v1/subscriptionPromotionalOffers', applePromoOfferBody(id, s, o, await offerPrices(o))); // offer codes (the offer's identifier) are unique per subscription: a 409 finds the existing one
log(TAG, ` promotional offer ${o.id}`); const r = await createOnce({ what: `${s.id} promotional offer ${o.id}`, tag: TAG, unique: true,
find: async () => (await c.all(`/v1/subscriptions/${subId}/promotionalOffers?limit=200`)).find((x: J) => x.attributes.offerCode === o.id) ?? null,
create: async () => (await c.asc('POST', '/v1/subscriptionPromotionalOffers', applePromoOfferBody(subId, s, o, await offerPrices(o)))).data });
if (r.created) log(TAG, ` promotional offer ${o.id}`);
} }
for (const oc of s.offerCodes ?? []) { for (const oc of s.offerCodes ?? []) {
let ocId: string | undefined; let ocId: string | undefined;
if (!cur?.offerCodes?.includes(oc.name)) { if (!cur?.offerCodes?.includes(oc.name)) {
ocId = (await c.asc('POST', '/v1/subscriptionOfferCodes', appleOfferCodeBody(id, s, oc, await offerPrices(oc)))).data.id as string; // campaign names are not known to be unique: look right before, never send the create twice
log(TAG, ` offer code campaign "${oc.name}"`); const r = await findOrCreate<J>({ what: `${s.id} offer code campaign "${oc.name}"`, tag: TAG,
find: async () => (await c.all(`/v1/subscriptions/${subId}/offerCodes?limit=200`)).find((x: J) => x.attributes.name === oc.name) ?? null,
journal: { root, key: `apple:${bundleId}:subscriptionOfferCode:${s.id}:${oc.name}`, byId: x => getById(c.asc, `/v1/subscriptionOfferCodes/${x}`) as Promise<J | null>, idOf: v => v.id },
create: async () => (await c.asc('POST', '/v1/subscriptionOfferCodes', appleOfferCodeBody(subId, s, oc, await offerPrices(oc)))).data });
ocId = r.value.id as string;
if (r.created) log(TAG, ` offer code campaign "${oc.name}"`);
} }
if (oc.customCodes?.length) await customCodes(c, ctx, id, s, oc, ocId); if (oc.customCodes?.length) await customCodes(c, ctx, subId, s, oc, ocId);
} }
if (s.reviewScreenshot && !cur?.reviewScreenshot) { if (s.reviewScreenshot) {
await c.uploadAsset('subscriptionAppStoreReviewScreenshots', { subscription: { data: { type: 'subscriptions', id } } }, resolve(s.reviewScreenshot)); // one screenshot per subscription: kept when Apple has these bytes (MD5) and did not fail them, else replaced
log(TAG, ` review screenshot ${s.reviewScreenshot}`); const file = resolve(s.reviewScreenshot);
const shot = cur?.reviewScreenshot || !cur ? (await c.maybe(`/v1/subscriptions/${subId}/appStoreReviewScreenshot`))?.data : null;
const ok = shot && shot.attributes?.sourceFileChecksum === md5(readFileSync(file)) && shot.attributes?.assetDeliveryState?.state !== 'FAILED';
if (!ok) {
if (shot) await c.asc('DELETE', `/v1/subscriptionAppStoreReviewScreenshots/${shot.id}`);
await c.uploadAsset('subscriptionAppStoreReviewScreenshots', { subscription: { data: { type: 'subscriptions', id: subId } } }, file);
log(TAG, ` review screenshot ${s.reviewScreenshot}`);
}
} }
} }
} }
@ -178,5 +219,11 @@ async function customCodes(c: Asc, ctx: Ctx, subId: string, s: Subscription, oc:
const todo = oc.customCodes!.filter(cc => !have.has(cc.code)); const todo = oc.customCodes!.filter(cc => !have.has(cc.code));
if (!todo.length) return; if (!todo.length) return;
if (!(await confirm({ ...ctx, args: { ...ctx.args, has: (n: string) => n !== 'yes' && ctx.args.has(n) } }, `Create ${todo.length} custom offer code(s) for ${s.id} (${todo.map(x => `${x.code} x${x.count}`).join(', ')})? Anyone with a code can redeem it.`))) return; if (!(await confirm({ ...ctx, args: { ...ctx.args, has: (n: string) => n !== 'yes' && ctx.args.has(n) } }, `Create ${todo.length} custom offer code(s) for ${s.id} (${todo.map(x => `${x.code} x${x.count}`).join(', ')})? Anyone with a code can redeem it.`))) return;
for (const cc of todo) { await c.asc('POST', '/v1/subscriptionOfferCodeCustomCodes', appleCustomCodeBody(id, cc)); log(TAG, ` custom code ${cc.code} (${cc.count})`); } for (const cc of todo) {
// a custom code is unique: a 409 finds the existing one
await createOnce({ what: `custom code ${cc.code}`, tag: TAG, unique: true,
find: async () => (await c.all(`/v1/subscriptionOfferCodes/${id}/customCodes?limit=200`)).find((x: J) => x.attributes.customCode === cc.code) ?? null,
create: async () => (await c.asc('POST', '/v1/subscriptionOfferCodeCustomCodes', appleCustomCodeBody(id, cc))).data });
log(TAG, ` custom code ${cc.code} (${cc.count})`);
}
} }

View file

@ -27,7 +27,8 @@ import {
parseTesters, pickBuild, planBetaInfo, planBetaReviewDetail, planDistribute, planGroups, planTesters, planWhatToTest, whatToTestFromListing, parseTesters, pickBuild, planBetaInfo, planBetaReviewDetail, planDistribute, planGroups, planTesters, planWhatToTest, whatToTestFromListing,
} from '../lib/testflight'; } from '../lib/testflight';
import { type Asc, ascClient, type J } from './asc'; import { type Asc, ascClient, type J } from './asc';
import { execute } from './jsonapi'; import { describeReq, execute } from './jsonapi';
import { findOrCreate, getById } from '../lib/upsert';
const TAG = 'testflight'; const TAG = 'testflight';
const USAGE = 'usage: storebound apple testflight status | groups | testers | what-to-test --build N | info | distribute --build N --group <names> | wait --build N | expire --keep N | --older-than D'; const USAGE = 'usage: storebound apple testflight status | groups | testers | what-to-test --build N | info | distribute --build N --group <names> | wait --build N | expire --keep N | --older-than D';
@ -138,7 +139,16 @@ export async function run(ctx: Ctx): Promise<void> {
const plan = planGroups(appId, await groups(c, appId), tf?.groups ?? []); const plan = planGroups(appId, await groups(c, appId), tf?.groups ?? []);
if (!tf?.groups?.length) warn(TAG, 'apple.testflight.groups is empty: nothing to create'); if (!tf?.groups?.length) warn(TAG, 'apple.testflight.groups is empty: nothing to create');
for (const n of plan.existing) log(TAG, `group "${n}": exists`); for (const n of plan.existing) log(TAG, `group "${n}": exists`);
await execute(c.asc, plan.create, { dry, tag: TAG }); if (dry) await execute(c.asc, plan.create, { dry, tag: TAG });
else for (const r of plan.create) {
// looked up right before the create (fresh list, then the journal for the list lag); a 409 finds it
const name = (r.body as J).data.attributes.name as string;
const made = await findOrCreate<J>({ what: `beta group "${name}"`, tag: TAG, unique: true,
find: async () => (await c.all(`/v1/apps/${appId}/betaGroups?limit=200`)).find((g: J) => g.attributes.name === name) ?? null,
journal: { root, key: `apple:${appId}:betaGroup:${name}`, byId: id => getById(c.asc, `/v1/betaGroups/${id}`) as Promise<J | null>, idOf: v => v.id },
create: async () => (await c.asc('POST', r.path, r.body)).data });
log(TAG, `${describeReq(r)}: ${made.created ? 'done' : 'exists already'}`);
}
done(TAG, dry ? `groups checked (dry run): ${plan.create.length} to create` : `groups done (${plan.create.length} created)`); done(TAG, dry ? `groups checked (dry run): ${plan.create.length} to create` : `groups done (${plan.create.length} created)`);
return; return;
} }

View file

@ -23,6 +23,7 @@ import { BUILD_WAIT_EXIT } from '../lib/build-ops';
import { type Asc, ascClient, type J } from './asc'; import { type Asc, ascClient, type J } from './asc';
import { waitForBuild, waitMessage } from './testflight'; import { waitForBuild, waitMessage } from './testflight';
import { describeReq } from './jsonapi'; import { describeReq } from './jsonapi';
import { createOnce } from '../lib/upsert';
const TAG = 'upload'; const TAG = 'upload';
@ -44,9 +45,20 @@ function slice(path: string, offset: number, length: number): Buffer {
async function apiUpload(c: Asc, appId: string, ipa: string, info: ReturnType<typeof ipaInfo>, deadline: number): Promise<void> { async function apiUpload(c: Asc, appId: string, ipa: string, info: ReturnType<typeof ipaInfo>, deadline: number): Promise<void> {
const size = statSync(ipa).size; const size = statSync(ipa).size;
const reqs = buildUploadReqs(appId, info, basename(ipa), size); const reqs = buildUploadReqs(appId, info, basename(ipa), size);
const up = (await c.asc('POST', reqs.upload.path, reqs.upload.body)).data; // one build upload per version + build number: an earlier one that did not fail is resumed, never doubled
log(TAG, `build upload ${up.id} created`); const prior = (await uploadsOf(c, appId, info)).find((u: J) => u.attributes?.state?.state !== 'FAILED');
const file = (await c.asc('POST', reqs.file(up.id).path, reqs.file(up.id).body)).data; const made = prior ? { value: prior, created: false } : await createOnce<J>({ what: `build upload ${info.version} (${info.build})`, tag: TAG,
find: async () => (await uploadsOf(c, appId, info)).find((u: J) => u.attributes?.state?.state !== 'FAILED') ?? null,
create: async () => (await c.asc('POST', reqs.upload.path, reqs.upload.body)).data });
const up = made.value;
log(TAG, made.created ? `build upload ${up.id} created` : `build upload ${up.id} exists (${up.attributes?.state?.state ?? '?'}): resuming it`);
const filesOf = async () => (await c.all(`/v1/buildUploads/${up.id}/buildUploadFiles?limit=10`)).filter((f: J) => f.attributes?.fileName === basename(ipa));
const fileNow = made.created ? undefined : (await filesOf())[0];
const file = fileNow ?? (await createOnce<J>({ what: `file reservation of build upload ${up.id}`, tag: TAG,
find: async () => (await filesOf())[0] ?? null,
create: async () => (await c.asc('POST', reqs.file(up.id).path, reqs.file(up.id).body)).data })).value;
if (fileNow && !(fileNow.attributes?.uploadOperations ?? []).length)
throw new Error(`build upload ${up.id} already has ${basename(ipa)} (${fileNow.attributes?.assetDeliveryState?.state ?? 'state unknown'}) and App Store Connect gives no upload parts for it: wait for it (storebound apple testflight wait --build ${info.build}), or raise the build number (storebound apple build)`);
const ops = (file.attributes.uploadOperations ?? []) as J[]; const ops = (file.attributes.uploadOperations ?? []) as J[];
log(TAG, `uploading ${(size / 1048576).toFixed(1)} MB in ${ops.length} part(s)`); log(TAG, `uploading ${(size / 1048576).toFixed(1)} MB in ${ops.length} part(s)`);
let sent = 0; let sent = 0;
@ -82,6 +94,10 @@ async function apiUpload(c: Asc, appId: string, ipa: string, info: ReturnType<ty
} }
} }
/** Build uploads of this version + build number (any state), newest first. */
const uploadsOf = (c: Asc, appId: string, info: { version: string; build: string }): Promise<J[]> =>
c.all(`/v1/apps/${appId}/buildUploads?filter[cfBundleShortVersionString]=${encodeURIComponent(info.version)}&filter[cfBundleVersion]=${encodeURIComponent(info.build)}&filter[platform]=IOS&limit=20`);
async function altoolUpload(root: string, ipa: string): Promise<void> { async function altoolUpload(root: string, ipa: string): Promise<void> {
if (process.platform !== 'darwin' || !Bun.which('xcrun')) throw new Error('--via altool needs macOS with Xcode; use --via api elsewhere'); if (process.platform !== 'darwin' || !Bun.which('xcrun')) throw new Error('--via altool needs macOS with Xcode; use --via api elsewhere');
const k = ascKeyFile(root); const k = ascKeyFile(root);
@ -122,6 +138,16 @@ export async function run(ctx: Ctx): Promise<void> {
const builds = await c.all(`/v1/builds?filter[app]=${appId}&limit=200&fields[builds]=version`); const builds = await c.all(`/v1/builds?filter[app]=${appId}&limit=200&fields[builds]=version`);
const numbers = builds.map(b => String(b.attributes.version)); const numbers = builds.map(b => String(b.attributes.version));
const next = nextBuildNumber(numbers); const next = nextBuildNumber(numbers);
// the same IPA uploaded by an earlier run: nothing to send again, only the wait
const already = via === 'api' && !dry ? (await uploadsOf(c, appId, info).catch(() => [])).find((u: J) => u.attributes?.state?.state === 'COMPLETE') : undefined;
if (already) {
log(TAG, `build ${info.version} (${info.build}) was uploaded already (build upload ${already.id}); not sending it again`);
if (!wait) { if (json) printJson({ ...{ ipa: relative(root, ipa), version: info.version, build: info.build }, uploaded: true, alreadyUploaded: true }); done(TAG, `build ${info.build} already uploaded`); return; }
const r = await waitForBuild(c, appId, info.build, { marketing: info.version, timeoutMs: timeout, tag: TAG });
if (json) printJson({ ipa: relative(root, ipa), version: info.version, build: info.build, uploaded: true, alreadyUploaded: true, processingState: r.seen.processingState ?? null });
if (r.outcome === 'valid') { done(TAG, waitMessage(info.build, r)); return; }
process.exit(r.outcome === 'failed' ? BUILD_WAIT_EXIT.failed : BUILD_WAIT_EXIT.timeout);
}
if (numbers.includes(info.build)) throw new Error(`build ${info.build} is already on App Store Connect: rebuild with --build-number ${next} (storebound apple build)`); if (numbers.includes(info.build)) throw new Error(`build ${info.build} is already on App Store Connect: rebuild with --build-number ${next} (storebound apple build)`);
if (numbers.length && compareBuild(info.build, next) < 0) warn(TAG, `build ${info.build} is lower than the highest uploaded one (next free: ${next}); Apple accepts it only if this version train has no higher build`); if (numbers.length && compareBuild(info.build, next) < 0) warn(TAG, `build ${info.build} is lower than the highest uploaded one (next free: ${next}); Apple accepts it only if this version train has no higher build`);
const reqs = buildUploadReqs(appId, info, basename(ipa), size); const reqs = buildUploadReqs(appId, info, basename(ipa), size);

View file

@ -49,7 +49,7 @@ const COMMANDS: Record<string, [() => Promise<Mod>, string]> = {
'play subscriptions': [() => import('./play/subscriptions'), 'auto-renewable subscriptions: listings, base plan, regional prices, intro offer, activate (--csv prices sheet; asks first)'], 'play subscriptions': [() => import('./play/subscriptions'), 'auto-renewable subscriptions: listings, base plan, regional prices, intro offer, activate (--csv prices sheet; asks first)'],
'play listing': [() => import('./play/listing'), 'app details + title / short / full description per language; pull [--write]: live listings back into the listing file (diff first)'], 'play listing': [() => import('./play/listing'), 'app details + title / short / full description per language; pull [--write]: live listings back into the listing file (diff first)'],
'play images': [() => import('./play/images'), 'icon, feature graphic, phone / 7" / 10" screenshots per language; --variant <id>'], 'play images': [() => import('./play/images'), 'icon, feature graphic, phone / 7" / 10" screenshots per language; --variant <id>'],
'play data-safety': [() => import('./play/data-safety'), 'Data safety form (CSV import through the API)'], 'play data-safety': [() => import('./play/data-safety'), '[--force] Data safety form (CSV import through the API; skipped when unchanged since the last upload)'],
'play release': [() => import('./play/release'), 'upload an AAB and put it on tracks with release notes'], 'play release': [() => import('./play/release'), 'upload an AAB and put it on tracks with release notes'],
'play build': [() => import('./play/build'), 'gradle bundleRelease (Flutter: flutter build appbundle) with the next free versionCode and the upload key from env / .storebound; --dry'], 'play build': [() => import('./play/build'), 'gradle bundleRelease (Flutter: flutter build appbundle) with the next free versionCode and the upload key from env / .storebound; --dry'],
'play upload': [() => import('./play/upload'), 'upload an AAB to a testing track (--track internal|alpha|beta), versionCode checked first; --dry read-only, asks first'], 'play upload': [() => import('./play/upload'), 'upload an AAB to a testing track (--track internal|alpha|beta), versionCode checked first; --dry read-only, asks first'],
@ -130,6 +130,9 @@ async function main(): Promise<void> {
if (!key) { console.error(`unknown command: ${argv.slice(0, 2).join(' ')}\n`); help(); process.exit(2); } if (!key) { console.error(`unknown command: ${argv.slice(0, 2).join(' ')}\n`); help(); process.exit(2); }
const args = parseArgs(argv.slice(key.split(' ').length)); const args = parseArgs(argv.slice(key.split(' ').length));
const loaded = await loadConfig(args.get('config')); const loaded = await loadConfig(args.get('config'));
// one writer per app and store (cli/lib/lock.ts): dry runs and reads take no lock
const { acquireLock, lockKeys } = await import('./lib/lock');
for (const k of lockKeys(key, args, loaded.config)) acquireLock(loaded.root, k, argv.filter(x => x !== '--force-unlock').join(' '), { force: args.has('force-unlock') });
await (await COMMANDS[key]![0]()).run({ loaded, args }); await (await COMMANDS[key]![0]()).run({ loaded, args });
} }

View file

@ -15,7 +15,7 @@ export function parseArgs(argv: string[], booleans: string[] = []): Args {
const repeated = new Map<string, string[]>(); const repeated = new Map<string, string[]>();
const set = (k: string, v: string) => { flags.set(k, v); repeated.set(k, [...(repeated.get(k) ?? []), v]); }; const set = (k: string, v: string) => { flags.set(k, v); repeated.set(k, [...(repeated.get(k) ?? []), v]); };
const _: string[] = []; const _: string[] = [];
const bool = new Set(['dry', 'yes', 'help', 'json', 'verbose', 'all-pages', 'apply', 'resume', 'force', 'include-drafts', ...booleans]); const bool = new Set(['dry', 'yes', 'help', 'json', 'verbose', 'all-pages', 'apply', 'resume', 'force', 'force-unlock', 'include-drafts', ...booleans]);
for (let i = 0; i < argv.length; i++) { for (let i = 0; i < argv.length; i++) {
const a = argv[i]!; const a = argv[i]!;
if (!a.startsWith('--')) { _.push(a); continue; } if (!a.startsWith('--')) { _.push(a); continue; }

View file

@ -100,6 +100,12 @@ export interface ShotFrame { src: string; x?: number; dy?: number; scale?: numbe
export interface Palette { bg: [string, string, string]; glow: string; ink: string; shadow: string } export interface Palette { bg: [string, string, string]; glow: string; ink: string; shadow: string }
export interface ShotScene { id: string; palette: string | Palette; hero?: boolean; frames: ShotFrame[]; badges?: ShotBadgeSlot[]; over?: number } export interface ShotScene { id: string; palette: string | Palette; hero?: boolean; frames: ShotFrame[]; badges?: ShotBadgeSlot[]; over?: number }
/** A local font file for shots.fonts. license: 'OFL-1.1', 'proprietary' or a short note (shown by `shots fonts`;
* `shots check` warns without one). weight / style: the @font-face descriptors (default: all weights, normal). */
export interface LocalFontSpec { family?: string; file: string; weight?: number | string; style?: 'normal' | 'italic'; license?: string }
/** A Google Fonts family name, a local font path (relative to the config) or a LocalFontSpec. */
export type FontSpec = string | LocalFontSpec;
export interface ShotsConfig { export interface ShotsConfig {
/** Look of the rendered screenshots: game (sunburst, sparkles, badges, bold outlined titles) or app (calm gradient, /** Look of the rendered screenshots: game (sunburst, sparkles, badges, bold outlined titles) or app (calm gradient,
* device frame, headline + subline, no sparkles or badges). Default: app for the app / subscription-app profiles, * device frame, headline + subline, no sparkles or badges). Default: app for the app / subscription-app profiles,
@ -113,11 +119,12 @@ export interface ShotsConfig {
out: string; out: string;
/** Optional logo image for the hero scene and the feature graphic. */ /** Optional logo image for the hero scene and the feature graphic. */
logo?: string; logo?: string;
/** Google Fonts families for titles / body text (downloaded and subset by `shots fonts`). Default: game Paytone One + /** Fonts for titles / body text: a Google Fonts family (downloaded and subset by `shots fonts`), or your own font file
* Montserrat, app Inter (all cover Latin Extended and Vietnamese). `fallback`: Noto families you accept for * (a path relative to the config, TTF / OTF / WOFF / WOFF2, or { family, file, weight?, style?, license? }).
* characters the title / body font lacks (e.g. ['Noto Sans Display']); without it a render that needs an automatic * Default: game Paytone One + Montserrat, app Inter (all cover Latin Extended and Vietnamese). `fallback`: fonts you
* fallback is an ERROR in `shots check`. */ * accept for characters the title / body font lacks, tried in order before the automatic Noto fallbacks (e.g.
fonts?: { title?: string; body?: string; fallback?: string[] }; * ['Noto Sans Display'] or a local file); a render that needs an automatic fallback is an ERROR in `shots check`. */
fonts?: { title?: FontSpec; body?: FontSpec; fallback?: FontSpec[] };
scenes: ShotScene[]; scenes: ShotScene[];
feature?: { frame: string; palette?: string | Palette }; feature?: { frame: string; palette?: string | Palette };
/** Phone screen size of the raw frames in CSS px (default 440 x 902 @3x, iPhone 6.9" minus the 54 px status bar). /** Phone screen size of the raw frames in CSS px (default 440 x 902 @3x, iPhone 6.9" minus the 54 px status bar).
@ -146,6 +153,10 @@ export interface ShotSpec {
/** Web: JS run in the page before the screenshot. */ /** Web: JS run in the page before the screenshot. */
eval?: string[]; eval?: string[];
device?: 'phone' | 'tablet' | 'both'; device?: 'phone' | 'tablet' | 'both';
/** Web, capture.session "per-language": how this shot is reached in the already booted page. "spa" (default):
* history.pushState to its path + a popstate event, then waitFor / eval (switch scenes from those); "reload":
* page.goto in the same page. A failing waitFor there falls back to a fresh page. */
navigate?: 'spa' | 'reload';
/** ios-sim: extra launch arguments, e.g. ["-shotScene", "{shot}", "-seed", "1"] (read them with UserDefaults / your engine). */ /** ios-sim: extra launch arguments, e.g. ["-shotScene", "{shot}", "-seed", "1"] (read them with UserDefaults / your engine). */
args?: string[]; args?: string[];
/** android-emu: intent extras for `am start` (string / int / float / boolean), "{lang}" replaced in strings. */ /** android-emu: intent extras for `am start` (string / int / float / boolean), "{lang}" replaced in strings. */
@ -194,6 +205,18 @@ export interface ShotsCapture {
* every device, or per device class. Default: the frame's bar share of the height (phone 54 / 956 = 162 px on a 6.9" * every device, or per device class. Default: the frame's bar share of the height (phone 54 / 956 = 162 px on a 6.9"
* iPhone, 13" iPad 24 / 1376 = 48 px). */ * iPhone, 13" iPad 24 / 1376 = 48 px). */
cropTop?: number | { phone?: number; tablet?: number }; cropTop?: number | { phone?: number; tablet?: number };
/** Web: parallel browser contexts (one headless Chromium). Default min(3, CPU cores / 4): game boots are GPU-heavy.
* `--concurrency N` overrides. */
concurrency?: number;
/** Web: "per-shot" (default: new context + full boot per frame) or "per-language" (one page per language and
* device, booted once; shots switch in it, see ShotSpec.navigate). */
session?: 'per-shot' | 'per-language';
/** Web, per-language: how long a reused page may take to reach a shot's waitFor before a fresh page is used.
* Default 15000 ms. */
reuseTimeoutMs?: number;
/** Web: files or folders of the app build (e.g. "apps/game/src" or "apps/game/dist") hashed into the capture cache,
* so a code change re-captures. Default: the HTML at baseUrl and the scripts it loads. */
build?: string | string[];
shots: Record<string, ShotSpec>; shots: Record<string, ShotSpec>;
} }
@ -831,6 +854,19 @@ export function validateConfig(c: unknown): string[] {
for (const k of ['copy', 'frames', 'out'] as const) if (!s[k]) e.push(`shots.${k}: missing`); for (const k of ['copy', 'frames', 'out'] as const) if (!s[k]) e.push(`shots.${k}: missing`);
if (s.style !== undefined && s.style !== 'game' && s.style !== 'app') e.push('shots.style: game | app'); if (s.style !== undefined && s.style !== 'game' && s.style !== 'app') e.push('shots.style: game | app');
if (!s.scenes?.length) e.push('shots.scenes: at least one scene'); if (!s.scenes?.length) e.push('shots.scenes: at least one scene');
const fontKeys: [string, unknown][] = [['title', s.fonts?.title], ['body', s.fonts?.body], ...(Array.isArray(s.fonts?.fallback) ? s.fonts!.fallback! : s.fonts?.fallback !== undefined ? [s.fonts.fallback] : []).map((f, i) => [`fallback[${i}]`, f] as [string, unknown])];
if (s.fonts?.fallback !== undefined && !Array.isArray(s.fonts.fallback)) e.push('shots.fonts.fallback: a list');
for (const [k, f] of fontKeys) {
if (f === undefined) continue;
if (typeof f === 'string') { if (!f.trim()) e.push(`shots.fonts.${k}: empty`); else if (/[\\/]|\.[a-z0-9]{2,5}$/i.test(f) && !/\.(ttf|otf|woff2?)$/i.test(f)) e.push(`shots.fonts.${k}: ${f}: a font file must be TTF, OTF, WOFF or WOFF2`); continue; }
const o = f as Partial<LocalFontSpec>;
if (!o || typeof o !== 'object' || typeof o.file !== 'string') { e.push(`shots.fonts.${k}: a Google Fonts family, a font file path or { family, file, weight?, style?, license? }`); continue; }
if (!/\.(ttf|otf|woff2?)$/i.test(o.file)) e.push(`shots.fonts.${k}.file: ${o.file}: TTF, OTF, WOFF or WOFF2`);
if (o.family !== undefined && (typeof o.family !== 'string' || !o.family.trim())) e.push(`shots.fonts.${k}.family: a name`);
if (o.style !== undefined && o.style !== 'normal' && o.style !== 'italic') e.push(`shots.fonts.${k}.style: normal | italic`);
if (o.weight !== undefined && !/^\d{3}( \d{3})?$/.test(String(o.weight))) e.push(`shots.fonts.${k}.weight: e.g. 400, 800 or "100 900"`);
if (o.license !== undefined && typeof o.license !== 'string') e.push(`shots.fonts.${k}.license: text ('OFL-1.1', 'proprietary', ...)`);
}
for (const sc of s.scenes ?? []) if (!sc.frames?.length) e.push(`shots.scenes[${sc.id}].frames: at least one frame`); for (const sc of s.scenes ?? []) if (!sc.frames?.length) e.push(`shots.scenes[${sc.id}].frames: at least one frame`);
e.push(...validateCapture(s.capture)); e.push(...validateCapture(s.capture));
const sceneIds = new Set((s.scenes ?? []).map(x => x.id)); const sceneIds = new Set((s.scenes ?? []).map(x => x.id));

View file

@ -129,7 +129,9 @@ export function normalizeKeywords(text: string, o: { name?: string; subtitle?: s
return { text: kept.join(','), dropped }; return { text: kept.join(','), dropped };
} }
export interface FieldCheck { store: 'apple' | 'play'; field: string; chars: number; limit: number; problems: string[] } /** `chars` is what the store counts: characters, except App Store keywords, which App Store Connect limits in UTF-8 bytes
* (`unit` says which). */
export interface FieldCheck { store: 'apple' | 'play'; field: string; chars: number; unit: 'chars' | 'bytes'; limit: number; problems: string[] }
/** Limits and rules of one translated section. `source` = the source language's fields (identical text is flagged). */ /** Limits and rules of one translated section. `source` = the source language's fields (identical text is flagged). */
export function checkFields(store: 'apple' | 'play', f: Record<string, string | undefined>, source: Record<string, string | undefined> = {}, status?: TStatus): FieldCheck[] { export function checkFields(store: 'apple' | 'play', f: Record<string, string | undefined>, source: Record<string, string | undefined> = {}, status?: TStatus): FieldCheck[] {
@ -139,10 +141,11 @@ export function checkFields(store: 'apple' | 'play', f: Record<string, string |
const v = f[k]; const v = f[k];
if (v === undefined) continue; if (v === undefined) continue;
const limit = (FIELD_LIMITS[store] as Record<string, number>)[k]!; const limit = (FIELD_LIMITS[store] as Record<string, number>)[k]!;
const c: FieldCheck = { store, field: k, chars: len(v), limit, problems: [] }; const bytes = store === 'apple' && k === 'keywords';
if (len(v) > limit) c.problems.push(`${len(v)} > ${limit} characters`); const n = bytes ? Buffer.byteLength(v, 'utf8') : len(v);
if (store === 'apple' && k === 'keywords') { const c: FieldCheck = { store, field: k, chars: n, unit: bytes ? 'bytes' : 'chars', limit, problems: [] };
if (Buffer.byteLength(v) > limit) c.problems.push(`${Buffer.byteLength(v)} bytes > ${limit}`); if (n > limit) c.problems.push(`${n} > ${limit} ${bytes ? 'bytes' : 'characters'}`);
if (bytes) {
if (/,\s|\s,/.test(v)) c.problems.push('spaces around commas'); if (/,\s|\s,/.test(v)) c.problems.push('spaces around commas');
const words = v.split(',').map(w => w.trim().toLocaleLowerCase()).filter(Boolean); const words = v.split(',').map(w => w.trim().toLocaleLowerCase()).filter(Boolean);
const dup = words.filter((w, i) => words.indexOf(w) !== i); const dup = words.filter((w, i) => words.indexOf(w) !== i);

90
cli/lib/lock.ts Normal file
View file

@ -0,0 +1,90 @@
// One writer per app and store: a write command takes .storebound/locks/<store>-<app>.lock before it changes
// anything, so two storebound runs (two terminals, an agent and a CI job, a flow and a hand run) never write to the
// same app at once and race each other into duplicates. Dry runs and reads take no lock.
// A lock whose process is gone (same machine) is taken over; a live one stops the run with who holds it. A lock
// left by a crash on another machine (shared folder) stays until --force-unlock or 12 hours.
import { hostname } from 'node:os';
import { mkdirSync, openSync, readFileSync, rmSync, writeSync, closeSync } from 'node:fs';
import { join } from 'node:path';
import type { ShipConfig } from './config';
import type { Args } from './args';
export interface LockInfo { pid: number; host: string; command: string; startedAt: string }
const STALE_MS = 12 * 3600_000;
export const lockPath = (root: string, key: string): string => join(root, '.storebound', 'locks', `${key.replace(/[^A-Za-z0-9._-]+/g, '_')}.lock`);
function alive(pid: number): boolean {
try { process.kill(pid, 0); return true; } catch (e) { return (e as NodeJS.ErrnoException).code === 'EPERM'; }
}
/** Whether a lock may be taken over: its process is gone (same host) or it is older than 12 hours. Pure apart from
* the process check. */
export function staleLock(l: LockInfo | null, now = Date.now(), host = hostname(), isAlive = alive): boolean {
if (!l) return true;
if (now - Date.parse(l.startedAt) > STALE_MS) return true;
return l.host === host && !isAlive(l.pid);
}
const held = new Set<string>();
let hooked = false;
/** Takes the lock `key` for `command` (`apple iap`); throws with the holder when another run has it. Released when the
* process exits. `force` removes a lock first (--force-unlock). */
export function acquireLock(root: string, key: string, command: string, o: { force?: boolean } = {}): () => void {
const path = lockPath(root, key);
mkdirSync(join(root, '.storebound', 'locks'), { recursive: true });
if (o.force) rmSync(path, { force: true });
for (let attempt = 0; ; attempt++) {
try {
const fd = openSync(path, 'wx', 0o600);
const info: LockInfo = { pid: process.pid, host: hostname(), command, startedAt: new Date().toISOString() };
writeSync(fd, JSON.stringify(info) + '\n');
closeSync(fd);
break;
} catch (e) {
if ((e as NodeJS.ErrnoException).code !== 'EEXIST' || attempt > 2) throw e;
let cur: LockInfo | null = null;
try { cur = JSON.parse(readFileSync(path, 'utf8')) as LockInfo; } catch { /* half written or unreadable: treat as stale */ }
if (!staleLock(cur)) {
throw new Error(`another storebound run is writing to ${key.replace(/^(\w+)-/, '$1 ')}: \`storebound ${cur!.command}\` (pid ${cur!.pid} on ${cur!.host}, since ${cur!.startedAt.slice(0, 16).replace('T', ' ')} UTC). `
+ `Two writers at once can create duplicates, so this run stops. Wait for it to finish; if it is no longer running, re-run with --force-unlock (removes ${join('.storebound', 'locks', path.split('/').pop()!)}).`);
}
rmSync(path, { force: true });
}
}
held.add(path);
if (!hooked) { hooked = true; process.on('exit', () => { for (const p of held) rmSync(p, { force: true }); }); }
return () => { rmSync(path, { force: true }); held.delete(path); };
}
/** Read-only subcommands and flags: no lock. */
const READ_SUBS = new Set(['status', 'list', 'devices', 'diff', 'export', 'pull', 'wait', 'options', 'coverage', 'report', 'campaigns', 'check', 'help']);
/**
* The lock keys a command run needs: none for dry runs and reads, `apple-<bundle id>` / `play-<package>` for store
* writes, both for `prices import`, `revenuecat-<project>` for revenuecat sync. Pure.
*/
export function lockKeys(command: string, args: Args, config: ShipConfig): string[] {
// a dry run never writes, so it never waits for (or blocks) a writer
const readOnly = args.has('dry');
if (readOnly) return [];
const sub = args._[0];
const apple = config.apple?.bundleId ? [`apple-${config.apple.bundleId}`] : [];
const play = config.play?.packageName ? [`play-${config.play.packageName}`] : [];
if (command.startsWith('apple ')) {
if (['apple status', 'apple next-build', 'apple build'].includes(command)) return [];
if (command === 'apple listing' && sub === 'pull') return [];
if (sub && READ_SUBS.has(sub)) return [];
if (command === 'apple adhoc' && ['options', 'export'].includes(sub ?? '')) return [];
return apple;
}
if (command.startsWith('play ')) {
if (['play status', 'play next-version-code', 'play build'].includes(command)) return [];
if (command === 'play listing' && sub === 'pull') return [];
return play;
}
if (command === 'prices') return sub === 'import' && !args.has('json') ? [...apple, ...play] : [];
if (command === 'revenuecat') return sub === 'sync' ? [`revenuecat-${config.apple?.bundleId ?? config.play?.packageName ?? 'project'}`] : [];
return [];
}

152
cli/lib/upsert.ts Normal file
View file

@ -0,0 +1,152 @@
// Idempotent writes against the stores. Every create in storebound goes through here or follows the same rules
// (docs/idempotency.md):
// 1. Look the thing up by its stable key first (product id, locale, version string, display type, name) and update or
// leave it instead of creating it again.
// 2. Right before a create, look again with a fresh GET (findOrCreate), and ask a journal of recent creates
// (.storebound/created.json) for an id the store's list may not show yet: a direct GET by id is not subject to the
// minute of list lag right after a create.
// 3. A create answered with 409 / ALREADY_EXISTS is "found": look it up and use it, never create again (createOnce).
// 4. A create whose outcome is unknown (timeout, connection reset, 5xx on a POST) is never sent blindly again: look
// first; only when the store itself refuses duplicates of that key (unique) is a second attempt safe (its 409
// then finds the first one). Otherwise stop with a message: re-running reads the store and converges.
// Retries of 429 / 5xx in the API clients are only for requests that end the same when sent twice (retrySafe).
import { existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from 'node:fs';
import { join } from 'node:path';
import { log, warn } from './log';
/** A write whose outcome is unknown: it may or may not have been applied (timeout, connection reset, 5xx answer to a
* POST). The API clients throw this instead of retrying such a request. */
export class AmbiguousWriteError extends Error {
readonly ambiguous = true;
constructor(message: string, readonly status: number | null = null) { super(message); this.name = 'AmbiguousWriteError'; }
}
export const isAmbiguous = (e: unknown): boolean => e instanceof AmbiguousWriteError || (e as { ambiguous?: boolean } | null)?.ambiguous === true;
/**
* The store refused a create because the thing already exists: Google ALREADY_EXISTS / 409, App Store Connect 409 with
* a "duplicate / already / unique / in use / taken" detail (Apple also answers 409 for plain validation errors, which
* are not conflicts), RevenueCat 409. Pure.
*/
export function isConflict(e: unknown): boolean {
const s = String((e as Error)?.message ?? e);
if (/ALREADY_EXISTS/.test(s)) return true;
if (!/(-> |: )409\b/.test(s)) return false;
return /duplicate|already|unique|in use|taken|exists/i.test(s.replace(/^.*?409/, ''));
}
/** POSTs that end the same when sent twice: relationship adds, Google custom actions that set a state, and the App
* Store "create or replace" singletons (price schedules, availability). */
const IDEMPOTENT_POST: RegExp[] = [
/\/relationships\//,
/:(validate|convertRegionPrices|batchUpdateStates|activate|deactivate|query|batchGet|reply)(\?|$)/,
/\/edits(\?|$)/, // a new edit; an unused one expires on its own and changes nothing
/\/dataSafety(\?|$)/,
/^\/v1\/(inAppPurchasePriceSchedules|appPriceSchedules|inAppPurchaseAvailabilities|subscriptionAvailabilities)(\?|$)/,
/^\/v2\/appAvailabilities(\?|$)/,
];
/** Whether a request may be sent again after a 5xx or a network error. GET, HEAD, PUT, PATCH and DELETE set a state;
* a POST creates unless its path is a known idempotent action. `path` may be a full URL. Pure. */
export function retrySafe(method: string, path: string): boolean {
const m = method.toUpperCase();
if (m !== 'POST') return true;
const p = path.replace(/^https:\/\/[^/]+/, '').replace(/^\/(androidpublisher|upload\/androidpublisher)\/v3\/applications\/[^/]+/, '');
return IDEMPOTENT_POST.some(re => re.test(p));
}
/** Waits between re-checks (STOREBOUND_UPSERT_WAIT_MS; the test preload sets 0). */
export const upsertTiming = { waitMs: Number(process.env.STOREBOUND_UPSERT_WAIT_MS ?? 3000), tries: 4, sleep: (ms: number): Promise<void> => (ms > 0 ? Bun.sleep(ms) : Promise.resolve()) };
export interface CreateOnce<T> {
/** what is being created, for log lines and errors: `in-app purchase com.example.gems` */
what: string;
/** the create request; returns the created thing */
create: () => Promise<T>;
/** a fresh lookup by the stable key (GET with a filter, or the list), null when it is not there */
find: () => Promise<T | null | undefined>;
/** the store refuses a second one with this key (409): a create may then be tried again after an unknown outcome */
unique?: boolean;
/** log tag */
tag?: string;
}
async function recheck<T>(o: CreateOnce<T>): Promise<T | null> {
for (let i = 0; i < upsertTiming.tries; i++) {
const v = await o.find();
if (v) return v;
if (i < upsertTiming.tries - 1) await upsertTiming.sleep(upsertTiming.waitMs * (i + 1));
}
return null;
}
/** One create, never two: a conflict finds the existing one, an unknown outcome looks before (maybe) trying again. */
export async function createOnce<T>(o: CreateOnce<T>, attempt = 1): Promise<{ value: T; created: boolean }> {
try {
return { value: await o.create(), created: true };
} catch (e) {
const msg = (e as Error).message;
if (isConflict(e)) {
const v = await recheck(o);
if (v) { log(o.tag ?? 'upsert', `${o.what}: already exists, using it (not created again)`); return { value: v, created: false }; }
throw new Error(`${o.what}: the store says it already exists but does not list it yet (its lists lag right after a create). Nothing was created twice; run the command again in a minute.\n${msg}`);
}
if (!isAmbiguous(e)) throw e;
warn(o.tag ?? 'upsert', `${o.what}: no clear answer to the create (${msg.slice(0, 160)}); checking whether it exists before anything else`);
const v = await recheck(o);
if (v) { log(o.tag ?? 'upsert', `${o.what}: the create went through`); return { value: v, created: true }; }
if (o.unique && attempt < 2) return createOnce(o, attempt + 1);
throw new Error(`${o.what}: the create got no clear answer and the store does not list it (yet). It was not sent again, so nothing is created twice: run the command again in a minute (it reads the store first).\n${msg}`);
}
}
// ---------------------------------------------------------------------------------------------- journal of creates
/** Creates of the last day, keyed by store + kind + stable key: { id, at }. Lives in .storebound/created.json. */
type Journal = Record<string, { id: string; at: string }>;
const JOURNAL_MS = 24 * 3600_000;
const journalPath = (root: string): string => join(root, '.storebound', 'created.json');
export function readJournal(root: string, now = Date.now()): Journal {
try {
const j = JSON.parse(readFileSync(journalPath(root), 'utf8')) as Journal;
return Object.fromEntries(Object.entries(j).filter(([, v]) => v && now - Date.parse(v.at) < JOURNAL_MS));
} catch { return {}; }
}
export function remember(root: string, key: string, id: string, now = new Date()): void {
try {
const j = readJournal(root, now.getTime());
j[key] = { id, at: now.toISOString() };
mkdirSync(join(root, '.storebound'), { recursive: true });
const tmp = journalPath(root) + `.${process.pid}.tmp`;
writeFileSync(tmp, JSON.stringify(j, null, 2) + '\n');
renameSync(tmp, journalPath(root));
} catch (e) { warn('upsert', `could not note the create in .storebound/created.json: ${(e as Error).message}`); }
}
export interface FindOrCreate<T> extends CreateOnce<T> {
/** journal of recent creates: guards against the list lag right after a create of an earlier run */
journal?: { root: string; key: string; byId: (id: string) => Promise<T | null | undefined>; idOf: (v: T) => string };
}
/** Looks with a fresh GET, then in the journal (direct GET by id), and only then creates (createOnce). */
export async function findOrCreate<T>(o: FindOrCreate<T>): Promise<{ value: T; created: boolean }> {
const found = await o.find();
if (found) return { value: found, created: false };
if (o.journal && existsSync(join(o.journal.root, '.storebound'))) {
const hit = readJournal(o.journal.root)[o.journal.key];
if (hit) {
const v = await o.journal.byId(hit.id).catch(() => null);
if (v) { log(o.tag ?? 'upsert', `${o.what}: created ${hit.at.slice(11, 16)} UTC by an earlier run (${hit.id}); the store's list does not show it yet, using it`); return { value: v, created: false }; }
}
}
const r = await createOnce(o);
if (o.journal && r.created) remember(o.journal.root, o.journal.key, o.journal.idOf(r.value));
return r;
}
/** Apple: GET of one resource by id, null on 404. For FindOrCreate.journal.byId. */
export async function getById(asc: (m: string, p: string) => Promise<{ data?: unknown }>, path: string): Promise<unknown | null> {
try { return (await asc('GET', path)).data ?? null; } catch (e) { if (/-> 404/.test(String(e))) return null; throw e; }
}

View file

@ -127,7 +127,7 @@ async function translate(ctx: Ctx): Promise<void> {
if (a.has('json')) printJson({ source: src.id, dryRun: dry, hook: !!hook, files: { apple: files.apple?.path ?? null, play: files.play?.path ?? null }, results: rows.map(r => ({ ...r, problems: problemsOf(r) })) }); if (a.has('json')) printJson({ source: src.id, dryRun: dry, hook: !!hook, files: { apple: files.apple?.path ?? null, play: files.play?.path ?? null }, results: rows.map(r => ({ ...r, problems: problemsOf(r) })) });
else { else {
console.log(textTable(['language', 'store', 'section', 'fields', 'problems'], rows.map(r => [r.lang, r.store === 'apple' ? 'App Store' : 'Google Play', r.action, console.log(textTable(['language', 'store', 'section', 'fields', 'problems'], rows.map(r => [r.lang, r.store === 'apple' ? 'App Store' : 'Google Play', r.action,
r.checks.map(c => `${c.field} ${c.chars}/${c.limit}`).join(', '), problemsOf(r).join('; ')]))); r.checks.map(c => `${c.field} ${c.chars}/${c.limit}${c.unit === 'bytes' ? ' B' : ''}`).join(', '), problemsOf(r).join('; ')])));
} }
const json = a.has('json'); const json = a.has('json');
if (dry) { if (!json) done(TAG, 'dry run: listing files not written'); return; } if (dry) { if (!json) done(TAG, 'dry run: listing files not written'); return; }
@ -167,7 +167,7 @@ function check(ctx: Ctx): void {
if (ctx.args.has('json')) printJson({ sections: r, ok: !bad.length, drafts: drafts.map(d => `${d.store}:${d.lang}`) }); if (ctx.args.has('json')) printJson({ sections: r, ok: !bad.length, drafts: drafts.map(d => `${d.store}:${d.lang}`) });
else { else {
console.log(textTable(['store', 'language', 'status', 'fields', 'problems'], r.map(x => [x.store === 'apple' ? 'App Store' : 'Google Play', x.lang, console.log(textTable(['store', 'language', 'status', 'fields', 'problems'], r.map(x => [x.store === 'apple' ? 'App Store' : 'Google Play', x.lang,
x.status === 'draft' ? 'DRAFT (review)' : x.status === 'translate' ? 'TO TRANSLATE' : 'reviewed', x.checks.map(c => `${c.field} ${c.chars}/${c.limit}`).join(', '), x.status === 'draft' ? 'DRAFT (review)' : x.status === 'translate' ? 'TO TRANSLATE' : 'reviewed', x.checks.map(c => `${c.field} ${c.chars}/${c.limit}${c.unit === 'bytes' ? ' B' : ''}`).join(', '),
x.checks.flatMap(c => c.problems.map(p => `${c.field}: ${p}`)).join('; ')]))); x.checks.flatMap(c => c.problems.map(p => `${c.field}: ${p}`)).join('; ')])));
if (drafts.length) warn(TAG, `${drafts.length} machine-translated draft(s) not reviewed yet: ${drafts.map(d => `${d.store}:${d.lang}`).join(', ')} (storebound listing reviewed --lang <ids> after reading them)`); if (drafts.length) warn(TAG, `${drafts.length} machine-translated draft(s) not reviewed yet: ${drafts.map(d => `${d.store}:${d.lang}`).join(', ')} (storebound listing reviewed --lang <ids> after reading them)`);
done(TAG, bad.length ? `${bad.length} section(s) with problems` : `${r.length} section(s) within the store limits`); done(TAG, bad.length ? `${bad.length} section(s) with problems` : `${r.length} section(s) within the store limits`);

View file

@ -163,7 +163,8 @@ export const TOOLS: ToolDef[] = [
props: { only, csv: { type: 'string', description: 'Prices CSV with rows for subscription ids' } }, props: { only, csv: { type: 'string', description: 'Prices CSV with rows for subscription ids' } },
args: i => [...opt('only', i.only), ...opt('csv', i.csv)] }, args: i => [...opt('only', i.only), ...opt('csv', i.csv)] },
{ name: 'play_data_safety', title: 'Push Play Data safety', command: ['play', 'data-safety'], write: true, destructive: true, network: true, idempotent: true, { name: 'play_data_safety', title: 'Push Play Data safety', command: ['play', 'data-safety'], write: true, destructive: true, network: true, idempotent: true,
description: 'Replace the whole Play Data safety form with the answers from the config (CSV import). dry_run prints the CSV.' }, description: 'Replace the whole Play Data safety form with the answers from the config (CSV import). Skipped when the CSV equals the last upload of this project (hash in .storebound/; force: true sends it anyway). dry_run prints the CSV.',
props: { force: { type: 'boolean', default: false, description: 'Upload even when the CSV equals the last upload (after a manual change in Play Console)' } }, args: i => flag('force', i.force) },
{ name: 'play_release_testing', title: 'Release to a Play testing track', command: ['play', 'release'], write: true, network: true, { name: 'play_release_testing', title: 'Release to a Play testing track', command: ['play', 'release'], write: true, network: true,
description: 'Upload an AAB (or reuse a versionCode) to a TESTING track (internal, alpha, beta) with release notes. Production is not available here.', description: 'Upload an AAB (or reuse a versionCode) to a TESTING track (internal, alpha, beta) with release notes. Production is not available here.',
props: { aab: { type: 'string', description: 'Path to the .aab' }, version_code: { type: 'integer', minimum: 1 }, props: { aab: { type: 'string', description: 'Path to the .aab' }, version_code: { type: 'integer', minimum: 1 },
@ -200,13 +201,18 @@ export const TOOLS: ToolDef[] = [
args: i => ['--phased', String(i.action), ...opt('version', i.version)] }, args: i => ['--phased', String(i.action), ...opt('version', i.version)] },
{ name: 'shots_capture', title: 'Capture raw frames', command: ['shots', 'capture'], write: true, network: false, idempotent: true, { name: 'shots_capture', title: 'Capture raw frames', command: ['shots', 'capture'], write: true, network: false, idempotent: true,
description: 'Raw app or game frames per language into shots.frames from a web build (Playwright), the iOS Simulator (simctl), an Android emulator / device (adb) or a folder check. Local files only; a simulator it boots is shut down again, an emulator is never booted. dry_run lists the plan and the simctl / adb commands.', description: 'Raw app or game frames per language into shots.frames from a web build (Playwright), the iOS Simulator (simctl), an Android emulator / device (adb) or a folder check. Local files only; a simulator it boots is shut down again, an emulator is never booted. dry_run lists the plan and the simctl / adb commands.',
props: { lang, only: { type: 'string', description: 'Comma-separated shot names' }, device: { type: 'string', enum: ['phone', 'tablet'] }, source }, props: { lang, only: { type: 'string', description: 'Comma-separated shot names' }, device: { type: 'string', enum: ['phone', 'tablet'] }, source,
args: i => [...opt('lang', i.lang), ...opt('only', i.only), ...opt('device', i.device), ...opt('source', i.source)] }, concurrency: { type: 'integer', minimum: 1, maximum: 32, description: 'Web: parallel browser contexts (default from the CPU count)' },
mode: { type: 'string', enum: ['changed', 'only-missing', 'force'], default: 'changed', description: 'Web: skip frames with unchanged inputs (default), only missing ones, or capture all' } },
args: i => [...opt('lang', i.lang), ...opt('only', i.only), ...opt('device', i.device), ...opt('source', i.source),
...(i.concurrency ? ['--concurrency', String(i.concurrency)] : []), ...(i.mode === 'force' ? ['--force'] : i.mode === 'only-missing' ? ['--only-missing'] : [])] },
{ name: 'shots_render', title: 'Render store screenshots', command: ['shots', 'render'], write: true, network: false, idempotent: true, { name: 'shots_render', title: 'Render store screenshots', command: ['shots', 'render'], write: true, network: false, idempotent: true,
description: 'Store screenshots + Play feature graphic in every size and language from the frames and the copy file (local PNGs, needs Playwright + ffmpeg; slow for many languages). dry_run lists what would be rendered.', description: 'Store screenshots + Play feature graphic in every size and language from the frames and the copy file (local PNGs, needs Playwright + ffmpeg; slow for many languages). dry_run lists what would be rendered.',
props: { lang, size: { type: 'string', description: 'Comma-separated size keys: ios69, ios65, iphone-medium, ipad13, play, tab7, tab10, feature' }, props: { lang, size: { type: 'string', description: 'Comma-separated size keys: ios69, ios65, iphone-medium, ipad13, play, tab7, tab10, feature' },
scenes: { type: 'string', description: 'Comma-separated scene ids' }, variant, all_variants: { type: 'boolean', default: false, description: 'Base + every variant' } }, scenes: { type: 'string', description: 'Comma-separated scene ids' }, variant, all_variants: { type: 'boolean', default: false, description: 'Base + every variant' },
args: i => [...opt('lang', i.lang), ...opt('size', i.size), ...opt('scenes', i.scenes), ...(i.all_variants === true ? ['--variants'] : opt('variant', i.variant))] }, concurrency: { type: 'integer', minimum: 1, maximum: 32, description: 'Parallel pages (default min(4, CPU cores / 2))' } },
args: i => [...opt('lang', i.lang), ...opt('size', i.size), ...opt('scenes', i.scenes), ...(i.all_variants === true ? ['--variants'] : opt('variant', i.variant)),
...(i.concurrency ? ['--concurrency', String(i.concurrency)] : [])] },
{ name: 'shots_gallery', title: 'Review gallery', command: ['shots', 'gallery'], write: true, network: false, idempotent: true, { name: 'shots_gallery', title: 'Review gallery', command: ['shots', 'gallery'], write: true, network: false, idempotent: true,
description: 'Write <renders>/index.html: one self-contained page with every language, size, scene, variant and app preview, store check issues highlighted. Open it in a browser to review before uploading.', description: 'Write <renders>/index.html: one self-contained page with every language, size, scene, variant and app preview, store check issues highlighted. Open it in a browser to review before uploading.',
props: { lang }, args: i => opt('lang', i.lang) }, props: { lang }, args: i => opt('lang', i.lang) },

View file

@ -3,6 +3,7 @@ import { playServiceAccount } from '../lib/credentials';
import { tokenSource } from '../lib/google-auth'; import { tokenSource } from '../lib/google-auth';
import { log, warn } from '../lib/log'; import { log, warn } from '../lib/log';
import { errorBody } from '../lib/http'; import { errorBody } from '../lib/http';
import { AmbiguousWriteError, isAmbiguous, retrySafe } from '../lib/upsert';
export type PlayClient = Awaited<ReturnType<typeof playClient>>; export type PlayClient = Awaited<ReturnType<typeof playClient>>;
@ -11,41 +12,88 @@ export async function playClient(root: string, packageName: string) {
const API = `https://androidpublisher.googleapis.com/androidpublisher/v3/applications/${packageName}`; const API = `https://androidpublisher.googleapis.com/androidpublisher/v3/applications/${packageName}`;
const UPLOAD = `https://androidpublisher.googleapis.com/upload/androidpublisher/v3/applications/${packageName}`; const UPLOAD = `https://androidpublisher.googleapis.com/upload/androidpublisher/v3/applications/${packageName}`;
/** JSON call (object body) or media upload (Uint8Array body + Content-Type). Throws with the API's text. */ /**
* JSON call (object body) or media upload (Uint8Array body + Content-Type). Throws with the API's text. 429 is
* retried (refused, not applied); 5xx and network errors only for requests that end the same when sent twice
* (retrySafe). A create or upload POST then throws AmbiguousWriteError instead of being sent again.
*/
async function call<T = unknown>(method: string, url: string, body?: unknown, headers: Record<string, string> = {}): Promise<T> { async function call<T = unknown>(method: string, url: string, body?: unknown, headers: Record<string, string> = {}): Promise<T> {
const bin = body instanceof Uint8Array; const bin = body instanceof Uint8Array;
const safe = retrySafe(method, url);
const short = (): string => url.replace(API, '').replace(UPLOAD, '');
for (let attempt = 0; ; attempt++) { for (let attempt = 0; ; attempt++) {
const r = await fetch(url, { let r: Response;
method, try {
headers: { Authorization: 'Bearer ' + (await token()), ...(body === undefined || bin ? {} : { 'Content-Type': 'application/json' }), ...headers }, r = await fetch(url, {
body: body === undefined ? undefined : bin ? (body as unknown as BodyInit) : JSON.stringify(body), method,
}); headers: { Authorization: 'Bearer ' + (await token()), ...(body === undefined || bin ? {} : { 'Content-Type': 'application/json' }), ...headers },
body: body === undefined ? undefined : bin ? (body as unknown as BodyInit) : JSON.stringify(body),
});
} catch (e) {
if (safe && attempt < 4) { await Bun.sleep(2000 * (attempt + 1)); continue; }
if (!safe) throw new AmbiguousWriteError(`${method} ${short()} -> no answer (${(e as Error).message})`);
throw e;
}
const text = await r.text(); const text = await r.text();
if (r.ok) return (text ? JSON.parse(text) : {}) as T; if (r.ok) return (text ? JSON.parse(text) : {}) as T;
if ((r.status === 429 || r.status >= 500) && attempt < 4) { await Bun.sleep(2000 * (attempt + 1)); continue; } if ((r.status === 429 || (r.status >= 500 && safe)) && attempt < 4) { await Bun.sleep(2000 * (attempt + 1)); continue; }
const short = url.replace(API, '').replace(UPLOAD, '');
// A wrong path casing answers with an HTML 404 page, not JSON: say so instead of dumping the HTML. // A wrong path casing answers with an HTML 404 page, not JSON: say so instead of dumping the HTML.
const msg = /^\s*</.test(text) ? `(HTML ${r.status} page: wrong path or casing?)` : errorBody(text); const msg = /^\s*</.test(text) ? `(HTML ${r.status} page: wrong path or casing?)` : errorBody(text);
throw new Error(`${method} ${short} -> ${r.status} ${msg}`); if (r.status >= 500 && !safe) throw new AmbiguousWriteError(`${method} ${short()} -> ${r.status} ${msg}`, r.status);
if (r.status === 404 && method === 'DELETE' && attempt > 0) return {} as T;
throw new Error(`${method} ${short()} -> ${r.status} ${msg}`);
} }
} }
async function editOnce<T>(fn: (id: string) => Promise<T>, o: { dry?: boolean; commitQuery?: string }): Promise<T> { /** At most one open edit per run: a second one would be invalidated by the first commit (or invalidate it). */
let openEdit: string | null = null;
async function openOne(): Promise<string> {
if (openEdit) throw new Error(`a Play edit (${openEdit}) is already open in this run: storebound never opens a second one`);
const e = await call<{ id: string }>('POST', `${API}/edits`, {}); const e = await call<{ id: string }>('POST', `${API}/edits`, {});
log('play', `edit ${e.id} opened`); openEdit = e.id;
return e.id;
}
async function closeOne(id: string, what: string): Promise<void> {
try { await call('DELETE', `${API}/edits/${id}`); } catch (err) {
// an edit that was committed or expired is gone already; anything else is reported (Play drops it later)
if (!/-> 404|not found|has been deleted|already been committed/i.test(String(err))) warn('play', `${what} edit ${id} not deleted: ${(err as Error).message.slice(0, 200)}`);
} finally { if (openEdit === id) openEdit = null; }
}
/** Thrown when a commit got no clear answer: the edit may be live, so it is never redone. */
class CommitUnknown extends Error { readonly noRedo = true; }
/** Controls of an edit: `unchanged()` when fn found nothing to change; the edit is then deleted, not committed. */
interface EditCtl { unchanged(): void }
async function editOnce<T>(fn: (id: string, ctl: EditCtl) => Promise<T>, o: { dry?: boolean; commitQuery?: string }): Promise<T> {
const id = await openOne();
log('play', `edit ${id} opened`);
let same = false;
let committing = false;
try { try {
const out = await fn(e.id); const out = await fn(id, { unchanged: () => { same = true; } });
if (o.dry) { if (o.dry) {
await call('POST', `${API}/edits/${e.id}:validate`); await call('POST', `${API}/edits/${id}:validate`);
log('play', 'dry run: edit validated, discarding'); log('play', 'dry run: edit validated, discarding');
await call('DELETE', `${API}/edits/${e.id}`); await closeOne(id, 'dry run');
} else if (same) {
log('play', 'nothing to change: edit discarded, not committed');
await closeOne(id, 'unchanged');
} else { } else {
await call('POST', `${API}/edits/${e.id}:commit${o.commitQuery ?? ''}`); committing = true;
log('play', `edit ${e.id} committed`); try { await call('POST', `${API}/edits/${id}:commit${o.commitQuery ?? ''}`); } catch (err) {
if (!isAmbiguous(err)) throw err;
openEdit = null;
throw new CommitUnknown(`the commit of edit ${id} got no clear answer (${(err as Error).message.slice(0, 200)}): it may be live. Not redone, so nothing is applied twice; check with \`storebound play status\` (or the dry run of this command) before running it again.`);
}
openEdit = null;
log('play', `edit ${id} committed`);
} }
return out; return out;
} catch (err) { } catch (err) {
await call('DELETE', `${API}/edits/${e.id}`).catch(() => {}); // nothing of an uncommitted edit is live: delete it (also after a failed commit, which leaves the edit open)
if (!(err instanceof CommitUnknown)) await closeOne(id, committing ? 'failed commit' : 'failed');
throw err; throw err;
} }
} }
@ -57,21 +105,23 @@ export async function playClient(root: string, packageName: string) {
* A failed delete is reported (Play drops an unused edit on its own after a while, but it should not linger). * A failed delete is reported (Play drops an unused edit on its own after a while, but it should not linger).
*/ */
async read<T>(fn: (id: string) => Promise<T>): Promise<T> { async read<T>(fn: (id: string) => Promise<T>): Promise<T> {
const e = await call<{ id: string }>('POST', `${API}/edits`, {}); const id = await openOne();
try { return await fn(e.id); } finally { try { return await fn(id); } finally { await closeOne(id, 'read-only'); }
await call('DELETE', `${API}/edits/${e.id}`).catch(err => warn('play', `read-only edit not deleted: ${(err as Error).message.slice(0, 200)}`));
}
}, },
/** /**
* Opens an edit, runs fn, then commits (or validates and discards when dry). Any Console save or other edit commit * Opens an edit, runs fn, then commits (or validates and discards when dry). Any Console save or other edit commit
* invalidates an open edit ("A change was made to the application outside of this Edit"); then the whole edit is * invalidates an open edit ("A change was made to the application outside of this Edit"); then the whole edit is
* redone, up to `retries` times. Keep edits small (e.g. one per language for images) so a retry is cheap. * redone, up to `retries` times. Keep edits small (e.g. one per language for images) so a retry is cheap.
*/ */
async edit<T>(fn: (id: string) => Promise<T>, o: { dry?: boolean; commitQuery?: string; retries?: number } = {}): Promise<T> { async edit<T>(fn: (id: string, ctl: EditCtl) => Promise<T>, o: { dry?: boolean; commitQuery?: string; retries?: number } = {}): Promise<T> {
for (let left = o.retries ?? 3; ; left--) { for (let left = o.retries ?? 3; ; left--) {
try { return await editOnce(fn, o); } catch (err) { try { return await editOnce(fn, o); } catch (err) {
if (left <= 0 || !/outside of this Edit|Edit has been deleted|edit.*(expired|not found)/i.test(String(err))) throw err; // redo only what never went live: an edit invalidated before or at its commit, or a request inside the edit
log('play', `app changed outside the edit (Console?), redoing it (${left} retries left)`); // without a clear answer (the edit was deleted, so nothing of it applies). Never after an unclear commit.
if (left <= 0 || (err as { noRedo?: boolean }).noRedo) throw err;
const outside = /outside of this Edit|Edit has been deleted|edit.*(expired|not found)/i.test(String(err));
if (!outside && !isAmbiguous(err)) throw err;
log('play', `${outside ? 'app changed outside the edit (Console?)' : 'a request got no clear answer; the edit was discarded'}, redoing it (${left} retries left)`);
} }
} }
}, },

View file

@ -1,7 +1,10 @@
// Play Data safety form through applications.dataSafety (CSV import; replaces every earlier answer). // Play Data safety form through applications.dataSafety (CSV import; replaces every earlier answer).
// storebound play data-safety [--csv out.csv] [--dry] // storebound play data-safety [--csv out.csv] [--dry] [--force]
// Answers come from `dataSafety` in storebound.config.ts. --dry only writes / prints the CSV. // Answers come from `dataSafety` in storebound.config.ts. --dry only writes / prints the CSV.
import { mkdirSync, writeFileSync } from 'node:fs'; // Play cannot read the form back, so a run compares with what this project uploaded last: the sha256 of that CSV
// (per package) is kept in .storebound/data-safety.sha256 (gitignored). The same CSV is not sent again; --force sends it
// anyway (e.g. after the form was changed in Play Console by hand).
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
import { join } from 'node:path'; import { join } from 'node:path';
import type { Ctx } from '../lib/ctx'; import type { Ctx } from '../lib/ctx';
import { need } from '../lib/ctx'; import { need } from '../lib/ctx';
@ -10,6 +13,27 @@ import { DATA_SAFETY_UPLOADED } from '../lib/declarations';
import { done, log } from '../lib/log'; import { done, log } from '../lib/log';
import { playClient } from './client'; import { playClient } from './client';
export const DATA_SAFETY_HASH = '.storebound/data-safety.sha256';
/** sha256 of the CSV for one package. Pure. */
export const dataSafetyHash = (pkg: string, csv: string): string => new Bun.CryptoHasher('sha256').update(`${pkg}\n${csv}`).digest('hex');
/** The hash of the last upload of this package, null when none is recorded. */
export function lastUpload(root: string, pkg: string): { sha256: string; at: string } | null {
const f = join(root, DATA_SAFETY_HASH);
if (!existsSync(f)) return null;
try { return (JSON.parse(readFileSync(f, 'utf8')) as Record<string, { sha256: string; at: string }>)[pkg] ?? null; } catch { return null; }
}
function rememberUpload(root: string, pkg: string, sha256: string): void {
const f = join(root, DATA_SAFETY_HASH);
let all: Record<string, { sha256: string; at: string }> = {};
try { if (existsSync(f)) all = JSON.parse(readFileSync(f, 'utf8')); } catch { all = {}; }
all[pkg] = { sha256, at: new Date().toISOString() };
mkdirSync(join(root, '.storebound'), { recursive: true });
writeFileSync(f, JSON.stringify(all, null, 2) + '\n');
}
export async function run(ctx: Ctx): Promise<void> { export async function run(ctx: Ctx): Promise<void> {
const { config, root, resolve } = ctx.loaded; const { config, root, resolve } = ctx.loaded;
const csv = dataSafetyCsv(need(config.dataSafety, 'dataSafety')); const csv = dataSafetyCsv(need(config.dataSafety, 'dataSafety'));
@ -17,10 +41,25 @@ export async function run(ctx: Ctx): Promise<void> {
if (out) { writeFileSync(resolve(out), csv); log('play', `wrote ${out}`); } if (out) { writeFileSync(resolve(out), csv); log('play', `wrote ${out}`); }
const types = Object.keys(config.dataSafety!.data); const types = Object.keys(config.dataSafety!.data);
log('play', `data safety: ${types.length} data types declared (${types.join(', ') || 'none'})`); log('play', `data safety: ${types.length} data types declared (${types.join(', ') || 'none'})`);
if (ctx.args.has('dry')) { if (!out) process.stdout.write(csv); return; } const pkg = need(config.play?.packageName, 'play.packageName');
const play = await playClient(root, need(config.play?.packageName, 'play.packageName')); const hash = dataSafetyHash(pkg, csv);
const last = lastUpload(root, pkg);
const same = last?.sha256 === hash;
if (ctx.args.has('dry')) {
if (!out) process.stdout.write(csv);
log('play', same ? `the same CSV was uploaded ${last!.at.slice(0, 16).replace('T', ' ')} UTC: a real run sends nothing (--force sends it again)` : 'a real run uploads it (it differs from the last upload, or none is recorded)');
return;
}
if (same && !ctx.args.has('force')) {
done('play', `data safety unchanged since the upload of ${last!.at.slice(0, 16).replace('T', ' ')} UTC: nothing sent (--force sends it again, e.g. after a change in Play Console)`);
return;
}
const play = await playClient(root, pkg);
await play.call('POST', `${play.API}/dataSafety`, { safetyLabels: csv }); await play.call('POST', `${play.API}/dataSafety`, { safetyLabels: csv });
// Play cannot read the form back: keep what went up, so the desktop app can show what changed since (gitignored). // keep what went up: the hash skips the next identical upload, the CSV lets the desktop app show what changed since
try { mkdirSync(join(root, '.storebound'), { recursive: true }); writeFileSync(join(root, DATA_SAFETY_UPLOADED), csv); } catch { /* the upload itself worked */ } try {
rememberUpload(root, pkg, hash);
writeFileSync(join(root, DATA_SAFETY_UPLOADED), csv);
} catch { /* the upload itself worked */ }
done('play', `data safety updated (${csv.split('\n').length - 2} rows)`); done('play', `data safety updated (${csv.split('\n').length - 2} rows)`);
} }

View file

@ -3,8 +3,9 @@
// storebound play images [--dry] [--only en-US,de-DE] [--src store/renders] [--variant b] // storebound play images [--dry] [--only en-US,de-DE] [--src store/renders] [--variant b]
// Per Play language, from <renders>/<language id>/: icon (play.icon, 512x512), featureGraphic (feature/1024x500.*), // Per Play language, from <renders>/<language id>/: icon (play.icon, 512x512), featureGraphic (feature/1024x500.*),
// phoneScreenshots (play/NN.*), sevenInchScreenshots (tab7/NN.*), tenInchScreenshots (tab10/NN.*). A .jpg next to a // phoneScreenshots (play/NN.*), sevenInchScreenshots (tab7/NN.*), tenInchScreenshots (tab10/NN.*). A .jpg next to a
// .png wins (JPEG q92 uploads about 4x faster; `storebound shots jpeg` makes them). Each image type is cleared // .png wins (JPEG q92 uploads about 4x faster; `storebound shots jpeg` makes them). Play's images are compared with
// before uploading, so re-runs are idempotent. --variant <id> uploads <renders>/<id>/<lang>/... (an A/B variant from // the local files first (sha256, in order): only a changed image type is cleared and uploaded, in one edit per
// language that is committed whole or not at all, so a re-run changes nothing and never doubles an image. --variant <id> uploads <renders>/<id>/<lang>/... (an A/B variant from
// shots.variants) to the main listing: Play store listing experiments themselves are set up in Play Console (no API). // shots.variants) to the main listing: Play store listing experiments themselves are set up in Play Console (no API).
// Uploads are slow: run in the background with a log file. // Uploads are slow: run in the background with a log file.
// --dry is read-only: one throwaway edit, the images Play has now (their sha256) compared with the local files per // --dry is read-only: one throwaway edit, the images Play has now (their sha256) compared with the local files per
@ -71,36 +72,51 @@ export async function run(ctx: Ctx): Promise<void> {
log('play', `${codes.length} languages, ${[...sets.values()][0]?.reduce((n, [, f]) => n + f.length, 0) ?? 0} images each`); log('play', `${codes.length} languages, ${[...sets.values()][0]?.reduce((n, [, f]) => n + f.length, 0) ?? 0} images each`);
const play = await playClient(root, pkg); const play = await playClient(root, pkg);
// one read-only edit: what Play has now (sha256 per image, in order) against the local files, per language and type
const plan = new Map<string, ImageChange[]>();
await play.read(async id => {
for (const code of codes) {
const out: ImageChange[] = [];
for (const [type, files] of sets.get(code)!) {
const remote = (await play.call<{ images?: { sha256?: string }[] }>('GET', `${play.API}/edits/${id}/listings/${code}/${type}`)).images ?? [];
out.push(imageChange(type, remote, files.map(sha256)));
}
plan.set(code, out);
}
});
const changedTypes = (code: string) => plan.get(code)!.filter(c => c.change === 'replace');
if (ctx.args.has('dry')) { if (ctx.args.has('dry')) {
let changed = 0; let changed = 0;
await play.read(async id => { for (const [i, code] of codes.entries()) {
for (const [i, code] of codes.entries()) { const rep = changedTypes(code);
const out: ImageChange[] = []; changed += rep.length;
for (const [type, files] of sets.get(code)!) { log('play', `${step(i + 1, codes.length, code)}: ${rep.length ? 'would replace ' + rep.map(c => `${c.type} (${c.remote} -> ${c.local})`).join(', ') : 'unchanged'}`);
const remote = (await play.call<{ images?: { sha256?: string }[] }>('GET', `${play.API}/edits/${id}/listings/${code}/${type}`)).images ?? []; }
out.push(imageChange(type, remote, files.map(sha256)));
}
const rep = out.filter(c => c.change === 'replace');
changed += rep.length;
log('play', `${step(i + 1, codes.length, code)}: ${rep.length ? 'would replace ' + rep.map(c => `${c.type} (${c.remote} -> ${c.local})`).join(', ') : 'unchanged'}`);
}
});
done('play', `dry run: ${changed ? changed + ' image set(s) would be replaced' : 'every image set is already on Play'}; read-only, nothing uploaded`); done('play', `dry run: ${changed ? changed + ' image set(s) would be replaced' : 'every image set is already on Play'}; read-only, nothing uploaded`);
return; return;
} }
for (const [i, code] of codes.entries()) { // only languages with a changed image type get an edit, and in it only the changed types are cleared and uploaded.
// The edit is all or nothing: a failure (or an upload without a clear answer) deletes it, and the redo starts from
// what Play has, so a re-run never doubles an image.
const todo = codes.filter(code => changedTypes(code).length);
if (!todo.length) { done('play', 'every image set is already on Play: nothing uploaded'); return; }
for (const [i, code] of todo.entries()) {
const set = sets.get(code)!; const set = sets.get(code)!;
await play.edit(async id => { await play.edit(async (id, ctl) => {
let n = 0; let n = 0, types = 0;
await Promise.all(set.map(async ([type, files]) => { await Promise.all(set.map(async ([type, files]) => {
await play.call('DELETE', `${play.API}/edits/${id}/listings/${code}/${type}`); const remote = (await play.call<{ images?: { sha256?: string }[] }>('GET', `${play.API}/edits/${id}/listings/${code}/${type}`)).images ?? [];
if (imageChange(type, remote, files.map(sha256)).change === 'same') return;
types++;
if (remote.length) await play.call('DELETE', `${play.API}/edits/${id}/listings/${code}/${type}`);
for (const f of files) { for (const f of files) {
await play.call('POST', `${play.UPLOAD}/edits/${id}/listings/${code}/${type}?uploadType=media`, new Uint8Array(readFileSync(f)), await play.call('POST', `${play.UPLOAD}/edits/${id}/listings/${code}/${type}?uploadType=media`, new Uint8Array(readFileSync(f)),
{ 'Content-Type': /\.png$/i.test(f) ? 'image/png' : 'image/jpeg' }); { 'Content-Type': /\.png$/i.test(f) ? 'image/png' : 'image/jpeg' });
n++; n++;
} }
})); }));
log('play', `${step(i + 1, codes.length, code)}: ${n} images`); if (!types) ctl.unchanged();
log('play', `${step(i + 1, todo.length, code)}: ${n} images in ${types} set(s)`);
}); });
} }
done('play', 'images done'); done('play', 'images done');

View file

@ -140,16 +140,24 @@ export async function run(ctx: Ctx): Promise<void> {
if (dry) { done('play', `dry run: ${p.changes ? `${p.changes} change(s)` : 'nothing to change'}; read-only, nothing written`); return; } if (dry) { done('play', `dry run: ${p.changes ? `${p.changes} change(s)` : 'nothing to change'}; read-only, nothing written`); return; }
if (!p.changes) { done('play', 'listing already up to date'); return; } if (!p.changes) { done('play', 'listing already up to date'); return; }
if (process.stdin.isTTY && !(await confirm(ctx, `Apply these ${p.changes} change(s) to Google Play (one edit, committed)?`))) { warn('play', 'nothing written'); return; } if (process.stdin.isTTY && !(await confirm(ctx, `Apply these ${p.changes} change(s) to Google Play (one edit, committed)?`))) { warn('play', 'nothing written'); return; }
const todo = p.listings.filter(l => l.action !== 'same').map(l => bodies.find(b => b.language === l.key)!); // The edit compares again with what it holds (a Console save or another run since the plan): only differing details
await play.edit(async id => { // and languages are PUT; nothing left to change = the edit is deleted, not committed. Commit only after every PUT worked.
if (p.details.action !== 'same') { let sent = 0;
await play.edit(async (id, ctl) => {
const cur = await play.call<Record<string, string>>('GET', `${play.API}/edits/${id}/details`);
const now = (await play.call<{ listings?: PlayListing[] }>('GET', `${play.API}/edits/${id}/listings`)).listings ?? [];
const q = playPlan(cur, details, now, bodies, { only: a.has('only') });
const todo = q.listings.filter(l => l.action !== 'same').map(l => bodies.find(b => b.language === l.key)!);
sent = q.changes;
if (!q.changes) { ctl.unchanged(); return; }
if (q.details.action !== 'same') {
await play.call('PUT', `${play.API}/edits/${id}/details`, details); await play.call('PUT', `${play.API}/edits/${id}/details`, details);
log('play', `details: ${p.details.changes.map(ch => ch.field).join(', ')}`); log('play', `details: ${q.details.changes.map(ch => ch.field).join(', ')}`);
} }
for (const [i, b] of todo.entries()) { for (const [i, b] of todo.entries()) {
await play.call('PUT', `${play.API}/edits/${id}/listings/${b.language}`, b); await play.call('PUT', `${play.API}/edits/${id}/listings/${b.language}`, b);
log('play', step(i + 1, todo.length, b.language!)); log('play', step(i + 1, todo.length, b.language!));
} }
}); });
done('play', 'listing done'); done('play', sent ? 'listing done' : 'listing already up to date (the edit was deleted, nothing committed)');
} }

View file

@ -10,10 +10,26 @@ import { readFileSync, statSync } from 'node:fs';
import type { Ctx } from '../lib/ctx'; import type { Ctx } from '../lib/ctx';
import { confirm, need } from '../lib/ctx'; import { confirm, need } from '../lib/ctx';
import { done, log } from '../lib/log'; import { done, log } from '../lib/log';
import { aabInfo } from '../lib/gradle';
import { playClient } from './client'; import { playClient } from './client';
import { usedVersionCodes } from './next-version-code';
import { readPlayCopy } from './listing'; import { readPlayCopy } from './listing';
const TRACKS = ['internal', 'alpha', 'beta', 'production']; const TRACKS = ['internal', 'alpha', 'beta', 'production'];
export interface PlayTrackRelease { versionCodes?: string[]; status?: string; userFraction?: number; name?: string; releaseNotes?: { language: string; text: string }[] }
/** The track holds exactly this release already (one release, same codes, status, fraction, name and notes): a PUT
* would change nothing. Pure. */
export function sameRelease(cur: PlayTrackRelease[], want: PlayTrackRelease): boolean {
if (cur.length !== 1) return false;
const c = cur[0]!;
const notes = (n?: { language: string; text: string }[]) => JSON.stringify([...(n ?? [])].sort((x, y) => x.language.localeCompare(y.language)).map(x => [x.language, x.text.trim()]));
return (c.versionCodes ?? []).map(String).join() === (want.versionCodes ?? []).join() && c.status === want.status
&& (want.userFraction === undefined || Math.abs((c.userFraction ?? -1) - want.userFraction) < 1e-9)
&& (want.name === undefined || c.name === want.name)
&& (want.releaseNotes === undefined || notes(c.releaseNotes) === notes(want.releaseNotes));
}
const STATUSES = ['completed', 'draft', 'inProgress', 'halted']; const STATUSES = ['completed', 'draft', 'inProgress', 'halted'];
export async function run(ctx: Ctx): Promise<void> { export async function run(ctx: Ctx): Promise<void> {
@ -33,23 +49,37 @@ export async function run(ctx: Ctx): Promise<void> {
const notes = readPlayCopy(ctx).filter(c => c.whatsNew).map(c => ({ language: c.lang, text: c.whatsNew })); const notes = readPlayCopy(ctx).filter(c => c.whatsNew).map(c => ({ language: c.lang, text: c.whatsNew }));
const play = await playClient(root, pkg); const play = await playClient(root, pkg);
try { try {
await play.edit(async id => { await play.edit(async (id, ctl) => {
let code = fixed ?? ''; let code = fixed ?? '';
let uploaded = false;
if (aab) { if (aab) {
log('play', `uploading ${aab} (${(statSync(aab).size / 1048576).toFixed(1)} MB), this can take minutes`); // a versionCode Play already has (an earlier run uploaded it) is reused, never uploaded twice
const b = await play.call<{ versionCode: number }>('POST', `${play.UPLOAD}/edits/${id}/bundles?uploadType=media`, const info = aabInfo(aab);
new Uint8Array(readFileSync(aab)), { 'Content-Type': 'application/octet-stream' }); if ((await usedVersionCodes(play, id)).includes(info.versionCode)) {
code = String(b.versionCode); code = String(info.versionCode);
log('play', `uploaded versionCode ${code}`); log('play', `versionCode ${code} is on Play already: not uploaded again`);
} else {
log('play', `uploading ${aab} (${(statSync(aab).size / 1048576).toFixed(1)} MB), this can take minutes`);
const b = await play.call<{ versionCode: number }>('POST', `${play.UPLOAD}/edits/${id}/bundles?uploadType=media`,
new Uint8Array(readFileSync(aab)), { 'Content-Type': 'application/octet-stream' });
code = String(b.versionCode);
uploaded = true;
log('play', `uploaded versionCode ${code}`);
}
} }
let changed = 0;
for (const t of tracks) { for (const t of tracks) {
const release: Record<string, unknown> = { versionCodes: [code], status: t.status }; const release: PlayTrackRelease = { versionCodes: [code], status: t.status };
if (notes.length) release.releaseNotes = notes; if (notes.length) release.releaseNotes = notes;
if (a.get('name')) release.name = a.get('name'); if (a.get('name')) release.name = a.get('name');
if (t.status === 'inProgress') release.userFraction = Number(fraction ?? 0.1); if (t.status === 'inProgress') release.userFraction = Number(fraction ?? 0.1);
const cur = (await play.call<{ releases?: PlayTrackRelease[] }>('GET', `${play.API}/edits/${id}/tracks/${t.track}`).catch(() => ({ releases: [] }))).releases ?? [];
if (sameRelease(cur, release)) { log('play', `${t.track}: already ${t.status} ${code}, unchanged`); continue; }
await play.call('PUT', `${play.API}/edits/${id}/tracks/${t.track}`, { track: t.track, releases: [release] }); await play.call('PUT', `${play.API}/edits/${id}/tracks/${t.track}`, { track: t.track, releases: [release] });
changed++;
log('play', `${t.track} <- ${code} (${t.status}, notes in ${notes.length} languages)`); log('play', `${t.track} <- ${code} (${t.status}, notes in ${notes.length} languages)`);
} }
if (!changed && !uploaded) ctl.unchanged();
}, { dry: a.has('dry'), retries: aab ? 0 : 3 }); }, { dry: a.has('dry'), retries: aab ? 0 : 3 });
} catch (e) { } catch (e) {
if (/draft app|status draft/i.test(String(e))) if (/draft app|status draft/i.test(String(e)))

View file

@ -10,6 +10,13 @@ import { done, log } from '../lib/log';
import { type PlayRelease, type RolloutAction, rolloutReleases } from '../lib/release'; import { type PlayRelease, type RolloutAction, rolloutReleases } from '../lib/release';
import { playClient } from './client'; import { playClient } from './client';
/** Same releases on the track, whatever their order and key order. Pure. */
export function sameReleases(a: PlayRelease[], b: PlayRelease[]): boolean {
const canon = (x: unknown): unknown => (Array.isArray(x) ? x.map(canon) : x && typeof x === 'object' ? Object.fromEntries(Object.entries(x).sort(([k], [l]) => k.localeCompare(l)).map(([k, v]) => [k, canon(v)])) : x);
const key = (rs: PlayRelease[]) => JSON.stringify(rs.map(r => JSON.stringify(canon(r))).sort());
return key(a) === key(b);
}
export function rolloutAction(a: Ctx['args']): RolloutAction { export function rolloutAction(a: Ctx['args']): RolloutAction {
const picked = ['halt', 'resume', 'complete'].filter(k => a.has(k)); const picked = ['halt', 'resume', 'complete'].filter(k => a.has(k));
if (picked.length > 1) throw new Error('pick one of --halt, --resume, --complete'); if (picked.length > 1) throw new Error('pick one of --halt, --resume, --complete');
@ -30,10 +37,12 @@ export async function run(ctx: Ctx): Promise<void> {
const dry = ctx.args.has('dry'); const dry = ctx.args.has('dry');
const play = await playClient(root, need(config.play?.packageName, 'play.packageName')); const play = await playClient(root, need(config.play?.packageName, 'play.packageName'));
let confirmed = dry || action.kind === 'halt' || track !== 'production'; let confirmed = dry || action.kind === 'halt' || track !== 'production';
await play.edit(async id => { await play.edit(async (id, ctl) => {
const cur = await play.call<{ releases?: PlayRelease[] }>('GET', `${play.API}/edits/${id}/tracks/${track}`); const cur = await play.call<{ releases?: PlayRelease[] }>('GET', `${play.API}/edits/${id}/tracks/${track}`);
const next = rolloutReleases(cur.releases ?? [], action); const next = rolloutReleases(cur.releases ?? [], action);
log('play', `${track}: ${next.summary}${dry ? ' (dry)' : ''}`); log('play', `${track}: ${next.summary}${dry ? ' (dry)' : ''}`);
// already like that (a re-run, or the same command twice): nothing is committed
if (sameReleases(next.releases, cur.releases ?? [])) { ctl.unchanged(); return; }
if (!confirmed) confirmed = await confirm(ctx, `${track}: ${next.summary}. Apply?`); if (!confirmed) confirmed = await confirm(ctx, `${track}: ${next.summary}. Apply?`);
if (!confirmed) throw new Error('not confirmed, nothing changed'); if (!confirmed) throw new Error('not confirmed, nothing changed');
await play.call('PUT', `${play.API}/edits/${id}/tracks/${track}`, { track, releases: next.releases }); await play.call('PUT', `${play.API}/edits/${id}/tracks/${track}`, { track, releases: next.releases });

View file

@ -20,7 +20,13 @@ import {
playSubscriptionBody, playSubscriptionBody,
} from '../lib/subscriptions'; } from '../lib/subscriptions';
import { subscriptionCsv } from '../apple/subscriptions'; import { subscriptionCsv } from '../apple/subscriptions';
import { playClient } from './client'; import { createOnce } from '../lib/upsert';
import { playClient, type PlayClient } from './client';
/** GET of one Play resource, null on 404. */
export async function getOrNull<T>(play: PlayClient, url: string): Promise<T | null> {
try { return await play.call<T>('GET', url); } catch (e) { if (/-> 404/.test(String(e))) return null; throw e; }
}
const TAG = 'play'; const TAG = 'play';
interface Converted { convertedRegionPrices?: Record<string, { regionCode: string; price: Money }>; convertedOtherRegionsPrice?: { usdPrice: Money; eurPrice: Money }; regionVersion?: { version: string } } interface Converted { convertedRegionPrices?: Record<string, { regionCode: string; price: Money }>; convertedOtherRegionsPrice?: { usdPrice: Money; eurPrice: Money }; regionVersion?: { version: string } }
@ -86,8 +92,12 @@ export async function run(ctx: Ctx): Promise<void> {
const id = encodeURIComponent(s.id); const id = encodeURIComponent(s.id);
const cur = existing.get(s.id); const cur = existing.get(s.id);
if (diff.create) { if (diff.create) {
await play.call('POST', `${play.API}/subscriptions?productId=${id}&regionsVersion.version=${v}`, body); // product ids are unique: a 409 (or an unclear answer, then a fresh GET) finds the existing one, never a second
log(TAG, `${s.id}: created`); const r = await createOnce({ what: `subscription ${s.id}`, tag: TAG, unique: true,
find: () => getOrNull<PlaySubscription>(play, `${play.API}/subscriptions/${id}`),
create: () => play.call<PlaySubscription>('POST', `${play.API}/subscriptions?productId=${id}&regionsVersion.version=${v}`, body) });
log(TAG, `${s.id}: ${r.created ? 'created' : 'exists already (made by an earlier run); run again to compare it with the config'}`);
if (!r.created) continue;
} else if (diff.listings.length || diff.basePlan !== 'same') { } else if (diff.listings.length || diff.basePlan !== 'same') {
if (diff.basePlan === 'changed' && diff.priceChanges.length && !liveOk) warn(TAG, `${s.id}: ${diff.priceChanges.length} price change(s) skipped (run in a terminal to confirm)`); if (diff.basePlan === 'changed' && diff.priceChanges.length && !liveOk) warn(TAG, `${s.id}: ${diff.priceChanges.length} price change(s) skipped (run in a terminal to confirm)`);
else { else {
@ -107,10 +117,14 @@ export async function run(ctx: Ctx): Promise<void> {
offerRegions = { regions: Object.values(oc.convertedRegionPrices).map(r => ({ regionCode: r.regionCode, price: r.price })), other: oc.convertedOtherRegionsPrice }; offerRegions = { regions: Object.values(oc.convertedRegionPrices).map(r => ({ regionCode: r.regionCode, price: r.price })), other: oc.convertedOtherRegionsPrice };
} }
const ob = playOfferBody(pkg, s, regions, offerRegions, extra); const ob = playOfferBody(pkg, s, regions, offerRegions, extra);
await play.call('POST', `${play.API}/subscriptions/${id}/basePlans/${basePlanId(s)}/offers?offerId=${encodeURIComponent(ob.offerId)}&regionsVersion.version=${v}`, ob); const offerUrl = `${play.API}/subscriptions/${id}/basePlans/${basePlanId(s)}/offers`;
await play.call('POST', `${play.API}/subscriptions/${id}/basePlans/${basePlanId(s)}/offers/${encodeURIComponent(ob.offerId)}:activate`, // offer ids are unique per base plan: a 409 or an unclear answer finds the existing offer, never a second one
{ packageName: pkg, productId: s.id, basePlanId: basePlanId(s), offerId: ob.offerId }); const r = await createOnce({ what: `${s.id} offer ${ob.offerId}`, tag: TAG, unique: true,
log(TAG, `${s.id}: offer ${ob.offerId} created and activated`); find: () => getOrNull<{ state?: string }>(play, `${offerUrl}/${encodeURIComponent(ob.offerId)}`),
create: () => play.call<{ state?: string }>('POST', `${offerUrl}?offerId=${encodeURIComponent(ob.offerId)}&regionsVersion.version=${v}`, ob) });
if (r.value.state !== 'ACTIVE')
await play.call('POST', `${offerUrl}/${encodeURIComponent(ob.offerId)}:activate`, { packageName: pkg, productId: s.id, basePlanId: basePlanId(s), offerId: ob.offerId });
log(TAG, `${s.id}: offer ${ob.offerId} ${r.created ? 'created and activated' : 'exists already, active'}`);
}; };
if (s.intro && offerMissing) await createOffer(s.intro); if (s.intro && offerMissing) await createOffer(s.intro);
for (const o of (s.offers ?? []).filter(x => extraMissing.includes(x.id))) await createOffer(o, o); for (const o of (s.offers ?? []).filter(x => extraMissing.includes(x.id))) await createOffer(o, o);

View file

@ -48,6 +48,12 @@ export async function run(ctx: Ctx): Promise<void> {
used: await usedVersionCodes(play, id), used: await usedVersionCodes(play, id),
current: (await play.call<{ releases?: { status: string; versionCodes?: string[] }[] }>('GET', `${play.API}/edits/${id}/tracks/${track}`).catch(() => ({ releases: [] }))).releases ?? [], current: (await play.call<{ releases?: { status: string; versionCodes?: string[] }[] }>('GET', `${play.API}/edits/${id}/tracks/${track}`).catch(() => ({ releases: [] }))).releases ?? [],
})); }));
// an earlier run did it already (the bundle is on Play and on this track): nothing to send again
if (used.includes(info.versionCode) && current.some(r => (r.versionCodes ?? []).includes(String(info.versionCode)) && r.status === status)) {
if (json) { printJson({ aab: relative(root, aab), packageName: info.packageName, versionCode: info.versionCode, track, status, uploaded: true, alreadyThere: true }); return; }
done(TAG, `versionCode ${info.versionCode} is on ${track} (${status}) already: nothing uploaded`);
return;
}
if (used.includes(info.versionCode)) throw new Error(`versionCode ${info.versionCode} is already on Google Play: rebuild with --version-code ${nextVersionCode(used)} (storebound play build)`); if (used.includes(info.versionCode)) throw new Error(`versionCode ${info.versionCode} is already on Google Play: rebuild with --version-code ${nextVersionCode(used)} (storebound play build)`);
const summary = { aab: relative(root, aab), packageName: info.packageName, versionCode: info.versionCode, versionName: info.versionName ?? null, sizeMB: Math.round(size / 104857.6) / 10, const summary = { aab: relative(root, aab), packageName: info.packageName, versionCode: info.versionCode, versionName: info.versionName ?? null, sizeMB: Math.round(size / 104857.6) / 10,
track, status, notes: notes.map(n => n.language), replaces: current.map(r => `${r.status} ${(r.versionCodes ?? []).join(',')}`) }; track, status, notes: notes.map(n => n.language), replaces: current.map(r => `${r.status} ${(r.versionCodes ?? []).join(',')}`) };

View file

@ -22,6 +22,7 @@
// POST .../packages/{id}/actions/attach_products // POST .../packages/{id}/actions/attach_products
import { credentialValue } from './lib/credentials'; import { credentialValue } from './lib/credentials';
import { confirm, type Ctx, need } from './lib/ctx'; import { confirm, type Ctx, need } from './lib/ctx';
import { AmbiguousWriteError, createOnce } from './lib/upsert';
import { errorBody } from './lib/http'; import { errorBody } from './lib/http';
import { done, log, printJson, warn } from './lib/log'; import { done, log, printJson, warn } from './lib/log';
import { import {
@ -46,11 +47,18 @@ export function rcClient(key: string, o: { retries?: number } = {}): RcClient {
const u = new URL(url, ORIGIN); const u = new URL(url, ORIGIN);
if (u.origin !== ORIGIN) throw new Error(`RevenueCat: refusing to send the key to ${u.origin}`); if (u.origin !== ORIGIN) throw new Error(`RevenueCat: refusing to send the key to ${u.origin}`);
for (let attempt = 0; ; attempt++) { for (let attempt = 0; ; attempt++) {
const r = await fetch(u, { let r: Response;
method, signal: AbortSignal.timeout(30_000), try {
headers: { 'Authorization': `Bearer ${key}`, 'Accept': 'application/json', ...(body === undefined ? {} : { 'Content-Type': 'application/json' }) }, r = await fetch(u, {
body: body === undefined ? undefined : JSON.stringify(body), method, signal: AbortSignal.timeout(30_000),
}); headers: { 'Authorization': `Bearer ${key}`, 'Accept': 'application/json', ...(body === undefined ? {} : { 'Content-Type': 'application/json' }) },
body: body === undefined ? undefined : JSON.stringify(body),
});
} catch (e) {
// a POST without an answer may have been applied: callers look before sending it again (createOnce)
if (method === 'POST') throw new AmbiguousWriteError(redact(`RevenueCat ${method} ${u.pathname}: no answer (${(e as Error).message})`));
throw e;
}
if (r.status === 429 && attempt < (o.retries ?? 3)) { if (r.status === 429 && attempt < (o.retries ?? 3)) {
const s = Math.min(60, Math.max(0, Number(r.headers.get('retry-after') ?? '5') || 0)); const s = Math.min(60, Math.max(0, Number(r.headers.get('retry-after') ?? '5') || 0));
warn(TAG, `rate limited, waiting ${s}s`); warn(TAG, `rate limited, waiting ${s}s`);
@ -59,6 +67,7 @@ export function rcClient(key: string, o: { retries?: number } = {}): RcClient {
continue; continue;
} }
const text = await r.text(); const text = await r.text();
if (!r.ok && r.status >= 500 && method === 'POST') throw new AmbiguousWriteError(redact(`RevenueCat ${method} ${u.pathname}: ${r.status} ${errorBody(text, 500)}`), r.status);
if (!r.ok) { if (!r.ok) {
const hint = r.status === 401 || r.status === 403 ? ' (REVENUECAT_SECRET_KEY: a v2 secret key with project configuration permissions for this project)' : ''; const hint = r.status === 401 || r.status === 403 ? ' (REVENUECAT_SECRET_KEY: a v2 secret key with project configuration permissions for this project)' : '';
throw new Error(redact(`RevenueCat ${method} ${u.pathname}: ${r.status} ${errorBody(text, 500)}${hint}`)); throw new Error(redact(`RevenueCat ${method} ${u.pathname}: ${r.status} ${errorBody(text, 500)}${hint}`));
@ -172,15 +181,22 @@ export async function execute(c: RcClient, projectId: string, plan: RcStep[], st
let offering = current?.id ?? ''; let offering = current?.id ?? '';
const pkgId = new Map((current?.packages ?? []).map(p => [p.lookup_key, p.id] as const)); const pkgId = new Map((current?.packages ?? []).map(p => [p.lookup_key, p.id] as const));
const pid = (k: string) => { const id = productId.get(k); if (!id) throw new Error(`RevenueCat product ${k} was not created`); return id; }; const pid = (k: string) => { const id = productId.get(k); if (!id) throw new Error(`RevenueCat product ${k} was not created`); return id; };
// creates go through createOnce: a 409 (lookup key / store identifier taken) or an unclear answer finds the existing
// one by its key with a fresh list, never a second one; attaching and is_current are the same when sent twice
const once = async (what: string, list: string, match: (x: J) => boolean, create: () => Promise<J>): Promise<string> =>
(await createOnce<J>({ what, tag: TAG, unique: true, find: async () => (await c.all(list)).find(match) ?? null, create })).value.id as string;
for (const [i, s] of plan.entries()) { for (const [i, s] of plan.entries()) {
log(TAG, `${i + 1}/${plan.length} ${s.what}`); log(TAG, `${i + 1}/${plan.length} ${s.what}`);
switch (s.op) { switch (s.op) {
case 'create-product': productId.set(s.key, (await c.post(`${P}/products`, s.body)).id); break; case 'create-product': productId.set(s.key, await once(s.what, `${P}/products?limit=100`, x => x.app_id === s.body.app_id && x.store_identifier === s.body.store_identifier, () => c.post(`${P}/products`, s.body))); break;
case 'create-entitlement': entId.set(s.lookupKey, (await c.post(`${P}/entitlements`, s.body)).id); break; case 'create-entitlement': entId.set(s.lookupKey, await once(s.what, `${P}/entitlements?limit=100`, x => x.lookup_key === s.lookupKey, () => c.post(`${P}/entitlements`, s.body))); break;
case 'attach-entitlement': case 'attach-entitlement':
await c.post(`${P}/entitlements/${encodeURIComponent(entId.get(s.lookupKey)!)}/actions/attach_products`, { product_ids: s.products.map(pid) }); break; await c.post(`${P}/entitlements/${encodeURIComponent(entId.get(s.lookupKey)!)}/actions/attach_products`, { product_ids: s.products.map(pid) }); break;
case 'create-offering': offering = (await c.post(`${P}/offerings`, s.body)).id; break; case 'create-offering': offering = await once(s.what, `${P}/offerings?limit=100`, x => x.lookup_key === s.lookupKey, () => c.post(`${P}/offerings`, s.body)); break;
case 'create-package': pkgId.set(s.lookupKey, (await c.post(`${P}/offerings/${encodeURIComponent(offering)}/packages`, s.body)).id); break; case 'create-package': {
const pkgs = `${P}/offerings/${encodeURIComponent(offering)}/packages`;
pkgId.set(s.lookupKey, await once(s.what, `${pkgs}?limit=100`, x => x.lookup_key === s.lookupKey, () => c.post(pkgs, s.body))); break;
}
case 'attach-package': case 'attach-package':
await c.post(`${P}/packages/${encodeURIComponent(pkgId.get(s.lookupKey)!)}/actions/attach_products`, { products: s.products.map(k => ({ product_id: pid(k), eligibility_criteria: 'all' })) }); break; await c.post(`${P}/packages/${encodeURIComponent(pkgId.get(s.lookupKey)!)}/actions/attach_products`, { products: s.products.map(k => ({ product_id: pid(k), eligibility_criteria: 'all' })) }); break;
case 'set-current': await c.post(`${P}/offerings/${encodeURIComponent(offering)}`, { is_current: true }); break; case 'set-current': await c.post(`${P}/offerings/${encodeURIComponent(offering)}`, { is_current: true }); break;

View file

@ -14,6 +14,7 @@
// Google Play's API only returns reviews created or edited in the last 7 days. // Google Play's API only returns reviews created or edited in the last 7 days.
import { existsSync, mkdirSync, readdirSync, readFileSync, writeFileSync } from 'node:fs'; import { existsSync, mkdirSync, readdirSync, readFileSync, writeFileSync } from 'node:fs';
import { join, relative, resolve } from 'node:path'; import { join, relative, resolve } from 'node:path';
import { isConflict } from './lib/upsert';
import { ascClient, type J } from './apple/asc'; import { ascClient, type J } from './apple/asc';
import type { Ctx } from './lib/ctx'; import type { Ctx } from './lib/ctx';
import { confirm } from './lib/ctx'; import { confirm } from './lib/ctx';
@ -138,8 +139,14 @@ export async function run(ctx: Ctx): Promise<void> {
for (const d of plan.send) { for (const d of plan.send) {
const req = replyRequest(d, config.play?.packageName); const req = replyRequest(d, config.play?.packageName);
try { try {
if (d.store === 'apple') await c!.asc(req.method, req.url, req.body); // Play's :reply replaces the reply (safe to send again); Apple keeps one response per review and answers a
else await play!.call(req.method, req.url, req.body); // second one with 409: then an earlier run sent it (and stopped before noting it), so it is noted now
if (d.store === 'apple') {
try { await c!.asc(req.method, req.url, req.body); } catch (e) {
if (!isConflict(e)) throw e;
warn(TAG, `${d.file.split('/').pop()}: the review already has a response on the App Store (an earlier run sent it); noted as sent, not sent again`);
}
} else await play!.call(req.method, req.url, req.body);
writeFileSync(d.file, markSent(texts.get(d.file)!, new Date().toISOString())); writeFileSync(d.file, markSent(texts.get(d.file)!, new Date().toISOString()));
sent++; sent++;
log(TAG, `sent ${d.file.split('/').pop()}`); log(TAG, `sent ${d.file.split('/').pop()}`);

231
cli/shots/capture-web.ts Normal file
View file

@ -0,0 +1,231 @@
// Web capture (shots.capture.source "web"), parallel and incremental:
// --concurrency N browser contexts in one headless Chromium (default shots.capture.concurrency, else from the CPU
// count: a WebGL game is GPU-bound, so capture defaults lower than render). Files do not depend on
// the order: every frame has its own path.
// session shots.capture.session "per-shot" (default: a new context + page and a full boot per frame) or
// "per-language": one page per language and device, booted once; the next shots switch scenes in
// that page (navigate "spa": history.pushState + popstate to the shot's path, then waitFor / eval;
// navigate "reload": page.goto in the same page). A precondition that fails there (waitFor times out
// after reuseTimeoutMs) falls back to a fresh page.
// cache .storebound/capture-cache.json: per frame a hash of its inputs (shot entry, URL + query, frame
// size, session, the copy text of its language, the app build (shots.capture.build files, else
// the HTML and scripts at baseUrl), fonts.css). Unchanged frames that exist are skipped;
// --only-missing only captures frames that do not exist; --force captures everything.
// timing per frame: goto, waitFor, eval, delay, screenshot, total, in the progress line and in
// .storebound/capture-report.json, with the slowest frames and steps at the end.
import { createHash } from 'node:crypto';
import { existsSync, mkdirSync, readdirSync, readFileSync, statSync, writeFileSync } from 'node:fs';
import { availableParallelism, cpus } from 'node:os';
import { join } from 'node:path';
import type { ShotSpec, ShotsCapture } from '../lib/config';
import { log, warn } from '../lib/log';
import { shotsFor } from './devices';
import type { FrameDims } from './lib';
type Device = 'phone' | 'tablet';
export const STEPS = ['goto', 'waitFor', 'eval', 'delay', 'screenshot'] as const;
export type Step = typeof STEPS[number];
export type Timing = Record<Step | 'total', number>;
/** frame = path relative to shots.frames (cache and report key), out = absolute file. */
export interface WebJob { lang: string; device: Device; name: string; spec: ShotSpec; url: string; path: string; out: string; frame: string; hash: string }
/** CPU cores (availableParallelism where Bun has it). */
export const coreCount = (): number => { try { return availableParallelism(); } catch { return cpus().length || 2; } };
/** Default parallel workers: render min(4, cores / 2); capture (GPU-heavy WebGL / game boots) min(3, cores / 4). */
export function defaultConcurrency(kind: 'render' | 'capture', cores = coreCount()): number {
return Math.max(1, kind === 'render' ? Math.min(4, Math.floor(cores / 2)) : Math.min(3, Math.floor(cores / 4)));
}
/** Runs fn over items with at most n at a time; results keep the input order. */
export async function pool<T, R>(items: T[], n: number, fn: (item: T, index: number) => Promise<R>): Promise<R[]> {
const out = new Array<R>(items.length);
let next = 0;
const worker = async (): Promise<void> => { while (next < items.length) { const i = next++; out[i] = await fn(items[i]!, i); } };
await Promise.all(Array.from({ length: Math.max(1, Math.min(n, items.length)) }, worker));
return out;
}
const fmtS = (ms: number): string => (ms >= 60_000 ? `${Math.floor(ms / 60_000)}m${String(Math.round((ms % 60_000) / 1000)).padStart(2, '0')}s` : `${(ms / 1000).toFixed(1)}s`);
/** "n/total, ETA" progress for parallel work: ETA = remaining items x average wall time per finished item. */
export function progressLine(done: number, total: number, startedMs: number, nowMs = Date.now()): string {
const w = String(total).length;
const eta = done ? ((nowMs - startedMs) / done) * (total - done) : 0;
return `${String(done).padStart(w)}/${total}${done < total && done ? ` ETA ${fmtS(eta)}` : ''}`;
}
export const timingText = (t: Timing): string => `goto ${fmtS(t.goto)} wait ${fmtS(t.waitFor)} eval ${fmtS(t.eval)} delay ${fmtS(t.delay)} shot ${fmtS(t.screenshot)} = ${fmtS(t.total)}`;
/** Slowest frames and the time per step over all frames (for the end of the log and the report). */
export function timingSummary(frames: { frame: string; timing: Timing }[], top = 5): { slowest: { frame: string; total: number; step: Step }[]; steps: Record<Step, number>; total: number } {
const steps = Object.fromEntries(STEPS.map(s => [s, 0])) as Record<Step, number>;
for (const f of frames) for (const s of STEPS) steps[s] += f.timing[s];
const slowest = [...frames].sort((a, b) => b.timing.total - a.timing.total).slice(0, top)
.map(f => ({ frame: f.frame, total: f.timing.total, step: STEPS.reduce((m, s) => (f.timing[s] > f.timing[m] ? s : m), STEPS[0] as Step) }));
return { slowest, steps, total: frames.reduce((t, f) => t + f.timing.total, 0) };
}
const sha = (s: string | Uint8Array): string => createHash('sha256').update(s).digest('hex').slice(0, 16);
/** Content hash of build files (a file or folder; node_modules / dot folders skipped): paths, sizes and mtimes. */
export function hashPaths(paths: string[]): string {
const h = createHash('sha256');
const walk = (p: string): void => {
if (!existsSync(p)) { h.update(`missing:${p}`); return; }
const st = statSync(p);
if (st.isDirectory()) { for (const e of readdirSync(p).sort()) if (e !== 'node_modules' && !e.startsWith('.')) walk(join(p, e)); return; }
h.update(`${p}:${st.size}:${st.mtimeMs}\n`);
};
for (const p of paths) walk(p);
return h.digest('hex').slice(0, 16);
}
/** App build fingerprint from the running server: the HTML at baseUrl and the scripts it loads (dev servers too). */
export async function hashServed(baseUrl: string): Promise<string> {
try {
const html = await (await fetch(baseUrl, { signal: AbortSignal.timeout(10_000) })).text();
const h = createHash('sha256').update(html);
for (const m of html.matchAll(/<script[^>]+src=["']([^"']+)["']/g)) {
try { h.update(await (await fetch(new URL(m[1]!, baseUrl), { signal: AbortSignal.timeout(10_000) })).text()); } catch { h.update(`unreachable:${m[1]}`); }
}
return h.digest('hex').slice(0, 16);
} catch { return 'unreachable'; }
}
/** Every web frame to capture, in a fixed order (lang, device, shot), with its URL and its input hash. */
export function planWeb(cap: ShotsCapture, o: { langs: string[]; devices: Device[]; only: string[]; dims: Record<Device, FrameDims>; framesDir: string;
build: string; copyText?: (lang: string) => string; fonts?: string }): WebJob[] {
const jobs: WebJob[] = [];
for (const lang of o.langs) for (const device of o.devices) for (const [name, spec] of shotsFor(cap, device, o.only)) {
const q = (cap.query ?? '').replaceAll('{lang}', lang);
const path = (spec.path ?? '/').replaceAll('{lang}', lang) + (q ? ((spec.path ?? '/').includes('?') ? '&' : '?') + q : '');
const url = (cap.baseUrl ?? '').replace(/\/$/, '') + path;
const hash = sha(JSON.stringify({ spec, url, dims: o.dims[device], session: cap.session ?? 'per-shot', build: o.build, copy: o.copyText?.(lang) ?? '', fonts: o.fonts ?? '' }));
const frame = `${lang}/${device}/${name}.png`;
jobs.push({ lang, device, name, spec, url, path, out: join(o.framesDir, frame), frame, hash });
}
return jobs;
}
export type Cache = Record<string, string>;
export const cachePath = (root: string): string => join(root, '.storebound', 'capture-cache.json');
export function readCache(root: string): Cache { try { return JSON.parse(readFileSync(cachePath(root), 'utf8')) as Cache; } catch { return {}; } }
/** Which jobs to run: --force all; --only-missing the ones whose file is missing; else missing or changed inputs. */
export function selectJobs(jobs: WebJob[], cache: Cache, mode: 'force' | 'only-missing' | 'changed', exists: (file: string) => boolean = existsSync): { run: WebJob[]; skipped: WebJob[] } {
const run: WebJob[] = [], skipped: WebJob[] = [];
for (const j of jobs) {
const keep = mode === 'force' ? false : mode === 'only-missing' ? exists(j.out) : exists(j.out) && cache[j.frame] === j.hash;
(keep ? skipped : run).push(j);
}
return { run, skipped };
}
export interface CaptureReport {
at: string; concurrency: number; session: string; wallMs: number;
frames: { frame: string; ok: boolean; reused: boolean; timing: Timing; error?: string }[];
skipped: string[];
summary: ReturnType<typeof timingSummary>;
}
/* eslint-disable @typescript-eslint/no-explicit-any */
/** Captures the jobs in a browser: per-shot contexts, or one page per language + device in per-language sessions. */
export async function captureWeb(browser: any, cap: ShotsCapture, jobs: WebJob[], o: { concurrency: number; dims: Record<Device, FrameDims> }): Promise<CaptureReport> {
const started = Date.now();
const perLang = cap.session === 'per-language';
const report: CaptureReport['frames'] = [];
let finished = 0;
const total = jobs.length;
const newPage = async (j: WebJob): Promise<{ c: any; page: any; errs: string[] }> => {
const d = o.dims[j.device];
const c = await browser.newContext({ viewport: { width: d.w, height: d.h }, deviceScaleFactor: d.dpr, hasTouch: true, isMobile: true, serviceWorkers: 'block', locale: j.lang });
const page = await c.newPage();
const errs: string[] = [];
page.on('pageerror', (e: Error) => errs.push(e.message));
return { c, page, errs };
};
const shoot = async (s: { page: any; errs: string[] }, j: WebJob, how: 'goto' | 'spa', waitTimeout: number): Promise<Timing> => {
const t: Timing = { goto: 0, waitFor: 0, eval: 0, delay: 0, screenshot: 0, total: 0 };
let t0 = Date.now();
if (how === 'goto') await s.page.goto(j.url, { waitUntil: 'domcontentloaded', timeout: 90000 });
else await s.page.evaluate(`(p => { if (location.pathname + location.search !== p) { history.pushState(null, '', p); dispatchEvent(new PopStateEvent('popstate', { state: null })); } })(${JSON.stringify(j.path)})`);
t.goto = Date.now() - t0; t0 = Date.now();
if (j.spec.waitFor) await s.page.waitForFunction(j.spec.waitFor, null, { timeout: waitTimeout, polling: 50 });
t.waitFor = Date.now() - t0; t0 = Date.now();
for (const js of j.spec.eval ?? []) await s.page.evaluate(js);
t.eval = Date.now() - t0; t0 = Date.now();
await s.page.waitForTimeout(j.spec.delayMs ?? 1000);
t.delay = Date.now() - t0; t0 = Date.now();
mkdirSync(join(j.out, '..'), { recursive: true });
await s.page.screenshot({ path: j.out });
t.screenshot = Date.now() - t0;
t.total = t.goto + t.waitFor + t.eval + t.delay + t.screenshot;
return t;
};
const record = (j: WebJob, r: { ok: boolean; reused: boolean; timing: Timing; error?: string; errs?: string[] }): void => {
finished++;
report.push({ frame: j.frame, ok: r.ok, reused: r.reused, timing: r.timing, ...(r.error ? { error: r.error } : {}) });
const head = `${progressLine(finished, total, started)} ${j.lang}/${j.device}/${j.name}`;
if (r.ok) log('shots', `${head}.png ${r.reused ? '(reused page) ' : ''}${timingText(r.timing)}${r.errs?.length ? ' (page errors: ' + r.errs.join(' | ').slice(0, 200) + ')' : ''}`);
else warn('shots', `${head}: ${r.error}`);
};
const zero = (): Timing => ({ goto: 0, waitFor: 0, eval: 0, delay: 0, screenshot: 0, total: 0 });
if (!perLang) {
await pool(jobs, o.concurrency, async j => {
const s = await newPage(j);
try { const timing = await shoot(s, j, 'goto', 120000); record(j, { ok: true, reused: false, timing, errs: s.errs }); }
catch (e) { record(j, { ok: false, reused: false, timing: zero(), error: (e as Error).message.split('\n')[0]!.slice(0, 300) }); }
await s.c.close();
});
} else {
// one group per language + device, its shots in order on one booted page
const groups = new Map<string, WebJob[]>();
for (const j of jobs) { const k = `${j.lang}/${j.device}`; groups.set(k, [...(groups.get(k) ?? []), j]); }
await pool([...groups.values()], o.concurrency, async group => {
let s: Awaited<ReturnType<typeof newPage>> | null = null;
for (const j of group) {
const nav = j.spec.navigate ?? 'spa';
if (s) {
try {
const timing = await shoot(s, j, nav === 'reload' ? 'goto' : 'spa', cap.reuseTimeoutMs ?? 15000);
record(j, { ok: true, reused: true, timing, errs: s.errs });
continue;
} catch (e) {
warn('shots', `${j.lang}/${j.device}/${j.name}: reused page failed (${(e as Error).message.split('\n')[0]!.slice(0, 120)}), fresh page`);
await s.c.close(); s = null;
}
}
s = await newPage(j);
try { const timing = await shoot(s, j, 'goto', 120000); record(j, { ok: true, reused: false, timing, errs: s.errs }); }
catch (e) { record(j, { ok: false, reused: false, timing: zero(), error: (e as Error).message.split('\n')[0]!.slice(0, 300) }); await s.c.close(); s = null; }
}
if (s) await s.c.close();
});
}
const order = new Map(jobs.map((j, i) => [j.frame, i] as const));
report.sort((a, b) => order.get(a.frame)! - order.get(b.frame)!);
const ok = report.filter(f => f.ok);
return { at: new Date().toISOString(), concurrency: o.concurrency, session: cap.session ?? 'per-shot', wallMs: Date.now() - started, frames: report, skipped: [],
summary: timingSummary(ok.map(f => ({ frame: f.frame, timing: f.timing }))) };
}
/* eslint-enable @typescript-eslint/no-explicit-any */
/** Log lines for the end of a capture: wall time, sum of frame time, slowest frames and steps. */
export function summaryLines(r: CaptureReport): string[] {
const s = r.summary;
const lines = [`${r.frames.filter(f => f.ok).length} frames in ${fmtS(r.wallMs)} wall (${fmtS(s.total)} of frame time, ${r.concurrency} at a time, session ${r.session})`];
if (s.total) lines.push('time per step: ' + STEPS.map(st => `${st} ${fmtS(s.steps[st])} (${Math.round((100 * s.steps[st]) / s.total)} %)`).join(', '));
for (const f of s.slowest) lines.push(`slowest: ${f.frame} ${fmtS(f.total)} (mostly ${f.step})`);
return lines;
}
export function writeCaptureState(root: string, report: CaptureReport, cache: Cache, captured: WebJob[]): void {
mkdirSync(join(root, '.storebound'), { recursive: true });
const okFrames = new Set(report.frames.filter(f => f.ok).map(f => f.frame));
for (const j of captured) if (okFrames.has(j.frame)) cache[j.frame] = j.hash;
writeFileSync(cachePath(root), JSON.stringify(Object.fromEntries(Object.entries(cache).sort()), null, 1) + '\n');
writeFileSync(join(root, '.storebound', 'capture-report.json'), JSON.stringify(report, null, 1) + '\n');
}

View file

@ -1,11 +1,12 @@
// Raw frames from a running build of your app or game, from any engine. // Raw frames from a running build of your app or game, from any engine.
// storebound shots capture [--lang en,tr] [--only hero,shop] [--device phone|tablet] [--source web|ios-sim|android-emu|folder] // storebound shots capture [--lang en,tr] [--only hero,shop] [--device phone|tablet] [--source web|ios-sim|android-emu|folder]
// [--dry] [--keep-booted] // [--dry] [--keep-booted] [--concurrency N] [--only-missing | --force]
// Frames go to <shots.frames>/<lang>/<device>/<name>.png: the screen BELOW the status bar (the template draws one). // Frames go to <shots.frames>/<lang>/<device>/<name>.png: the screen BELOW the status bar (the template draws one).
// shots.capture.source (default web): // shots.capture.source (default web):
// web headless Chromium (playwright), mobile viewport + touch at phone / tablet size. Each shot: // web headless Chromium (playwright), mobile viewport + touch at phone / tablet size. Each shot:
// { path, waitFor?: JS expression that turns true, eval?: [JS], delayMs?, device? }; URL = baseUrl + path + // { path, waitFor?: JS expression that turns true, eval?: [JS], delayMs?, device? }; URL = baseUrl + path +
// query, "{lang}" replaced. // query, "{lang}" replaced. Parallel contexts, per-language page reuse, the input-hash cache and the
// per-frame timing (goto / waitFor / eval / delay / screenshot): capture-web.ts.
// ios-sim xcrun simctl: boots the simulator (ios.device / ios.devices.tablet), optional install of ios.app, status // ios-sim xcrun simctl: boots the simulator (ios.device / ios.devices.tablet), optional install of ios.app, status
// bar 9:41 / full battery / full bars, one warm-up launch after a cold boot (ios.warmupMs), then per shot: // bar 9:41 / full battery / full bars, one warm-up launch after a cold boot (ios.warmupMs), then per shot:
// launch ios.bundleId with -AppleLanguages (xx) -AppleLocale + ios.args + shot.args, open shot.url // launch ios.bundleId with -AppleLanguages (xx) -AppleLocale + ios.args + shot.args, open shot.url
@ -33,7 +34,8 @@ import { checkFrameFolder } from './check';
import { adbAppLocaleArgs, adbDemoExitArgs, adbDemoModeArgs, adbForceStopArgs, adbInstallArgs, adbScreencapArgs, adbStartArgs, adbStateArgs, adbTarget, import { adbAppLocaleArgs, adbDemoExitArgs, adbDemoModeArgs, adbForceStopArgs, adbInstallArgs, adbScreencapArgs, adbStartArgs, adbStateArgs, adbTarget,
cropArgs, cropTopFor, fill, pickSimulator, shotsFor, simBootArgs, simBootStatusArgs, simInstallArgs, simLaunchArgs, simOpenUrlArgs, cropArgs, cropTopFor, fill, pickSimulator, shotsFor, simBootArgs, simBootStatusArgs, simInstallArgs, simLaunchArgs, simOpenUrlArgs,
simScreenshotArgs, simShutdownArgs, simStatusBarArgs, simStatusBarClearArgs, simTarget, SOURCES, type Source, TOOL_HINTS, validateCapture } from './devices'; simScreenshotArgs, simShutdownArgs, simStatusBarArgs, simStatusBarClearArgs, simTarget, SOURCES, type Source, TOOL_HINTS, validateCapture } from './devices';
import { type FrameDims, frameDims } from './lib'; import { type CopyFile, copyText, type FrameDims, frameDims } from './lib';
import { captureWeb, defaultConcurrency, hashPaths, hashServed, planWeb, readCache, selectJobs, summaryLines, writeCaptureState } from './capture-web';
import { imageInfo } from './spec'; import { imageInfo } from './spec';
type Device = 'phone' | 'tablet'; type Device = 'phone' | 'tablet';
@ -120,29 +122,29 @@ export async function run(ctx: Ctx): Promise<void> {
for (const lang of langs) for (const device of devices) for (const [name, s] of shotsFor(cap, device, only)) progress(`${lang}/${device}/${name}.png <- ${cap.baseUrl}${(s.path ?? '/').replaceAll('{lang}', lang)}`); for (const lang of langs) for (const device of devices) for (const [name, s] of shotsFor(cap, device, only)) progress(`${lang}/${device}/${name}.png <- ${cap.baseUrl}${(s.path ?? '/').replaceAll('{lang}', lang)}`);
return done('shots', `capture planned (dry run): ${total} frames from web`); return done('shots', `capture planned (dry run): ${total} frames from web`);
} }
const browser = await launch(root, { headless: true, args: ['--mute-audio', '--ignore-gpu-blocklist', '--enable-webgl', '--enable-gpu'] }); need(cap.baseUrl, 'shots.capture.baseUrl');
try { const concurrency = a.num('concurrency', cap.concurrency ?? defaultConcurrency('capture'));
for (const lang of langs) for (const device of devices) for (const [name, s] of shotsFor(cap, device, only)) { const builds = cap.build ? (Array.isArray(cap.build) ? cap.build : [cap.build]).map(p => resolve(p)) : [];
const d = dims[device]; const build = builds.length ? hashPaths(builds) : await hashServed(cap.baseUrl!);
const q = (cap.query ?? '').replaceAll('{lang}', lang); let copy: CopyFile = {};
const path = (s.path ?? '/').replaceAll('{lang}', lang); try { copy = JSON.parse(readFileSync(resolve(shots.copy), 'utf8')) as CopyFile; } catch { /* no copy yet */ }
const url = need(cap.baseUrl, 'shots.capture.baseUrl').replace(/\/$/, '') + path + (q ? (path.includes('?') ? '&' : '?') + q : ''); const fontsCss = join(root, '.storebound', 'fonts', 'fonts.css');
const c = await browser.newContext({ viewport: { width: d.w, height: d.h }, deviceScaleFactor: d.dpr, hasTouch: true, isMobile: true, serviceWorkers: 'block', locale: lang }); const jobs = planWeb(cap, { langs, devices, only, dims, framesDir, build, copyText: l => copyText(l, copy[l]),
const page = await c.newPage(); fonts: existsSync(fontsCss) ? readFileSync(fontsCss, 'utf8') : '' });
const errs: string[] = []; const cache = readCache(root);
page.on('pageerror', (e: Error) => errs.push(e.message)); const { run: todo, skipped } = selectJobs(jobs, cache, a.has('force') ? 'force' : a.has('only-missing') ? 'only-missing' : 'changed');
try { if (skipped.length) log('shots', `${skipped.length} of ${jobs.length} frames unchanged, skipped (${a.has('only-missing') ? '--only-missing' : 'same inputs as last time'}; --force captures them again)`);
await page.goto(url, { waitUntil: 'domcontentloaded', timeout: 90000 }); log('shots', `capturing ${todo.length} frames, ${Math.min(concurrency, todo.length) || 0} at a time${cap.session === 'per-language' ? ', one page per language' : ''}`);
if (s.waitFor) await page.waitForFunction(s.waitFor, null, { timeout: 120000, polling: 50 }); if (todo.length) {
for (const js of s.eval ?? []) await page.evaluate(js); const browser = await launch(root, { headless: true, args: ['--mute-audio', '--ignore-gpu-blocklist', '--enable-webgl', '--enable-gpu'] });
await page.waitForTimeout(s.delayMs ?? 1000); let report;
await page.screenshot({ path: target(lang, device, name) }); try { report = await captureWeb(browser, cap, todo, { concurrency, dims }); } finally { await browser.close(); }
n++; report.skipped = skipped.map(j => j.frame);
progress(`${lang}/${device}/${name}.png${errs.length ? ' (page errors: ' + errs.join(' | ').slice(0, 200) + ')' : ''}`); writeCaptureState(root, report, cache, todo);
} catch (e) { failed++; warn('shots', `${lang}/${device}/${name}: ${(e as Error).message.slice(0, 300)}`); } for (const l of summaryLines(report)) log('shots', l);
await c.close(); n += report.frames.filter(f => f.ok).length;
} failed += report.frames.filter(f => !f.ok).length;
} finally { await browser.close(); } }
} }
if (source === 'ios-sim') { if (source === 'ios-sim') {

View file

@ -6,7 +6,8 @@
// Per set: Play 2-8 phone screenshots, Apple 1-10 per display type, 13" iPad set when apple.ipad, feature graphic. // Per set: Play 2-8 phone screenshots, Apple 1-10 per display type, 13" iPad set when apple.ipad, feature graphic.
// Per app preview: 886x1920 / 1200x1600, H.264, <= 30 fps, 15-30 s, stereo AAC track, <= 500 MB, <= 3 per set. // Per app preview: 886x1920 / 1200x1600, H.264, <= 30 fps, 15-30 s, stereo AAC track, <= 500 MB, <= 3 per set.
// Text overflow flags come from the last `shots render` (report.json). Frames: with capture.source "folder" every frame // Text overflow flags come from the last `shots render` (report.json). Frames: with capture.source "folder" every frame
// the scenes use must exist with the right aspect. Exit 1 on any error (warnings do not fail). // the scenes use must exist with the right aspect. Local font files (shots.fonts paths) without a license note: warn.
// Exit 1 on any error (warnings do not fail).
import { existsSync, readdirSync, readFileSync } from 'node:fs'; import { existsSync, readdirSync, readFileSync } from 'node:fs';
import { join } from 'node:path'; import { join } from 'node:path';
import type { ShipConfig } from '../lib/config'; import type { ShipConfig } from '../lib/config';
@ -17,6 +18,8 @@ import { defaultSizes, frameDims, SIZES } from './lib';
import type { LocalIssue, RendersCheck } from '../lib/validate-rules'; import type { LocalIssue, RendersCheck } from '../lib/validate-rules';
import { allIssues, requiredSets, type RendersScan, scanRenders, storeOf } from './report'; import { allIssues, requiredSets, type RendersScan, scanRenders, storeOf } from './report';
import { imageInfo } from './spec'; import { imageInfo } from './spec';
import { fontsDirOf, readManifest } from './fonts';
import { localFontIssues } from './local-fonts';
/** Frame files the scenes (and the feature graphic) use, per language and device class. */ /** Frame files the scenes (and the feature graphic) use, per language and device class. */
export function framesNeeded(config: ShipConfig, langs: string[]): { lang: string; device: 'phone' | 'tablet'; name: string }[] { export function framesNeeded(config: ShipConfig, langs: string[]): { lang: string; device: 'phone' | 'tablet'; name: string }[] {
@ -58,9 +61,9 @@ export function rendersCheck(config: ShipConfig, dir: string): Record<'apple' |
} }
export async function run(ctx: Ctx): Promise<void> { export async function run(ctx: Ctx): Promise<void> {
const { config, resolve } = ctx.loaded; const { config, resolve, root } = ctx.loaded;
const scan = scanProject(ctx); const scan = scanProject(ctx);
const issues = allIssues(scan); const issues = [...allIssues(scan), ...localFontIssues(readManifest(fontsDirOf(root))).map(issue => ({ where: '.storebound/fonts/fonts.json', issue }))];
const cap = config.shots?.capture; const cap = config.shots?.capture;
const frameIssues = cap?.source === 'folder' ? checkFrameFolder(config, resolve(cap.folder ?? config.shots!.frames), scan.langs) : []; const frameIssues = cap?.source === 'folder' ? checkFrameFolder(config, resolve(cap.folder ?? config.shots!.frames), scan.langs) : [];
if (ctx.args.has('json')) { if (ctx.args.has('json')) {

View file

@ -8,20 +8,25 @@ const tag = (d: DataView, o: number): string => String.fromCharCode(d.getUint8(o
const view = (b: Uint8Array): DataView => new DataView(b.buffer, b.byteOffset, b.byteLength); const view = (b: Uint8Array): DataView => new DataView(b.buffer, b.byteOffset, b.byteLength);
/** The raw cmap table of a font file (any of the formats above). Throws on anything else. */ /** The raw cmap table of a font file (any of the formats above). Throws on anything else. */
export function cmapTable(file: Uint8Array): Uint8Array { export const cmapTable = (file: Uint8Array): Uint8Array => fontTable(file, 'cmap');
/** One raw (untransformed) table of a font file, e.g. 'cmap' or 'name'. Throws when the file is not a font or the
* table is missing. */
export function fontTable(file: Uint8Array, want: string): Uint8Array {
const d = view(file); const d = view(file);
if (file.byteLength < 12) throw new Error('not a font file (too short)');
const sig = tag(d, 0); const sig = tag(d, 0);
if (sig === 'wOF2') return woff2Cmap(file); if (sig === 'wOF2') return woff2Table(file, want);
if (sig === 'wOFF') { if (sig === 'wOFF') {
const n = d.getUint16(12); const n = d.getUint16(12);
for (let i = 0; i < n; i++) { for (let i = 0; i < n; i++) {
const o = 44 + i * 20; const o = 44 + i * 20;
if (tag(d, o) !== 'cmap') continue; if (tag(d, o) !== want) continue;
const off = d.getUint32(o + 4), comp = d.getUint32(o + 8), orig = d.getUint32(o + 12); const off = d.getUint32(o + 4), comp = d.getUint32(o + 8), orig = d.getUint32(o + 12);
const raw = file.subarray(off, off + comp); const raw = file.subarray(off, off + comp);
return comp < orig ? new Uint8Array(inflateSync(raw)) : raw; return comp < orig ? new Uint8Array(inflateSync(raw)) : raw;
} }
throw new Error('woff: no cmap table'); throw new Error(`woff: no ${want} table`);
} }
let base = 0; let base = 0;
if (sig === 'ttcf') base = d.getUint32(12); if (sig === 'ttcf') base = d.getUint32(12);
@ -29,9 +34,9 @@ export function cmapTable(file: Uint8Array): Uint8Array {
const n = d.getUint16(base + 4); const n = d.getUint16(base + 4);
for (let i = 0; i < n; i++) { for (let i = 0; i < n; i++) {
const o = base + 12 + i * 16; const o = base + 12 + i * 16;
if (tag(d, o) === 'cmap') { const off = d.getUint32(o + 8); return file.subarray(off, off + d.getUint32(o + 12)); } if (tag(d, o) === want) { const off = d.getUint32(o + 8); return file.subarray(off, off + d.getUint32(o + 12)); }
} }
throw new Error('sfnt: no cmap table'); throw new Error(`sfnt: no ${want} table`);
} }
const WOFF2_TAGS = ['cmap', 'head', 'hhea', 'hmtx', 'maxp', 'name', 'OS/2', 'post', 'cvt ', 'fpgm', 'glyf', 'loca', 'prep', 'CFF ', 'VORG', const WOFF2_TAGS = ['cmap', 'head', 'hhea', 'hmtx', 'maxp', 'name', 'OS/2', 'post', 'cvt ', 'fpgm', 'glyf', 'loca', 'prep', 'CFF ', 'VORG',
@ -39,7 +44,7 @@ const WOFF2_TAGS = ['cmap', 'head', 'hhea', 'hmtx', 'maxp', 'name', 'OS/2', 'pos
'CBDT', 'CBLC', 'COLR', 'CPAL', 'SVG ', 'sbix', 'acnt', 'avar', 'bdat', 'bloc', 'bsln', 'cvar', 'fdsc', 'feat', 'fmtx', 'fvar', 'gvar', 'CBDT', 'CBLC', 'COLR', 'CPAL', 'SVG ', 'sbix', 'acnt', 'avar', 'bdat', 'bloc', 'bsln', 'cvar', 'fdsc', 'feat', 'fmtx', 'fvar', 'gvar',
'hsty', 'just', 'lcar', 'mort', 'morx', 'opbd', 'prop', 'trak', 'Zapf', 'Silf', 'Glat', 'Gloc', 'Feat', 'Sill']; 'hsty', 'just', 'lcar', 'mort', 'morx', 'opbd', 'prop', 'trak', 'Zapf', 'Silf', 'Glat', 'Gloc', 'Feat', 'Sill'];
function woff2Cmap(file: Uint8Array): Uint8Array { function woff2Table(file: Uint8Array, want: string): Uint8Array {
const d = view(file); const d = view(file);
const n = d.getUint16(12), compLen = d.getUint32(20); const n = d.getUint16(12), compLen = d.getUint32(20);
let p = 48; let p = 48;
@ -56,10 +61,10 @@ function woff2Cmap(file: Uint8Array): Uint8Array {
const version = (flags >> 6) & 3; const version = (flags >> 6) & 3;
const transformed = t === 'glyf' || t === 'loca' ? version === 0 : version !== 0; const transformed = t === 'glyf' || t === 'loca' ? version === 0 : version !== 0;
const len = transformed ? base128() : orig; const len = transformed ? base128() : orig;
if (t === 'cmap') found = [off, orig]; if (t === want) found = [off, orig];
off += len; off += len;
} }
if (!found) throw new Error('woff2: no cmap table'); if (!found) throw new Error(`woff2: no ${want} table`);
if (tag(d, 4) === 'ttcf') { // collection directory: skip it (version, numFonts 255UInt16, per font numTables + indices) if (tag(d, 4) === 'ttcf') { // collection directory: skip it (version, numFonts 255UInt16, per font numTables + indices)
p += 4; p += 4;
const u16 = (): number => { const c = d.getUint8(p++); return c === 253 ? (p += 2, d.getUint16(p - 2)) : c === 255 ? 253 + d.getUint8(p++) : c === 254 ? 506 + d.getUint8(p++) : c; }; const u16 = (): number => { const c = d.getUint8(p++); return c === 253 ? (p += 2, d.getUint16(p - 2)) : c === 255 ? 253 + d.getUint8(p++) : c === 254 ? 506 + d.getUint8(p++) : c; };
@ -110,6 +115,28 @@ function readSub(d: DataView, o: number, out: Set<number>): void {
} }
} }
/** Family name from the name table (typographic family 16, else family 1; Windows Unicode, else Mac Roman), or null. */
export function fontFamilyName(file: Uint8Array): string | null {
let t: Uint8Array;
try { t = fontTable(file, 'name'); } catch { return null; }
const d = view(t);
const count = d.getUint16(2), strings = d.getUint16(4);
const recs: { pid: number; lang: number; id: number; text: string }[] = [];
for (let i = 0; i < count && 6 + i * 12 + 12 <= t.byteLength; i++) {
const o = 6 + i * 12;
const pid = d.getUint16(o), lang = d.getUint16(o + 4), id = d.getUint16(o + 6), len = d.getUint16(o + 8), off = strings + d.getUint16(o + 10);
if ((id !== 1 && id !== 16) || off + len > t.byteLength) continue;
const raw = t.subarray(off, off + len);
let text = '';
if (pid === 3 || pid === 0) for (let k = 0; k + 1 < raw.length; k += 2) text += String.fromCharCode((raw[k]! << 8) | raw[k + 1]!);
else if (pid === 1) text = String.fromCharCode(...raw);
else continue;
recs.push({ pid, lang, id, text: text.trim() });
}
const score = (r: { pid: number; lang: number; id: number }): number => (r.id === 16 ? 10 : 0) + (r.pid === 3 ? 4 : r.pid === 0 ? 3 : 1) + (r.lang === 0x409 || r.lang === 0 ? 1 : 0);
return recs.filter(r => r.text).sort((a, b) => score(b) - score(a))[0]?.text ?? null;
}
/** Code points a font file covers. */ /** Code points a font file covers. */
export const fontCodepoints = (file: Uint8Array): Set<number> => cmapCodepoints(cmapTable(file)); export const fontCodepoints = (file: Uint8Array): Set<number> => cmapCodepoints(cmapTable(file));

View file

@ -28,10 +28,14 @@ export function validateCapture(c: ShotsCapture | undefined): string[] {
if (!c.ios?.device && !c.ios?.devices?.phone) e.push('shots.capture.ios.device: simulator name or UDID, e.g. "iPhone 17 Pro Max"'); if (!c.ios?.device && !c.ios?.devices?.phone) e.push('shots.capture.ios.device: simulator name or UDID, e.g. "iPhone 17 Pro Max"');
} }
if (c.ios?.warmupMs !== undefined && !(Number.isInteger(c.ios.warmupMs) && c.ios.warmupMs >= 0)) e.push('shots.capture.ios.warmupMs: milliseconds (integer >= 0)'); if (c.ios?.warmupMs !== undefined && !(Number.isInteger(c.ios.warmupMs) && c.ios.warmupMs >= 0)) e.push('shots.capture.ios.warmupMs: milliseconds (integer >= 0)');
if (c.concurrency !== undefined && !(Number.isInteger(c.concurrency) && c.concurrency >= 1 && c.concurrency <= 32)) e.push('shots.capture.concurrency: integer 1-32');
if (c.session !== undefined && !['per-shot', 'per-language'].includes(c.session)) e.push('shots.capture.session: per-shot | per-language');
if (c.reuseTimeoutMs !== undefined && !(Number.isInteger(c.reuseTimeoutMs) && c.reuseTimeoutMs > 0)) e.push('shots.capture.reuseTimeoutMs: milliseconds (integer > 0)');
if (src === 'android-emu' && !c.android?.package) e.push('shots.capture.android.package: application id (source android-emu)'); if (src === 'android-emu' && !c.android?.package) e.push('shots.capture.android.package: application id (source android-emu)');
if (src !== 'folder' && (!c.shots || !Object.keys(c.shots).length)) e.push('shots.capture.shots: at least one shot'); if (src !== 'folder' && (!c.shots || !Object.keys(c.shots).length)) e.push('shots.capture.shots: at least one shot');
for (const [name, s] of Object.entries(c.shots ?? {})) { for (const [name, s] of Object.entries(c.shots ?? {})) {
if (!/^[A-Za-z0-9._-]+$/.test(name)) e.push(`shots.capture.shots.${name}: name must be a file name (letters, digits, . _ -)`); if (!/^[A-Za-z0-9._-]+$/.test(name)) e.push(`shots.capture.shots.${name}: name must be a file name (letters, digits, . _ -)`);
if (s.navigate && !['spa', 'reload'].includes(s.navigate)) e.push(`shots.capture.shots.${name}.navigate: spa | reload`);
if (s.device && !['phone', 'tablet', 'both'].includes(s.device)) e.push(`shots.capture.shots.${name}.device: phone | tablet | both`); if (s.device && !['phone', 'tablet', 'both'].includes(s.device)) e.push(`shots.capture.shots.${name}.device: phone | tablet | both`);
if (src === 'web' && s.path === undefined) e.push(`shots.capture.shots.${name}.path: URL path (source web)`); if (src === 'web' && s.path === undefined) e.push(`shots.capture.shots.${name}.path: URL path (source web)`);
if ((src === 'web' || src === 'folder') && (s.args || s.extras)) e.push(`shots.capture.shots.${name}: args / extras only apply to ios-sim / android-emu`); if ((src === 'web' || src === 'folder') && (s.args || s.extras)) e.push(`shots.capture.shots.${name}: args / extras only apply to ios-sim / android-emu`);

View file

@ -8,14 +8,16 @@
// character no fallback covers fails with the language and the characters. // character no fallback covers fails with the language and the characters.
// storebound shots fonts [--json] (re-run after editing the copy or the fonts) // storebound shots fonts [--json] (re-run after editing the copy or the fonts)
// Out: <project>/.storebound/fonts/*.woff2 + fonts.css + fonts.json (faces and the per-language stacks, read by render). // Out: <project>/.storebound/fonts/*.woff2 + fonts.css + fonts.json (faces and the per-language stacks, read by render).
// All of these fonts are SIL Open Font License 1.1. // The Google fonts are SIL Open Font License 1.1. A local font file in shots.fonts (local-fonts.ts) gets the same cmap
// check, is read in place (never copied) and recorded with its path, sha256 and license note.
import { mkdirSync, readFileSync, writeFileSync } from 'node:fs'; import { mkdirSync, readFileSync, writeFileSync } from 'node:fs';
import { join } from 'node:path'; import { join } from 'node:path';
import type { Ctx } from '../lib/ctx'; import type { Ctx } from '../lib/ctx';
import { need } from '../lib/ctx'; import { need } from '../lib/ctx';
import { done, log, printJson, warn } from '../lib/log'; import { done, log, printJson, warn } from '../lib/log';
import { shotsStyle } from '../lib/config'; import { shotsStyle } from '../lib/config';
import { type CopyFile, shotFonts } from './lib'; import type { CopyFile } from './lib';
import { localByFamily, resolveShotFonts } from './local-fonts';
import { fontCodepoints } from './cmap'; import { fontCodepoints } from './cmap';
import { type FaceJob, type FontManifest, fontsCss, planFonts, roleTexts, type RoleTexts } from './glyphs'; import { type FaceJob, type FontManifest, fontsCss, planFonts, roleTexts, type RoleTexts } from './glyphs';
@ -54,14 +56,24 @@ export async function run(ctx: Ctx): Promise<void> {
const dir = fontsDirOf(root); const dir = fontsDirOf(root);
mkdirSync(dir, { recursive: true }); mkdirSync(dir, { recursive: true });
const style = shotsStyle(config); const style = shotsStyle(config);
const { title, body, titleVariable } = shotFonts(shots, style); const sf = resolveShotFonts(shots, style, resolve, root);
const { titleVariable } = sf;
const title = sf.title.family, body = sf.body.family;
const local = localByFamily([sf.title, sf.body, ...sf.fallback]);
const texts = langTexts(copy, config.languages.map(l => l.id), config.app.name); const texts = langTexts(copy, config.languages.map(l => l.id), config.app.name);
const sizes: string[] = []; const sizes: string[] = [];
const manifest = await planFonts({ const manifest = await planFonts({
langs: texts, title, body, titleAxis: titleVariable ? 'wght@100..900' : null, bodyAxis: 'wght@100..900', langs: texts, title, body, titleAxis: titleVariable ? 'wght@100..900' : null, bodyAxis: 'wght@100..900',
fallbackTitleAxis: style === 'app' ? 'wght@100..900' : 'wght@800', chosen: shots.fonts?.fallback, base: ASCII, fallbackTitleAxis: style === 'app' ? 'wght@100..900' : 'wght@800', chosen: sf.fallback.map(f => f.family), base: ASCII, local,
}, async (job: FaceJob) => { }, async (job: FaceJob) => {
if (job.local) { // read in place: the whole file's cmap
const bin = new Uint8Array(readFileSync(join(root, job.local.path)));
const cps = fontCodepoints(bin);
const line = `${job.local.path} ${job.family}, ${(bin.byteLength / 1024).toFixed(1)} KB, ${cps.size} in the cmap, sha256 ${job.local.hash}, license: ${job.local.license ?? 'none noted (add license to the shots.fonts entry)'}`;
if (!sizes.includes(line)) sizes.push(line);
return cps;
}
let bin: Uint8Array; let bin: Uint8Array;
try { bin = new Uint8Array(await fetchSubset(job.family, job.chars, job.axis)); } catch (e) { try { bin = new Uint8Array(await fetchSubset(job.family, job.chars, job.axis)); } catch (e) {
if (job.kind !== 'fallback') throw e; if (job.kind !== 'fallback') throw e;
@ -77,12 +89,13 @@ export async function run(ctx: Ctx): Promise<void> {
writeFileSync(join(dir, 'fonts.json'), JSON.stringify(manifest, null, 1) + '\n'); writeFileSync(join(dir, 'fonts.json'), JSON.stringify(manifest, null, 1) + '\n');
if (ctx.args.has('json')) return printJson(manifest); if (ctx.args.has('json')) return printJson(manifest);
for (const s of sizes) log('shots', s); for (const s of sizes) log('shots', s);
for (const f of Object.values(local)) if (!f.license) warn('shots', `local font ${f.path} has no license note: add license ('OFL-1.1', 'proprietary', ...) to its shots.fonts entry`);
let auto = 0; let auto = 0;
for (const [lang, l] of Object.entries(manifest.langs)) for (const f of l.fallbacks) { for (const [lang, l] of Object.entries(manifest.langs)) for (const f of l.fallbacks) {
const ok = manifest.faces[f.face]?.chosen; const ok = manifest.faces[f.face]?.chosen;
if (!ok) auto++; if (!ok) auto++;
const base = f.role === 'title' ? title : body; const base = f.role === 'title' ? title : body;
(ok ? log : warn)('shots', `${lang} ${f.role}: ${[...f.chars].join(' ')} not in ${base} -> ${f.family}${ok ? ' (shots.fonts.fallback)' : ' (automatic fallback)'}`); (ok ? log : warn)('shots', `${lang} ${f.role}: ${[...f.chars].join(' ')} not in ${base} -> ${f.family}${ok ? ' (shots.fonts.fallback)' : ' (automatic fallback)'}${manifest.faces[f.face]?.local ? ' (local file)' : ''}`);
} }
if (auto) warn('shots', `${auto} automatic fallbacks: renders that use them fail \`shots check\`. Pick title / body fonts that cover them${style === 'game' ? ' (titles: e.g. Paytone One, Baloo 2, Rowdies)' : ''} or accept the fallback with shots.fonts.fallback: ['<family>']`); if (auto) warn('shots', `${auto} automatic fallbacks: renders that use them fail \`shots check\`. Pick title / body fonts that cover them${style === 'game' ? ' (titles: e.g. Paytone One, Baloo 2, Rowdies)' : ''} or accept the fallback with shots.fonts.fallback: ['<family>']`);
done('shots', 'fonts done'); done('shots', 'fonts done');

View file

@ -16,6 +16,7 @@
import type { SceneCopy } from './lib'; import type { SceneCopy } from './lib';
import { scriptFont } from './lib'; import { scriptFont } from './lib';
import { needsGlyph } from './cmap'; import { needsGlyph } from './cmap';
import { type LocalFontInfo, localFile, slug } from './local-fonts';
export type Role = 'title' | 'body'; export type Role = 'title' | 'body';
export interface RoleTexts { title: string; body: string } export interface RoleTexts { title: string; body: string }
@ -61,16 +62,16 @@ export function fallbackFamilies(ch: string, lang: string, role: Role): string[]
export const FALLBACK_ORDER = ['Noto Sans Display', 'Noto Sans', 'Noto Sans KR', 'Noto Sans JP', 'Noto Sans SC', 'Noto Sans TC', export const FALLBACK_ORDER = ['Noto Sans Display', 'Noto Sans', 'Noto Sans KR', 'Noto Sans JP', 'Noto Sans SC', 'Noto Sans TC',
...NOTO_SCRIPTS.map(s => `Noto Sans ${s}`), ...SYMBOLS]; ...NOTO_SCRIPTS.map(s => `Noto Sans ${s}`), ...SYMBOLS];
const slug = (s: string): string => s.toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-|-$/g, '');
export type FaceKind = 'title' | 'body' | 'script' | 'fallback'; export type FaceKind = 'title' | 'body' | 'script' | 'fallback';
/** One downloaded face: `face` is the @font-face family name the templates use. axis: css2 axis spec ("wght@800", /** One downloaded face: `face` is the @font-face family name the templates use. axis: css2 axis spec ("wght@800",
* "wght@100..900") or null for the font's default instance. */ * "wght@100..900") or null for the font's default instance. */
export interface FaceJob { face: string; family: string; file: string; kind: FaceKind; role?: Role; axis: string | null; chars: string } export interface FaceJob { face: string; family: string; file: string; kind: FaceKind; role?: Role; axis: string | null; chars: string; local?: LocalFontInfo }
export interface LangFonts { title: string[]; body: string[]; fallbacks: { role: Role; face: string; family: string; chars: string }[] } export interface LangFonts { title: string[]; body: string[]; fallbacks: { role: Role; face: string; family: string; chars: string }[] }
export interface FontManifest { export interface FontManifest {
v: 1; v: 1;
faces: Record<string, { family: string; file: string; kind: FaceKind; role?: Role; chosen: boolean }>; /** local: a local font file (shots.fonts entry with a path), read in place, never copied; file = its served path. */
faces: Record<string, { family: string; file: string; kind: FaceKind; role?: Role; chosen: boolean; local?: LocalFontInfo }>;
langs: Record<string, LangFonts>; langs: Record<string, LangFonts>;
} }
@ -81,8 +82,11 @@ export interface PlanInput {
titleAxis: string | null; bodyAxis: string | null; titleAxis: string | null; bodyAxis: string | null;
/** Weight axis for automatic title fallbacks (game titles are heavy display faces: "wght@800"). */ /** Weight axis for automatic title fallbacks (game titles are heavy display faces: "wght@800"). */
fallbackTitleAxis: string | null; fallbackTitleAxis: string | null;
/** shots.fonts.fallback: families the owner accepts as fallbacks. */ /** shots.fonts.fallback: families the owner accepts as fallbacks, in order. Families that are not one of the automatic
* Noto fallbacks (another Google family, a local file) are tried first, for every character the stack lacks. */
chosen?: string[]; chosen?: string[];
/** Local font files by family (title, body or fallback). */
local?: Record<string, LocalFontInfo>;
/** Extra characters for the title / body downloads (ASCII, so later copy edits rarely need a new download). */ /** Extra characters for the title / body downloads (ASCII, so later copy edits rarely need a new download). */
base?: string; base?: string;
} }
@ -100,6 +104,11 @@ export class GlyphError extends Error {
* and returns the code points its cmap covers. Throws GlyphError when no fallback covers a character. */ * and returns the code points its cmap covers. Throws GlyphError when no fallback covers a character. */
export async function planFonts(input: PlanInput, get: (job: FaceJob) => Promise<Set<number>>): Promise<FontManifest> { export async function planFonts(input: PlanInput, get: (job: FaceJob) => Promise<Set<number>>): Promise<FontManifest> {
const chosen = new Set((input.chosen ?? []).map(s => s.toLowerCase())); const chosen = new Set((input.chosen ?? []).map(s => s.toLowerCase()));
const local = input.local ?? {};
const extra = [...new Set(input.chosen ?? [])].filter(f => !FALLBACK_ORDER.some(n => n.toLowerCase() === f.toLowerCase()));
const order = [...extra, ...FALLBACK_ORDER];
const fileOf = (family: string, google: string): string => local[family] ? localFile(family, local[family]!) : google;
const loc = (family: string): { local?: LocalFontInfo } => (local[family] ? { local: local[family] } : {});
const base = input.base ?? ''; const base = input.base ?? '';
const scripts = new Map<string, { family: string; chars: Set<string> }>(); const scripts = new Map<string, { family: string; chars: Set<string> }>();
const langScript = new Map<string, string | null>(); const langScript = new Map<string, string | null>();
@ -121,13 +130,13 @@ export async function planFonts(input: PlanInput, get: (job: FaceJob) => Promise
const add = async (job: FaceJob, isChosen: boolean): Promise<void> => { const add = async (job: FaceJob, isChosen: boolean): Promise<void> => {
cps.set(job.face, await get(job)); cps.set(job.face, await get(job));
if (job.kind === 'fallback' && ![...job.chars].some(ch => cps.get(job.face)!.has(ch.codePointAt(0)!))) return; // covers none: not a face if (job.kind === 'fallback' && ![...job.chars].some(ch => cps.get(job.face)!.has(ch.codePointAt(0)!))) return; // covers none: not a face
faces[job.face] = { family: job.family, file: job.file, kind: job.kind, ...(job.role ? { role: job.role } : {}), chosen: isChosen }; faces[job.face] = { family: job.family, file: job.file, kind: job.kind, ...(job.role ? { role: job.role } : {}), chosen: isChosen, ...(job.local ? { local: job.local } : {}) };
}; };
await add({ face: input.title, family: input.title, file: 'title.woff2', kind: 'title', role: 'title', axis: input.titleAxis, chars: sorted(roleChars.title) }, true); await add({ face: input.title, family: input.title, file: fileOf(input.title, 'title.woff2'), kind: 'title', role: 'title', axis: input.titleAxis, chars: sorted(roleChars.title), ...loc(input.title) }, true);
if (input.body !== input.title) await add({ face: input.body, family: input.body, file: 'body.woff2', kind: 'body', role: 'body', axis: input.bodyAxis, chars: sorted(roleChars.body) }, true); if (input.body !== input.title) await add({ face: input.body, family: input.body, file: fileOf(input.body, 'body.woff2'), kind: 'body', role: 'body', axis: input.bodyAxis, chars: sorted(roleChars.body), ...loc(input.body) }, true);
else { // same family: one download (title axis) with both roles' characters else { // same family: one download (title axis) with both roles' characters
const t = faces[input.title]!; const t = faces[input.title]!;
cps.set(input.title, await get({ face: input.title, family: input.title, file: t.file, kind: 'title', role: 'title', axis: input.titleAxis, chars: sorted([...roleChars.title, ...roleChars.body]) })); cps.set(input.title, await get({ face: input.title, family: input.title, file: t.file, kind: 'title', role: 'title', axis: input.titleAxis, chars: sorted([...roleChars.title, ...roleChars.body]), ...loc(input.title) }));
} }
for (const [s, v] of scripts) await add({ face: `Store ${s}`, family: v.family, file: `${s}.woff2`, kind: 'script', axis: 'wght@100..900', chars: sorted(v.chars) }, true); for (const [s, v] of scripts) await add({ face: `Store ${s}`, family: v.family, file: `${s}.woff2`, kind: 'script', axis: 'wght@100..900', chars: sorted(v.chars) }, true);
@ -139,17 +148,17 @@ export async function planFonts(input: PlanInput, get: (job: FaceJob) => Promise
langs[lang] = { title: [...(x ? [x] : []), input.title], body: [...(x ? [x] : []), input.body], fallbacks: [] }; langs[lang] = { title: [...(x ? [x] : []), input.title], body: [...(x ? [x] : []), input.body], fallbacks: [] };
for (const role of ['title', 'body'] as const) { for (const role of ['title', 'body'] as const) {
const stack = langs[lang]![role].map(f => cps.get(f)!); const stack = langs[lang]![role].map(f => cps.get(f)!);
for (const ch of uncoveredChars(t[role], stack)) pending.push({ lang, role, ch, tries: fallbackFamilies(ch, lang, role) }); for (const ch of uncoveredChars(t[role], stack)) pending.push({ lang, role, ch, tries: [...extra, ...fallbackFamilies(ch, lang, role)] });
} }
} }
const gaps = new Map<string, { lang: string; role: Role; chars: string[]; tried: string[] }>(); const gaps = new Map<string, { lang: string; role: Role; chars: string[]; tried: string[] }>();
const hits = new Map<string, Map<string, Set<string>>>(); // lang|role -> face -> chars const hits = new Map<string, Map<string, Set<string>>>(); // lang|role -> face -> chars
for (const role of ['title', 'body'] as const) for (const family of FALLBACK_ORDER) { for (const role of ['title', 'body'] as const) for (const family of order) {
const want = pending.filter(p => p.role === role && p.tries.includes(family)); const want = pending.filter(p => p.role === role && p.tries.includes(family));
if (!want.length) continue; if (!want.length) continue;
const face = `Store fb ${slug(family)} ${role}`; const face = `Store fb ${slug(family)} ${role}`;
const axis = family === 'Noto Color Emoji' ? null : role === 'title' ? input.fallbackTitleAxis : 'wght@100..900'; const axis = family === 'Noto Color Emoji' ? null : role === 'title' ? input.fallbackTitleAxis : 'wght@100..900';
await add({ face, family, file: `fb-${slug(family)}-${role}.woff2`, kind: 'fallback', role, axis, chars: sorted(want.map(p => p.ch)) }, chosen.has(family.toLowerCase())); await add({ face, family, file: fileOf(family, `fb-${slug(family)}-${role}.woff2`), kind: 'fallback', role, axis, chars: sorted(want.map(p => p.ch)), ...loc(family) }, chosen.has(family.toLowerCase()));
const got = cps.get(face)!; const got = cps.get(face)!;
for (const p of want) { for (const p of want) {
p.tries = p.tries.filter(f => f !== family); p.tries = p.tries.filter(f => f !== family);
@ -165,14 +174,14 @@ export async function planFonts(input: PlanInput, get: (job: FaceJob) => Promise
for (const p of pending) { for (const p of pending) {
if ((p as { done?: boolean }).done) continue; if ((p as { done?: boolean }).done) continue;
const k = `${p.lang}|${p.role}`; const k = `${p.lang}|${p.role}`;
const g = gaps.get(k) ?? { lang: p.lang, role: p.role, chars: [], tried: [...langs[p.lang]![p.role].map(f => faces[f]?.family ?? f), ...fallbackFamilies(p.ch, p.lang, p.role)] }; const g = gaps.get(k) ?? { lang: p.lang, role: p.role, chars: [], tried: [...langs[p.lang]![p.role].map(f => faces[f]?.family ?? f), ...extra, ...fallbackFamilies(p.ch, p.lang, p.role)] };
g.chars.push(p.ch); g.chars.push(p.ch);
gaps.set(k, g); gaps.set(k, g);
} }
if (gaps.size) throw new GlyphError([...gaps.values()]); if (gaps.size) throw new GlyphError([...gaps.values()]);
for (const [k, m] of hits) { for (const [k, m] of hits) {
const [lang, role] = k.split('|') as [string, Role]; const [lang, role] = k.split('|') as [string, Role];
for (const face of [...m.keys()].sort((a, b) => FALLBACK_ORDER.indexOf(faces[a]!.family) - FALLBACK_ORDER.indexOf(faces[b]!.family))) { for (const face of [...m.keys()].sort((a, b) => order.indexOf(faces[a]!.family) - order.indexOf(faces[b]!.family))) {
langs[lang]![role].push(face); langs[lang]![role].push(face);
langs[lang]!.fallbacks.push({ role, face, family: faces[face]!.family, chars: [...m.get(face)!].join('') }); langs[lang]!.fallbacks.push({ role, face, family: faces[face]!.family, chars: [...m.get(face)!].join('') });
} }
@ -187,6 +196,12 @@ export function fontsCss(m: FontManifest): string {
for (const [face, f] of Object.entries(m.faces)) { for (const [face, f] of Object.entries(m.faces)) {
if (seen.has(face)) continue; if (seen.has(face)) continue;
seen.add(face); seen.add(face);
if (f.local) { // the project's own font file, served in place; ?v= changes with its content
const l = f.local;
css += `/* ${f.family}: local file ${l.path} (${l.license ? 'license: ' + l.license.replace(/\*\//g, '') : 'no license note'}) */\n`;
css += `@font-face { font-family: '${face}'; font-weight: ${l.weight ?? '100 900'}; font-style: ${l.style ?? 'normal'}; font-synthesis: none; src: url(${f.file}?v=${l.hash}) format('${l.format}'); }\n`;
continue;
}
css += `@font-face { font-family: '${face}'; font-weight: 100 900; font-synthesis: none; src: url(${f.file}) format('woff2'); }\n`; css += `@font-face { font-family: '${face}'; font-weight: 100 900; font-synthesis: none; src: url(${f.file}) format('woff2'); }\n`;
} }
return css; return css;

View file

@ -1,4 +1,5 @@
// Pure parts of the screenshot pipeline: store sizes, palettes, per-language script fonts, copy validation, job data. // Pure parts of the screenshot pipeline: store sizes, palettes, per-language script fonts, copy validation, job data.
import { specFamily } from './local-fonts';
import type { Palette, ShipConfig, ShotScene, ShotsConfig } from '../lib/config'; import type { Palette, ShipConfig, ShotScene, ShotsConfig } from '../lib/config';
/** Store image sizes. ios69 = 6.9" (APP_IPHONE_67), ios65 = 6.5" (APP_IPHONE_65), iphone-medium = 6.3" iPhone with /** Store image sizes. ios69 = 6.9" (APP_IPHONE_67), ios65 = 6.5" (APP_IPHONE_65), iphone-medium = 6.3" iPhone with
@ -54,7 +55,8 @@ export const palette = (p: string | Palette | undefined, style: 'game' | 'app' =
* Cyrillic. The app title is a variable font (bold headline weights). Per-script stack: glyphs.ts. */ * Cyrillic. The app title is a variable font (bold headline weights). Per-script stack: glyphs.ts. */
export function shotFonts(shots: Pick<ShotsConfig, 'fonts'> | undefined, style: 'game' | 'app'): { title: string; body: string; titleVariable: boolean } { export function shotFonts(shots: Pick<ShotsConfig, 'fonts'> | undefined, style: 'game' | 'app'): { title: string; body: string; titleVariable: boolean } {
const d = style === 'app' ? { title: 'Inter', body: 'Inter' } : { title: 'Paytone One', body: 'Montserrat' }; const d = style === 'app' ? { title: 'Inter', body: 'Inter' } : { title: 'Paytone One', body: 'Montserrat' };
return { title: shots?.fonts?.title ?? d.title, body: shots?.fonts?.body ?? d.body, titleVariable: style === 'app' }; // local files: the family given in the config, else the file name (resolveShotFonts reads the real name table)
return { title: specFamily(shots?.fonts?.title ?? d.title), body: specFamily(shots?.fonts?.body ?? d.body), titleVariable: style === 'app' };
} }
export interface SceneCopy { eyebrow?: string; title: [string, string]; sub?: string; badges?: [string, string?][] } export interface SceneCopy { eyebrow?: string; title: [string, string]; sub?: string; badges?: [string, string?][] }

130
cli/shots/local-fonts.ts Normal file
View file

@ -0,0 +1,130 @@
// A game's or app's own brand font as a local file, next to the Google Fonts families:
// shots.fonts.title / body / fallback[] = 'Paytone One' (Google Fonts family)
// | 'assets/fonts/er-display.woff2' (local file, family from its name table)
// | { family, file, weight?, style?, license? }
// Paths are relative to the config. TTF / OTF / WOFF / WOFF2 (checked by extension and signature). The file is never
// copied: `shots fonts` reads its cmap and records it in fonts.json (path relative to the project, sha256, license),
// fonts.css points at /fonts/local/<slug>.<ext>?v=<hash> and the render / video servers serve that route from the
// original file. A changed file changes fonts.css (capture cache) and fails render until `shots fonts` runs again.
import { createHash } from 'node:crypto';
import { existsSync, readFileSync, statSync } from 'node:fs';
import { basename, join, relative } from 'node:path';
import { fontFamilyName } from './cmap';
import type { FontManifest } from './glyphs';
import type { FontSpec, LocalFontSpec, ShotsConfig } from '../lib/config';
export type { FontSpec, LocalFontSpec };
export type FontFormat = 'truetype' | 'opentype' | 'woff' | 'woff2';
/** A local font as fonts.json records it. path: relative to the project root. */
export interface LocalFontInfo { path: string; format: FontFormat; hash: string; bytes: number; weight?: string; style?: string; license?: string }
/** A resolved font of shots.fonts: the family name the templates use, plus the file for a local font. */
export interface FontRef { family: string; local?: LocalFontInfo & { abs: string } }
const EXT: Record<string, FontFormat> = { ttf: 'truetype', otf: 'opentype', woff: 'woff', woff2: 'woff2' };
/** A string spec is a local file when it ends in a font extension or contains a path separator. */
export const isLocalSpec = (s: FontSpec): boolean => typeof s !== 'string' || /\.(ttf|otf|woff2?)$/i.test(s) || /[\\/]/.test(s);
/** Format from the file signature (null = not a font file storebound reads). */
export function sniffFormat(bin: Uint8Array): FontFormat | null {
if (bin.byteLength < 12) return null;
const sig = String.fromCharCode(bin[0]!, bin[1]!, bin[2]!, bin[3]!);
if (sig === 'wOF2') return 'woff2';
if (sig === 'wOFF') return 'woff';
if (sig === 'OTTO') return 'opentype';
if (sig === 'true' || (bin[0] === 0 && bin[1] === 1 && bin[2] === 0 && bin[3] === 0)) return 'truetype';
return null;
}
export const fileHash = (bin: Uint8Array): string => createHash('sha256').update(bin).digest('hex').slice(0, 16);
/** Resolves and validates one local font spec (file exists, TTF / OTF / WOFF / WOFF2). Throws with the config key. */
export function loadLocalFont(spec: FontSpec, key: string, resolve: (p: string) => string, root: string): FontRef {
const o: LocalFontSpec = typeof spec === 'string' ? { file: spec } : spec;
if (!o.file || typeof o.file !== 'string') throw new Error(`${key}: { family, file } needs a file path (relative to the config)`);
const ext = o.file.split('.').pop()!.toLowerCase();
if (!EXT[ext]) throw new Error(`${key}: ${o.file}: not a font file (TTF, OTF, WOFF or WOFF2)`);
const abs = resolve(o.file);
if (!existsSync(abs) || !statSync(abs).isFile()) throw new Error(`${key}: ${o.file} not found (paths are relative to the config: ${abs})`);
const bin = new Uint8Array(readFileSync(abs));
const format = sniffFormat(bin);
if (!format) throw new Error(`${key}: ${o.file} is not a TTF / OTF / WOFF / WOFF2 font (unknown signature)`);
const sfnt = (f: FontFormat): boolean => f === 'truetype' || f === 'opentype';
if (format !== EXT[ext] && !(sfnt(format) && sfnt(EXT[ext]!))) throw new Error(`${key}: ${o.file} is ${format.toUpperCase()} inside, but named .${ext}: rename it`);
const family = (o.family ?? fontFamilyName(bin) ?? basename(o.file).replace(/\.[^.]+$/, '')).trim();
if (!family) throw new Error(`${key}: ${o.file}: no family name (give { family, file })`);
return { family, local: { abs, path: relative(root, abs), format, hash: fileHash(bin), bytes: bin.byteLength,
...(o.weight !== undefined ? { weight: String(o.weight) } : {}), ...(o.style ? { style: o.style } : {}), ...(o.license ? { license: o.license } : {}) } };
}
/** shots.fonts resolved: Google families as names, local files loaded and validated (throws on a bad file). */
export function resolveShotFonts(shots: Pick<ShotsConfig, 'fonts'> | undefined, style: 'game' | 'app', resolve: (p: string) => string, root: string): { title: FontRef; body: FontRef; fallback: FontRef[]; titleVariable: boolean } {
const d = style === 'app' ? { title: 'Inter', body: 'Inter' } : { title: 'Paytone One', body: 'Montserrat' };
const ref = (s: FontSpec, key: string): FontRef => isLocalSpec(s) ? loadLocalFont(s, key, resolve, root) : { family: s as string };
const fallback = (shots?.fonts?.fallback ?? []).map((f, i) => ref(f, `shots.fonts.fallback[${i}]`));
return { title: ref(shots?.fonts?.title ?? d.title, 'shots.fonts.title'), body: ref(shots?.fonts?.body ?? d.body, 'shots.fonts.body'), fallback, titleVariable: style === 'app' };
}
/** Local files by family for planFonts (one family, one file). */
export function localByFamily(refs: FontRef[]): Record<string, LocalFontInfo> {
const out: Record<string, LocalFontInfo> = {};
for (const r of refs) if (r.local) {
const { abs: _abs, ...info } = r.local;
if (out[r.family] && out[r.family]!.hash !== info.hash) throw new Error(`shots.fonts: two different files for the family "${r.family}" (give each a distinct family)`);
out[r.family] = info;
}
return out;
}
/** Family name of a spec without reading files (Google family, { family }, else the file name). For callers that only
* need a name and never a local file's name table. */
export const specFamily = (s: FontSpec): string => typeof s !== 'string' ? s.family ?? basename(s.file).replace(/\.[^.]+$/, '')
: isLocalSpec(s) ? basename(s).replace(/\.[^.]+$/, '') : s;
export const slug = (s: string): string => s.toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-|-$/g, '');
/** fonts.json file name of a local face: served at /fonts/local/<slug>.<ext>. */
export const localFile = (family: string, l: Pick<LocalFontInfo, 'format'>): string =>
`local/${slug(family)}.${l.format === 'truetype' ? 'ttf' : l.format === 'opentype' ? 'otf' : l.format}`;
/** Absolute file of a face: the downloaded subset in .storebound/fonts, or the local font file itself. */
export function faceFilePath(fontsDir: string, root: string, f: FontManifest['faces'][string]): string {
return f.local ? join(root, f.local.path) : join(fontsDir, f.file);
}
/** File for a /fonts/<rel> request of the render / video page (null = not ours). */
export function fontsRoute(rel: string, fontsDir: string, root: string, m: FontManifest | null): string | null {
rel = rel.split('?')[0]!;
if (rel.includes('..')) return null;
if (rel.startsWith('local/')) {
const f = Object.values(m?.faces ?? {}).find(x => x.local && x.file === rel);
return f ? faceFilePath(fontsDir, root, f) : null;
}
return join(fontsDir, rel);
}
export const FONT_TYPES: Record<string, string> = { woff2: 'font/woff2', woff: 'font/woff', ttf: 'font/ttf', otf: 'font/otf', css: 'text/css' };
/** Local faces whose file changed (or vanished) since `shots fonts`. */
export function staleLocalFonts(m: FontManifest, root: string): string[] {
const out: string[] = [];
for (const f of new Map(Object.values(m.faces).filter(x => x.local).map(x => [x.local!.path, x] as const)).values()) {
const abs = join(root, f.local!.path);
if (!existsSync(abs)) out.push(`${f.family}: ${f.local!.path} is gone`);
else if (fileHash(new Uint8Array(readFileSync(abs))) !== f.local!.hash) out.push(`${f.family}: ${f.local!.path} changed`);
}
return out;
}
/** `shots check` warnings for local fonts without a license note. */
export function localFontIssues(m: FontManifest | null): { level: 'warn'; code: 'font-license'; msg: string }[] {
const seen = new Set<string>();
const out: { level: 'warn'; code: 'font-license'; msg: string }[] = [];
for (const f of Object.values(m?.faces ?? {})) {
if (!f.local || f.local.license || seen.has(f.local.path)) continue;
seen.add(f.local.path);
out.push({ level: 'warn', code: 'font-license', msg: `local font ${f.family} (${f.local.path}) has no license note: add license ('OFL-1.1', 'proprietary', or a short note) to its shots.fonts entry, and make sure you may embed it in store graphics` });
}
return out;
}

View file

@ -1,6 +1,6 @@
// Store screenshots + Play feature graphic at exact pixel sizes, from raw app or game frames and per-language copy. // Store screenshots + Play feature graphic at exact pixel sizes, from raw app or game frames and per-language copy.
// storebound shots render [--lang en,tr] [--size ios69,play,feature] [--scenes 1,3] [--variant b | --variants] // storebound shots render [--lang en,tr] [--size ios69,play,feature] [--scenes 1,3] [--variant b | --variants]
// [--no-sheet] [--port 8792] [--dry] // [--no-sheet] [--port 8792] [--concurrency N] [--dry]
// Out: <shots.out>/<lang>/<size>/NN.png (24-bit RGB: Play rejects alpha / palette PNGs), <lang>/feature/1024x500.png, // Out: <shots.out>/<lang>/<size>/NN.png (24-bit RGB: Play rejects alpha / palette PNGs), <lang>/feature/1024x500.png,
// <lang>/sheet.png (contact sheet of every size packed into a grid, for a quick review) and <shots.out>/report.json (text overflow flags // <lang>/sheet.png (contact sheet of every size packed into a grid, for a quick review) and <shots.out>/report.json (text overflow flags
// per image, read by `shots check` and `shots gallery`). Variants go to <shots.out>/<variant>/<lang>/... // per image, read by `shots check` and `shots gallery`). Variants go to <shots.out>/<variant>/<lang>/...
@ -8,7 +8,8 @@
// Fonts: the per-language stacks from .storebound/fonts/fonts.json (`shots fonts`). Before rendering, every character // Fonts: the per-language stacks from .storebound/fonts/fonts.json (`shots fonts`). Before rendering, every character
// of the copy is checked against the cmaps of the downloaded faces (stale fonts fail with the language and the // of the copy is checked against the cmaps of the downloaded faces (stale fonts fail with the language and the
// characters); while rendering, the page measures which face drew every character, and report.json records it per // characters); while rendering, the page measures which face drew every character, and report.json records it per
// image (`fonts`: faces used, automatic fallbacks, system-font glyphs) for `shots check`. // image (`fonts`: faces used, automatic fallbacks, system-font glyphs) for `shots check`. Local font files (shots.fonts
// paths) are served from where they are at /fonts/local/... (local-fonts.ts); a file changed since `shots fonts` fails.
// Style: shotsStyle() = shots.style, else app for the app / subscription-app profiles (template-app.html, // Style: shotsStyle() = shots.style, else app for the app / subscription-app profiles (template-app.html,
// feature-app.html: calm gradient, device frame, headline + subline), else game (template.html, feature.html). // feature-app.html: calm gradient, device frame, headline + subline), else game (template.html, feature.html).
import { existsSync, mkdirSync, readFileSync, rmSync } from 'node:fs'; import { existsSync, mkdirSync, readFileSync, rmSync } from 'node:fs';
@ -16,17 +17,18 @@ import { fontCodepoints } from './cmap';
import { readManifest } from './fonts'; import { readManifest } from './fonts';
import { type FontManifest, fontUsage, measureFonts, roleTexts, uncoveredChars } from './glyphs'; import { type FontManifest, fontUsage, measureFonts, roleTexts, uncoveredChars } from './glyphs';
import { join, relative } from 'node:path'; import { join, relative } from 'node:path';
import { spawnSync } from 'node:child_process';
import type { Ctx } from '../lib/ctx'; import type { Ctx } from '../lib/ctx';
import { need } from '../lib/ctx'; import { need } from '../lib/ctx';
import { done, log, warn } from '../lib/log'; import { done, log, warn } from '../lib/log';
import { launch } from './browser'; import { launch } from './browser';
import { defaultConcurrency, pool, progressLine } from './capture-web';
import { shotsStyle } from '../lib/config'; import { shotsStyle } from '../lib/config';
import { type CopyFile, SIZES, copyText, defaultSizes, frameDims, mergeCopy, palette, renderDir, scriptFont, sheetLayout, shotFonts, validateCopy, variantScenes } from './lib'; import { type CopyFile, SIZES, copyText, defaultSizes, frameDims, mergeCopy, palette, renderDir, scriptFont, sheetLayout, validateCopy, variantScenes } from './lib';
import { faceFilePath, fontsRoute, resolveShotFonts, staleLocalFonts } from './local-fonts';
import { type RenderReport, mergeReport, readReport } from './report'; import { type RenderReport, mergeReport, readReport } from './report';
const TYPES: Record<string, string> = { html: 'text/html; charset=utf-8', png: 'image/png', jpg: 'image/jpeg', jpeg: 'image/jpeg', const TYPES: Record<string, string> = { html: 'text/html; charset=utf-8', png: 'image/png', jpg: 'image/jpeg', jpeg: 'image/jpeg',
webp: 'image/webp', woff2: 'font/woff2', css: 'text/css', svg: 'image/svg+xml' }; webp: 'image/webp', woff2: 'font/woff2', woff: 'font/woff', ttf: 'font/ttf', otf: 'font/otf', css: 'text/css', svg: 'image/svg+xml' };
export async function run(ctx: Ctx): Promise<void> { export async function run(ctx: Ctx): Promise<void> {
const { config, root, resolve } = ctx.loaded; const { config, root, resolve } = ctx.loaded;
@ -62,7 +64,8 @@ export async function run(ctx: Ctx): Promise<void> {
const { phone, tablet } = frameDims(config); const { phone, tablet } = frameDims(config);
const rtl = new Map(config.languages.map(l => [l.id, !!l.rtl] as const)); const rtl = new Map(config.languages.map(l => [l.id, !!l.rtl] as const));
const style = shotsStyle(config); const style = shotsStyle(config);
const sf = shotFonts(shots, style); const rf = resolveShotFonts(shots, style, resolve, root);
const sf = { title: rf.title.family, body: rf.body.family };
const fonts = { title: sf.title, body: sf.body }; const fonts = { title: sf.title, body: sf.body };
const manifest = existsSync(join(fontsDir, 'fonts.css')) ? readManifest(fontsDir) : null; const manifest = existsSync(join(fontsDir, 'fonts.css')) ? readManifest(fontsDir) : null;
if (existsSync(join(fontsDir, 'fonts.css')) && !manifest) warn('shots', 'fonts from an older storebound (no fonts.json): run `storebound shots fonts` again for the glyph coverage check'); if (existsSync(join(fontsDir, 'fonts.css')) && !manifest) warn('shots', 'fonts from an older storebound (no fonts.json): run `storebound shots fonts` again for the glyph coverage check');
@ -79,10 +82,14 @@ export async function run(ctx: Ctx): Promise<void> {
const x = stacks(lang, job.copy).script; const x = stacks(lang, job.copy).script;
if (x) faces[x] = { family: x, file: '', kind: 'script', chosen: true }; if (x) faces[x] = { family: x, file: '', kind: 'script', chosen: true };
} }
if (manifest) {
const stale = staleLocalFonts(manifest, root);
if (stale.length) throw new Error(`shots render: local font files changed since \`shots fonts\`: run \`storebound shots fonts\`\n ${stale.join('\n ')}`);
}
if (manifest) { // glyph coverage of the local font files, before rendering anything if (manifest) { // glyph coverage of the local font files, before rendering anything
const cmaps = new Map<string, Set<number>>(); const cmaps = new Map<string, Set<number>>();
const cps = (face: string): Set<number> => { const cps = (face: string): Set<number> => {
if (!cmaps.has(face)) { const f = manifest.faces[face]; cmaps.set(face, f && existsSync(join(fontsDir, f.file)) ? fontCodepoints(readFileSync(join(fontsDir, f.file))) : new Set()); } if (!cmaps.has(face)) { const f = manifest.faces[face]; const file = f && faceFilePath(fontsDir, root, f); cmaps.set(face, file && existsSync(file) ? fontCodepoints(readFileSync(file)) : new Set()); }
return cmaps.get(face)!; return cmaps.get(face)!;
}; };
const gaps: string[] = []; const gaps: string[] = [];
@ -121,7 +128,7 @@ export async function run(ctx: Ctx): Promise<void> {
let file = ''; let file = '';
if (p === '/logo' && shots.logo) file = resolve(shots.logo); if (p === '/logo' && shots.logo) file = resolve(shots.logo);
else if (p.startsWith('/frames/')) file = join(framesDir, p.slice(8)); else if (p.startsWith('/frames/')) file = join(framesDir, p.slice(8));
else if (p.startsWith('/fonts/')) file = join(fontsDir, p.slice(7)); else if (p.startsWith('/fonts/')) file = fontsRoute(p.slice(7), fontsDir, root, manifest) ?? '';
else if (p.startsWith('/out/')) file = join(outDir, p.slice(5)); else if (p.startsWith('/out/')) file = join(outDir, p.slice(5));
else if (/^\/(template|feature)(-app)?\.html$/.test(p)) file = join(tpl, p.slice(1)); else if (/^\/(template|feature)(-app)?\.html$/.test(p)) file = join(tpl, p.slice(1));
if (!file || file.includes('..')) return new Response('no', { status: 404 }); if (!file || file.includes('..')) return new Response('no', { status: 404 });
@ -132,14 +139,24 @@ export async function run(ctx: Ctx): Promise<void> {
}); });
const base = `http://127.0.0.1:${server.port}`; const base = `http://127.0.0.1:${server.port}`;
const browser = await launch(root, { headless: true }); const browser = await launch(root, { headless: true });
const page = await (await browser.newContext({ deviceScaleFactor: 1 })).newPage(); const concurrency = a.num('concurrency', defaultConcurrency('render'));
const errs: string[] = []; // one page per worker, each with its own error list
page.on('pageerror', (e: Error) => errs.push(e.message)); // eslint-disable-next-line @typescript-eslint/no-explicit-any
page.on('response', (r: { status(): number; url(): string }) => { if (r.status() >= 400 && !r.url().includes('/fonts/')) errs.push(`${r.status()} ${r.url()}`); }); const pages: { page: any; errs: string[] }[] = [];
let warnings = 0; const pageFor = async (k: number): Promise<{ page: any; errs: string[] }> => { // eslint-disable-line @typescript-eslint/no-explicit-any
if (pages[k]) return pages[k]!;
const page = await (await browser.newContext({ deviceScaleFactor: 1 })).newPage(); // own context: no background-tab throttling
const errs: string[] = [];
page.on('pageerror', (e: Error) => errs.push(e.message));
page.on('response', (r: { status(): number; url(): string }) => { if (r.status() >= 400 && !r.url().includes('/fonts/')) errs.push(`${r.status()} ${r.url()}`); });
return (pages[k] = { page, errs });
};
let warnings = 0, finished = 0, total = 0;
const started = Date.now();
const report: RenderReport = {}; const report: RenderReport = {};
async function shoot(url: string, w: number, h: number, out: string): Promise<void> { async function shoot(p: { page: any; errs: string[] }, url: string, w: number, h: number, out: string): Promise<void> { // eslint-disable-line @typescript-eslint/no-explicit-any
const { page, errs } = p;
errs.length = 0; errs.length = 0;
mkdirSync(join(out, '..'), { recursive: true }); mkdirSync(join(out, '..'), { recursive: true });
await page.setViewportSize({ width: w, height: h }); await page.setViewportSize({ width: w, height: h });
@ -157,47 +174,63 @@ export async function run(ctx: Ctx): Promise<void> {
} }
if (overflow.length) warn('shots', `${relative(root, out)}: ${overflow.join('; ')}`); if (overflow.length) warn('shots', `${relative(root, out)}: ${overflow.join('; ')}`);
await page.screenshot({ path: tmp, clip: { x: 0, y: 0, width: w, height: h } }); await page.screenshot({ path: tmp, clip: { x: 0, y: 0, width: w, height: h } });
const r = spawnSync('ffmpeg', ['-v', 'error', '-y', '-i', tmp, '-pix_fmt', 'rgb24', out]); const ff = Bun.spawn(['ffmpeg', '-v', 'error', '-y', '-i', tmp, '-pix_fmt', 'rgb24', out], { stderr: 'pipe' });
const code = await ff.exited;
rmSync(tmp, { force: true }); rmSync(tmp, { force: true });
if (r.status !== 0) throw new Error('ffmpeg: ' + r.stderr); if (code !== 0) throw new Error('ffmpeg: ' + await new Response(ff.stderr).text());
if (errs.length) warnings++; if (errs.length) warnings++;
log('shots', `${errs.length ? 'WARN ' : ''}${relative(root, out)}${errs.length ? ' ' + errs.join(' | ') : ''}`); finished++;
log('shots', `${progressLine(finished, total, started)} ${errs.length ? 'WARN ' : ''}${relative(root, out)}${errs.length ? ' ' + errs.join(' | ') : ''}`);
} }
try { // every image first (in parallel), then one contact sheet per language from its finished images
for (const [v, job] of jobs) for (const lang of langs) { const items: { url: string; w: number; h: number; out: string }[] = [];
const sceneIds = a.list('scenes').length ? a.list('scenes') : job.scenes.map(s => s.id); const sheets: { dir: string; rows: { size: string; files: string[] }[] }[] = [];
const vq = v ? `&v=${encodeURIComponent(v)}` : ''; for (const [v, job] of jobs) for (const lang of langs) {
const dir = renderDir(outDir, lang, v || undefined); const sceneIds = a.list('scenes').length ? a.list('scenes') : job.scenes.map(s => s.id);
const rows: { size: string; files: string[] }[] = []; const vq = v ? `&v=${encodeURIComponent(v)}` : '';
for (const size of sizes) { const dir = renderDir(outDir, lang, v || undefined);
const s = SIZES[size]!; const rows: { size: string; files: string[] }[] = [];
if (s.feature) { for (const size of sizes) {
const f = join(dir, 'feature', '1024x500.png'); const s = SIZES[size]!;
await shoot(`${base}/${style === 'app' ? 'feature-app' : 'feature'}.html?lang=${encodeURIComponent(lang)}${vq}`, s.w, s.h, f); if (s.feature) {
rows.push({ size, files: [f] }); const f = join(dir, 'feature', '1024x500.png');
continue; items.push({ url: `${base}/${style === 'app' ? 'feature-app' : 'feature'}.html?lang=${encodeURIComponent(lang)}${vq}`, w: s.w, h: s.h, out: f });
} rows.push({ size, files: [f] });
const files: string[] = []; continue;
for (const [i, id] of sceneIds.entries()) {
const f = join(dir, size, `${String(i + 1).padStart(2, '0')}.png`);
await shoot(`${base}/${style === 'app' ? 'template-app' : 'template'}.html?s=${encodeURIComponent(id)}&w=${s.w}&h=${s.h}&lang=${encodeURIComponent(lang)}${s.device ? '&device=tablet' : ''}${size === 'tab7' || size === 'tab10' ? '&style=android' : ''}${vq}`, s.w, s.h, f);
files.push(f);
}
rows.push({ size, files });
} }
if (!a.has('no-sheet') && rows.length) { const files: string[] = [];
const lay = sheetLayout(rows); for (const [i, id] of sceneIds.entries()) {
const imgs = lay.tiles.map(t => `<img src="/out/${relative(outDir, t.file)}" style="position:absolute;left:${t.x}px;top:${t.y}px;width:${t.w}px;height:${t.h}px;border-radius:14px">`).join(''); const f = join(dir, size, `${String(i + 1).padStart(2, '0')}.png`);
const W = lay.w, y = lay.h; items.push({ url: `${base}/${style === 'app' ? 'template-app' : 'template'}.html?s=${encodeURIComponent(id)}&w=${s.w}&h=${s.h}&lang=${encodeURIComponent(lang)}${s.device ? '&device=tablet' : ''}${size === 'tab7' || size === 'tab10' ? '&style=android' : ''}${vq}`, w: s.w, h: s.h, out: f });
const sheet = join(dir, '.sheet.html'); files.push(f);
await Bun.write(sheet, `<!doctype html><body style="margin:0;background:#141418;width:${W}px;height:${y}px;position:relative">${imgs}
<script>Promise.all([...document.images].map(i=>i.decode())).then(()=>document.body.dataset.ready='1')</script></body>`);
await shoot(`${base}/out/${relative(outDir, sheet)}`, W, y, join(dir, 'sheet.png'));
rmSync(sheet, { force: true });
} }
rows.push({ size, files });
} }
if (!a.has('no-sheet') && rows.length) sheets.push({ dir, rows });
}
total = items.length + sheets.length;
log('shots', `rendering ${items.length} images${sheets.length ? ` + ${sheets.length} contact sheets` : ''}, ${Math.min(concurrency, items.length)} at a time (--concurrency)`);
try {
const slot = new Map<number, number>();
let nextSlot = 0;
const withPage = async (fn: (p: { page: any; errs: string[] }) => Promise<void>): Promise<void> => { // eslint-disable-line @typescript-eslint/no-explicit-any
const free = [...Array(concurrency).keys()].find(k => !slot.has(k)) ?? nextSlot++ % concurrency;
slot.set(free, 1);
try { await fn(await pageFor(free)); } finally { slot.delete(free); }
};
await pool(items, concurrency, it => withPage(p => shoot(p, it.url, it.w, it.h, it.out)));
await pool(sheets, concurrency, async ({ dir, rows }) => {
const lay = sheetLayout(rows);
const imgs = lay.tiles.map(t => `<img src="/out/${relative(outDir, t.file)}" style="position:absolute;left:${t.x}px;top:${t.y}px;width:${t.w}px;height:${t.h}px;border-radius:14px">`).join('');
const sheet = join(dir, '.sheet.html');
await Bun.write(sheet, `<!doctype html><body style="margin:0;background:#141418;width:${lay.w}px;height:${lay.h}px;position:relative">${imgs}
<script>Promise.all([...document.images].map(i=>i.decode())).then(()=>document.body.dataset.ready='1')</script></body>`);
await withPage(p => shoot(p, `${base}/out/${relative(outDir, sheet)}`, lay.w, lay.h, join(dir, 'sheet.png')));
rmSync(sheet, { force: true });
});
} finally { } finally {
for (const p of pages) await p?.page.context().close().catch(() => {});
await browser.close(); await browser.close();
server.stop(true); server.stop(true);
if (Object.keys(report).length) await Bun.write(join(outDir, 'report.json'), JSON.stringify(mergeReport(readReport(outDir), report), null, 1) + '\n'); if (Object.keys(report).length) await Bun.write(join(outDir, 'report.json'), JSON.stringify(mergeReport(readReport(outDir), report), null, 1) + '\n');

View file

@ -17,7 +17,8 @@ import { done, log, warn } from '../lib/log';
import { launch } from './browser'; import { launch } from './browser';
import { shotsStyle } from '../lib/config'; import { shotsStyle } from '../lib/config';
import { readManifest } from './fonts'; import { readManifest } from './fonts';
import { captionsFor, type CopyFile, copyText, palette, renderDir, scriptFont, shotFonts } from './lib'; import { captionsFor, type CopyFile, copyText, palette, renderDir, scriptFont } from './lib';
import { FONT_TYPES, fontsRoute, resolveShotFonts } from './local-fonts';
import { ffprobe } from './report'; import { ffprobe } from './report';
import { checkVideo, PREVIEW_RULES, VIDEO_TARGETS } from './spec'; import { checkVideo, PREVIEW_RULES, VIDEO_TARGETS } from './spec';
@ -159,12 +160,13 @@ export async function run(ctx: Ctx): Promise<void> {
const style = shotsStyle(ctx.loaded.config); const style = shotsStyle(ctx.loaded.config);
const pal = palette(shots.scenes[0]?.palette, style); const pal = palette(shots.scenes[0]?.palette, style);
return Response.json({ text: texts[Number(url.searchParams.get('i'))] ?? '', ink: style === 'app' ? '#111827' : pal.ink, rtl: rtl.get(lang), return Response.json({ text: texts[Number(url.searchParams.get('i'))] ?? '', ink: style === 'app' ? '#111827' : pal.ink, rtl: rtl.get(lang),
fonts: { title: shotFonts(shots, style).title, script: script ? `Store ${script.slug}` : null, titleStack: readManifest(fontsDir)?.langs[lang]?.title } }); fonts: { title: resolveShotFonts(shots, style, ctx.loaded.resolve, root).title.family, script: script ? `Store ${script.slug}` : null, titleStack: readManifest(fontsDir)?.langs[lang]?.title } });
} }
if (url.pathname === '/caption.html') return new Response(Bun.file(join(import.meta.dir, 'caption.html')), { headers: { 'content-type': 'text/html; charset=utf-8' } }); if (url.pathname === '/caption.html') return new Response(Bun.file(join(import.meta.dir, 'caption.html')), { headers: { 'content-type': 'text/html; charset=utf-8' } });
if (url.pathname.startsWith('/fonts/') && !url.pathname.includes('..')) { if (url.pathname.startsWith('/fonts/')) {
const f = Bun.file(join(fontsDir, url.pathname.slice(7))); const file = fontsRoute(decodeURIComponent(url.pathname.slice(7)), fontsDir, root, readManifest(fontsDir));
if (await f.exists()) return new Response(f, { headers: { 'content-type': url.pathname.endsWith('.css') ? 'text/css' : 'font/woff2' } }); const f = file ? Bun.file(file) : null;
if (f && await f.exists()) return new Response(f, { headers: { 'content-type': FONT_TYPES[file!.split('.').pop()!.toLowerCase()] ?? 'font/woff2' } });
} }
return new Response('no', { status: 404 }); return new Response('no', { status: 404 });
} }); } });

View file

@ -12,8 +12,8 @@ Specs: Apple [App Store Connect API 4.5.1](https://developer.apple.com/sample-co
| API | operations | GET | other | wrapped by a command | owner only | | API | operations | GET | other | wrapped by a command | owner only |
|---|---:|---:|---:|---:|---:| |---|---:|---:|---:|---:|---:|
| App Store Connect API 4.5.1 | 1303 | 826 | 477 | 187 | 166 | | App Store Connect API 4.5.1 | 1303 | 826 | 477 | 197 | 166 |
| Google Play Android Developer API v3 (revision 20261008) | 145 | 45 | 100 | 34 | 49 | | Google Play Android Developer API v3 (revision 20261008) | 145 | 45 | 100 | 32 | 49 |
| Google Play Developer Reporting API v1beta1 (revision 20261008) | 25 | 15 | 10 | 6 | 0 | | Google Play Developer Reporting API v1beta1 (revision 20261008) | 25 | 15 | 10 | 6 | 0 |
## App Store Connect API 4.5.1 ## App Store Connect API 4.5.1
@ -187,7 +187,7 @@ Specs: Apple [App Store Connect API 4.5.1](https://developer.apple.com/sample-co
| `appCustomProductPages_appCustomProductPageVersions_getToManyRelationship` | GET | `/v1/appCustomProductPages/{id}/relationships/appCustomProductPageVersions` | read | | | `appCustomProductPages_appCustomProductPageVersions_getToManyRelationship` | GET | `/v1/appCustomProductPages/{id}/relationships/appCustomProductPageVersions` | read | |
| `appCustomProductPages_createInstance` | POST | `/v1/appCustomProductPages` | write (dry by default) | apple/cpp | | `appCustomProductPages_createInstance` | POST | `/v1/appCustomProductPages` | write (dry by default) | apple/cpp |
| `appCustomProductPages_deleteInstance` | DELETE | `/v1/appCustomProductPages/{id}` | owner only (typed confirm) | | | `appCustomProductPages_deleteInstance` | DELETE | `/v1/appCustomProductPages/{id}` | owner only (typed confirm) | |
| `appCustomProductPages_getInstance` | GET | `/v1/appCustomProductPages/{id}` | read | | | `appCustomProductPages_getInstance` | GET | `/v1/appCustomProductPages/{id}` | read | apple/cpp |
| `appCustomProductPages_updateInstance` | PATCH | `/v1/appCustomProductPages/{id}` | write (dry by default) | apple/cpp | | `appCustomProductPages_updateInstance` | PATCH | `/v1/appCustomProductPages/{id}` | write (dry by default) | apple/cpp |
| **appEncryptionDeclarationDocuments** (3) | | | | | | **appEncryptionDeclarationDocuments** (3) | | | | |
| `appEncryptionDeclarationDocuments_createInstance` | POST | `/v1/appEncryptionDeclarationDocuments` | write (dry by default) | | | `appEncryptionDeclarationDocuments_createInstance` | POST | `/v1/appEncryptionDeclarationDocuments` | write (dry by default) | |
@ -226,7 +226,7 @@ Specs: Apple [App Store Connect API 4.5.1](https://developer.apple.com/sample-co
| **appEvents** (6) | | | | | | **appEvents** (6) | | | | |
| `appEvents_createInstance` | POST | `/v1/appEvents` | write (dry by default) | apple/events | | `appEvents_createInstance` | POST | `/v1/appEvents` | write (dry by default) | apple/events |
| `appEvents_deleteInstance` | DELETE | `/v1/appEvents/{id}` | owner only (typed confirm) | | | `appEvents_deleteInstance` | DELETE | `/v1/appEvents/{id}` | owner only (typed confirm) | |
| `appEvents_getInstance` | GET | `/v1/appEvents/{id}` | read | | | `appEvents_getInstance` | GET | `/v1/appEvents/{id}` | read | apple/events |
| `appEvents_localizations_getToManyRelated` | GET | `/v1/appEvents/{id}/localizations` | read | apple/events | | `appEvents_localizations_getToManyRelated` | GET | `/v1/appEvents/{id}/localizations` | read | apple/events |
| `appEvents_localizations_getToManyRelationship` | GET | `/v1/appEvents/{id}/relationships/localizations` | read | | | `appEvents_localizations_getToManyRelationship` | GET | `/v1/appEvents/{id}/relationships/localizations` | read | |
| `appEvents_updateInstance` | PATCH | `/v1/appEvents/{id}` | write (dry by default) | | | `appEvents_updateInstance` | PATCH | `/v1/appEvents/{id}` | write (dry by default) | |
@ -264,8 +264,8 @@ Specs: Apple [App Store Connect API 4.5.1](https://developer.apple.com/sample-co
| `appPreviewSets_deleteInstance` | DELETE | `/v1/appPreviewSets/{id}` | owner only (typed confirm) | | | `appPreviewSets_deleteInstance` | DELETE | `/v1/appPreviewSets/{id}` | owner only (typed confirm) | |
| `appPreviewSets_getInstance` | GET | `/v1/appPreviewSets/{id}` | read | | | `appPreviewSets_getInstance` | GET | `/v1/appPreviewSets/{id}` | read | |
| **appPreviews** (4) | | | | | | **appPreviews** (4) | | | | |
| `appPreviews_createInstance` | POST | `/v1/appPreviews` | write (dry by default) | apple/previews | | `appPreviews_createInstance` | POST | `/v1/appPreviews` | write (dry by default) | |
| `appPreviews_deleteInstance` | DELETE | `/v1/appPreviews/{id}` | owner only (typed confirm) | apple/previews | | `appPreviews_deleteInstance` | DELETE | `/v1/appPreviews/{id}` | owner only (typed confirm) | |
| `appPreviews_getInstance` | GET | `/v1/appPreviews/{id}` | read | | | `appPreviews_getInstance` | GET | `/v1/appPreviews/{id}` | read | |
| `appPreviews_updateInstance` | PATCH | `/v1/appPreviews/{id}` | write (dry by default) | apple/previews | | `appPreviews_updateInstance` | PATCH | `/v1/appPreviews/{id}` | write (dry by default) | apple/previews |
| **appPricePoints** (3) | | | | | | **appPricePoints** (3) | | | | |
@ -289,10 +289,10 @@ Specs: Apple [App Store Connect API 4.5.1](https://developer.apple.com/sample-co
| `appScreenshotSets_deleteInstance` | DELETE | `/v1/appScreenshotSets/{id}` | owner only (typed confirm) | | | `appScreenshotSets_deleteInstance` | DELETE | `/v1/appScreenshotSets/{id}` | owner only (typed confirm) | |
| `appScreenshotSets_getInstance` | GET | `/v1/appScreenshotSets/{id}` | read | | | `appScreenshotSets_getInstance` | GET | `/v1/appScreenshotSets/{id}` | read | |
| **appScreenshots** (4) | | | | | | **appScreenshots** (4) | | | | |
| `appScreenshots_createInstance` | POST | `/v1/appScreenshots` | write (dry by default) | apple/screenshots | | `appScreenshots_createInstance` | POST | `/v1/appScreenshots` | write (dry by default) | |
| `appScreenshots_deleteInstance` | DELETE | `/v1/appScreenshots/{id}` | owner only (typed confirm) | apple/screenshots | | `appScreenshots_deleteInstance` | DELETE | `/v1/appScreenshots/{id}` | owner only (typed confirm) | |
| `appScreenshots_getInstance` | GET | `/v1/appScreenshots/{id}` | read | | | `appScreenshots_getInstance` | GET | `/v1/appScreenshots/{id}` | read | |
| `appScreenshots_updateInstance` | PATCH | `/v1/appScreenshots/{id}` | write (dry by default) | apple/screenshots | | `appScreenshots_updateInstance` | PATCH | `/v1/appScreenshots/{id}` | write (dry by default) | |
| **appStoreReviewAttachments** (4) | | | | | | **appStoreReviewAttachments** (4) | | | | |
| `appStoreReviewAttachments_createInstance` | POST | `/v1/appStoreReviewAttachments` | write (dry by default) | | | `appStoreReviewAttachments_createInstance` | POST | `/v1/appStoreReviewAttachments` | write (dry by default) | |
| `appStoreReviewAttachments_deleteInstance` | DELETE | `/v1/appStoreReviewAttachments/{id}` | owner only (typed confirm) | | | `appStoreReviewAttachments_deleteInstance` | DELETE | `/v1/appStoreReviewAttachments/{id}` | owner only (typed confirm) | |
@ -319,14 +319,14 @@ Specs: Apple [App Store Connect API 4.5.1](https://developer.apple.com/sample-co
| `appStoreVersionExperimentTreatments_appStoreVersionExperimentTreatmentLocalizations_getToManyRelationship` | GET | `/v1/appStoreVersionExperimentTreatments/{id}/relationships/appStoreVersionExperimentTreatmentLocalizations` | read | | | `appStoreVersionExperimentTreatments_appStoreVersionExperimentTreatmentLocalizations_getToManyRelationship` | GET | `/v1/appStoreVersionExperimentTreatments/{id}/relationships/appStoreVersionExperimentTreatmentLocalizations` | read | |
| `appStoreVersionExperimentTreatments_createInstance` | POST | `/v1/appStoreVersionExperimentTreatments` | write (dry by default) | lib/ppo | | `appStoreVersionExperimentTreatments_createInstance` | POST | `/v1/appStoreVersionExperimentTreatments` | write (dry by default) | lib/ppo |
| `appStoreVersionExperimentTreatments_deleteInstance` | DELETE | `/v1/appStoreVersionExperimentTreatments/{id}` | owner only (typed confirm) | | | `appStoreVersionExperimentTreatments_deleteInstance` | DELETE | `/v1/appStoreVersionExperimentTreatments/{id}` | owner only (typed confirm) | |
| `appStoreVersionExperimentTreatments_getInstance` | GET | `/v1/appStoreVersionExperimentTreatments/{id}` | read | | | `appStoreVersionExperimentTreatments_getInstance` | GET | `/v1/appStoreVersionExperimentTreatments/{id}` | read | apple/ppo |
| `appStoreVersionExperimentTreatments_updateInstance` | PATCH | `/v1/appStoreVersionExperimentTreatments/{id}` | write (dry by default) | | | `appStoreVersionExperimentTreatments_updateInstance` | PATCH | `/v1/appStoreVersionExperimentTreatments/{id}` | write (dry by default) | |
| **appStoreVersionExperiments** (12) | | | | | | **appStoreVersionExperiments** (12) | | | | |
| `appStoreVersionExperimentsV2_appStoreVersionExperimentTreatments_getToManyRelated` | GET | `/v2/appStoreVersionExperiments/{id}/appStoreVersionExperimentTreatments` | read | apple/ppo | | `appStoreVersionExperimentsV2_appStoreVersionExperimentTreatments_getToManyRelated` | GET | `/v2/appStoreVersionExperiments/{id}/appStoreVersionExperimentTreatments` | read | apple/ppo |
| `appStoreVersionExperimentsV2_appStoreVersionExperimentTreatments_getToManyRelationship` | GET | `/v2/appStoreVersionExperiments/{id}/relationships/appStoreVersionExperimentTreatments` | read | | | `appStoreVersionExperimentsV2_appStoreVersionExperimentTreatments_getToManyRelationship` | GET | `/v2/appStoreVersionExperiments/{id}/relationships/appStoreVersionExperimentTreatments` | read | |
| `appStoreVersionExperimentsV2_createInstance` | POST | `/v2/appStoreVersionExperiments` | write (dry by default) | lib/ppo | | `appStoreVersionExperimentsV2_createInstance` | POST | `/v2/appStoreVersionExperiments` | write (dry by default) | lib/ppo |
| `appStoreVersionExperimentsV2_deleteInstance` | DELETE | `/v2/appStoreVersionExperiments/{id}` | owner only (typed confirm) | | | `appStoreVersionExperimentsV2_deleteInstance` | DELETE | `/v2/appStoreVersionExperiments/{id}` | owner only (typed confirm) | |
| `appStoreVersionExperimentsV2_getInstance` | GET | `/v2/appStoreVersionExperiments/{id}` | read | | | `appStoreVersionExperimentsV2_getInstance` | GET | `/v2/appStoreVersionExperiments/{id}` | read | apple/ppo |
| `appStoreVersionExperimentsV2_updateInstance` | PATCH | `/v2/appStoreVersionExperiments/{id}` | owner only (typed confirm) | lib/ppo | | `appStoreVersionExperimentsV2_updateInstance` | PATCH | `/v2/appStoreVersionExperiments/{id}` | owner only (typed confirm) | lib/ppo |
| `appStoreVersionExperiments_appStoreVersionExperimentTreatments_getToManyRelated` | GET | `/v1/appStoreVersionExperiments/{id}/appStoreVersionExperimentTreatments` | read | | | `appStoreVersionExperiments_appStoreVersionExperimentTreatments_getToManyRelated` | GET | `/v1/appStoreVersionExperiments/{id}/appStoreVersionExperimentTreatments` | read | |
| `appStoreVersionExperiments_appStoreVersionExperimentTreatments_getToManyRelationship` | GET | `/v1/appStoreVersionExperiments/{id}/relationships/appStoreVersionExperimentTreatments` | read | | | `appStoreVersionExperiments_appStoreVersionExperimentTreatments_getToManyRelationship` | GET | `/v1/appStoreVersionExperiments/{id}/relationships/appStoreVersionExperimentTreatments` | read | |
@ -386,7 +386,7 @@ Specs: Apple [App Store Connect API 4.5.1](https://developer.apple.com/sample-co
| `appStoreVersions_deleteInstance` | DELETE | `/v1/appStoreVersions/{id}` | owner only (typed confirm) | | | `appStoreVersions_deleteInstance` | DELETE | `/v1/appStoreVersions/{id}` | owner only (typed confirm) | |
| `appStoreVersions_gameCenterAppVersion_getToOneRelated` | GET | `/v1/appStoreVersions/{id}/gameCenterAppVersion` | read | | | `appStoreVersions_gameCenterAppVersion_getToOneRelated` | GET | `/v1/appStoreVersions/{id}/gameCenterAppVersion` | read | |
| `appStoreVersions_gameCenterAppVersion_getToOneRelationship` | GET | `/v1/appStoreVersions/{id}/relationships/gameCenterAppVersion` | read | | | `appStoreVersions_gameCenterAppVersion_getToOneRelationship` | GET | `/v1/appStoreVersions/{id}/relationships/gameCenterAppVersion` | read | |
| `appStoreVersions_getInstance` | GET | `/v1/appStoreVersions/{id}` | read | | | `appStoreVersions_getInstance` | GET | `/v1/appStoreVersions/{id}` | read | apple/listing |
| `appStoreVersions_routingAppCoverage_getToOneRelated` | GET | `/v1/appStoreVersions/{id}/routingAppCoverage` | read | | | `appStoreVersions_routingAppCoverage_getToOneRelated` | GET | `/v1/appStoreVersions/{id}/routingAppCoverage` | read | |
| `appStoreVersions_routingAppCoverage_getToOneRelationship` | GET | `/v1/appStoreVersions/{id}/relationships/routingAppCoverage` | read | | | `appStoreVersions_routingAppCoverage_getToOneRelationship` | GET | `/v1/appStoreVersions/{id}/relationships/routingAppCoverage` | read | |
| `appStoreVersions_updateInstance` | PATCH | `/v1/appStoreVersions/{id}` | write (dry by default) | apple/listing | | `appStoreVersions_updateInstance` | PATCH | `/v1/appStoreVersions/{id}` | write (dry by default) | apple/listing |
@ -443,7 +443,7 @@ Specs: Apple [App Store Connect API 4.5.1](https://developer.apple.com/sample-co
| `apps_betaLicenseAgreement_getToOneRelationship` | GET | `/v1/apps/{id}/relationships/betaLicenseAgreement` | read | | | `apps_betaLicenseAgreement_getToOneRelationship` | GET | `/v1/apps/{id}/relationships/betaLicenseAgreement` | read | |
| `apps_betaTesterUsages_getMetrics` | GET | `/v1/apps/{id}/metrics/betaTesterUsages` | read | | | `apps_betaTesterUsages_getMetrics` | GET | `/v1/apps/{id}/metrics/betaTesterUsages` | read | |
| `apps_betaTesters_deleteToManyRelationship` | DELETE | `/v1/apps/{id}/relationships/betaTesters` | owner only (typed confirm) | | | `apps_betaTesters_deleteToManyRelationship` | DELETE | `/v1/apps/{id}/relationships/betaTesters` | owner only (typed confirm) | |
| `apps_buildUploads_getToManyRelated` | GET | `/v1/apps/{id}/buildUploads` | read | | | `apps_buildUploads_getToManyRelated` | GET | `/v1/apps/{id}/buildUploads` | read | apple/upload |
| `apps_buildUploads_getToManyRelationship` | GET | `/v1/apps/{id}/relationships/buildUploads` | read | | | `apps_buildUploads_getToManyRelationship` | GET | `/v1/apps/{id}/relationships/buildUploads` | read | |
| `apps_builds_getToManyRelated` | GET | `/v1/apps/{id}/builds` | read | | | `apps_builds_getToManyRelated` | GET | `/v1/apps/{id}/builds` | read | |
| `apps_builds_getToManyRelationship` | GET | `/v1/apps/{id}/relationships/builds` | read | | | `apps_builds_getToManyRelationship` | GET | `/v1/apps/{id}/relationships/builds` | read | |
@ -571,7 +571,7 @@ Specs: Apple [App Store Connect API 4.5.1](https://developer.apple.com/sample-co
| `betaGroups_createInstance` | POST | `/v1/betaGroups` | write (dry by default) | lib/testflight | | `betaGroups_createInstance` | POST | `/v1/betaGroups` | write (dry by default) | lib/testflight |
| `betaGroups_deleteInstance` | DELETE | `/v1/betaGroups/{id}` | owner only (typed confirm) | | | `betaGroups_deleteInstance` | DELETE | `/v1/betaGroups/{id}` | owner only (typed confirm) | |
| `betaGroups_getCollection` | GET | `/v1/betaGroups` | read | apple/testflight | | `betaGroups_getCollection` | GET | `/v1/betaGroups` | read | apple/testflight |
| `betaGroups_getInstance` | GET | `/v1/betaGroups/{id}` | read | | | `betaGroups_getInstance` | GET | `/v1/betaGroups/{id}` | read | apple/testflight |
| `betaGroups_publicLinkUsages_getMetrics` | GET | `/v1/betaGroups/{id}/metrics/publicLinkUsages` | read | | | `betaGroups_publicLinkUsages_getMetrics` | GET | `/v1/betaGroups/{id}/metrics/publicLinkUsages` | read | |
| `betaGroups_updateInstance` | PATCH | `/v1/betaGroups/{id}` | write (dry by default) | | | `betaGroups_updateInstance` | PATCH | `/v1/betaGroups/{id}` | write (dry by default) | |
| **betaLicenseAgreements** (5) | | | | | | **betaLicenseAgreements** (5) | | | | |
@ -627,7 +627,7 @@ Specs: Apple [App Store Connect API 4.5.1](https://developer.apple.com/sample-co
| `buildUploadFiles_getInstance` | GET | `/v1/buildUploadFiles/{id}` | read | | | `buildUploadFiles_getInstance` | GET | `/v1/buildUploadFiles/{id}` | read | |
| `buildUploadFiles_updateInstance` | PATCH | `/v1/buildUploadFiles/{id}` | write (dry by default) | lib/xcode | | `buildUploadFiles_updateInstance` | PATCH | `/v1/buildUploadFiles/{id}` | write (dry by default) | lib/xcode |
| **buildUploads** (5) | | | | | | **buildUploads** (5) | | | | |
| `buildUploads_buildUploadFiles_getToManyRelated` | GET | `/v1/buildUploads/{id}/buildUploadFiles` | read | | | `buildUploads_buildUploadFiles_getToManyRelated` | GET | `/v1/buildUploads/{id}/buildUploadFiles` | read | apple/upload |
| `buildUploads_buildUploadFiles_getToManyRelationship` | GET | `/v1/buildUploads/{id}/relationships/buildUploadFiles` | read | | | `buildUploads_buildUploadFiles_getToManyRelationship` | GET | `/v1/buildUploads/{id}/relationships/buildUploadFiles` | read | |
| `buildUploads_createInstance` | POST | `/v1/buildUploads` | write (dry by default) | lib/xcode | | `buildUploads_createInstance` | POST | `/v1/buildUploads` | write (dry by default) | lib/xcode |
| `buildUploads_deleteInstance` | DELETE | `/v1/buildUploads/{id}` | owner only (typed confirm) | | | `buildUploads_deleteInstance` | DELETE | `/v1/buildUploads/{id}` | owner only (typed confirm) | |
@ -1194,7 +1194,7 @@ Specs: Apple [App Store Connect API 4.5.1](https://developer.apple.com/sample-co
| `inAppPurchaseOfferCodes_createInstance` | POST | `/v1/inAppPurchaseOfferCodes` | write (dry by default) | apple/offer-codes | | `inAppPurchaseOfferCodes_createInstance` | POST | `/v1/inAppPurchaseOfferCodes` | write (dry by default) | apple/offer-codes |
| `inAppPurchaseOfferCodes_customCodes_getToManyRelated` | GET | `/v1/inAppPurchaseOfferCodes/{id}/customCodes` | read | apple/offer-codes | | `inAppPurchaseOfferCodes_customCodes_getToManyRelated` | GET | `/v1/inAppPurchaseOfferCodes/{id}/customCodes` | read | apple/offer-codes |
| `inAppPurchaseOfferCodes_customCodes_getToManyRelationship` | GET | `/v1/inAppPurchaseOfferCodes/{id}/relationships/customCodes` | read | | | `inAppPurchaseOfferCodes_customCodes_getToManyRelationship` | GET | `/v1/inAppPurchaseOfferCodes/{id}/relationships/customCodes` | read | |
| `inAppPurchaseOfferCodes_getInstance` | GET | `/v1/inAppPurchaseOfferCodes/{id}` | read | | | `inAppPurchaseOfferCodes_getInstance` | GET | `/v1/inAppPurchaseOfferCodes/{id}` | read | apple/offer-codes |
| `inAppPurchaseOfferCodes_oneTimeUseCodes_getToManyRelated` | GET | `/v1/inAppPurchaseOfferCodes/{id}/oneTimeUseCodes` | read | apple/offer-codes | | `inAppPurchaseOfferCodes_oneTimeUseCodes_getToManyRelated` | GET | `/v1/inAppPurchaseOfferCodes/{id}/oneTimeUseCodes` | read | apple/offer-codes |
| `inAppPurchaseOfferCodes_oneTimeUseCodes_getToManyRelationship` | GET | `/v1/inAppPurchaseOfferCodes/{id}/relationships/oneTimeUseCodes` | read | | | `inAppPurchaseOfferCodes_oneTimeUseCodes_getToManyRelationship` | GET | `/v1/inAppPurchaseOfferCodes/{id}/relationships/oneTimeUseCodes` | read | |
| `inAppPurchaseOfferCodes_prices_getToManyRelated` | GET | `/v1/inAppPurchaseOfferCodes/{id}/prices` | read | | | `inAppPurchaseOfferCodes_prices_getToManyRelated` | GET | `/v1/inAppPurchaseOfferCodes/{id}/prices` | read | |
@ -1230,7 +1230,7 @@ Specs: Apple [App Store Connect API 4.5.1](https://developer.apple.com/sample-co
| `inAppPurchasesV2_content_getToOneRelationship` | GET | `/v2/inAppPurchases/{id}/relationships/content` | read | | | `inAppPurchasesV2_content_getToOneRelationship` | GET | `/v2/inAppPurchases/{id}/relationships/content` | read | |
| `inAppPurchasesV2_createInstance` | POST | `/v2/inAppPurchases` | write (dry by default) | apple/iap | | `inAppPurchasesV2_createInstance` | POST | `/v2/inAppPurchases` | write (dry by default) | apple/iap |
| `inAppPurchasesV2_deleteInstance` | DELETE | `/v2/inAppPurchases/{id}` | owner only (typed confirm) | | | `inAppPurchasesV2_deleteInstance` | DELETE | `/v2/inAppPurchases/{id}` | owner only (typed confirm) | |
| `inAppPurchasesV2_getInstance` | GET | `/v2/inAppPurchases/{id}` | read | | | `inAppPurchasesV2_getInstance` | GET | `/v2/inAppPurchases/{id}` | read | apple/iap |
| `inAppPurchasesV2_iapPriceSchedule_getToOneRelated` | GET | `/v2/inAppPurchases/{id}/iapPriceSchedule` | read | apple/iap, apple/iap-review, catalog | | `inAppPurchasesV2_iapPriceSchedule_getToOneRelated` | GET | `/v2/inAppPurchases/{id}/iapPriceSchedule` | read | apple/iap, apple/iap-review, catalog |
| `inAppPurchasesV2_iapPriceSchedule_getToOneRelationship` | GET | `/v2/inAppPurchases/{id}/relationships/iapPriceSchedule` | read | | | `inAppPurchasesV2_iapPriceSchedule_getToOneRelationship` | GET | `/v2/inAppPurchases/{id}/relationships/iapPriceSchedule` | read | |
| `inAppPurchasesV2_images_getToManyRelated` | GET | `/v2/inAppPurchases/{id}/images` | read | | | `inAppPurchasesV2_images_getToManyRelated` | GET | `/v2/inAppPurchases/{id}/images` | read | |
@ -1304,14 +1304,14 @@ Specs: Apple [App Store Connect API 4.5.1](https://developer.apple.com/sample-co
| `promotedPurchases_getInstance` | GET | `/v1/promotedPurchases/{id}` | read | | | `promotedPurchases_getInstance` | GET | `/v1/promotedPurchases/{id}` | read | |
| `promotedPurchases_updateInstance` | PATCH | `/v1/promotedPurchases/{id}` | write (dry by default) | lib/offers | | `promotedPurchases_updateInstance` | PATCH | `/v1/promotedPurchases/{id}` | write (dry by default) | lib/offers |
| **reviewSubmissionItems** (3) | | | | | | **reviewSubmissionItems** (3) | | | | |
| `reviewSubmissionItems_createInstance` | POST | `/v1/reviewSubmissionItems` | write (dry by default) | apple/events, lib/cpp-events, lib/review-submit | | `reviewSubmissionItems_createInstance` | POST | `/v1/reviewSubmissionItems` | write (dry by default) | apple/submission, lib/cpp-events, lib/review-submit |
| `reviewSubmissionItems_deleteInstance` | DELETE | `/v1/reviewSubmissionItems/{id}` | owner only (typed confirm) | | | `reviewSubmissionItems_deleteInstance` | DELETE | `/v1/reviewSubmissionItems/{id}` | owner only (typed confirm) | |
| `reviewSubmissionItems_updateInstance` | PATCH | `/v1/reviewSubmissionItems/{id}` | write (dry by default) | | | `reviewSubmissionItems_updateInstance` | PATCH | `/v1/reviewSubmissionItems/{id}` | write (dry by default) | |
| **reviewSubmissions** (6) | | | | | | **reviewSubmissions** (6) | | | | |
| `reviewSubmissions_createInstance` | POST | `/v1/reviewSubmissions` | owner only (typed confirm) | apple/cpp, apple/events, lib/review-submit | | `reviewSubmissions_createInstance` | POST | `/v1/reviewSubmissions` | owner only (typed confirm) | apple/cpp, apple/events, apple/submission, lib/review-submit |
| `reviewSubmissions_getCollection` | GET | `/v1/reviewSubmissions` | read | apple/cpp, apple/events, apple/iap-submit, apple/status | | `reviewSubmissions_getCollection` | GET | `/v1/reviewSubmissions` | read | apple/events, apple/iap-submit, apple/status, apple/submission |
| `reviewSubmissions_getInstance` | GET | `/v1/reviewSubmissions/{id}` | read | | | `reviewSubmissions_getInstance` | GET | `/v1/reviewSubmissions/{id}` | read | apple/submission |
| `reviewSubmissions_items_getToManyRelated` | GET | `/v1/reviewSubmissions/{id}/items` | read | apple/iap-submit | | `reviewSubmissions_items_getToManyRelated` | GET | `/v1/reviewSubmissions/{id}/items` | read | apple/iap-submit, apple/submission |
| `reviewSubmissions_items_getToManyRelationship` | GET | `/v1/reviewSubmissions/{id}/relationships/items` | read | | | `reviewSubmissions_items_getToManyRelationship` | GET | `/v1/reviewSubmissions/{id}/relationships/items` | read | |
| `reviewSubmissions_updateInstance` | PATCH | `/v1/reviewSubmissions/{id}` | owner only (typed confirm) | apple/cpp, apple/events, apple/iap-submit, apple/status, lib/review-submit | | `reviewSubmissions_updateInstance` | PATCH | `/v1/reviewSubmissions/{id}` | owner only (typed confirm) | apple/cpp, apple/events, apple/iap-submit, apple/status, lib/review-submit |
| **routingAppCoverages** (4) | | | | | | **routingAppCoverages** (4) | | | | |
@ -1344,7 +1344,7 @@ Specs: Apple [App Store Connect API 4.5.1](https://developer.apple.com/sample-co
| `scmRepositories_pullRequests_getToManyRelationship` | GET | `/v1/scmRepositories/{id}/relationships/pullRequests` | read | | | `scmRepositories_pullRequests_getToManyRelationship` | GET | `/v1/scmRepositories/{id}/relationships/pullRequests` | read | |
| **subscriptionAppStoreReviewScreenshots** (4) | | | | | | **subscriptionAppStoreReviewScreenshots** (4) | | | | |
| `subscriptionAppStoreReviewScreenshots_createInstance` | POST | `/v1/subscriptionAppStoreReviewScreenshots` | write (dry by default) | apple/subscriptions | | `subscriptionAppStoreReviewScreenshots_createInstance` | POST | `/v1/subscriptionAppStoreReviewScreenshots` | write (dry by default) | apple/subscriptions |
| `subscriptionAppStoreReviewScreenshots_deleteInstance` | DELETE | `/v1/subscriptionAppStoreReviewScreenshots/{id}` | owner only (typed confirm) | | | `subscriptionAppStoreReviewScreenshots_deleteInstance` | DELETE | `/v1/subscriptionAppStoreReviewScreenshots/{id}` | owner only (typed confirm) | apple/subscriptions |
| `subscriptionAppStoreReviewScreenshots_getInstance` | GET | `/v1/subscriptionAppStoreReviewScreenshots/{id}` | read | | | `subscriptionAppStoreReviewScreenshots_getInstance` | GET | `/v1/subscriptionAppStoreReviewScreenshots/{id}` | read | |
| `subscriptionAppStoreReviewScreenshots_updateInstance` | PATCH | `/v1/subscriptionAppStoreReviewScreenshots/{id}` | write (dry by default) | apple/subscriptions | | `subscriptionAppStoreReviewScreenshots_updateInstance` | PATCH | `/v1/subscriptionAppStoreReviewScreenshots/{id}` | write (dry by default) | apple/subscriptions |
| **subscriptionAvailabilities** (4) | | | | | | **subscriptionAvailabilities** (4) | | | | |
@ -1374,7 +1374,7 @@ Specs: Apple [App Store Connect API 4.5.1](https://developer.apple.com/sample-co
| **subscriptionGroups** (10) | | | | | | **subscriptionGroups** (10) | | | | |
| `subscriptionGroups_createInstance` | POST | `/v1/subscriptionGroups` | write (dry by default) | apple/subscriptions | | `subscriptionGroups_createInstance` | POST | `/v1/subscriptionGroups` | write (dry by default) | apple/subscriptions |
| `subscriptionGroups_deleteInstance` | DELETE | `/v1/subscriptionGroups/{id}` | owner only (typed confirm) | | | `subscriptionGroups_deleteInstance` | DELETE | `/v1/subscriptionGroups/{id}` | owner only (typed confirm) | |
| `subscriptionGroups_getInstance` | GET | `/v1/subscriptionGroups/{id}` | read | | | `subscriptionGroups_getInstance` | GET | `/v1/subscriptionGroups/{id}` | read | apple/subscriptions |
| `subscriptionGroups_subscriptionGroupLocalizations_getToManyRelated` | GET | `/v1/subscriptionGroups/{id}/subscriptionGroupLocalizations` | read | apple/subscriptions, validate | | `subscriptionGroups_subscriptionGroupLocalizations_getToManyRelated` | GET | `/v1/subscriptionGroups/{id}/subscriptionGroupLocalizations` | read | apple/subscriptions, validate |
| `subscriptionGroups_subscriptionGroupLocalizations_getToManyRelationship` | GET | `/v1/subscriptionGroups/{id}/relationships/subscriptionGroupLocalizations` | read | | | `subscriptionGroups_subscriptionGroupLocalizations_getToManyRelationship` | GET | `/v1/subscriptionGroups/{id}/relationships/subscriptionGroupLocalizations` | read | |
| `subscriptionGroups_subscriptions_getToManyRelated` | GET | `/v1/subscriptionGroups/{id}/subscriptions` | read | apple/iap-submit, apple/promoted, apple/subscriptions, apple/win-back, catalog, prices, validate | | `subscriptionGroups_subscriptions_getToManyRelated` | GET | `/v1/subscriptionGroups/{id}/subscriptions` | read | apple/iap-submit, apple/promoted, apple/subscriptions, apple/win-back, catalog, prices, validate |
@ -1417,7 +1417,7 @@ Specs: Apple [App Store Connect API 4.5.1](https://developer.apple.com/sample-co
| `subscriptionOfferCodes_createInstance` | POST | `/v1/subscriptionOfferCodes` | write (dry by default) | apple/subscriptions | | `subscriptionOfferCodes_createInstance` | POST | `/v1/subscriptionOfferCodes` | write (dry by default) | apple/subscriptions |
| `subscriptionOfferCodes_customCodes_getToManyRelated` | GET | `/v1/subscriptionOfferCodes/{id}/customCodes` | read | apple/subscriptions | | `subscriptionOfferCodes_customCodes_getToManyRelated` | GET | `/v1/subscriptionOfferCodes/{id}/customCodes` | read | apple/subscriptions |
| `subscriptionOfferCodes_customCodes_getToManyRelationship` | GET | `/v1/subscriptionOfferCodes/{id}/relationships/customCodes` | read | | | `subscriptionOfferCodes_customCodes_getToManyRelationship` | GET | `/v1/subscriptionOfferCodes/{id}/relationships/customCodes` | read | |
| `subscriptionOfferCodes_getInstance` | GET | `/v1/subscriptionOfferCodes/{id}` | read | | | `subscriptionOfferCodes_getInstance` | GET | `/v1/subscriptionOfferCodes/{id}` | read | apple/subscriptions |
| `subscriptionOfferCodes_oneTimeUseCodes_getToManyRelated` | GET | `/v1/subscriptionOfferCodes/{id}/oneTimeUseCodes` | read | | | `subscriptionOfferCodes_oneTimeUseCodes_getToManyRelated` | GET | `/v1/subscriptionOfferCodes/{id}/oneTimeUseCodes` | read | |
| `subscriptionOfferCodes_oneTimeUseCodes_getToManyRelationship` | GET | `/v1/subscriptionOfferCodes/{id}/relationships/oneTimeUseCodes` | read | | | `subscriptionOfferCodes_oneTimeUseCodes_getToManyRelationship` | GET | `/v1/subscriptionOfferCodes/{id}/relationships/oneTimeUseCodes` | read | |
| `subscriptionOfferCodes_prices_getToManyRelated` | GET | `/v1/subscriptionOfferCodes/{id}/prices` | read | | | `subscriptionOfferCodes_prices_getToManyRelated` | GET | `/v1/subscriptionOfferCodes/{id}/prices` | read | |
@ -1461,7 +1461,7 @@ Specs: Apple [App Store Connect API 4.5.1](https://developer.apple.com/sample-co
| `subscriptions_appStoreReviewScreenshot_getToOneRelationship` | GET | `/v1/subscriptions/{id}/relationships/appStoreReviewScreenshot` | read | | | `subscriptions_appStoreReviewScreenshot_getToOneRelationship` | GET | `/v1/subscriptions/{id}/relationships/appStoreReviewScreenshot` | read | |
| `subscriptions_createInstance` | POST | `/v1/subscriptions` | write (dry by default) | apple/subscriptions | | `subscriptions_createInstance` | POST | `/v1/subscriptions` | write (dry by default) | apple/subscriptions |
| `subscriptions_deleteInstance` | DELETE | `/v1/subscriptions/{id}` | owner only (typed confirm) | | | `subscriptions_deleteInstance` | DELETE | `/v1/subscriptions/{id}` | owner only (typed confirm) | |
| `subscriptions_getInstance` | GET | `/v1/subscriptions/{id}` | read | | | `subscriptions_getInstance` | GET | `/v1/subscriptions/{id}` | read | apple/subscriptions |
| `subscriptions_images_getToManyRelated` | GET | `/v1/subscriptions/{id}/images` | read | | | `subscriptions_images_getToManyRelated` | GET | `/v1/subscriptions/{id}/images` | read | |
| `subscriptions_images_getToManyRelationship` | GET | `/v1/subscriptions/{id}/relationships/images` | read | | | `subscriptions_images_getToManyRelationship` | GET | `/v1/subscriptions/{id}/relationships/images` | read | |
| `subscriptions_introductoryOffers_deleteToManyRelationship` | DELETE | `/v1/subscriptions/{id}/relationships/introductoryOffers` | owner only (typed confirm) | | | `subscriptions_introductoryOffers_deleteToManyRelationship` | DELETE | `/v1/subscriptions/{id}/relationships/introductoryOffers` | owner only (typed confirm) | |
@ -1607,7 +1607,7 @@ Specs: Apple [App Store Connect API 4.5.1](https://developer.apple.com/sample-co
| `androidpublisher.edits.testers.update` | PUT | `androidpublisher/v3/applications/{packageName}/edits/{editId}/testers/{track}` | owner only (typed confirm) | | | `androidpublisher.edits.testers.update` | PUT | `androidpublisher/v3/applications/{packageName}/edits/{editId}/testers/{track}` | owner only (typed confirm) | |
| **androidpublisher.edits.tracks** (5) | | | | | | **androidpublisher.edits.tracks** (5) | | | | |
| `androidpublisher.edits.tracks.create` | POST | `androidpublisher/v3/applications/{packageName}/edits/{editId}/tracks` | write (dry by default) | | | `androidpublisher.edits.tracks.create` | POST | `androidpublisher/v3/applications/{packageName}/edits/{editId}/tracks` | write (dry by default) | |
| `androidpublisher.edits.tracks.get` | GET | `androidpublisher/v3/applications/{packageName}/edits/{editId}/tracks/{track}` | read | play/rollout, play/upload | | `androidpublisher.edits.tracks.get` | GET | `androidpublisher/v3/applications/{packageName}/edits/{editId}/tracks/{track}` | read | play/release, play/rollout, play/upload |
| `androidpublisher.edits.tracks.list` | GET | `androidpublisher/v3/applications/{packageName}/edits/{editId}/tracks` | read | play/next-version-code, play/status, validate | | `androidpublisher.edits.tracks.list` | GET | `androidpublisher/v3/applications/{packageName}/edits/{editId}/tracks` | read | play/next-version-code, play/status, validate |
| `androidpublisher.edits.tracks.patch` | PATCH | `androidpublisher/v3/applications/{packageName}/edits/{editId}/tracks/{track}` | write (dry by default) | | | `androidpublisher.edits.tracks.patch` | PATCH | `androidpublisher/v3/applications/{packageName}/edits/{editId}/tracks/{track}` | write (dry by default) | |
| `androidpublisher.edits.tracks.update` | PUT | `androidpublisher/v3/applications/{packageName}/edits/{editId}/tracks/{track}` | write (dry by default) | play/release, play/rollout, play/upload | | `androidpublisher.edits.tracks.update` | PUT | `androidpublisher/v3/applications/{packageName}/edits/{editId}/tracks/{track}` | write (dry by default) | play/release, play/rollout, play/upload |
@ -1663,7 +1663,7 @@ Specs: Apple [App Store Connect API 4.5.1](https://developer.apple.com/sample-co
| `androidpublisher.monetization.subscriptions.batchUpdate` | POST | `androidpublisher/v3/applications/{packageName}/subscriptions:batchUpdate` | write (dry by default) | | | `androidpublisher.monetization.subscriptions.batchUpdate` | POST | `androidpublisher/v3/applications/{packageName}/subscriptions:batchUpdate` | write (dry by default) | |
| `androidpublisher.monetization.subscriptions.create` | POST | `androidpublisher/v3/applications/{packageName}/subscriptions` | write (dry by default) | play/subscriptions | | `androidpublisher.monetization.subscriptions.create` | POST | `androidpublisher/v3/applications/{packageName}/subscriptions` | write (dry by default) | play/subscriptions |
| `androidpublisher.monetization.subscriptions.delete` | DELETE | `androidpublisher/v3/applications/{packageName}/subscriptions/{productId}` | owner only (typed confirm) | | | `androidpublisher.monetization.subscriptions.delete` | DELETE | `androidpublisher/v3/applications/{packageName}/subscriptions/{productId}` | owner only (typed confirm) | |
| `androidpublisher.monetization.subscriptions.get` | GET | `androidpublisher/v3/applications/{packageName}/subscriptions/{productId}` | read | prices | | `androidpublisher.monetization.subscriptions.get` | GET | `androidpublisher/v3/applications/{packageName}/subscriptions/{productId}` | read | play/subscriptions, prices |
| `androidpublisher.monetization.subscriptions.list` | GET | `androidpublisher/v3/applications/{packageName}/subscriptions` | read | catalog, play/subscriptions, validate | | `androidpublisher.monetization.subscriptions.list` | GET | `androidpublisher/v3/applications/{packageName}/subscriptions` | read | catalog, play/subscriptions, validate |
| `androidpublisher.monetization.subscriptions.patch` | PATCH | `androidpublisher/v3/applications/{packageName}/subscriptions/{productId}` | write (dry by default) | play/subscriptions | | `androidpublisher.monetization.subscriptions.patch` | PATCH | `androidpublisher/v3/applications/{packageName}/subscriptions/{productId}` | write (dry by default) | play/subscriptions |
| **androidpublisher.monetization.subscriptions.basePlans** (6) | | | | | | **androidpublisher.monetization.subscriptions.basePlans** (6) | | | | |
@ -1674,11 +1674,11 @@ Specs: Apple [App Store Connect API 4.5.1](https://developer.apple.com/sample-co
| `androidpublisher.monetization.subscriptions.basePlans.delete` | DELETE | `androidpublisher/v3/applications/{packageName}/subscriptions/{productId}/basePlans/{basePlanId}` | owner only (typed confirm) | | | `androidpublisher.monetization.subscriptions.basePlans.delete` | DELETE | `androidpublisher/v3/applications/{packageName}/subscriptions/{productId}/basePlans/{basePlanId}` | owner only (typed confirm) | |
| `androidpublisher.monetization.subscriptions.basePlans.migratePrices` | POST | `androidpublisher/v3/applications/{packageName}/subscriptions/{productId}/basePlans/{basePlanId}:migratePrices` | owner only (typed confirm) | | | `androidpublisher.monetization.subscriptions.basePlans.migratePrices` | POST | `androidpublisher/v3/applications/{packageName}/subscriptions/{productId}/basePlans/{basePlanId}:migratePrices` | owner only (typed confirm) | |
| **androidpublisher.monetization.subscriptions.basePlans.offers** (10) | | | | | | **androidpublisher.monetization.subscriptions.basePlans.offers** (10) | | | | |
| `androidpublisher.monetization.subscriptions.basePlans.offers.activate` | POST | `androidpublisher/v3/applications/{packageName}/subscriptions/{productId}/basePlans/{basePlanId}/offers/{offerId}:activate` | owner only (typed confirm) | play/subscriptions | | `androidpublisher.monetization.subscriptions.basePlans.offers.activate` | POST | `androidpublisher/v3/applications/{packageName}/subscriptions/{productId}/basePlans/{basePlanId}/offers/{offerId}:activate` | owner only (typed confirm) | |
| `androidpublisher.monetization.subscriptions.basePlans.offers.batchGet` | POST | `androidpublisher/v3/applications/{packageName}/subscriptions/{productId}/basePlans/{basePlanId}/offers:batchGet` | read | | | `androidpublisher.monetization.subscriptions.basePlans.offers.batchGet` | POST | `androidpublisher/v3/applications/{packageName}/subscriptions/{productId}/basePlans/{basePlanId}/offers:batchGet` | read | |
| `androidpublisher.monetization.subscriptions.basePlans.offers.batchUpdate` | POST | `androidpublisher/v3/applications/{packageName}/subscriptions/{productId}/basePlans/{basePlanId}/offers:batchUpdate` | write (dry by default) | | | `androidpublisher.monetization.subscriptions.basePlans.offers.batchUpdate` | POST | `androidpublisher/v3/applications/{packageName}/subscriptions/{productId}/basePlans/{basePlanId}/offers:batchUpdate` | write (dry by default) | |
| `androidpublisher.monetization.subscriptions.basePlans.offers.batchUpdateStates` | POST | `androidpublisher/v3/applications/{packageName}/subscriptions/{productId}/basePlans/{basePlanId}/offers:batchUpdateStates` | owner only (typed confirm) | | | `androidpublisher.monetization.subscriptions.basePlans.offers.batchUpdateStates` | POST | `androidpublisher/v3/applications/{packageName}/subscriptions/{productId}/basePlans/{basePlanId}/offers:batchUpdateStates` | owner only (typed confirm) | |
| `androidpublisher.monetization.subscriptions.basePlans.offers.create` | POST | `androidpublisher/v3/applications/{packageName}/subscriptions/{productId}/basePlans/{basePlanId}/offers` | write (dry by default) | play/subscriptions | | `androidpublisher.monetization.subscriptions.basePlans.offers.create` | POST | `androidpublisher/v3/applications/{packageName}/subscriptions/{productId}/basePlans/{basePlanId}/offers` | write (dry by default) | |
| `androidpublisher.monetization.subscriptions.basePlans.offers.deactivate` | POST | `androidpublisher/v3/applications/{packageName}/subscriptions/{productId}/basePlans/{basePlanId}/offers/{offerId}:deactivate` | owner only (typed confirm) | | | `androidpublisher.monetization.subscriptions.basePlans.offers.deactivate` | POST | `androidpublisher/v3/applications/{packageName}/subscriptions/{productId}/basePlans/{basePlanId}/offers/{offerId}:deactivate` | owner only (typed confirm) | |
| `androidpublisher.monetization.subscriptions.basePlans.offers.delete` | DELETE | `androidpublisher/v3/applications/{packageName}/subscriptions/{productId}/basePlans/{basePlanId}/offers/{offerId}` | owner only (typed confirm) | | | `androidpublisher.monetization.subscriptions.basePlans.offers.delete` | DELETE | `androidpublisher/v3/applications/{packageName}/subscriptions/{productId}/basePlans/{basePlanId}/offers/{offerId}` | owner only (typed confirm) | |
| `androidpublisher.monetization.subscriptions.basePlans.offers.get` | GET | `androidpublisher/v3/applications/{packageName}/subscriptions/{productId}/basePlans/{basePlanId}/offers/{offerId}` | read | | | `androidpublisher.monetization.subscriptions.basePlans.offers.get` | GET | `androidpublisher/v3/applications/{packageName}/subscriptions/{productId}/basePlans/{basePlanId}/offers/{offerId}` | read | |

110
docs/idempotency.md Normal file
View file

@ -0,0 +1,110 @@
# Idempotency: every write converges
storebound must never create duplicates or leave a live store half changed. Every write command is an upsert: run it
twice, or again after a crash or a lost answer, and the store holds exactly one of each thing. This page lists every
write path, the key it matches on, and what a second run does.
## The rules (all write paths)
1. **Read first, by a stable key.** Product id, locale, display type + position (MD5), version string, name, UDID,
email. What matches is left alone or updated; only what is missing is created.
2. **Look again right before a create** (`findOrCreate` in `cli/lib/upsert.ts`): a fresh GET, then the journal of
recent creates in `.storebound/created.json` (one day). The stores' lists lag for a minute after a create; a direct
GET by the journaled id does not, so a re-run during the lag reuses what the last run made.
3. **409 / ALREADY_EXISTS means found** (`createOnce`): the existing one is looked up and used, the create is never
sent again. Apple also answers 409 for plain validation errors; those are not treated as found (`isConflict`).
4. **An unclear answer is never retried blindly.** A POST that times out or gets a 5xx may have been applied. The API
clients throw `AmbiguousWriteError` instead of retrying it; `createOnce` then looks it up. Only when the store
refuses duplicates of that key (unique) is one more create safe (its 409 finds the first). Otherwise the run stops
with "run it again in a minute": the next run reads the store first.
5. **Retries (429, 5xx, network) only for requests that end the same when sent twice** (`retrySafe`): GET, PUT, PATCH,
DELETE, relationship adds, Google state actions (`:activate`, `:batchUpdateStates`, `:validate`), the App Store
"create or replace" singletons (price schedules, availability). A DELETE that answers 404 after a retry counts as
done. `storebound api` follows the same rule.
6. **Play edits are all or nothing.** One edit open at a time per run (a second is refused); every error path deletes
the edit; commit only on success; nothing to change = the edit is deleted, not committed. An edit is redone only
when nothing of it went live (invalidated by a Console save, or a request inside it had no clear answer). A commit
without a clear answer is never redone: the run stops and says to check first.
7. **Asset sets keep what matches.** Screenshots and previews: the leading files that already match (MD5, same
position, delivered) stay, the rest is deleted and uploaded in order, the order is read back and fixed. A crash
midway leaves a prefix; the re-run keeps it and finishes. A reservation without a clear answer is cleaned up (the
set is re-read, unknown assets deleted) before the file is sent again.
8. **One writer per app and store.** Write runs take `.storebound/locks/<store>-<app>.lock` (dry runs and reads do
not). A second run stops with who holds the lock; a lock of a process that is gone is taken over; `--force-unlock`
removes one left by a crash on another machine (also stale after 12 hours).
Tests: `tests/idempotency.test.ts` runs the write commands twice against stateful store doubles
(`tests/helpers/fake-stores.ts`) and asserts the second run makes zero writes, plus the crash and lost-answer cases.
## Commands
Status: **ok** = converges, second run writes nothing (tested twice where marked T; the rows without T are covered
by the shared helpers, see "Not run twice in the tests" below). **owner** = a public or irreversible step that asks
first. No write path sends the same values again on a second run.
| command | key | on re-run | status |
|---|---|---|---|
| `apple bundle` | bundle identifier | found by filter, 409 = found | ok T |
| `apple iap` | product id; locale; one schedule / availability per product | product: findOrCreate (journal for list lag), unique (lost answer: look, then one retry); localizations: createOnce; schedule and availability only when missing | ok T (twice, lost answer before / after, 409, crash + lag) |
| `apple iap-review` | one review screenshot per product (MD5 + not FAILED) | kept when the bytes match, else deleted and uploaded | ok |
| `apple subscriptions` | group reference name; product id; locale; promo offer code; campaign name; custom code | groups and subscriptions: findOrCreate + journal; localizations / offers / codes: createOnce; prices compared per territory, only differences sent; a subscription found late is read live (prices, intro) | ok T (twice, lost answer) |
| `apple offer-codes` | campaign name per product; custom code; one-time batches by count | campaign: findOrCreate + journal (names not known unique: no blind retry); codes: 409 = exists | ok T |
| `apple win-back` | offer id per subscription | only missing offers; 409 = exists | ok T (owner: asks) |
| `apple promoted` | product per promoted purchase | plan from the store; order PATCH only when it differs | ok T |
| `apple preorder` | territory | plan from the store; a territory already on pre-order with that date is left | ok T (owner) |
| `apple cpp create` | page name; locale | page: findOrCreate + journal; locales from a fresh read, 409 = exists | ok T |
| `apple cpp upload` / `apple ppo upload` / `apple screenshots` | display type; position + MD5 | set: findOrCreate (unique, 409 = found); screenshots: matching prefix kept, rest replaced in order | ok T (twice, crash on the 3rd file) |
| `apple cpp submit` / `apple events submit` / `apple iap-submit` | the app's draft submission; version id per item | draft reused or created once (journal); an item in the draft is not added again | ok T (iap-submit) (owner: submit asks) |
| `apple events create` | reference name; locale | event: findOrCreate + journal; localizations planned from a fresh read | ok T |
| `apple events upload` | asset type per locale (size + delivered) | kept when the same size is delivered, else replaced | ok |
| `apple ppo create` | test name (open tests only); treatment name; locale | findOrCreate + journal for test and treatments | ok T |
| `apple previews` | preview type; position + MD5; poster time code | like screenshots; poster PATCH only where it differs | ok |
| `apple testflight groups` | group name | findOrCreate + journal, 409 = found | ok T |
| `apple testflight testers` | email (masked) per group | only missing testers / memberships; 409 = exists | ok T (owner: asks) |
| `apple testflight what-to-test` / `info` | locale; fields | POST missing locales, PATCH changed fields only | ok |
| `apple testflight distribute` | group per build | relationship adds (same when sent twice); beta review once per build (409 = exists) | ok (owner) |
| `apple testflight expire` | build | only unexpired builds planned | ok (owner) |
| `apple review-details` | one detail per version | only changed fields PATCHed; create once (409 = found) | ok T |
| `apple release --phased` | one phased release per version | NONE when already in that state; create once | ok T |
| `apple encryption` | build without an answer | only unanswered builds | ok T |
| `apple adhoc register` | UDID | only unregistered; 409 / lost answer: direct lookup by UDID | ok T |
| `apple adhoc profile` | profile name (+ dated names of earlier runs) | a current profile (also a dated one) ends the run; a new name never collides | ok |
| `apple signing ensure` | certificate with a local key (SPKI); profile with that certificate | a lost certificate answer keeps the pending key and finds the certificate by its public key (now or on the next run); profiles: createOnce, re-read | ok |
| `apple upload` | build upload per version + build number | a COMPLETE upload is not sent again (only the wait); an unfinished one is resumed; reservations createOnce | ok |
| `apple listing` | version string (editable, iOS); locale; build per version | plan from the store; version: findOrCreate (fresh GET right before the create, journal, unique: a lost answer looks first); localizations: createOnce (409 / lost answer = found, then only differing fields PATCHed); unchanged localizations skipped; the build is not re-attached when it is attached | ok T (twice, changed field, lost answer, 409) |
| `apple appinfo` | categories, age rating, rights, price, availability | each compared with the live value, only what differs is PATCHed; price schedule and availability only when missing (an existing price is never changed here: `prices import`) | ok T (twice, one changed answer) |
| `analytics setup` | the ONGOING request | exists = done; createOnce | ok |
| `reviews send` | review id (local `sent:` mark) | Apple 409 (already answered) = noted as sent; Play `:reply` replaces | ok (owner) |
| `play products` | product id | PATCH `allowMissing` (create or update) only when it differs; activation only when not ACTIVE | ok T |
| `play subscriptions` | product id; base plan id; offer id | create: 409 = found (createOnce); PATCH only differences; activation only when needed | ok T (twice, lost answer) |
| `play images` | language + image type, sha256 in order | one read edit compares; only changed types are cleared and uploaded, in one edit per language | ok T |
| `play listing` | language; details | compared like the dry run (`playPlan`), again inside the write edit: only differing details / languages PUT; nothing differs = the edit is deleted, not committed; commit only after every PUT worked | ok T (twice, store current by the time the edit opens) |
| `play release` | versionCode; track release | a versionCode Play has is reused, not uploaded; an identical track release is not PUT; nothing changed = edit deleted | ok T |
| `play upload` | versionCode + track | already on that track with that status = done | ok T |
| `play rollout` | track releases | the same releases = edit deleted, not committed | ok T |
| `play data-safety` | sha256 of package + CSV (`.storebound/data-safety.sha256`) | Play cannot read the form back: the hash of the last upload is kept locally, the same CSV is not sent again; `--force` sends it anyway (after a hand edit in Play Console) | ok T |
| `play share` | none (a new link each time) | a new internal sharing artifact per run (asks first; links expire in 60 days) | owner |
| `prices import` | product x store x region | diff first; only changed product / store pairs written (`--reconvert` always rewrites Play regions) | ok T (Play) |
| `revenuecat sync` | store identifier + app; lookup keys | plan from a fresh read; creates through createOnce (409 = found by key); attaches are the same twice | ok |
| `ads apple bid / budget / pause` | campaign / keyword | PUT of a value | ok (owner) |
| `web init` | file content | unchanged files are not written | ok |
| `flow run --apply` | steps (state file) | each step is one of the commands above; `--resume` skips finished steps | ok |
| `console fill` | none | fills fields in a visible browser, never presses Save / Submit / Next | ok (never saves) |
| `storebound api <write>` | none (one raw request) | a POST without a clear answer is not retried; check with a GET first | raw |
## Not run twice in the tests
No row is open. These write paths are not run twice against the store doubles; they converge through the shared,
tested helpers instead:
- `apple previews`: the asset-set logic of `apple screenshots` (`keptPrefix`, `syncAssets`), tested with a crash on
the 3rd file. `apple iap-review` and `apple events upload` compare MD5 / size with what is delivered before sending.
- `apple signing ensure`, `apple upload`: reservations and certificates through `createOnce` and lookups by key
(tested in `helpers`); `apple adhoc profile` stops at a current profile. The doubles have no certificate, profile
or build upload pipeline.
- `revenuecat sync`: `createOnce` with RevenueCat's 409 (`isConflict` tested); there is no RevenueCat double.
- `apple testflight what-to-test` / `info` / `distribute` / `expire`, `apple cpp upload` / `ppo upload` (the
screenshot path), `analytics setup`, `reviews send`, `ads apple`, `web init`, `flow run`: plan from the store (or
the file) first, the same helpers.
- `prices import` on the App Store side: the doubles have no manual price list per schedule; the Play side is tested,
and both share `diffPrices` / `sameSchedule`.

View file

@ -9,6 +9,7 @@ fails without the fix.
| # | date | lesson | guard | | # | date | lesson | guard |
|---|---|---|---| |---|---|---|---|
| 9 | 2026-10 | A retried or repeated write can duplicate on a live store: a POST create retried after a timeout or 5xx, a re-run during the list lag right after a create, two runs at once, a Play edit redone after an unclear commit, a crash in the middle of a screenshot set. | Every write is an upsert (`cli/lib/upsert.ts`: findOrCreate with a fresh re-check and a journal of recent creates, createOnce treating 409 as found, no blind retry of a create); API clients retry only requests that are safe twice; Play edits are all or nothing; asset sets keep the matching prefix; one lock per app and store (`cli/lib/lock.ts`, `--force-unlock`). Table: `docs/idempotency.md`; `tests/idempotency.test.ts` runs every write twice and asserts zero writes on the second run. |
| 8 | 2026-10 | Capacitor plugins in a monorepo live in `apps/native/node_modules`, so `scan` missed them. | `scan` / `privacy-manifest` resolve workspace packages (`cli/lib/scan/project.ts`, `cli/lib/node-packages.ts`). | | 8 | 2026-10 | Capacitor plugins in a monorepo live in `apps/native/node_modules`, so `scan` missed them. | `scan` / `privacy-manifest` resolve workspace packages (`cli/lib/scan/project.ts`, `cli/lib/node-packages.ts`). |
| 7 | 2026-10 | Console UIs: the App Store Connect sign-in runs in an `idmsa.apple.com` iframe; "User Access" is a plain heading, not a labeled group; Chrome drops session cookies without `--restore-last-session`. | `console` session and fill (`cli/console/`), `tests/console-*.test.ts`. | | 7 | 2026-10 | Console UIs: the App Store Connect sign-in runs in an `idmsa.apple.com` iframe; "User Access" is a plain heading, not a labeled group; Chrome drops session cookies without `--restore-last-session`. | `console` session and fill (`cli/console/`), `tests/console-*.test.ts`. |
| 6 | 2026-10 | Screenshot fonts: only part of the glyphs a language needs were available, so Turkish / Polish / Vietnamese characters fell back to another font silently. | `shots fonts` / `shots check` check every language's characters against the fonts (`tests/glyphs.test.ts`). | | 6 | 2026-10 | Screenshot fonts: only part of the glyphs a language needs were available, so Turkish / Polish / Vietnamese characters fell back to another font silently. | `shots fonts` / `shots check` check every language's characters against the fonts (`tests/glyphs.test.ts`). |

View file

@ -18,6 +18,8 @@ CLI: `storebound` (or `bunx storebound` in a project that has it, or `bun <toolc
2. **Play Data safety (API).** Fill `dataSafety` (types not listed = not collected; `shared` = third party that is not 2. **Play Data safety (API).** Fill `dataSafety` (types not listed = not collected; `shared` = third party that is not
your service provider, e.g. ad networks; analytics vendors acting as processors are "collected" only). your service provider, e.g. ad networks; analytics vendors acting as processors are "collected" only).
`storebound play data-safety --dry --csv store/data-safety.csv` to review, then `storebound play data-safety`. `storebound play data-safety --dry --csv store/data-safety.csv` to review, then `storebound play data-safety`.
Play cannot read the form back: storebound keeps the hash of the last upload in `.storebound/data-safety.sha256`
and skips an unchanged CSV. After a hand edit in Play Console, `--force` sends the config's answers again.
Then check the summary in Play Console > Policy and programs > App content > Data safety. Then check the summary in Play Console > Policy and programs > App content > Data safety.
3. **Play App content (UI).** Policy and programs > App content, each card with `store/declarations.md`: 3. **Play App content (UI).** Policy and programs > App content, each card with `store/declarations.md`:
privacy policy, app access, ads, content rating (IARC questionnaire), target audience and content, news, COVID-19, privacy policy, app access, ads, content rating (IARC questionnaire), target audience and content, news, COVID-19,

View file

@ -1,6 +1,6 @@
--- ---
name: store-pitfalls name: store-pitfalls
description: Known pitfalls with App Store Connect, Google Play and storebound itself, learned from real releases, and what to do about each - store lists that lag right after a create, dry runs that print a count instead of a plan, App Store names taken on a storefront, keyword limits in bytes, Data safety answers without code evidence, missing font glyphs in screenshots, console sign-in and sessions, Capacitor monorepo plugins. Read it before any store task, and when a store command does something you did not expect. description: Known pitfalls with App Store Connect, Google Play and storebound itself, learned from real releases, and what to do about each - store lists that lag right after a create, dry runs that print a count instead of a plan, App Store names taken on a storefront, keyword limits in bytes, Data safety answers without code evidence, missing font glyphs in screenshots, console sign-in and sessions, Capacitor monorepo plugins, duplicates from retries, re-runs and parallel runs. Read it before any store task, and when a store command does something you did not expect.
--- ---
# Store pitfalls # Store pitfalls
@ -67,6 +67,22 @@ Each entry: **symptom** → cause → what to do → what guards it now. The ful
- **Do:** run `scan` from the project root; check that the stack line lists the plugins. - **Do:** run `scan` from the project root; check that the stack line lists the plugins.
- **Guard:** `storebound scan` and `privacy-manifest` resolve packages through workspace folders too. - **Guard:** `storebound scan` and `privacy-manifest` resolve packages through workspace folders too.
## 9. A second run (or a retry) creates a duplicate
- **Symptom:** two in-app purchases or subscription groups with the same name, a screenshot twice in a set, an
offer code campaign or a custom product page twice, after a timeout, a crash, a re-run a minute later, or two runs
at once.
- **Cause:** a POST create retried after a timeout or a 5xx may have been applied the first time; the store's lists lag
right after a create, so a re-run does not see what the last run made; two writers race; a Play edit redone after
a commit whose answer was lost.
- **Do:** just run the command again: every write command is an upsert and converges (`docs/idempotency.md`). Never
retry a create by hand (`storebound api`) without a GET first. If a run says another storebound run holds the lock,
wait for it; use `--force-unlock` only when that run is really gone. Never run two write commands for the same app
in parallel.
- **Guard:** `cli/lib/upsert.ts` (findOrCreate: fresh re-check + journal `.storebound/created.json`; createOnce: 409
= found, an unclear answer is looked up, never re-sent blindly), retries only for requests that are safe twice,
Play edits deleted on every error and never redone after an unclear commit, screenshot / preview sets keep the
matching prefix in order, `.storebound/locks/` per app and store. `tests/idempotency.test.ts`.
## Adding a lesson ## Adding a lesson
Every bug found in real use gets an entry in `docs/pitfalls.md` and here, and, where possible, a check or a test Every bug found in real use gets an entry in `docs/pitfalls.md` and here, and, where possible, a check or a test
that fails without the fix (see CONTRIBUTING.md). that fails without the fix (see CONTRIBUTING.md).

View file

@ -52,6 +52,16 @@ in a plain Android tablet body instead of the iPad one.
`<shots.frames>/<lang>/<phone|tablet>/<shot>.png`, local, not committed. `<shots.frames>/<lang>/<phone|tablet>/<shot>.png`, local, not committed.
- `web` (default): `baseUrl`, `query` with `{lang}`, shots: `path` / `waitFor` / `eval` / `delayMs` / `device`. - `web` (default): `baseUrl`, `query` with `{lang}`, shots: `path` / `waitFor` / `eval` / `delayMs` / `device`.
Phone 440 x 902 @3x and tablet 800 x 1240 @2x by default; the template draws the status bar above. Phone 440 x 902 @3x and tablet 800 x 1240 @2x by default; the template draws the status bar above.
Speed (many languages): frames run in parallel browser contexts (`--concurrency N`, `capture.concurrency`;
default min(3, cores / 4) since game boots are GPU-heavy). `capture.session: 'per-language'` boots the app once
per language and device and switches shots in that page (per shot `navigate: 'spa'` = pushState + popstate to its
path, then `waitFor` / `eval`; `'reload'` = goto in the same page); a shot whose `waitFor` fails there within
`reuseTimeoutMs` (15000) gets a fresh page. Re-runs skip frames whose inputs did not change (shot entry, URL,
frame size, the language's copy, the build: `capture.build` files / folders, else the HTML and scripts at
`baseUrl`, fonts) via `.storebound/capture-cache.json`; `--only-missing` captures only missing files, `--force`
everything. Each progress line shows n/total, ETA and the time per step (goto, wait, eval, delay, shot); the end
lists the slowest frames and steps, and `.storebound/capture-report.json` keeps them. Put the slow step right
(a long `waitFor` = the game reaching the scene: add a URL switch that jumps there).
- `ios-sim` (Unity, Godot, Flutter, native, Capacitor: any Simulator build): `ios.device` (simulator name or UDID, - `ios-sim` (Unity, Godot, Flutter, native, Capacitor: any Simulator build): `ios.device` (simulator name or UDID,
e.g. a 6.9" iPhone), `ios.bundleId`, optional `ios.app` (.app to install), `ios.args`. Per shot: `args` (launch e.g. a 6.9" iPhone), `ios.bundleId`, optional `ios.app` (.app to install), `ios.args`. Per shot: `args` (launch
arguments, read them with UserDefaults / your engine) and / or `url` (deep link). Language goes in as arguments, read them with UserDefaults / your engine) and / or `url` (deep link). Language goes in as
@ -82,6 +92,11 @@ in a plain Android tablet body instead of the iPad one.
titles; Montserrat, Inter, Nunito for body), or, if the owner accepts mixed glyphs, `shots.fonts.fallback: titles; Montserrat, Inter, Nunito for body), or, if the owner accepts mixed glyphs, `shots.fonts.fallback:
['Noto Sans Display']`. Lilita One lacks most of Latin Extended (Turkish ş ğ İ, Polish, Czech, Romanian) and ['Noto Sans Display']`. Lilita One lacks most of Latin Extended (Turkish ş ğ İ, Polish, Czech, Romanian) and
Vietnamese. Re-run after copy or font edits (render refuses fonts that no longer cover the copy). Vietnamese. Re-run after copy or font edits (render refuses fonts that no longer cover the copy).
A game's own brand font: put the file path (relative to the config; TTF / OTF / WOFF / WOFF2) or
`{ family, file, license }` into `shots.fonts.title` / `body` / `fallback`. The same coverage check runs on it, listed
fallbacks are tried before Noto (`pl title: ę ł ą Ł not in ER Display -> Noto Sans Display`), the file is served in
place (never copied) and its hash is in fonts.json, so edits to the font need `shots fonts` again. Ask the owner for
the license ('OFL-1.1', 'proprietary', ...) and put it in `license`; `shots check` warns without one.
5. **Style and scenes.** `shots.style`: `game` (sunburst, sparkles, outlined titles, badges) or `app` (calm gradient, 5. **Style and scenes.** `shots.style`: `game` (sunburst, sparkles, outlined titles, badges) or `app` (calm gradient,
one clean device frame, headline + subline, no sparkles; badges only as quiet chips when the copy has them). one clean device frame, headline + subline, no sparkles; badges only as quiet chips when the copy has them).
Default: `app` for the app / subscription-app profiles, `game` otherwise. `shots.scenes`: palette (game: fire, Default: `app` for the app / subscription-app profiles, `game` otherwise. `shots.scenes`: palette (game: fire,
@ -89,7 +104,7 @@ in a plain Android tablet body instead of the iPad one.
`{ bg, glow, ink, shadow }`), frames (one front phone + optional back phones with x / dy / scale / rotate), badge `{ bg, glow, ink, shadow }`), frames (one front phone + optional back phones with x / dy / scale / rotate), badge
slots (y, side, icon, hot, stars). Fonts default to Paytone One + Montserrat (game) or Inter (app): all cover slots (y, side, icon, hot, stars). Fonts default to Paytone One + Montserrat (game) or Inter (app): all cover
Latin Extended and Vietnamese. Latin Extended and Vietnamese.
6. **Render.** `storebound shots render --lang en` first, review `store/renders/en/sheet.png` (contact sheet, sizes 6. **Render.** (`--concurrency N`, default min(4, cores / 2) pages) `storebound shots render --lang en` first, review `store/renders/en/sheet.png` (contact sheet, sizes
packed into a grid), then packed into a grid), then
all languages in the background: `storebound shots render > .storebound/render.log 2>&1 &`. all languages in the background: `storebound shots render > .storebound/render.log 2>&1 &`.
7. **JPEG for Play.** `storebound shots jpeg` (q92, about 4x smaller than PNG; App Store stays PNG). 7. **JPEG for Play.** `storebound shots jpeg` (q92, about 4x smaller than PNG; App Store stays PNG).

179
tests/capture-web.test.ts Normal file
View file

@ -0,0 +1,179 @@
// `shots capture` (web) against a local fixture web app: parallel contexts give the same files as serial, the
// per-language session boots the app once per language, the input-hash cache skips unchanged frames and re-runs changed
// ones, and every frame has a timing breakdown. Pure helpers are tested without a browser. Skips the browser part when
// no Chromium can launch. The browser runs run in a `storebound` child process (its own Chromium), like a real run.
import { afterAll, describe, expect, test } from 'bun:test';
import { spawnSync } from 'node:child_process';
import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import type { ShipConfig, ShotsCapture } from '../cli/lib/config';
import { type CaptureReport, defaultConcurrency, planWeb, pool, progressLine, selectJobs, timingSummary } from '../cli/shots/capture-web';
import { BASE } from './helpers/store-mock';
const CLI = join(import.meta.dir, '..', 'cli', 'index.ts');
describe('capture helpers', () => {
test('default concurrency: render min(4, cores/2), capture (GPU-heavy) min(3, cores/4), at least 1', () => {
expect(defaultConcurrency('render', 10)).toBe(4);
expect(defaultConcurrency('capture', 10)).toBe(2);
expect(defaultConcurrency('capture', 2)).toBe(1);
expect(defaultConcurrency('render', 1)).toBe(1);
});
test('pool keeps the input order and never runs more than n at once', async () => {
let now = 0, peak = 0;
const out = await pool([5, 1, 3, 2, 4], 2, async x => { now++; peak = Math.max(peak, now); await Bun.sleep(x * 3); now--; return x * 10; });
expect(out).toEqual([50, 10, 30, 20, 40]);
expect(peak).toBe(2);
});
test('progress line with an ETA', () => {
expect(progressLine(5, 20, 0, 10_000)).toBe(' 5/20 ETA 30.0s');
expect(progressLine(20, 20, 0, 10_000)).toBe('20/20');
});
test('cache selection: changed inputs and missing files run; --only-missing; --force', () => {
const cap: ShotsCapture = { source: 'web', baseUrl: 'http://x', shots: { a: { path: '/a' }, b: { path: '/b' } } };
const dims = { phone: { w: 10, h: 20, dpr: 1, bar: 0 }, tablet: { w: 10, h: 20, dpr: 1, bar: 0 } };
const jobs = planWeb(cap, { langs: ['en', 'tr'], devices: ['phone'], only: [], dims, framesDir: '/f', build: 'b1' });
expect(jobs.map(j => j.frame)).toEqual(['en/phone/a.png', 'en/phone/b.png', 'tr/phone/a.png', 'tr/phone/b.png']);
const cache = Object.fromEntries(jobs.map(j => [j.frame, j.hash]));
const all = () => true;
expect(selectJobs(jobs, cache, 'changed', all).run).toEqual([]);
const jobs2 = planWeb({ ...cap, shots: { ...cap.shots, b: { path: '/b', delayMs: 5 } } }, { langs: ['en', 'tr'], devices: ['phone'], only: [], dims, framesDir: '/f', build: 'b1' });
expect(selectJobs(jobs2, cache, 'changed', all).run.map(j => j.frame)).toEqual(['en/phone/b.png', 'tr/phone/b.png']);
const jobs3 = planWeb(cap, { langs: ['en', 'tr'], devices: ['phone'], only: [], dims, framesDir: '/f', build: 'b2' });
expect(selectJobs(jobs3, cache, 'changed', all).run.length).toBe(4);
expect(selectJobs(jobs3, cache, 'only-missing', (f: string) => !f.endsWith('tr/phone/a.png')).run.map(j => j.frame)).toEqual(['tr/phone/a.png']);
expect(selectJobs(jobs, cache, 'force', all).run.length).toBe(4);
});
test('timing summary: slowest frames with their slowest step, time per step', () => {
const t = (goto: number, waitFor: number) => ({ goto, waitFor, eval: 0, delay: 100, screenshot: 50, total: goto + waitFor + 150 });
const s = timingSummary([{ frame: 'en/phone/a.png', timing: t(1000, 200) }, { frame: 'en/phone/b.png', timing: t(100, 5000) }], 1);
expect(s.slowest).toEqual([{ frame: 'en/phone/b.png', total: 5250, step: 'waitFor' }]);
expect(s.steps).toEqual({ goto: 1100, waitFor: 5200, eval: 0, delay: 200, screenshot: 100 });
});
});
// ---- a local fixture web app: /scene/<name>?lang=xx, a SPA (popstate) that counts its boots ----
let boots = 0;
const server = Bun.serve({
port: 0, hostname: '127.0.0.1',
fetch(req) {
const u = new URL(req.url);
if (u.pathname === '/app.js') {
boots++;
return new Response(`
const COLORS = { en: '#1e66f5', de: '#40a02b', tr: '#d20f39' };
function show() {
const name = location.pathname.split('/').pop() || 'home', lang = new URLSearchParams(location.search).get('lang');
document.body.style.cssText = 'margin:0;background:' + (COLORS[lang] || '#000') + ';font:24px sans-serif;color:#fff';
document.body.innerHTML = '<div style="padding:20px">' + lang + ' ' + name + '</div><div style="margin:20px;width:' + (40 * name.length) + 'px;height:60px;background:#fff"></div>';
window.scene = null;
window.shows = (window.shows || 0) + 1;
setTimeout(() => { window.scene = name; }, 30);
}
addEventListener('popstate', show);
show();`, { headers: { 'content-type': 'text/javascript' } });
}
return new Response('<!doctype html><html><head><meta charset="utf-8"><script src="/app.js" defer></script></head><body></body></html>', { headers: { 'content-type': 'text/html' } });
},
});
afterAll(() => server.stop(true));
const canLaunch = spawnSync('bun', ['-e', `const { launch } = await import(${JSON.stringify(join(import.meta.dir, '..', 'cli', 'shots', 'browser.ts'))}); await (await launch(${JSON.stringify(join(import.meta.dir, '..'))}, { headless: true })).close();`], { timeout: 60_000 }).status === 0;
if (!canLaunch) console.warn('capture-web browser tests skipped: no Chromium could launch (bunx playwright install chromium)');
/** A temp project with this config; cli() runs `storebound shots capture <args>` in it. */
function project(cfg: ShipConfig, files: Record<string, string>): { root: string; setConfig: (c: ShipConfig) => void; cli: (args: string[]) => Promise<{ out: string; err: string; code: number }> } {
const root = mkdtempSync(join(tmpdir(), 'sb-cap-'));
for (const [p, t] of Object.entries(files)) { mkdirSync(join(root, p, '..'), { recursive: true }); writeFileSync(join(root, p), t); }
const setConfig = (c: ShipConfig) => writeFileSync(join(root, 'storebound.config.json'), JSON.stringify(c));
setConfig(cfg);
// async: the fixture server lives in this process and must keep answering while the child captures
return { root, setConfig, cli: async args => {
const r = Bun.spawn(['bun', CLI, 'shots', 'capture', ...args], { cwd: root, stdout: 'pipe', stderr: 'pipe', env: { ...process.env, NO_COLOR: '1' } });
const [out, err, code] = await Promise.all([new Response(r.stdout).text(), new Response(r.stderr).text(), r.exited]);
return { out, err, code };
} };
}
const shot = (name: string, extra: object = {}) => ({ path: `/scene/${name}`, waitFor: `window.scene === '${name}'`, delayMs: 20, ...extra });
function config(capture: Partial<ShotsCapture> = {}): ShipConfig {
return { ...BASE, shots: { copy: 'store/shots/copy.json', frames: 'store/shots/frames', out: 'store/renders', scenes: [{ id: 'hero', palette: 'fire', frames: [{ src: 'hero.png' }] }], phone: { w: 180, h: 240, dpr: 1 },
capture: { source: 'web', baseUrl: `http://127.0.0.1:${server.port}`, query: 'lang={lang}', build: 'src', shots: { hero: shot('hero'), battle: shot('battle'), king: shot('king') }, ...capture } } };
}
const files = { 'store/shots/copy.json': '{}', 'src/main.js': 'v1' };
const frames = (root: string) => ['en', 'de', 'tr'].flatMap(l => ['hero', 'battle', 'king'].map(n => join(root, 'store/shots/frames', l, 'phone', `${n}.png`)));
describe.skipIf(!canLaunch)('shots capture (web) with a fixture app', () => {
test('concurrency 3 gives the same files as serial', async () => {
const p = project(config(), files);
try {
await p.cli(['--device', 'phone', '--concurrency', '1', '--force']);
const serial = frames(p.root).map(f => readFileSync(f));
const r = await p.cli(['--device', 'phone', '--concurrency', '3', '--force']);
expect(r.out).toContain('capturing 9 frames, 3 at a time');
const parallel = frames(p.root).map(f => readFileSync(f));
expect(parallel.length).toBe(9);
for (const [i, b] of parallel.entries()) expect(Buffer.compare(b, serial[i]!)).toBe(0);
} finally { rmSync(p.root, { recursive: true, force: true }); }
}, 120_000);
test('per-language session boots the app once per language; per-shot once per frame', async () => {
const p = project(config({ session: 'per-language' }), files);
try {
boots = 0;
const r = await p.cli(['--device', 'phone', '--concurrency', '2', '--force']);
expect(boots).toBe(3);
expect(r.out).toContain('(reused page)');
const report = JSON.parse(readFileSync(join(p.root, '.storebound/capture-report.json'), 'utf8')) as CaptureReport;
expect(report.frames.filter(f => f.reused).length).toBe(6);
expect(frames(p.root).every(f => existsSync(f))).toBe(true);
// a shot whose precondition fails in the reused page (it needs a freshly booted page) falls back to a fresh page
const bad = project(config({ session: 'per-language', reuseTimeoutMs: 300, shots: { hero: shot('hero'), fresh: { path: '/scene/fresh', waitFor: "window.scene === 'fresh' && window.shows === 1", delayMs: 20 } } }), files);
try {
boots = 0;
const r2 = await bad.cli(['--device', 'phone', '--lang', 'en', '--force']);
expect(r2.err).toContain('en/phone/fresh: reused page failed');
expect(r2.out).toContain('capture done: 2 frames');
expect(boots).toBe(2);
} finally { rmSync(bad.root, { recursive: true, force: true }); }
boots = 0;
const q = project(config(), files);
await q.cli(['--device', 'phone', '--lang', 'en', '--force']);
rmSync(q.root, { recursive: true, force: true });
expect(boots).toBe(3);
} finally { rmSync(p.root, { recursive: true, force: true }); }
}, 180_000);
test('cache skips unchanged frames, re-runs changed ones; --only-missing; timing breakdown', async () => {
const p = project(config(), files);
try {
let r = await p.cli(['--device', 'phone']);
expect(r.out).toContain('capturing 9 frames');
expect(r.out).toMatch(/tr\/phone\/king\.png goto [\d.]+s wait [\d.]+s eval [\d.]+s delay [\d.]+s shot [\d.]+s = [\d.]+s/);
expect(r.out).toContain('time per step: goto');
expect(r.out).toContain('slowest: ');
const report = JSON.parse(readFileSync(join(p.root, '.storebound/capture-report.json'), 'utf8')) as CaptureReport;
expect(Object.keys(report.frames[0]!.timing).sort()).toEqual(['delay', 'eval', 'goto', 'screenshot', 'total', 'waitFor']);
expect(report.summary.slowest.length).toBe(5);
r = await p.cli(['--device', 'phone']);
expect(r.out).toContain('9 of 9 frames unchanged, skipped');
expect(r.out).toContain('capturing 0 frames');
// a changed shot entry re-runs only that shot (3 languages)
p.setConfig(config({ shots: { hero: shot('hero'), battle: shot('battle', { delayMs: 40 }), king: shot('king') } }));
r = await p.cli(['--device', 'phone']);
expect(r.out).toContain('6 of 9 frames unchanged');
expect(r.out).toContain('capturing 3 frames');
// a build change re-runs everything; --only-missing only the missing file
await Bun.write(join(p.root, 'src/main.js'), 'v2 changed');
rmSync(frames(p.root)[0]!);
r = await p.cli(['--device', 'phone', '--only-missing']);
expect(r.out).toContain('capturing 1 frames');
r = await p.cli(['--device', 'phone']);
expect(r.out).toContain('capturing 8 frames'); // the one just captured already has the new inputs
} finally { rmSync(p.root, { recursive: true, force: true }); }
}, 180_000);
});

View file

@ -0,0 +1,439 @@
// Stateful doubles of App Store Connect and Google Play for the idempotency tests (tests/idempotency.test.ts): what a
// command creates is there on the next read, so running a command twice shows whether the second run writes again.
// Shapes follow the recorded answers in store-mock.ts and the APIs' documented JSON:API / androidpublisher formats;
// every id and name is a placeholder. Faults can be injected per request (a 5xx after the write was applied = an
// unclear answer; a 5xx before it; a failing upload part), and new resources can be hidden from list reads for a
// while (the stores' list lag right after a create). Nothing here reaches the network.
import { json, type Call } from './store-mock';
export type R = { type: string; id: string; attributes: Record<string, unknown>; rels: Record<string, string | string[]>; order: number; hiddenReads?: number };
export interface Fault { method: string; match: RegExp; when: 'before' | 'after'; status?: number; times?: number }
const ASC = 'https://api.appstoreconnect.apple.com';
const UPLOAD_HOST = 'https://upload.example.com/';
/** list relationship -> resource type of its items (parent type / relationship name); `one` for to-one relationships. */
const LISTS: Record<string, { type: string; one?: boolean }> = {
'apps/inAppPurchasesV2': { type: 'inAppPurchases' },
'inAppPurchases/inAppPurchaseLocalizations': { type: 'inAppPurchaseLocalizations' },
'inAppPurchases/iapPriceSchedule': { type: 'inAppPurchasePriceSchedules', one: true },
'inAppPurchases/inAppPurchaseAvailability': { type: 'inAppPurchaseAvailabilities', one: true },
'inAppPurchases/appStoreReviewScreenshot': { type: 'inAppPurchaseAppStoreReviewScreenshots', one: true },
'inAppPurchases/offerCodes': { type: 'inAppPurchaseOfferCodes' },
'inAppPurchaseOfferCodes/customCodes': { type: 'inAppPurchaseOfferCodeCustomCodes' },
'inAppPurchaseOfferCodes/oneTimeUseCodes': { type: 'inAppPurchaseOfferCodeOneTimeUseCodes' },
'apps/appStoreVersions': { type: 'appStoreVersions' },
'appStoreVersions/appStoreVersionLocalizations': { type: 'appStoreVersionLocalizations' },
'appStoreVersions/appStoreReviewDetail': { type: 'appStoreReviewDetails', one: true },
'appStoreVersions/appStoreVersionPhasedRelease': { type: 'appStoreVersionPhasedReleases', one: true },
'appStoreVersionLocalizations/appScreenshotSets': { type: 'appScreenshotSets' },
'appStoreVersionLocalizations/appPreviewSets': { type: 'appPreviewSets' },
'appScreenshotSets/appScreenshots': { type: 'appScreenshots' },
'appPreviewSets/appPreviews': { type: 'appPreviews' },
'apps/appCustomProductPages': { type: 'appCustomProductPages' },
'appCustomProductPages/appCustomProductPageVersions': { type: 'appCustomProductPageVersions' },
'appCustomProductPageVersions/appCustomProductPageLocalizations': { type: 'appCustomProductPageLocalizations' },
'appCustomProductPageLocalizations/appScreenshotSets': { type: 'appScreenshotSets' },
'apps/appEvents': { type: 'appEvents' },
'appEvents/localizations': { type: 'appEventLocalizations' },
'apps/betaGroups': { type: 'betaGroups' },
'apps/subscriptionGroups': { type: 'subscriptionGroups' },
'subscriptionGroups/subscriptionGroupLocalizations': { type: 'subscriptionGroupLocalizations' },
'subscriptionGroups/subscriptions': { type: 'subscriptions' },
'subscriptions/subscriptionLocalizations': { type: 'subscriptionLocalizations' },
'subscriptions/subscriptionAvailability': { type: 'subscriptionAvailabilities', one: true },
'subscriptions/appStoreReviewScreenshot': { type: 'subscriptionAppStoreReviewScreenshots', one: true },
'subscriptions/promotionalOffers': { type: 'subscriptionPromotionalOffers' },
'subscriptions/offerCodes': { type: 'subscriptionOfferCodes' },
'subscriptions/prices': { type: 'subscriptionPrices' },
'subscriptions/introductoryOffers': { type: 'subscriptionIntroductoryOffers' },
'subscriptionOfferCodes/customCodes': { type: 'subscriptionOfferCodeCustomCodes' },
'apps/appStoreVersionExperimentsV2': { type: 'appStoreVersionExperiments' },
'appStoreVersionExperiments/appStoreVersionExperimentTreatments': { type: 'appStoreVersionExperimentTreatments' },
'appStoreVersionExperimentTreatments/appStoreVersionExperimentTreatmentLocalizations': { type: 'appStoreVersionExperimentTreatmentLocalizations' },
'appStoreVersionExperimentTreatmentLocalizations/appScreenshotSets': { type: 'appScreenshotSets' },
'reviewSubmissions/items': { type: 'reviewSubmissionItems' },
'apps/buildUploads': { type: 'buildUploads' },
'inAppPurchases/versions': { type: 'inAppPurchaseVersions' },
'subscriptions/versions': { type: 'subscriptionVersions' },
'subscriptionGroups/versions': { type: 'subscriptionGroupVersions' },
'appStoreVersions/build': { type: 'builds', one: true },
'apps/appInfos': { type: 'appInfos' },
'appInfos/appInfoLocalizations': { type: 'appInfoLocalizations' },
'appInfos/ageRatingDeclaration': { type: 'ageRatingDeclarations', one: true },
'apps/appPriceSchedule': { type: 'appPriceSchedules', one: true },
'apps/appPricePoints': { type: 'appPricePoints' },
'apps/appAvailabilityV2': { type: 'appAvailabilities', one: true },
'appAvailabilities/territoryAvailabilities': { type: 'territoryAvailabilities' },
'apps/promotedPurchases': { type: 'promotedPurchases' },
'subscriptions/winBackOffers': { type: 'winBackOffers' },
'betaGroups/betaTesters': { type: 'betaTesters' },
};
/** Stores refuse a second resource with the same key (409 ENTITY_ERROR ... already exists). */
const UNIQUE: Record<string, (r: R) => string> = {
inAppPurchases: r => String(r.attributes.productId),
inAppPurchaseLocalizations: r => `${r.rels.inAppPurchaseV2}|${r.attributes.locale}`,
appScreenshotSets: r => `${Object.values(r.rels).join()}|${r.attributes.screenshotDisplayType}`,
appPreviewSets: r => `${Object.values(r.rels).join()}|${r.attributes.previewType}`,
betaGroups: r => String(r.attributes.name),
subscriptionGroups: r => String(r.attributes.referenceName),
subscriptions: r => String(r.attributes.productId),
subscriptionLocalizations: r => `${r.rels.subscription}|${r.attributes.locale}`,
subscriptionGroupLocalizations: r => `${r.rels.subscriptionGroup}|${r.attributes.locale}`,
appCustomProductPageLocalizations: r => `${r.rels.appCustomProductPageVersion}|${r.attributes.locale}`,
appEventLocalizations: r => `${r.rels.appEvent}|${r.attributes.locale}`,
appStoreReviewDetails: r => String(r.rels.appStoreVersion),
devices: r => String(r.attributes.udid).toLowerCase(),
reviewSubmissions: r => `${r.rels.app}|${r.attributes.state}`,
appStoreVersionPhasedReleases: r => String(r.rels.appStoreVersion),
bundleIds: r => String(r.attributes.identifier),
appStoreVersions: r => `${r.rels.app}|${r.attributes.platform}|${r.attributes.versionString}`,
appInfoLocalizations: r => `${r.rels.appInfo}|${r.attributes.locale}`,
appStoreVersionLocalizations: r => `${r.rels.appStoreVersion}|${r.attributes.locale}`,
promotedPurchases: r => String(r.rels.inAppPurchaseV2 ?? r.rels.subscription),
winBackOffers: r => `${r.rels.subscription}|${r.attributes.offerId}`,
betaTesters: r => String(r.attributes.email).toLowerCase(),
};
const ASSETS = /Screenshots$|^appPreviews$|^appScreenshots$/;
/** Attributes a new resource of a type starts with. */
const DEFAULTS: Record<string, Record<string, unknown>> = {
inAppPurchases: { state: 'MISSING_METADATA' }, subscriptions: { state: 'MISSING_METADATA' }, reviewSubmissions: { state: 'READY_FOR_REVIEW' },
appCustomProductPageVersions: { state: 'PREPARE_FOR_SUBMISSION' }, devices: { status: 'ENABLED', deviceClass: 'IPHONE' },
appStoreVersionPhasedReleases: { phasedReleaseState: 'INACTIVE' }, appEvents: { eventState: 'DRAFT' }, appStoreVersionExperiments: { state: 'PREPARE_FOR_SUBMISSION' },
appStoreVersions: { appVersionState: 'PREPARE_FOR_SUBMISSION' }, betaTesters: { state: 'NOT_INVITED' },
};
const singular = (t: string): string => t.replace(/s$/, '');
export class FakeAsc {
res = new Map<string, R>();
next = 1;
/** new resources of these types stay out of list reads for that many reads */
lag: Record<string, number> = {};
/** every uploaded part, by asset id */
parts: string[] = [];
constructor(readonly appId = '6400000101') {
this.put({ type: 'apps', id: appId, attributes: { bundleId: 'com.example.notes', name: 'Example Notes' }, rels: {} });
for (const t of ['USA', 'DEU', 'TUR']) this.put({ type: 'territories', id: t, attributes: { currency: t === 'USA' ? 'USD' : t === 'DEU' ? 'EUR' : 'TRY' }, rels: {} });
}
put(r: Omit<R, 'order'>): R { const x = { ...r, order: this.next++ } as R; this.res.set(x.id, x); return x; }
all(type: string): R[] { return [...this.res.values()].filter(r => r.type === type).sort((a, b) => a.order - b.order); }
view(r: R): Record<string, unknown> {
const relationships: Record<string, unknown> = {};
for (const [k, v] of Object.entries(r.rels)) relationships[k] = { data: Array.isArray(v) ? v.map(id => ({ type: this.res.get(id)?.type, id })) : { type: this.res.get(v)?.type, id: v } };
return { type: r.type, id: r.id, attributes: r.attributes, relationships };
}
children(parent: string, type: string): R[] {
return this.all(type).filter(r => Object.values(r.rels).some(v => (Array.isArray(v) ? v.includes(parent) : v === parent)));
}
private visible(rs: R[]): R[] {
return rs.filter(r => { if (r.hiddenReads && r.hiddenReads > 0) { r.hiddenReads--; return false; } return true; });
}
private filtered(rs: R[], q: URLSearchParams): R[] {
let out = rs;
for (const [k, v] of q) {
const m = /^filter\[([\w.]+)\]$/.exec(k);
if (!m) continue;
const f = m[1]!, vals = v.split(',');
if (f.includes('.')) continue;
out = out.filter(r => (f in r.attributes ? vals.includes(String(r.attributes[f])) : Object.entries(r.rels).some(([rk, rv]) => (rk === f || rk === singular(f)) && vals.some(x => (Array.isArray(rv) ? rv.includes(x) : rv === x)))));
}
return out;
}
create(body: { data: { type: string; attributes?: Record<string, unknown>; relationships?: Record<string, { data: unknown }> }; included?: { type: string; id: string; attributes?: Record<string, unknown>; relationships?: Record<string, { data: unknown }> }[] }): R | Response {
const temp = new Map<string, string>();
const idFor = (t: string) => `${singular(t).replace(/[A-Z]/g, c => c.toLowerCase()).slice(0, 12)}-${this.next}`;
for (const inc of body.included ?? []) temp.set(inc.id, `${idFor(inc.type)}-${temp.size}`);
const relsOf = (rel?: Record<string, { data: unknown }>): Record<string, string | string[]> => Object.fromEntries(Object.entries(rel ?? {}).filter(([, v]) => v?.data).map(([k, v]) => [k,
Array.isArray(v.data) ? (v.data as { id: string }[]).map(d => temp.get(d.id) ?? d.id) : temp.get((v.data as { id: string }).id) ?? (v.data as { id: string }).id]));
const d = body.data;
const main: Omit<R, 'order'> = { type: d.type, id: idFor(d.type), attributes: { ...DEFAULTS[d.type], ...(d.attributes ?? {}) }, rels: relsOf(d.relationships) };
const u = UNIQUE[d.type];
if (u && this.all(d.type).some(r => u(r) === u(main as R)))
return json({ errors: [{ status: '409', code: 'ENTITY_ERROR.ATTRIBUTE.INVALID.DUPLICATE', title: 'The provided entity includes an attribute with a value that has already been used', detail: 'already exists' }] }, 409);
if (ASSETS.test(d.type) && d.attributes?.fileName) {
main.attributes.uploadOperations = [{ method: 'PUT', url: `${UPLOAD_HOST}${main.id}`, offset: 0, length: Number(d.attributes.fileSize ?? 0), requestHeaders: [{ name: 'Content-Type', value: 'image/png' }] }];
main.attributes.assetDeliveryState = { state: 'AWAITING_UPLOAD' };
}
// a tester belongs to the app of its groups (GET /v1/betaTesters?filter[apps]=)
if (d.type === 'betaTesters') { const g = [main.rels.betaGroups].flat()[0]; const app = g ? this.res.get(String(g))?.rels.app : undefined; if (app) main.rels.app = app; }
const r = this.put(main);
if (this.lag[d.type]) r.hiddenReads = this.lag[d.type];
for (const inc of body.included ?? []) {
// included resources are linked back to whatever points at them
this.put({ type: inc.type, id: temp.get(inc.id)!, attributes: { ...DEFAULTS[inc.type], ...(inc.attributes ?? {}) }, rels: relsOf(inc.relationships) });
}
for (const holder of [r, ...(body.included ?? []).map(i => this.res.get(temp.get(i.id)!)!)]) {
for (const [k, v] of Object.entries(holder.rels)) for (const cid of Array.isArray(v) ? v : [v]) {
const child = this.res.get(cid);
if (child && [...temp.values()].includes(cid) && !child.rels[singular(holder.type)]) child.rels[singular(holder.type)] = holder.id;
void k;
}
}
return r;
}
handle(method: string, path: string, body: unknown): Response {
const u = new URL(ASC + path);
const parts = u.pathname.split('/').filter(Boolean); // v1, type, id?, rel?
const [, type, id, rel, rel2] = parts;
if (method === 'GET') {
if (type === 'territories') return json({ data: this.all('territories').map(r => this.view(r)), links: {} });
if (rel === 'pricePoints') {
const t = u.searchParams.get('filter[territory]') ?? 'USA';
return json({ data: [0.99, 1.99, 2.99, 4.99, 9.99].map(p => ({ type: 'inAppPurchasePricePoints', id: `pp-${t}-${p}`, attributes: { customerPrice: String(p) }, relationships: { territory: { data: { type: 'territories', id: t } } } })),
included: [{ type: 'territories', id: t, attributes: { currency: 'USD' } }], links: {} });
}
if (rel === 'equalizations') {
// every other territory at the same price (enough for the comparisons the commands make)
const [, , price] = id!.split('-');
return json({ data: ['DEU', 'TUR'].map(t => ({ type: 'subscriptionPricePoints', id: `pp-${t}-${price}`, attributes: { customerPrice: price }, relationships: { territory: { data: { type: 'territories', id: t } } } })), links: {} });
}
if (!id) return json({ data: this.visible(this.filtered(this.all(type!), u.searchParams)).map(r => this.view(r)), links: {} });
const self = this.res.get(id);
if (!rel) return self && self.type === type ? json({ data: this.view(self) }) : json({ errors: [{ status: '404' }] }, 404);
if (rel === 'relationships') return json({ data: this.children(id, rel2!).map(r => ({ type: r.type, id: r.id })) });
const l = LISTS[`${type}/${rel}`];
if (!l) return json({ errors: [{ status: '599', detail: `fake: no list ${type}/${rel}` }] }, 599);
const kids = this.filtered(this.children(id, l.type), u.searchParams);
if (l.one) {
const one = kids[0];
if (!one) return json({ errors: [{ status: '404' }] }, 404);
const included = (u.searchParams.get('include') ?? '').split(',').filter(Boolean).flatMap(() => [...this.res.values()].filter(r => Object.values(r.rels).some(v => v === one.id || (Array.isArray(v) && v.includes(one.id))) && r.id !== one.id).map(r => this.view(r)));
return json({ data: this.view(one), included });
}
const shown = this.visible(kids);
// price lists carry their price points (customerPrice from the placeholder id pp-<territory>-<price>)
const points = [...new Set(shown.map(r => r.rels.subscriptionPricePoint ?? r.rels.inAppPurchasePricePoint).filter((x): x is string => typeof x === 'string'))];
const included = points.map(pid => ({ type: 'subscriptionPricePoints', id: pid, attributes: { customerPrice: pid.split('-')[2] } }));
return json({ data: shown.map(r => this.view(r)), included, links: {} });
}
if (method === 'POST') {
if (rel === 'relationships') { return new Response(null, { status: 204 }); }
const out = this.create(body as Parameters<FakeAsc['create']>[0]);
return out instanceof Response ? out : json({ data: this.view(out) }, 201);
}
if (method === 'PATCH') {
if (rel === 'relationships' && type && id) {
const data = (body as { data: { id: string } | { id: string }[] | null }).data;
if (data && !Array.isArray(data)) {
// a to-one relationship (the build of a version): the target points back at the patched resource
for (const r of this.children(id, this.res.get(data.id)?.type ?? '')) delete r.rels[singular(type)];
const t = this.res.get(data.id);
if (t) t.rels[singular(type)] = id;
return new Response(null, { status: 204 });
}
// order of a screenshot / preview set
const ids = ((data ?? []) as { id: string }[]).map(d => d.id);
const base = Math.min(...ids.map(i => this.res.get(i)?.order ?? 0));
ids.forEach((i, k) => { const r = this.res.get(i); if (r) r.order = base + k; });
return new Response(null, { status: 204 });
}
let self = id ? this.res.get(id) : undefined;
// the age rating declaration is addressed by its app info's id
if (self && type && self.type !== type) self = this.children(self.id, type)[0];
if (!self) return json({ errors: [{ status: '404' }] }, 404);
const attrs = (body as { data: { attributes?: Record<string, unknown> } }).data.attributes ?? {};
for (const [k, v] of Object.entries((body as { data: { relationships?: Record<string, { data: { id: string } | null }> } }).data.relationships ?? {})) {
if (v.data) self.rels[k] = v.data.id; else delete self.rels[k];
}
// inline creates (subscription prices, introductory offers) become children of the patched resource
for (const inc of (body as { included?: { type: string; attributes?: Record<string, unknown>; relationships?: Record<string, { data: { id: string } }> }[] }).included ?? [])
this.put({ type: inc.type, id: `${inc.type.slice(0, 8)}-${this.next}`, attributes: { ...(inc.attributes ?? {}) },
rels: { [singular(self.type)]: self.id, ...Object.fromEntries(Object.entries(inc.relationships ?? {}).map(([k, v]) => [k, v.data.id])) } });
if (attrs.uploaded === true && ASSETS.test(self.type)) {
const { uploaded: _u, ...rest } = attrs;
Object.assign(self.attributes, rest, { assetDeliveryState: { state: this.parts.includes(self.id) ? 'COMPLETE' : 'FAILED' } });
delete self.attributes.uploadOperations;
} else Object.assign(self.attributes, attrs);
return json({ data: this.view(self) });
}
if (method === 'DELETE') {
if (!id || !this.res.has(id)) return json({ errors: [{ status: '404' }] }, 404);
this.res.delete(id);
return new Response(null, { status: 204 });
}
return json({ errors: [{ status: '599' }] }, 599);
}
}
// ------------------------------------------------------------------------------------------------------------ Play
type PlayState = { details: Record<string, unknown>; listings: Record<string, Record<string, unknown>>; images: Record<string, string[]>; tracks: Record<string, { releases: unknown[] }>; bundles: number[] };
const clone = <T>(x: T): T => JSON.parse(JSON.stringify(x)) as T;
export class FakePlay {
live: PlayState = { details: {}, listings: {}, images: {}, tracks: {}, bundles: [] };
edits = new Map<string, PlayState>();
committed: string[] = [];
products = new Map<string, Record<string, unknown>>();
subs = new Map<string, Record<string, unknown>>();
offers = new Map<string, Record<string, unknown>>();
next = 1;
/** every Data safety CSV that was posted */
dataSafety: string[] = [];
/** versionCode the next bundle upload reports */
bundleCode = 42;
handle(method: string, url: string, body: unknown): Response {
const u = new URL(url);
const p = u.pathname.replace(/^\/(upload\/)?androidpublisher\/v3\/applications\/[^/]+/, '');
const err = (code: number, status: string, message: string) => json({ error: { code, status, message } }, code);
let m: RegExpExecArray | null;
if (method === 'POST' && p === '/edits') { const id = `edit-${this.next++}`; this.edits.set(id, clone(this.live)); return json({ id, expiryTimeSeconds: '9999999999' }); }
if ((m = /^\/edits\/([^/:]+)(.*)$/.exec(p))) {
const [, id, rest] = m;
const e = this.edits.get(id!);
if (!e) return err(404, 'NOT_FOUND', 'Edit has been deleted');
if (method === 'DELETE' && rest === '') { this.edits.delete(id!); return new Response(null, { status: 204 }); }
if (method === 'POST' && rest === ':validate') return json({ id });
if (method === 'POST' && rest === ':commit') { this.live = e; this.edits.delete(id!); this.committed.push(id!); return json({ id }); }
if (rest === '/details') { if (method === 'PUT') e.details = body as Record<string, unknown>; return json(e.details); }
if (rest === '/listings') return json({ listings: Object.values(e.listings) });
if ((m = /^\/listings\/([^/]+)$/.exec(rest!))) { if (method === 'PUT') e.listings[m[1]!] = body as Record<string, unknown>; return json(e.listings[m[1]!] ?? {}); }
if ((m = /^\/listings\/([^/]+)\/(\w+)$/.exec(rest!))) {
const k = `${m[1]}/${m[2]}`;
if (method === 'DELETE') { e.images[k] = []; return json({ deleted: [] }); }
if (method === 'POST') { const h = new Bun.CryptoHasher('sha256').update(body as Uint8Array).digest('hex'); (e.images[k] ??= []).push(h); return json({ image: { sha256: h } }); }
return json({ images: (e.images[k] ?? []).map(sha256 => ({ sha256 })) });
}
if (rest === '/tracks') return json({ tracks: Object.entries(e.tracks).map(([track, t]) => ({ track, ...t })) });
if ((m = /^\/tracks\/(\w+)$/.exec(rest!))) {
if (method === 'PUT') e.tracks[m[1]!] = { releases: (body as { releases: unknown[] }).releases };
return json({ track: m[1], releases: e.tracks[m[1]!]?.releases ?? [] });
}
if (rest === '/bundles') {
if (method === 'POST') { const code = this.bundleCode; e.bundles.push(code); return json({ versionCode: code }); }
return json({ bundles: e.bundles.map(versionCode => ({ versionCode })) });
}
if (rest === '/apks') return json({ apks: [] });
return err(599, 'UNKNOWN', `fake play: ${method} ${p}`);
}
if (method === 'POST' && p === '/pricing:convertRegionPrices') {
const usd = body as { price: { units?: string; nanos?: number } };
const price = { currencyCode: 'USD', units: usd.price.units ?? '0', nanos: usd.price.nanos ?? 0 };
return json({ convertedRegionPrices: { US: { regionCode: 'US', price }, DE: { regionCode: 'DE', price: { ...price, currencyCode: 'EUR' } } },
convertedOtherRegionsPrice: { usdPrice: price, eurPrice: { ...price, currencyCode: 'EUR' } }, regionVersion: { version: '2022/02' } });
}
if (method === 'GET' && p === '/oneTimeProducts') return json({ oneTimeProducts: [...this.products.values()] });
if ((m = /^\/(?:oneTimeProducts|onetimeproducts)\/([^/:]+)$/.exec(p))) {
const id = decodeURIComponent(m[1]!);
if (method === 'PATCH') {
const cur = this.products.get(id);
if (!cur && u.searchParams.get('allowMissing') !== 'true') return err(404, 'NOT_FOUND', 'no product');
const b = clone(body as Record<string, unknown>);
const mask = (u.searchParams.get('updateMask') ?? '').split(',');
const next = cur ? { ...cur, ...(mask.includes('listings') ? { listings: b.listings } : {}), ...(mask.includes('purchaseOptions') ? { purchaseOptions: b.purchaseOptions } : {}) } : b;
for (const o of (next.purchaseOptions as { state?: string; purchaseOptionId: string }[] | undefined) ?? []) {
const was = (cur?.purchaseOptions as { state?: string; purchaseOptionId: string }[] | undefined)?.find(x => x.purchaseOptionId === o.purchaseOptionId)?.state;
o.state = was ?? 'DRAFT';
}
this.products.set(id, next);
return json(next);
}
const cur = this.products.get(id);
return cur ? json(cur) : err(404, 'NOT_FOUND', 'no product');
}
if ((m = /^\/oneTimeProducts\/([^/]+)\/purchaseOptions:batchUpdateStates$/.exec(p))) {
const cur = this.products.get(decodeURIComponent(m[1]!));
for (const o of (cur?.purchaseOptions as { state?: string }[] | undefined) ?? []) o.state = 'ACTIVE';
return json({});
}
if (method === 'GET' && p === '/subscriptions') return json({ subscriptions: [...this.subs.values()] });
if (method === 'POST' && p === '/subscriptions') {
const id = u.searchParams.get('productId')!;
if (this.subs.has(id)) return err(409, 'ALREADY_EXISTS', `Subscription ${id} already exists`);
const b = clone(body as Record<string, unknown>);
for (const bp of (b.basePlans as { state?: string }[] | undefined) ?? []) bp.state = 'DRAFT';
this.subs.set(id, b);
return json(b);
}
if ((m = /^\/subscriptions\/([^/:]+)$/.exec(p))) {
const id = decodeURIComponent(m[1]!);
const cur = this.subs.get(id);
if (!cur) return err(404, 'NOT_FOUND', 'no subscription');
if (method === 'PATCH') {
const b = clone(body as Record<string, unknown>);
const states = new Map(((cur.basePlans as { basePlanId: string; state?: string }[] | undefined) ?? []).map(x => [x.basePlanId, x.state]));
for (const bp of (b.basePlans as { basePlanId: string; state?: string }[] | undefined) ?? []) bp.state = states.get(bp.basePlanId) ?? 'DRAFT';
this.subs.set(id, { ...cur, ...b });
}
return json(this.subs.get(id));
}
if ((m = /^\/subscriptions\/([^/]+)\/basePlans\/([^/:]+):activate$/.exec(p))) {
const cur = this.subs.get(decodeURIComponent(m[1]!));
for (const bp of (cur?.basePlans as { basePlanId: string; state?: string }[] | undefined) ?? []) if (bp.basePlanId === m[2]) bp.state = 'ACTIVE';
return json(cur ?? {});
}
if ((m = /^\/subscriptions\/([^/]+)\/basePlans\/([^/]+)\/offers$/.exec(p))) {
const key = `${decodeURIComponent(m[1]!)}/${m[2]}`;
if (method === 'GET') return json({ subscriptionOffers: [...this.offers.entries()].filter(([k]) => k.startsWith(key + '/')).map(([, v]) => v) });
const oid = u.searchParams.get('offerId')!;
if (this.offers.has(`${key}/${oid}`)) return err(409, 'ALREADY_EXISTS', `Offer ${oid} already exists`);
const b = { ...clone(body as Record<string, unknown>), state: 'DRAFT' };
this.offers.set(`${key}/${oid}`, b);
return json(b);
}
if ((m = /^\/subscriptions\/([^/]+)\/basePlans\/([^/]+)\/offers\/([^/:]+)(:activate)?$/.exec(p))) {
const o = this.offers.get(`${decodeURIComponent(m[1]!)}/${m[2]}/${decodeURIComponent(m[3]!)}`);
if (!o) return err(404, 'NOT_FOUND', 'no offer');
if (m[4]) o.state = 'ACTIVE';
return json(o);
}
if (method === 'POST' && p === '/dataSafety') { this.dataSafety.push(String((body as { safetyLabels?: string }).safetyLabels ?? '')); return json({}); }
return err(599, 'UNKNOWN', `fake play: ${method} ${p}`);
}
}
// ------------------------------------------------------------------------------------------------- fetch + counting
export interface Stores { asc: FakeAsc; play: FakePlay; calls: Call[]; faults: Fault[]; failPart?: (n: number) => boolean }
const realFetch = globalThis.fetch;
/** Installs both fakes as fetch. Faults apply in order (`times`, default 1). */
export function installStores(): Stores {
const s: Stores = { asc: new FakeAsc(), play: new FakePlay(), calls: [], faults: [] };
let partNo = 0;
globalThis.fetch = (async (input: string | URL | Request, init?: RequestInit) => {
const url = String(input instanceof Request ? input.url : input);
const method = (init?.method ?? 'GET').toUpperCase();
const raw = init?.body;
let body: unknown = raw;
if (typeof raw === 'string') { try { body = JSON.parse(raw); } catch { body = raw; } }
if (url === 'https://oauth2.googleapis.com/token') return json({ access_token: 'ya29.test', expires_in: 3600 });
const path = url.replace(/^https:\/\/[^/]+/, '');
s.calls.push({ method, url, path, body });
const f = s.faults.find(x => (x.times ?? 1) > 0 && x.method === method && x.match.test(decodeURIComponent(path)));
if (f) f.times = (f.times ?? 1) - 1;
if (f?.when === 'before') return json({ errors: [{ status: String(f.status ?? 503), detail: 'injected' }] }, f.status ?? 503);
let res: Response;
if (url.startsWith(UPLOAD_HOST)) {
partNo++;
if (s.failPart?.(partNo)) return new Response('injected part failure', { status: 400 });
s.asc.parts.push(url.slice(UPLOAD_HOST.length));
res = new Response(null, { status: 200 });
} else if (url.startsWith(ASC)) res = s.asc.handle(method, path, body);
else if (url.startsWith('https://androidpublisher.googleapis.com/')) res = s.play.handle(method, url, body instanceof Uint8Array ? body : body);
else res = new Response(`fake: no route ${method} ${url}`, { status: 599 });
if (f?.when === 'after') return json({ errors: [{ status: String(f.status ?? 502), detail: 'injected after the write' }] }, f.status ?? 502);
return res;
}) as typeof fetch;
return s;
}
export function uninstallStores(): void { globalThis.fetch = realFetch; }
/** Store writes in a call log: every non-GET request, except Play's edit bookkeeping (opening and deleting a
* throwaway edit, validating it) and the price conversion, which change nothing on the store. */
export function writes(calls: Call[]): Call[] {
return calls.filter(c => c.method !== 'GET' && !(c.method === 'POST' && /\/edits$/.test(c.path)) && !(c.method === 'DELETE' && /\/edits\/[^/]+$/.test(c.path))
&& !/:validate$|pricing:convertRegionPrices$/.test(c.path));
}

4
tests/helpers/preload.ts Normal file
View file

@ -0,0 +1,4 @@
// Loaded before every test file (bunfig.toml): no real waiting between the re-checks of cli/lib/upsert.ts or for
// the store's list to settle after a create (cli/apple/iap.ts).
process.env.STOREBOUND_UPSERT_WAIT_MS = '0';
process.env.STOREBOUND_LIST_SETTLE_MS ??= '0';

640
tests/idempotency.test.ts Normal file
View file

@ -0,0 +1,640 @@
// Idempotency of every store write (docs/idempotency.md): each write command runs twice against stateful doubles of
// App Store Connect and Google Play (tests/helpers/fake-stores.ts) and the second run must not write anything. Plus
// the failure cases: a crash in the middle of a screenshot set, a create whose answer is lost (5xx after the store
// applied it), a create the store refuses as a duplicate (409), the list lag right after a create, Play edits on
// error paths and an unclear commit, the lock between two runs, and the retry rules of the API clients.
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, test } from 'bun:test';
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
import { hostname } from 'node:os';
import { join } from 'node:path';
import type { ShipConfig } from '../cli/lib/config';
import { parseArgs } from '../cli/lib/args';
import { acquireLock, lockKeys, staleLock } from '../cli/lib/lock';
import { AmbiguousWriteError, createOnce, findOrCreate, isConflict, readJournal, retrySafe, upsertTiming } from '../cli/lib/upsert';
import { keptPrefix } from '../cli/apple/asc';
import { sameRelease } from '../cli/play/release';
import { sameReleases } from '../cli/play/rollout';
import { playClient } from '../cli/play/client';
import { run as appleIap } from '../cli/apple/iap';
import { run as appleScreenshots } from '../cli/apple/screenshots';
import { run as appleSubscriptions } from '../cli/apple/subscriptions';
import { run as appleCpp } from '../cli/apple/cpp';
import { run as appleEvents } from '../cli/apple/events';
import { run as appleTestflight } from '../cli/apple/testflight';
import { run as appleReviewDetails } from '../cli/apple/review-details';
import { run as appleBundle } from '../cli/apple/bundle';
import { run as appleOfferCodes } from '../cli/apple/offer-codes';
import { run as applePpo } from '../cli/apple/ppo';
import { run as appleIapSubmit } from '../cli/apple/iap-submit';
import { run as appleRelease } from '../cli/apple/release';
import { run as appleEncryption } from '../cli/apple/encryption';
import { run as appleAdhoc } from '../cli/apple/adhoc';
import { run as playProducts } from '../cli/play/products';
import { run as playSubscriptions } from '../cli/play/subscriptions';
import { run as playImages } from '../cli/play/images';
import { run as playRollout } from '../cli/play/rollout';
import { run as playRelease } from '../cli/play/release';
import { run as appleListing } from '../cli/apple/listing';
import { run as appleAppinfo } from '../cli/apple/appinfo';
import { run as applePromoted } from '../cli/apple/promoted';
import { run as appleWinBack } from '../cli/apple/win-back';
import { run as applePreorder } from '../cli/apple/preorder';
import { run as playListing } from '../cli/play/listing';
import { run as playDataSafety } from '../cli/play/data-safety';
import { run as playUpload } from '../cli/play/upload';
import { run as prices } from '../cli/prices';
import { makeAab } from './build-fixtures';
import { ascEnv, BASE, capture, playEnv, project, restore } from './helpers/store-mock';
import { installStores, type Stores, uninstallStores, writes } from './helpers/fake-stores';
let s: Stores;
beforeAll(() => { ascEnv(); playEnv(); });
afterAll(restore);
beforeEach(() => { upsertTiming.waitMs = 0; s = installStores(); });
afterEach(() => { uninstallStores(); });
const APP = '6400000101';
const LISTING = '# Play\n\n## English\n<!-- play: en-US -->\n\n### Play short description\n```\nNotes.\n```\n\n### Play full description\n```\nNotes for everyone.\n```\n\n### What\'s new\n```\nFixes.\n```\n';
const png = (n: number): Uint8Array => new Uint8Array([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, n, n + 1, n + 2]);
const CONFIG: ShipConfig = {
...BASE,
languages: [{ id: 'en', apple: 'en-US', play: 'en-US' }, { id: 'de', apple: 'de-DE', play: 'de-DE' }],
products: [
{ id: 'com.example.notes.pro', type: 'non_consumable', usd: 2.99, listings: { en: { title: 'Pro', description: 'Every feature.' }, de: { title: 'Pro', description: 'Alles.' } } },
{ id: 'com.example.notes.tip', type: 'consumable', usd: 0.99, listings: { en: { title: 'Tip', description: 'Say thanks.' } },
offerCodes: [{ name: 'Launch', free: true, eligibility: ['NON_SPENDER'] } as never] },
],
apple: {
...BASE.apple!, testflight: { groups: [{ name: 'Friends' }, { name: 'Public', internal: false } as never] } as never,
customProductPages: [{ name: 'Spring', promotionalText: { en: 'Spring sale' } } as never],
events: [{ referenceName: 'Spring event', badge: 'SPECIAL_EVENT', listings: { en: { name: 'Spring', shortDescription: 'Short', longDescription: 'Long text' } },
schedule: { publishStart: '2030-03-01T00:00:00Z', eventStart: '2030-03-02T00:00:00Z', eventEnd: '2030-03-09T00:00:00Z' } } as never],
},
};
/** Runs a command twice; returns the writes of each run. The second must be empty. */
async function twice(run: () => Promise<void>): Promise<{ first: string[]; second: string[] }> {
const a = s.calls.length;
const r1 = await capture(run);
if (r1.exit) throw new Error(`first run exit ${r1.exit}:\n${r1.out}\n${r1.err}`);
const b = s.calls.length;
const r2 = await capture(run);
if (r2.exit) throw new Error(`second run exit ${r2.exit}:\n${r2.out}\n${r2.err}`);
const show = (from: number, to?: number) => writes(s.calls.slice(from, to)).map(c => `${c.method} ${decodeURIComponent(c.path).replace(/\?.*/, '')}`);
return { first: show(a, b), second: show(b) };
}
const count = (type: string): number => s.asc.all(type).length;
describe('App Store Connect: a second run writes nothing', () => {
test('apple iap: product, localizations, price schedule, availability once', async () => {
const p = project(CONFIG);
const r = await twice(() => appleIap(p.ctx([])));
expect(r.first).toContain('POST /v2/inAppPurchases');
expect(r.second).toEqual([]);
expect(count('inAppPurchases')).toBe(2);
expect(count('inAppPurchaseLocalizations')).toBe(3);
expect(count('inAppPurchasePriceSchedules')).toBe(2);
expect(count('inAppPurchaseAvailabilities')).toBe(2);
});
test('apple screenshots: sets and screenshots once, in order', async () => {
const files: Record<string, Uint8Array> = {};
for (const l of ['en', 'de']) for (const d of ['ios69', 'ios65']) for (const n of [1, 2, 3]) files[`store/renders/${l}/${d}/0${n}.png`] = png(n + (d === 'ios65' ? 10 : 0) + (l === 'de' ? 20 : 0));
const p = project(CONFIG, files);
const v = s.asc.put({ type: 'appStoreVersions', id: 'ver-1', attributes: { versionString: '1.0.0', appVersionState: 'PREPARE_FOR_SUBMISSION', platform: 'IOS' }, rels: { app: APP } });
for (const loc of ['en-US', 'de-DE']) s.asc.put({ type: 'appStoreVersionLocalizations', id: `loc-${loc}`, attributes: { locale: loc }, rels: { appStoreVersion: v.id } });
const r = await twice(() => appleScreenshots(p.ctx([])));
expect(r.first.filter(x => x === 'POST /v1/appScreenshotSets')).toHaveLength(4);
expect(r.second).toEqual([]);
expect(count('appScreenshotSets')).toBe(4);
expect(count('appScreenshots')).toBe(12);
});
test('apple cpp create, events create, testflight groups, review-details, bundle, offer-codes, ppo create', async () => {
const p = project({ ...CONFIG, shots: { variants: { b: {} } } as never }, { '.storebound/credentials.json': JSON.stringify({ reviewContact: { firstName: 'Ex', lastName: 'Ample', phone: '+1 555 0100', email: 'review@example.com' } }) });
s.asc.put({ type: 'appStoreVersions', id: 'ver-1', attributes: { versionString: '1.0.0', appVersionState: 'PREPARE_FOR_SUBMISSION', platform: 'IOS' }, rels: { app: APP } });
s.asc.put({ type: 'inAppPurchases', id: 'iap-tip', attributes: { productId: 'com.example.notes.tip', state: 'READY_TO_SUBMIT' }, rels: { app: APP } });
for (const [name, run] of [
['cpp', () => appleCpp(p.ctx(['create']))],
['events', () => appleEvents(p.ctx(['create']))],
['testflight', () => appleTestflight(p.ctx(['groups']))],
['review-details', () => appleReviewDetails(p.ctx([]))],
['bundle', () => appleBundle(p.ctx([]))],
['offer-codes', () => appleOfferCodes(p.ctx([]))],
['ppo', () => applePpo(p.ctx(['create', '--variant', 'b']))],
] as [string, () => Promise<void>][]) {
const r = await twice(run);
expect([name, r.first.length > 0]).toEqual([name, true]);
expect([name, r.second]).toEqual([name, []]);
}
expect(count('appCustomProductPages')).toBe(1);
expect(count('appEvents')).toBe(1);
expect(count('appEventLocalizations')).toBe(1);
expect(count('betaGroups')).toBe(2);
expect(count('appStoreReviewDetails')).toBe(1);
expect(count('bundleIds')).toBe(1);
expect(count('inAppPurchaseOfferCodes')).toBe(1);
expect(count('appStoreVersionExperiments')).toBe(1);
expect(count('appStoreVersionExperimentTreatments')).toBe(1);
});
});
describe('App Store Connect: more commands, twice', () => {
test('apple subscriptions: group, subscription, localizations, availability, prices once', async () => {
const cfg: ShipConfig = { ...CONFIG, subscriptions: [{ name: 'Pro', listings: { en: { name: 'Pro' } }, subscriptions: [
{ id: 'com.example.notes.monthly', period: 'P1M', level: 1, usd: 4.99, listings: { en: { title: 'Monthly', description: 'Pro every month.' }, de: { title: 'Monatlich', description: 'Pro jeden Monat.' } } },
] } as never] };
const p = project(cfg);
const r = await twice(() => appleSubscriptions(p.ctx(['--yes'])));
expect(r.first).toContain('POST /v1/subscriptions');
expect(r.second).toEqual([]);
expect([count('subscriptionGroups'), count('subscriptions'), count('subscriptionLocalizations'), count('subscriptionAvailabilities'), count('subscriptionPrices')]).toEqual([1, 1, 2, 1, 3]);
});
test('apple iap-submit --no-submit: one draft, the item once', async () => {
const p = project({ ...CONFIG, products: [CONFIG.products![1]!] });
s.asc.put({ type: 'appStoreVersions', id: 'ver-1', attributes: { versionString: '1.0.0', appVersionState: 'READY_FOR_SALE', platform: 'IOS' }, rels: { app: APP } });
s.asc.put({ type: 'inAppPurchases', id: 'iap-old', attributes: { productId: 'com.example.notes.old', inAppPurchaseType: 'CONSUMABLE', state: 'APPROVED' }, rels: { app: APP } });
s.asc.put({ type: 'inAppPurchases', id: 'iap-tip', attributes: { productId: 'com.example.notes.tip', inAppPurchaseType: 'CONSUMABLE', state: 'READY_TO_SUBMIT' }, rels: { app: APP } });
s.asc.put({ type: 'inAppPurchaseVersions', id: 'iapv-tip', attributes: { version: 1, state: 'PREPARE_FOR_SUBMISSION' }, rels: { inAppPurchase: 'iap-tip' } });
const r = await twice(() => appleIapSubmit(p.ctx(['--no-submit', '--yes'])));
expect(r.first).toEqual(['POST /v1/reviewSubmissions', 'POST /v1/reviewSubmissionItems']);
expect(r.second).toEqual([]);
expect([count('reviewSubmissions'), count('reviewSubmissionItems')]).toEqual([1, 1]);
});
test('apple release --phased on, apple encryption, apple adhoc register', async () => {
const p = project({ ...CONFIG, apple: { ...CONFIG.apple!, usesNonExemptEncryption: false } }, { '.storebound/devices.csv': 'name,udid,platform\nTest phone,00008101-001A2B3C4D5E6F70,IOS\n' });
s.asc.put({ type: 'appStoreVersions', id: 'ver-2', attributes: { versionString: '1.1.0', appVersionState: 'PREPARE_FOR_SUBMISSION', platform: 'IOS' }, rels: { app: APP } });
s.asc.put({ type: 'builds', id: 'b-7', attributes: { version: '7', usesNonExemptEncryption: null, expired: false }, rels: { app: APP } });
for (const [name, run] of [
['release', () => appleRelease(p.ctx(['--phased', 'on']))],
['encryption', () => appleEncryption(p.ctx([]))],
['adhoc', () => appleAdhoc(p.ctx(['register', '--yes']))],
] as [string, () => Promise<void>][]) {
const r = await twice(run);
expect([name, r.first.length]).toEqual([name, 1]);
expect([name, r.second]).toEqual([name, []]);
}
expect([count('appStoreVersionPhasedReleases'), count('devices')]).toEqual([1, 1]);
});
});
describe('Google Play: a second run writes nothing', () => {
test('play products: created, activated, then unchanged', async () => {
const p = project(CONFIG);
const r = await twice(() => playProducts(p.ctx([])));
expect(r.first.some(x => x.startsWith('PATCH /androidpublisher/v3/applications/com.example.notes/onetimeproducts/'))).toBe(true);
expect(r.second).toEqual([]);
expect(s.play.products.size).toBe(2);
});
test('play images: uploaded once; the second run compares and commits nothing', async () => {
const files: Record<string, string | Uint8Array> = { 'store/listing.md': LISTING, 'store/icons/play-512.png': png(1) };
files['store/renders/en/feature/1024x500.png'] = png(2);
for (const [sub, base] of [['play', 10], ['tab7', 20], ['tab10', 30]] as const) for (const n of [1, 2]) files[`store/renders/en/${sub}/0${n}.png`] = png(base + n);
const p = project(CONFIG, files);
const r = await twice(() => playImages(p.ctx([])));
expect(r.first.filter(x => x.startsWith('POST /upload/'))).toHaveLength(8);
expect(r.second).toEqual([]);
expect(s.play.committed).toHaveLength(1);
expect(s.play.edits.size).toBe(0);
});
test('play subscriptions: created with base plan and intro offer once', async () => {
const cfg: ShipConfig = { ...CONFIG, subscriptions: [{ name: 'Pro', listings: { en: { name: 'Pro' } }, subscriptions: [
{ id: 'com.example.notes.monthly', period: 'P1M', level: 1, usd: 4.99, listings: { en: { title: 'Monthly', description: 'Pro every month.', benefits: ['Sync'] } }, intro: { type: 'free', duration: 'P1W' } },
] } as never] };
const p = project(cfg);
const r = await twice(() => playSubscriptions(p.ctx(['--yes'])));
expect(r.first.some(x => x.endsWith('/subscriptions'))).toBe(true);
expect(r.second).toEqual([]);
expect(s.play.subs.size).toBe(1);
expect(s.play.offers.size).toBe(1);
});
test('play rollout and play release --version-code: the same state is not committed again', async () => {
s.play.live.tracks.production = { releases: [{ versionCodes: ['41'], status: 'inProgress', userFraction: 0.1 }] };
s.play.live.bundles = [41, 42];
const p = project(CONFIG, { 'store/listing.md': LISTING });
let r = await twice(() => playRollout(p.ctx(['--fraction', '0.2', '--yes'])));
expect(r.first).toContain('PUT /androidpublisher/v3/applications/com.example.notes/edits/edit-1/tracks/production');
expect(r.second).toEqual([]);
r = await twice(() => playRelease(p.ctx(['--version-code', '42', '--internal', 'completed'])));
expect(r.first.some(x => x.endsWith('/tracks/internal'))).toBe(true);
expect(r.second).toEqual([]);
expect(s.play.edits.size).toBe(0);
});
});
const APPLE_MD = '# App Store\n\n## en-US · English\n\n### subtitle\n```\nNotes that sync\n```\n\n### keywords\n```\nnotes,lists\n```\n\n### description\n```\nNotes for everyone.\n```\n\n### promotionalText\n```\nNow with lists.\n```\n\n### whatsNew\n```\nFaster sync.\n```\n';
/** An app with a released 1.0.0, an editable app info and a processed build 9 of 1.1.0. */
function appleListingStore(): void {
s.asc.put({ type: 'appStoreVersions', id: 'ver-old', attributes: { versionString: '1.0.0', appVersionState: 'READY_FOR_SALE', platform: 'IOS' }, rels: { app: APP } });
s.asc.put({ type: 'appInfos', id: 'info-1', attributes: { state: 'PREPARE_FOR_SUBMISSION' }, rels: { app: APP } });
s.asc.put({ type: 'builds', id: 'b-9', attributes: { version: '9', processingState: 'VALID' }, rels: { app: APP } });
}
describe('listing, appinfo, data safety and the owner commands: a second run writes nothing', () => {
test('apple listing: version created (re-checked first), build attached, localizations created once; then nothing', async () => {
appleListingStore();
const p = project(CONFIG, { 'store/appstore.md': APPLE_MD });
const r = await twice(() => appleListing(p.ctx(['--version', '1.1.0', '--build', '9'])));
expect(r.first).toEqual(['POST /v1/appStoreVersions', expect.stringMatching(/^PATCH \/v1\/appStoreVersions\/.+\/relationships\/build$/), 'POST /v1/appInfoLocalizations', 'POST /v1/appStoreVersionLocalizations']);
expect(r.second).toEqual([]);
expect([count('appStoreVersions'), count('appInfoLocalizations'), count('appStoreVersionLocalizations')]).toEqual([2, 1, 1]);
// a changed text: only that field of that localization is PATCHed, the build is not attached again
const q = project(CONFIG, { 'store/appstore.md': APPLE_MD.replace('Notes for everyone.', 'Notes for all.') });
const before = s.calls.length;
await capture(() => appleListing(q.ctx(['--version', '1.1.0', '--build', '9'])));
const w = writes(s.calls.slice(before));
expect(w.map(c => `${c.method} ${c.path.replace(/\/[^/]+$/, '/:id')}`)).toEqual(['PATCH /v1/appStoreVersionLocalizations/:id']);
expect(Object.keys((w[0]!.body as { data: { attributes: object } }).data.attributes)).toEqual(['description']);
});
test('apple listing: a lost answer on the version create is found by the re-check, not created twice', async () => {
appleListingStore();
const p = project(CONFIG, { 'store/appstore.md': APPLE_MD });
s.faults.push({ method: 'POST', match: /^\/v1\/appStoreVersions$/, when: 'after', status: 502 });
const r = await capture(() => appleListing(p.ctx(['--version', '1.1.0'])));
expect(r.exit).toBe(null);
expect(r.err).toContain('no clear answer');
expect(count('appStoreVersions')).toBe(2);
expect(s.calls.filter(c => c.method === 'POST' && c.path === '/v1/appStoreVersions')).toHaveLength(1);
expect(count('appStoreVersionLocalizations')).toBe(1);
});
test('apple listing: a localization the list does not show yet answers 409; it is used and updated, not created twice', async () => {
appleListingStore();
s.asc.put({ type: 'appStoreVersions', id: 'ver-2', attributes: { versionString: '1.1.0', appVersionState: 'PREPARE_FOR_SUBMISSION', platform: 'IOS', copyright: `${new Date().getFullYear()} Example Notes`, releaseType: 'MANUAL' }, rels: { app: APP } });
s.asc.put({ type: 'appStoreVersionLocalizations', id: 'vloc-en', attributes: { locale: 'en-US', description: 'Old.' }, rels: { appStoreVersion: 'ver-2' }, hiddenReads: 1 } as never);
const p = project(CONFIG, { 'store/appstore.md': APPLE_MD });
const r = await capture(() => appleListing(p.ctx(['--version', '1.1.0'])));
expect(r.exit).toBe(null);
expect(r.out).toContain('already exists, using it');
expect(count('appStoreVersionLocalizations')).toBe(1);
expect(s.asc.res.get('vloc-en')!.attributes.description).toBe('Notes for everyone.');
});
test('play listing: one committed edit with only what differs; the second run opens no write edit and commits nothing', async () => {
s.play.live.listings['de-DE'] = { language: 'de-DE', title: 'Notizen' };
const p = project(CONFIG, { 'store/listing.md': LISTING });
const r = await twice(() => playListing(p.ctx([])));
expect(r.first).toEqual(['PUT /androidpublisher/v3/applications/com.example.notes/edits/edit-2/details', 'PUT /androidpublisher/v3/applications/com.example.notes/edits/edit-2/listings/en-US',
'POST /androidpublisher/v3/applications/com.example.notes/edits/edit-2:commit']);
expect(r.second).toEqual([]);
expect(s.play.committed).toHaveLength(1);
expect(s.play.edits.size).toBe(0);
expect(s.play.live.listings['de-DE']).toEqual({ language: 'de-DE', title: 'Notizen' }); // a language not in the file is left alone
});
test('play listing: the store reached the wanted state between plan and edit: the edit is deleted, not committed', async () => {
const p = project(CONFIG, { 'store/listing.md': LISTING });
// the plan sees an empty listing; right after it (before the write edit opens) another run makes it current
const orig = s.play.handle.bind(s.play);
let planned = false;
s.play.handle = (method, url, body) => {
if (method === 'DELETE' && /\/edits\/edit-1$/.test(url) && !planned) {
planned = true;
s.play.live.details = { defaultLanguage: 'en-US', contactEmail: 'support@example.com', contactWebsite: 'https://example.com' };
s.play.live.listings['en-US'] = { language: 'en-US', title: 'Example Notes', shortDescription: 'Notes.', fullDescription: 'Notes for everyone.' };
}
return orig(method, url, body);
};
const r = await capture(() => playListing(p.ctx([])));
expect(r.out).toContain('nothing committed');
expect(s.play.committed).toEqual([]);
expect(writes(s.calls)).toEqual([]);
expect(s.play.edits.size).toBe(0);
});
test('apple appinfo: categories, age rating, content rights, price and availability set once, then compared and left', async () => {
s.asc.put({ type: 'appInfos', id: 'info-1', attributes: { state: 'PREPARE_FOR_SUBMISSION' }, rels: { app: APP } });
s.asc.put({ type: 'ageRatingDeclarations', id: 'age-1', attributes: {}, rels: { appInfo: 'info-1' } });
s.asc.put({ type: 'appPricePoints', id: 'app-pp-free', attributes: { customerPrice: '0.0' }, rels: { app: APP, territory: 'USA' } });
const cfg: ShipConfig = { ...CONFIG, apple: { ...CONFIG.apple!, info: { categories: { primary: 'PRODUCTIVITY' }, contentRights: 'DOES_NOT_USE_THIRD_PARTY_CONTENT', ageRating: { advertising: true } } as never } };
const p = project(cfg);
const r = await twice(() => appleAppinfo(p.ctx([])));
expect(r.first).toEqual(['PATCH /v1/appInfos/info-1', 'PATCH /v1/ageRatingDeclarations/info-1', `PATCH /v1/apps/${APP}`, 'POST /v1/appPriceSchedules', 'POST /v2/appAvailabilities']);
expect(r.second).toEqual([]);
expect(s.asc.res.get('age-1')!.attributes.advertising).toBe(true);
// one answer changed in the config: only the age rating is PATCHed
const q = project({ ...cfg, apple: { ...cfg.apple!, info: { ...cfg.apple!.info!, ageRating: { advertising: true, contests: 'INFREQUENT_OR_MILD' } } as never } });
const before = s.calls.length;
await capture(() => appleAppinfo(q.ctx([])));
expect(writes(s.calls.slice(before)).map(c => `${c.method} ${c.path}`)).toEqual(['PATCH /v1/ageRatingDeclarations/info-1']);
});
test('play data-safety: the same CSV is not sent again (hash in .storebound/); a changed one or --force is', async () => {
const ds = { collectsData: true, encryptedInTransit: true, deletionRequest: false, data: { PSL_DEVICE_ID: { shared: false, collect: ['analytics'] } } };
const p = project({ ...CONFIG, dataSafety: ds as never });
const r = await twice(() => playDataSafety(p.ctx([])));
expect(r.first).toEqual(['POST /androidpublisher/v3/applications/com.example.notes/dataSafety']);
expect(r.second).toEqual([]);
expect(existsSync(join(p.root, '.storebound', 'data-safety.sha256'))).toBe(true);
let before = s.calls.length;
await capture(() => playDataSafety(p.ctx(['--force'])));
expect(writes(s.calls.slice(before))).toHaveLength(1);
const q = project({ ...CONFIG, dataSafety: { ...ds, data: { ...ds.data, PSL_EMAIL: { shared: false, collect: ['functionality'] } } } as never });
mkdirSync(join(q.root, '.storebound'), { recursive: true });
writeFileSync(join(q.root, '.storebound', 'data-safety.sha256'), readFileSync(join(p.root, '.storebound', 'data-safety.sha256')));
before = s.calls.length;
await capture(() => playDataSafety(q.ctx([])));
expect(writes(s.calls.slice(before))).toHaveLength(1);
expect(s.play.dataSafety).toHaveLength(3);
});
test('apple promoted: created and ordered once', async () => {
s.asc.put({ type: 'inAppPurchases', id: 'iap-pro', attributes: { productId: 'com.example.notes.pro', state: 'APPROVED' }, rels: { app: APP } });
s.asc.put({ type: 'inAppPurchases', id: 'iap-tip', attributes: { productId: 'com.example.notes.tip', state: 'APPROVED' }, rels: { app: APP } });
const p = project({ ...CONFIG, apple: { ...CONFIG.apple!, promoted: { products: ['com.example.notes.tip', 'com.example.notes.pro'] } } });
const r = await twice(() => applePromoted(p.ctx(['--yes'])));
expect(r.first.filter(x => x === 'POST /v1/promotedPurchases')).toHaveLength(2);
expect(r.second).toEqual([]);
expect(count('promotedPurchases')).toBe(2);
});
test('apple win-back: the missing offer is created once', async () => {
const g = s.asc.put({ type: 'subscriptionGroups', id: 'grp-1', attributes: { referenceName: 'Pro' }, rels: { app: APP } });
s.asc.put({ type: 'subscriptions', id: 'sub-1', attributes: { productId: 'com.example.notes.monthly', state: 'APPROVED' }, rels: { subscriptionGroup: g.id } });
const cfg: ShipConfig = { ...CONFIG, subscriptions: [{ name: 'Pro', listings: { en: { name: 'Pro' } }, subscriptions: [
{ id: 'com.example.notes.monthly', period: 'P1M', level: 1, usd: 4.99, listings: { en: { title: 'Monthly', description: 'Pro every month.' } },
winBack: [{ id: 'come-back', type: 'free', duration: 'P1M', paidMonths: 1, lapsedMonths: { min: 1, max: 6 }, start: '2099-01-01' }] },
] } as never] };
const p = project(cfg);
const r = await twice(() => appleWinBack(p.ctx(['--yes'])));
expect(r.first).toEqual(['POST /v1/winBackOffers']);
expect(r.second).toEqual([]);
expect(count('winBackOffers')).toBe(1);
});
test('apple preorder enable: territories set once; the same date again changes nothing', async () => {
const av = s.asc.put({ type: 'appAvailabilities', id: 'avail-1', attributes: { availableInNewTerritories: true }, rels: { app: APP } });
for (const t of ['USA', 'DEU']) s.asc.put({ type: 'territoryAvailabilities', id: `ta-${t}`, attributes: { available: true, contentStatuses: ['CANNOT_SELL'] }, rels: { appAvailability: av.id, territory: t } });
const p = project(CONFIG);
const date = new Date(Date.now() + 30 * 86400_000).toISOString().slice(0, 10);
const r = await twice(() => applePreorder(p.ctx(['enable', '--release-date', date, '--yes'])));
expect(r.first).toEqual(['PATCH /v1/territoryAvailabilities/ta-USA', 'PATCH /v1/territoryAvailabilities/ta-DEU']);
expect(r.second).toEqual([]);
});
test('apple testflight testers: created in their groups once', async () => {
s.asc.put({ type: 'betaGroups', id: 'grp-friends', attributes: { name: 'Friends', isInternalGroup: false }, rels: { app: APP } });
const p = project(CONFIG, { '.storebound/testers.csv': 'email,first_name,groups\ntester1@example.com,Ex,Friends\ntester2@example.com,Am,Friends\n' });
const r = await twice(() => appleTestflight(p.ctx(['testers', '--yes'])));
expect(r.first).toEqual(['POST /v1/betaTesters', 'POST /v1/betaTesters']);
expect(r.second).toEqual([]);
expect(count('betaTesters')).toBe(2);
});
test('play upload: uploaded once; the same versionCode on the same track is not sent again', async () => {
const p = project(CONFIG, { 'store/listing.md': LISTING, 'build/android/app.aab': makeAab('com.example.notes', 42) });
const r = await twice(() => playUpload(p.ctx(['--yes'])));
expect(r.first).toEqual(['POST /upload/androidpublisher/v3/applications/com.example.notes/edits/edit-2/bundles', 'PUT /androidpublisher/v3/applications/com.example.notes/edits/edit-2/tracks/internal',
'POST /androidpublisher/v3/applications/com.example.notes/edits/edit-2:commit']);
expect(r.second).toEqual([]);
expect(s.play.committed).toHaveLength(1);
});
test('prices import (Play): changed regions written once, then the diff is empty', async () => {
const p = project({ ...CONFIG, products: [CONFIG.products![0]!] }, { 'store/prices.csv': 'product,store,region,currency,price\ncom.example.notes.pro,play,DE,EUR,3.49\n' });
await capture(() => playProducts(p.ctx([])));
const r = await twice(() => prices(p.ctx(['import', '--store', 'play', '--yes'])));
expect(r.first.some(x => x.startsWith('PATCH /androidpublisher/v3/applications/com.example.notes/onetimeproducts/'))).toBe(true);
expect(r.second).toEqual([]);
});
});
describe('crash midway and unclear answers', () => {
test('screenshot set: an upload that dies on the 3rd file leaves a prefix; the re-run finishes it without duplicates, in order', async () => {
const files: Record<string, Uint8Array> = {};
for (const n of [1, 2, 3, 4]) files[`store/renders/en/ios69/0${n}.png`] = png(n);
const cfg = { ...CONFIG, languages: [{ id: 'en', apple: 'en-US', play: 'en-US' }] };
const p = project(cfg, files);
const v = s.asc.put({ type: 'appStoreVersions', id: 'ver-1', attributes: { versionString: '1.0.0', appVersionState: 'PREPARE_FOR_SUBMISSION', platform: 'IOS' }, rels: { app: APP } });
s.asc.put({ type: 'appStoreVersionLocalizations', id: 'loc-en', attributes: { locale: 'en-US' }, rels: { appStoreVersion: v.id } });
// an older set: two screenshots, the first already the wanted 01.png
const set = s.asc.put({ type: 'appScreenshotSets', id: 'set-old', attributes: { screenshotDisplayType: 'APP_IPHONE_67' }, rels: { appStoreVersionLocalization: 'loc-en' } });
const md5 = (b: Uint8Array) => new Bun.CryptoHasher('md5').update(b).digest('hex');
s.asc.put({ type: 'appScreenshots', id: 'shot-a', attributes: { sourceFileChecksum: md5(png(1)), assetDeliveryState: { state: 'COMPLETE' }, fileName: '01.png' }, rels: { appScreenshotSet: set.id } });
s.asc.put({ type: 'appScreenshots', id: 'shot-x', attributes: { sourceFileChecksum: 'stale', assetDeliveryState: { state: 'COMPLETE' }, fileName: 'old.png' }, rels: { appScreenshotSet: set.id } });
s.failPart = n => n === 2; // the 2nd uploaded part (03.png) fails
const r1 = await capture(() => appleScreenshots(p.ctx(['--only', 'en-US']))).catch(e => ({ out: String(e), err: '', exit: 1 }));
expect(r1.out + r1.err).toContain('03.png part 0 -> 400');
expect(s.asc.children(set.id, 'appScreenshots').map(x => x.id)).toContain('shot-a');
s.failPart = undefined;
const r2 = await capture(() => appleScreenshots(p.ctx(['--only', 'en-US'])));
expect(r2.exit).toBe(null);
const shots = s.asc.children(set.id, 'appScreenshots');
expect(shots.map(x => x.attributes.sourceFileChecksum)).toEqual([1, 2, 3, 4].map(n => md5(png(n))));
expect(shots.every(x => (x.attributes.assetDeliveryState as { state: string }).state === 'COMPLETE')).toBe(true);
expect(shots[0]!.id).toBe('shot-a'); // the matching first screenshot was kept, not uploaded again
expect(count('appScreenshotSets')).toBe(1);
const before = s.calls.length;
await capture(() => appleScreenshots(p.ctx(['--only', 'en-US'])));
expect(writes(s.calls.slice(before))).toEqual([]);
});
test('IAP create: a 5xx after Apple created it is found, not created again', async () => {
const p = project({ ...CONFIG, products: [CONFIG.products![0]!] });
s.faults.push({ method: 'POST', match: /^\/v2\/inAppPurchases$/, when: 'after', status: 502 });
const r = await capture(() => appleIap(p.ctx([])));
expect(r.exit).toBe(null);
expect(r.err).toContain('no clear answer');
expect(count('inAppPurchases')).toBe(1);
expect(s.calls.filter(c => c.method === 'POST' && c.path === '/v2/inAppPurchases')).toHaveLength(1);
expect(count('inAppPurchaseLocalizations')).toBe(2);
});
test('IAP create: a 5xx before anything happened is tried once more (product ids are unique, so that is safe)', async () => {
const p = project({ ...CONFIG, products: [CONFIG.products![0]!] });
s.faults.push({ method: 'POST', match: /^\/v2\/inAppPurchases$/, when: 'before', status: 503 });
const r = await capture(() => appleIap(p.ctx([])));
expect(r.exit).toBe(null);
expect(count('inAppPurchases')).toBe(1);
expect(s.calls.filter(c => c.method === 'POST' && c.path === '/v2/inAppPurchases')).toHaveLength(2);
});
test('IAP create: crash right after the create (process gone); the re-run during the list lag uses the journal, no duplicate', async () => {
const p = project({ ...CONFIG, products: [CONFIG.products![0]!] });
// first run: the product is created, then the run dies on the localization
s.faults.push({ method: 'POST', match: /^\/v1\/inAppPurchaseLocalizations$/, when: 'before', status: 400 });
s.asc.lag.inAppPurchases = 50;
await capture(() => appleIap(p.ctx([]))).catch(e => expect(String(e)).toContain('injected'));
expect(count('inAppPurchases')).toBe(1);
expect(Object.keys(readJournal(p.root))).toEqual(['apple:com.example.notes:iap:com.example.notes.pro']);
const r = await capture(() => appleIap(p.ctx([])));
expect(r.out).toContain('does not show it yet, using it');
expect(count('inAppPurchases')).toBe(1);
expect(count('inAppPurchaseLocalizations')).toBe(2);
});
test('a 409 duplicate is treated as found (no second create, no error)', async () => {
const p = project({ ...CONFIG, products: [CONFIG.products![0]!] });
s.asc.put({ type: 'inAppPurchases', id: 'iap-old', attributes: { productId: 'com.example.notes.pro', state: 'MISSING_METADATA' }, rels: { app: APP }, hiddenReads: 1 } as never);
const r = await capture(() => appleIap(p.ctx([])));
expect(r.exit).toBe(null);
expect(r.out).toContain('already exists, using it');
expect(count('inAppPurchases')).toBe(1);
});
test('apple subscriptions: a lost answer on the subscription create is found again; the second run creates nothing', async () => {
const cfg: ShipConfig = { ...CONFIG, subscriptions: [{ name: 'Pro', listings: { en: { name: 'Pro' } }, subscriptions: [
{ id: 'com.example.notes.monthly', period: 'P1M', level: 1, usd: 4.99, listings: { en: { title: 'Monthly', description: 'Pro every month.' } } },
] } as never] };
const p = project(cfg);
s.faults.push({ method: 'POST', match: /^\/v1\/subscriptions$/, when: 'after', status: 504 });
await capture(() => appleSubscriptions(p.ctx(['--yes'])));
expect(count('subscriptions')).toBe(1);
expect(count('subscriptionGroups')).toBe(1);
const before = s.calls.length;
await capture(() => appleSubscriptions(p.ctx(['--yes'])));
expect(writes(s.calls.slice(before)).filter(c => c.method === 'POST' && /subscription(Group)?s$|Localizations$/.test(c.path))).toEqual([]);
expect(count('subscriptions')).toBe(1);
expect(count('subscriptionLocalizations')).toBe(1);
});
test('Play subscription create: 409 ALREADY_EXISTS is found, offers are not created twice', async () => {
const cfg: ShipConfig = { ...CONFIG, subscriptions: [{ name: 'Pro', listings: { en: { name: 'Pro' } }, subscriptions: [
{ id: 'com.example.notes.yearly', period: 'P1Y', level: 1, usd: 29.99, listings: { en: { title: 'Yearly', description: 'Pro every year.', benefits: ['Sync'] } } },
] } as never] };
const p = project(cfg);
s.faults.push({ method: 'POST', match: /\/subscriptions\?productId=/, when: 'after', status: 500 });
const r = await capture(() => playSubscriptions(p.ctx(['--yes'])));
expect(r.err).toContain('no clear answer');
expect(s.play.subs.size).toBe(1);
expect(s.calls.filter(c => c.method === 'POST' && /\/subscriptions\?productId=/.test(c.path))).toHaveLength(1);
});
});
describe('Play edits', () => {
test('an error inside the edit deletes it; nothing is committed; no second edit is ever open', async () => {
const play = await playClient(project().root, 'com.example.notes');
await expect(play.edit(async () => { throw new Error('boom'); })).rejects.toThrow('boom');
expect(s.play.edits.size).toBe(0);
expect(s.play.committed).toEqual([]);
await expect(play.edit(async () => { await play.read(async () => 1); })).rejects.toThrow('already open');
expect(s.play.edits.size).toBe(0);
});
test('an upload without a clear answer inside an edit: the edit is discarded and redone from scratch, one image', async () => {
const play = await playClient(project().root, 'com.example.notes');
s.faults.push({ method: 'POST', match: /phoneScreenshots\?uploadType=media/, when: 'after', status: 503 });
await play.edit(async id => {
await play.call('POST', `${play.UPLOAD}/edits/${id}/listings/en-US/phoneScreenshots?uploadType=media`, png(1), { 'Content-Type': 'image/png' });
});
expect(s.play.live.images['en-US/phoneScreenshots']).toHaveLength(1);
expect(s.play.committed).toHaveLength(1);
expect(s.play.edits.size).toBe(0);
});
test('a commit without a clear answer is never redone', async () => {
const play = await playClient(project().root, 'com.example.notes');
s.faults.push({ method: 'POST', match: /:commit$/, when: 'after', status: 502 });
let runs = 0;
await expect(play.edit(async id => { runs++; await play.call('PUT', `${play.API}/edits/${id}/details`, { defaultLanguage: 'en-US' }); })).rejects.toThrow('may be live');
expect(runs).toBe(1);
expect(s.play.committed).toHaveLength(1);
});
test('unchanged(): the edit is deleted, not committed', async () => {
const play = await playClient(project().root, 'com.example.notes');
await play.edit(async (_id, ctl) => { ctl.unchanged(); });
expect(s.play.committed).toEqual([]);
expect(s.play.edits.size).toBe(0);
});
});
describe('helpers', () => {
test('retrySafe: POST creates are not retried, idempotent actions are', () => {
expect(retrySafe('GET', '/v1/apps')).toBe(true);
expect(retrySafe('PATCH', '/v1/apps/1')).toBe(true);
expect(retrySafe('DELETE', '/v1/appScreenshots/1')).toBe(true);
expect(retrySafe('POST', '/v2/inAppPurchases')).toBe(false);
expect(retrySafe('POST', '/v1/appScreenshots')).toBe(false);
expect(retrySafe('POST', '/v1/betaGroups/1/relationships/builds')).toBe(true);
expect(retrySafe('POST', '/v1/inAppPurchasePriceSchedules')).toBe(true);
const play = 'https://androidpublisher.googleapis.com/androidpublisher/v3/applications/com.example.notes';
expect(retrySafe('POST', `${play}/edits`)).toBe(true);
expect(retrySafe('POST', `${play}/edits/e1:commit`)).toBe(false);
expect(retrySafe('POST', `${play}/edits/e1:validate`)).toBe(true);
expect(retrySafe('POST', `${play}/pricing:convertRegionPrices`)).toBe(true);
expect(retrySafe('POST', `${play}/subscriptions?productId=x`)).toBe(false);
expect(retrySafe('POST', 'https://androidpublisher.googleapis.com/upload/androidpublisher/v3/applications/com.example.notes/edits/e1/bundles?uploadType=media')).toBe(false);
});
test('isConflict: duplicates only, not Apple validation errors', () => {
expect(isConflict(new Error('POST /v2/inAppPurchases -> 409 {"errors":[{"code":"ENTITY_ERROR.ATTRIBUTE.INVALID.DUPLICATE","detail":"already been used"}]}'))).toBe(true);
expect(isConflict(new Error('POST /v2/inAppPurchases -> 409 {"errors":[{"code":"ENTITY_ERROR.ATTRIBUTE.INVALID","detail":"name is too long"}]}'))).toBe(false);
expect(isConflict(new Error('POST /subscriptions -> 409 {"error":{"status":"ALREADY_EXISTS"}}'))).toBe(true);
expect(isConflict(new Error('RevenueCat POST /v2/projects/p/products: 409 {"message":"resource already exists"}'))).toBe(true);
expect(isConflict(new Error('GET /v1/apps -> 404'))).toBe(false);
});
test('createOnce / findOrCreate: unknown outcome without a unique key stops instead of creating twice', async () => {
let made = 0;
const lost = () => { made++; throw new AmbiguousWriteError('POST /v1/appEvents -> 504'); };
await expect(createOnce({ what: 'event', find: async () => null, create: lost })).rejects.toThrow('not sent again');
expect(made).toBe(1);
made = 0;
await expect(createOnce({ what: 'group', unique: true, find: async () => null, create: lost })).rejects.toThrow('not sent again');
expect(made).toBe(2);
const root = project().root;
const one = await findOrCreate({ what: 'x', find: async () => null, create: async () => ({ id: 'n1' }),
journal: { root, key: 'k', byId: async () => null, idOf: v => v.id } });
expect(one).toEqual({ value: { id: 'n1' }, created: true });
const again = await findOrCreate({ what: 'x', find: async () => null, create: async () => { throw new Error('must not create'); },
journal: { root, key: 'k', byId: async id => ({ id }), idOf: v => v.id } });
expect(again).toEqual({ value: { id: 'n1' }, created: false });
});
test('keptPrefix, sameRelease, sameReleases', () => {
const d = (checksum: string, state = 'COMPLETE') => ({ checksum, state });
expect(keptPrefix([d('a'), d('b'), d('x')], ['a', 'b', 'c'])).toBe(2);
expect(keptPrefix([d('a', 'AWAITING_UPLOAD'), d('b')], ['a', 'b'])).toBe(0);
expect(keptPrefix([], ['a'])).toBe(0);
const notes = [{ language: 'en-US', text: 'Fixes' }];
expect(sameRelease([{ versionCodes: ['42'], status: 'completed', releaseNotes: notes }], { versionCodes: ['42'], status: 'completed', releaseNotes: notes })).toBe(true);
expect(sameRelease([{ versionCodes: ['42'], status: 'draft' }], { versionCodes: ['42'], status: 'completed' })).toBe(false);
expect(sameReleases([{ status: 'completed', versionCodes: ['1'] }, { versionCodes: ['2'], status: 'inProgress', userFraction: 0.2 }],
[{ userFraction: 0.2, status: 'inProgress', versionCodes: ['2'] }, { versionCodes: ['1'], status: 'completed' }])).toBe(true);
});
});
describe('one writer per app and store', () => {
test('a second run is refused with the holder; a dead holder is taken over; --force-unlock removes it', () => {
const root = project().root;
const release = acquireLock(root, 'apple-com.example.notes', 'apple iap');
expect(() => acquireLock(root, 'apple-com.example.notes', 'apple screenshots')).toThrow('`storebound apple iap`');
release();
acquireLock(root, 'apple-com.example.notes', 'apple screenshots')();
// a lock held by another live process
mkdirSync(join(root, '.storebound', 'locks'), { recursive: true });
const file = join(root, '.storebound', 'locks', 'play-com.example.notes.lock');
writeFileSync(file, JSON.stringify({ pid: process.ppid, host: hostname(), command: 'play images', startedAt: new Date().toISOString() }));
expect(() => acquireLock(root, 'play-com.example.notes', 'play listing')).toThrow('another storebound run is writing to play com.example.notes: `storebound play images`');
const free = acquireLock(root, 'play-com.example.notes', 'play listing', { force: true });
expect(JSON.parse(readFileSync(file, 'utf8')).command).toBe('play listing');
free();
expect(existsSync(file)).toBe(false);
expect(staleLock({ pid: 999999999, host: 'h', command: 'x', startedAt: new Date().toISOString() }, Date.now(), 'h', () => false)).toBe(true);
expect(staleLock({ pid: 1, host: 'other', command: 'x', startedAt: new Date().toISOString() }, Date.now(), 'h', () => false)).toBe(false);
expect(staleLock({ pid: 1, host: 'other', command: 'x', startedAt: new Date(Date.now() - 13 * 3600_000).toISOString() }, Date.now(), 'h')).toBe(true);
});
test('which runs take a lock', () => {
const k = (cmd: string, argv: string[]) => lockKeys(cmd, parseArgs(argv), CONFIG);
expect(k('apple iap', [])).toEqual(['apple-com.example.notes']);
expect(k('apple iap', ['--dry'])).toEqual([]);
expect(k('apple testflight', ['status'])).toEqual([]);
expect(k('apple listing', ['pull'])).toEqual([]);
expect(k('play images', [])).toEqual(['play-com.example.notes']);
expect(k('play status', [])).toEqual([]);
expect(k('prices', ['import'])).toEqual(['apple-com.example.notes', 'play-com.example.notes']);
expect(k('prices', ['export'])).toEqual([]);
expect(k('validate', [])).toEqual([]);
});
});

View file

@ -97,3 +97,12 @@ describe('listing commands', () => {
expect(sb(dir, 'listing', 'translate', '--to', 'xx').stderr.toString()).toContain('not a language of the config'); expect(sb(dir, 'listing', 'translate', '--to', 'xx').stderr.toString()).toContain('not a language of the config');
}); });
}); });
test('listing check counts App Store keywords in UTF-8 bytes, the other fields in characters', () => {
const ru = 'игра,бег,прыжки,монеты,уровни,рекорд,аркада,быстро'; // 50 characters, 93 bytes
const kw = checkFields('apple', { keywords: ru, subtitle: 'Быстрая аркада' }).find(c => c.field === 'keywords')!;
expect(kw).toMatchObject({ chars: 93, unit: 'bytes', problems: [] });
expect(checkFields('apple', { keywords: ru + ',звук' }).find(c => c.field === 'keywords')!.problems).toContain('102 > 100 bytes');
expect(checkFields('apple', { subtitle: 'Быстрая аркада' })[0]).toMatchObject({ chars: 14, unit: 'chars' });
});

216
tests/local-fonts.test.ts Normal file
View file

@ -0,0 +1,216 @@
// A game's own brand font as a local file in shots.fonts: validation, cmap coverage with listed + Noto fallbacks,
// @font-face served in place, `shots check` (local + listed fallback pass, license warning) and the content hash that
// invalidates the capture cache and stale renders. Fonts are tiny hand-made sfnt / WOFF2 files (cmap + name only).
import { afterAll, describe, expect, test } from 'bun:test';
import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { brotliCompressSync } from 'node:zlib';
import { fontCodepoints, fontFamilyName } from '../cli/shots/cmap';
import { type FaceJob, fontIssues, fontsCss, fontUsage, type FontManifest, GlyphError, planFonts } from '../cli/shots/glyphs';
import { langTexts } from '../cli/shots/fonts';
import { faceFilePath, fontsRoute, loadLocalFont, localByFamily, localFontIssues, resolveShotFonts, staleLocalFonts } from '../cli/shots/local-fonts';
import { planWeb } from '../cli/shots/capture-web';
import { validateConfig } from '../cli/lib/config';
// ---- minimal font writer: an sfnt (or WOFF2) with a format 12 cmap and a name table ----
function cmapBytes(cps: number[]): Uint8Array {
const sorted = [...new Set(cps)].sort((a, b) => a - b);
const len = 16 + sorted.length * 12;
const b = new DataView(new ArrayBuffer(12 + len));
b.setUint16(2, 1); b.setUint16(4, 3); b.setUint16(6, 10); b.setUint32(8, 12);
b.setUint16(12, 12); b.setUint32(16, len); b.setUint32(24, sorted.length);
sorted.forEach((c, i) => { b.setUint32(28 + i * 12, c); b.setUint32(32 + i * 12, c); b.setUint32(36 + i * 12, i + 1); });
return new Uint8Array(b.buffer);
}
function nameBytes(family: string): Uint8Array {
const str = new Uint8Array(family.length * 2);
[...family].forEach((c, i) => { str[i * 2] = 0; str[i * 2 + 1] = c.charCodeAt(0); });
const b = new DataView(new ArrayBuffer(6 + 12 + str.length));
b.setUint16(2, 1); b.setUint16(4, 18);
b.setUint16(6, 3); b.setUint16(8, 1); b.setUint16(10, 0x409); b.setUint16(12, 1); b.setUint16(14, str.length); b.setUint16(16, 0);
new Uint8Array(b.buffer).set(str, 18);
return new Uint8Array(b.buffer);
}
function sfnt(cps: number[], family: string, sig = 0x00010000): Uint8Array {
const tables: [string, Uint8Array][] = [['cmap', cmapBytes(cps)], ['name', nameBytes(family)]];
let off = 12 + tables.length * 16;
const out = new Uint8Array(off + tables.reduce((n, [, t]) => n + t.length, 0));
const d = new DataView(out.buffer);
d.setUint32(0, sig); d.setUint16(4, tables.length);
tables.forEach(([tag, t], i) => {
out.set([...tag].map(c => c.charCodeAt(0)), 12 + i * 16);
d.setUint32(12 + i * 16 + 8, off); d.setUint32(12 + i * 16 + 12, t.length);
out.set(t, off); off += t.length;
});
return out;
}
function woff2(cps: number[], family: string): Uint8Array {
const cmap = cmapBytes(cps), name = nameBytes(family);
const stream = new Uint8Array(brotliCompressSync(Buffer.concat([cmap, name])));
const b128 = (n: number): number[] => { const b = [n & 0x7f]; n = Math.floor(n / 128); while (n) { b.unshift(0x80 | (n & 0x7f)); n = Math.floor(n / 128); } return b; };
const dir = [0, ...b128(cmap.length), 5, ...b128(name.length)]; // known-table indices: 0 = cmap, 5 = name
const out = new Uint8Array(48 + dir.length + stream.length);
const d = new DataView(out.buffer);
out.set([...'wOF2'].map(c => c.charCodeAt(0)), 0); d.setUint32(4, 0x00010000); d.setUint32(8, out.length);
d.setUint16(12, 2); d.setUint32(20, stream.length);
out.set(dir, 48); out.set(stream, 48 + dir.length);
return out;
}
const range = (a: number, b: number): number[] => Array.from({ length: b - a + 1 }, (_, i) => a + i);
const cpsOf = (s: string): number[] => [...s].map(c => c.codePointAt(0)!);
// "ER Display": ASCII, Latin-1 and Latin Extended-A without ą ę Ł ł; no Vietnamese (ơ ư, U+1EA0..)
const ER = [...range(0x20, 0x7e), ...range(0xa0, 0xff), ...range(0x100, 0x17f).filter(c => ![0x105, 0x119, 0x141, 0x142].includes(c))];
const root = mkdtempSync(join(tmpdir(), 'sb-localfonts-'));
afterAll(() => rmSync(root, { recursive: true, force: true }));
const fontsDir = join(root, '.storebound', 'fonts');
mkdirSync(join(root, 'assets', 'fonts'), { recursive: true });
mkdirSync(fontsDir, { recursive: true });
writeFileSync(join(root, 'assets/fonts/er-display.woff2'), woff2(ER, 'ER Display'));
writeFileSync(join(root, 'assets/fonts/er-pl.ttf'), sfnt([0x105, 0x119, 0x141, 0x142], 'ER Polish'));
const resolve = (p: string): string => join(root, p);
describe('local font files: config and validation', () => {
test('a path reads the family from the name table (WOFF2 and TTF); the object form sets family, weight, license', () => {
expect(fontFamilyName(woff2(ER, 'ER Display'))).toBe('ER Display');
const a = loadLocalFont('assets/fonts/er-display.woff2', 'shots.fonts.title', resolve, root);
expect(a.family).toBe('ER Display');
expect(a.local).toMatchObject({ path: 'assets/fonts/er-display.woff2', format: 'woff2' });
expect(a.local!.hash).toMatch(/^[0-9a-f]{16}$/);
const b = loadLocalFont({ family: 'ER Pl', file: 'assets/fonts/er-pl.ttf', weight: 800, license: 'OFL-1.1' }, 'k', resolve, root);
expect(b).toMatchObject({ family: 'ER Pl', local: { format: 'truetype', weight: '800', license: 'OFL-1.1' } });
expect(fontCodepoints(readFileSync(join(root, 'assets/fonts/er-display.woff2'))).size).toBe(ER.length);
});
test('missing file, wrong extension, not a font, mismatched format', () => {
writeFileSync(join(root, 'assets/fonts/bad.ttf'), new Uint8Array(64));
writeFileSync(join(root, 'assets/fonts/fake.ttf'), woff2(ER, 'X'));
expect(() => loadLocalFont('assets/fonts/nope.ttf', 'shots.fonts.title', resolve, root)).toThrow('shots.fonts.title: assets/fonts/nope.ttf not found');
expect(() => loadLocalFont({ file: 'assets/fonts/er.svg' }, 'k', resolve, root)).toThrow('not a font file (TTF, OTF, WOFF or WOFF2)');
expect(() => loadLocalFont('assets/fonts/bad.ttf', 'k', resolve, root)).toThrow('unknown signature');
expect(() => loadLocalFont('assets/fonts/fake.ttf', 'k', resolve, root)).toThrow('is WOFF2 inside, but named .ttf');
});
test('validateConfig: font entry shapes', () => {
const base = { app: { name: 'X', privacyPolicyUrl: 'https://example.com/p', supportUrl: 'https://example.com', website: 'https://example.com', contactEmail: 'a@example.com' }, languages: [{ id: 'en' }] };
const shots = (fonts: unknown) => ({ ...base, shots: { copy: 'c.json', frames: 'f', out: 'o', scenes: [{ id: 'a', palette: 'fire', frames: [{ src: 'a' }] }], fonts } });
expect(validateConfig(shots({ title: 'assets/er.woff2', body: 'Montserrat', fallback: [{ family: 'ER Pl', file: 'a/er-pl.ttf', license: 'OFL-1.1' }, 'Noto Sans Display'] }))).toEqual([]);
const errs = validateConfig(shots({ title: 'assets/er.svg', body: { family: 'B' }, fallback: [{ file: 'x.otf', style: 'oblique', weight: 'bold' }] }));
expect(errs).toEqual([
'shots.fonts.title: assets/er.svg: a font file must be TTF, OTF, WOFF or WOFF2',
'shots.fonts.body: a Google Fonts family, a font file path or { family, file, weight?, style?, license? }',
'shots.fonts.fallback[0].style: normal | italic',
'shots.fonts.fallback[0].weight: e.g. 400, 800 or "100 900"',
]);
});
});
// Copy: ER Display covers tr / de / fr titles; Polish needs ą ę Ł ł, Vietnamese needs ơ ư ạ ...
const COPY = {
tr: { s1: { title: ["Taş Devri'nden", 'İleri!'] as [string, string], sub: 'Kasabaları yağmala' } },
de: { s1: { title: ['Größer', 'schöner'] as [string, string] } },
pl: { s1: { title: ['Gęsi łąka', 'Łódź'] as [string, string], sub: 'Szybka gra' } },
vi: { s1: { title: ['Chiến đấu', 'Thế giới'] as [string, string], sub: 'Trận nhanh' } },
};
const NOTO: Record<string, (cp: number) => boolean> = {
Montserrat: () => true, // body: covers everything here
'Noto Sans Display': cp => cp < 0x250 || (cp >= 0x1ea0 && cp <= 0x1ef9),
};
/** Getter like `shots fonts`: local files read in place, Google families faked. */
function getter(asked: FaceJob[]): (job: FaceJob) => Promise<Set<number>> {
return async job => {
asked.push(job);
if (job.local) return fontCodepoints(readFileSync(join(root, job.local.path)));
const f = NOTO[job.family] ?? (() => false);
return new Set(cpsOf(job.chars).filter(f));
};
}
async function plan(fallback: (string | { family?: string; file: string; license?: string })[] = [], asked: FaceJob[] = []): Promise<FontManifest> {
const sf = resolveShotFonts({ fonts: { title: 'assets/fonts/er-display.woff2', body: 'Montserrat', fallback } }, 'game', resolve, root);
return planFonts({ langs: langTexts(COPY, [], 'STAR DASH'), title: sf.title.family, body: sf.body.family, titleAxis: null, bodyAxis: 'wght@100..900',
fallbackTitleAxis: 'wght@800', chosen: sf.fallback.map(f => f.family), local: localByFamily([sf.title, sf.body, ...sf.fallback]) }, getter(asked));
}
const fb = (m: FontManifest, lang: string) => m.langs[lang]!.fallbacks.filter(f => f.role === 'title').map(f => `${f.family}:${[...f.chars].join(' ')}`);
describe('coverage of a local title font', () => {
test('pl / vi titles: the characters ER Display lacks go to Noto Sans Display (automatic); tr / de need nothing', async () => {
const asked: FaceJob[] = [];
const m = await plan([], asked);
expect(fb(m, 'pl')).toEqual(['Noto Sans Display:ę ł ą Ł']);
expect(fb(m, 'vi')).toEqual(['Noto Sans Display:ế ấ ớ']);
for (const l of ['tr', 'de']) expect(m.langs[l]!.fallbacks).toEqual([]);
expect(m.faces['ER Display']).toMatchObject({ family: 'ER Display', kind: 'title', chosen: true, file: 'local/er-display.woff2', local: { path: 'assets/fonts/er-display.woff2', format: 'woff2' } });
expect(m.faces['Store fb noto-sans-display title']!.chosen).toBe(false);
expect(asked.find(j => j.family === 'ER Display')!.local).toBeDefined(); // read in place, not downloaded
});
test('listed fallbacks first (a local file), then Noto; chosen, so check passes', async () => {
const m = await plan([{ file: 'assets/fonts/er-pl.ttf', license: 'OFL-1.1' }, 'Noto Sans Display']);
expect(fb(m, 'pl')).toEqual(['ER Polish:ę ł ą Ł']);
expect(fb(m, 'vi')).toEqual(['Noto Sans Display:ế ấ ớ']);
expect(m.langs.pl!.title).toEqual(['ER Display', 'Store fb er-polish title']);
expect(m.faces['Store fb er-polish title']).toMatchObject({ kind: 'fallback', chosen: true, file: 'local/er-polish.ttf', local: { license: 'OFL-1.1' } });
expect(m.faces['Store fb noto-sans-display title']!.chosen).toBe(true);
});
test('nothing covers it: GlyphError names the local font and the fallbacks tried', async () => {
const err = await planFonts({ langs: langTexts({ pl: { s1: { title: ['ą', 'x'] } } }, [], ''), title: 'ER Display', body: 'Montserrat', titleAxis: null, bodyAxis: null,
fallbackTitleAxis: null, local: localByFamily([loadLocalFont('assets/fonts/er-display.woff2', 'k', resolve, root)]) },
async job => job.local ? fontCodepoints(readFileSync(join(root, job.local.path))) : new Set()).catch(e => e);
expect(err).toBeInstanceOf(GlyphError);
expect(err.message).toContain('pl (title): "ą" U+0105; tried ER Display, Noto Sans Display, Noto Sans');
});
});
describe('render and check with a local font', () => {
test('fonts.css: @font-face for the local file, served in place at /fonts/local/... with its hash', async () => {
const m = await plan([{ file: 'assets/fonts/er-pl.ttf', license: 'OFL-1.1' }]);
const css = fontsCss(m);
const h = m.faces['ER Display']!.local!.hash;
expect(css).toContain(`@font-face { font-family: 'ER Display'; font-weight: 100 900; font-style: normal; font-synthesis: none; src: url(local/er-display.woff2?v=${h}) format('woff2'); }`);
expect(css).toContain("src: url(local/er-polish.ttf?v=");
expect(css).toContain("format('truetype')");
expect(css).toContain('/* ER Polish: local file assets/fonts/er-pl.ttf (license: OFL-1.1) */');
expect(fontsRoute(`local/er-display.woff2?v=${h}`, fontsDir, root, m)).toBe(join(root, 'assets/fonts/er-display.woff2'));
expect(fontsRoute('local/other.ttf', fontsDir, root, m)).toBeNull();
expect(fontsRoute('local/../../x.ttf', fontsDir, root, m)).toBeNull();
expect(fontsRoute('fb-noto-sans-display-title.woff2', fontsDir, root, m)).toBe(join(fontsDir, 'fb-noto-sans-display-title.woff2'));
expect(faceFilePath(fontsDir, root, m.faces['ER Display']!)).toBe(join(root, 'assets/fonts/er-display.woff2'));
// nothing is copied into .storebound/fonts
expect(Object.values(m.faces).filter(f => f.local).every(f => f.file.startsWith('local/'))).toBe(true);
});
test('check: the local font and the listed fallback are not fallbacks the config did not choose; an automatic Noto is', async () => {
const m = await plan([{ file: 'assets/fonts/er-pl.ttf' }]);
const ok = fontUsage([
{ el: 'h1 span:1', text: 'Gęsi łąka', faces: { 'ER Display': 'Gsika', 'Store fb er-polish title': 'ęłą' } },
{ el: 'p.sub', text: 'Szybka gra', faces: { Montserrat: 'Szybkagr' } },
], m.faces);
expect(ok.used).toEqual({ 'ER Display': 5, 'ER Polish': 3, Montserrat: 8 });
expect(fontIssues(ok)).toEqual([]);
const bad = fontUsage([{ el: 'h1 span:1', text: 'Chiến', faces: { 'ER Display': 'Chin', 'Store fb noto-sans-display title': 'ế' } }], m.faces);
expect(fontIssues(bad).map(i => i.code)).toEqual(['font-fallback']);
// license note: warn for the local file without one
expect(localFontIssues(m).map(i => i.msg.split(' has')[0])).toEqual(['local font ER Display (assets/fonts/er-display.woff2)', 'local font ER Polish (assets/fonts/er-pl.ttf)']);
const licensed = await plan([{ file: 'assets/fonts/er-pl.ttf', license: 'proprietary' }]);
expect(localFontIssues(licensed).length).toBe(1);
});
test('a changed font file changes the content hash: capture cache misses, render refuses the stale fonts.json', async () => {
const m1 = await plan();
const cap = { source: 'web' as const, baseUrl: 'http://x', shots: { a: { path: '/a' } } };
const dims = { phone: { w: 10, h: 20, dpr: 1, bar: 0 }, tablet: { w: 10, h: 20, dpr: 1, bar: 0 } };
const job = (m: FontManifest) => planWeb(cap, { langs: ['tr'], devices: ['phone'], only: [], dims, framesDir: '/f', build: 'b', fonts: fontsCss(m) })[0]!.hash;
expect(staleLocalFonts(m1, root)).toEqual([]);
writeFileSync(join(root, 'assets/fonts/er-display.woff2'), woff2([...ER, 0x105], 'ER Display'));
try {
expect(staleLocalFonts(m1, root)).toEqual(['ER Display: assets/fonts/er-display.woff2 changed']);
const m2 = await plan();
expect(m2.faces['ER Display']!.local!.hash).not.toBe(m1.faces['ER Display']!.local!.hash);
expect(job(m2)).not.toBe(job(m1));
expect(fb(m2, 'pl')).toEqual(['Noto Sans Display:ę ł Ł']); // ą now in the font
} finally { writeFileSync(join(root, 'assets/fonts/er-display.woff2'), woff2(ER, 'ER Display')); }
});
});

View file

@ -154,6 +154,8 @@ describe('IAP review submission plan', () => {
{ method: 'POST', match: /^\/v1\/reviewSubmissions$/, answer: { data: { type: 'reviewSubmissions', id: 'rs-new', attributes: { state: 'READY_FOR_REVIEW' } } } }, { method: 'POST', match: /^\/v1\/reviewSubmissions$/, answer: { data: { type: 'reviewSubmissions', id: 'rs-new', attributes: { state: 'READY_FOR_REVIEW' } } } },
{ method: 'POST', match: /^\/v1\/reviewSubmissionItems$/, answer: { data: { type: 'reviewSubmissionItems', id: 'it-1' } } }, { method: 'POST', match: /^\/v1\/reviewSubmissionItems$/, answer: { data: { type: 'reviewSubmissionItems', id: 'it-1' } } },
{ method: 'PATCH', match: /^\/v1\/reviewSubmissions\/rs-new$/, answer: { data: { type: 'reviewSubmissions', id: 'rs-new', attributes: { state: 'WAITING_FOR_REVIEW' } } } }, { method: 'PATCH', match: /^\/v1\/reviewSubmissions\/rs-new$/, answer: { data: { type: 'reviewSubmissions', id: 'rs-new', attributes: { state: 'WAITING_FOR_REVIEW' } } } },
// the journal of recent creates (.storebound/created.json) names rs-new: read by id, it was submitted, so a new draft is made
{ match: /^\/v1\/reviewSubmissions\/rs-new$/, answer: { data: { type: 'reviewSubmissions', id: 'rs-new', attributes: { state: 'WAITING_FOR_REVIEW' } } } },
]); ]);
let calls = routes(); let calls = routes();
let r = await capture(() => iapSubmit(p.ctx(['--dry', '--json']))); let r = await capture(() => iapSubmit(p.ctx(['--dry', '--json'])));