// App Store version + listing text (idempotent; re-run after editing the App Store listing file). // storebound apple listing --version 1.0.0 [--build 3] [--dry] // - the editable iOS version: versionString, copyright, release type MANUAL; with --build that processed build is attached // - appInfoLocalizations: name, subtitle, privacy policy URL per locale // - appStoreVersionLocalizations: description, keywords, promotional text, what's new (not on a first version), // support + marketing URL per locale // And the other way round (read-only on App Store Connect): // storebound apple listing pull [--version x.y.z] [--write] [--json] // the live texts (the editable version if there is one, else the newest; --version picks one) into the App Store // listing file: shows the diff, writes only with --write. import { existsSync, readFileSync, writeFileSync } from 'node:fs'; import { type AppleCopy, parseAppStoreListing } from '../lib/copy'; import { compactDiff } from '../lib/diff'; import { mergeAppleListing, overLimits } from '../lib/listing-pull'; import type { Ctx } from '../lib/ctx'; import { need } from '../lib/ctx'; import { done, log, printJson, warn } from '../lib/log'; import { ascClient, type J } from './asc'; export function readAppleCopy(ctx: Ctx) { const { config, resolve } = ctx.loaded; const only = ctx.args.list('only'); const all = parseAppStoreListing(readFileSync(resolve(config.apple?.listing ?? 'store/appstore.md'), 'utf8')); return Object.fromEntries(Object.entries(all).filter(([l]) => !only.length || only.includes(l))); } async function upsertLocs(c: Awaited>, list: string, type: string, parentRel: string, parentType: string, parentId: string, want: Record>): Promise { const have = new Map((await c.all(`${list}?limit=200`)).map((l: J) => [l.attributes.locale, l] as const)); for (const [locale, raw] of Object.entries(want)) { const attributes = Object.fromEntries(Object.entries(raw).filter(([, v]) => v !== undefined)); const cur = have.get(locale); if (!cur) { await c.asc('POST', `/v1/${type}`, { data: { type, attributes: { locale, ...attributes }, relationships: { [parentRel]: { data: { type: parentType, id: parentId } } } } }); log('apple', `${type} ${locale}: created`); } else if (Object.entries(attributes).some(([k, v]) => cur.attributes[k] !== v)) { await c.asc('PATCH', `/v1/${type}/${cur.id}`, { data: { type, id: cur.id, attributes } }); log('apple', `${type} ${locale}: updated`); } else log('apple', `${type} ${locale}: up to date`); } } /** apple listing pull: live texts -> diff -> (with --write) the App Store listing file. */ async function pull(ctx: Ctx): Promise { const { config, root, resolve } = ctx.loaded; const a = ctx.args; const c = await ascClient(root, need(config.apple?.bundleId, 'apple.bundleId'), config.apple?.appId); const appId = await c.app(); const versions = await c.all(`/v1/apps/${appId}/appStoreVersions?filter[platform]=IOS&limit=50`); const want = a.get('version'); const sorted = [...versions].sort((x: J, y: J) => String(y.attributes.createdDate ?? '').localeCompare(String(x.attributes.createdDate ?? ''))); const v = want ? sorted.find((x: J) => x.attributes.versionString === want) : (await c.editableVersion()) ?? sorted[0]; if (!v) throw new Error(want ? `no iOS version ${want} on App Store Connect` : 'no iOS version on App Store Connect yet'); const infos = await c.all(`/v1/apps/${appId}/appInfos`); const live = v.attributes.appVersionState ?? v.attributes.appStoreState; // the app info that belongs to the version: the editable one while a version is being prepared, else the live one const info = ['READY_FOR_DISTRIBUTION', 'READY_FOR_SALE'].includes(live) ? infos.find((i: J) => ['READY_FOR_DISTRIBUTION', 'READY_FOR_SALE'].includes(i.attributes.state ?? i.attributes.appStoreState)) ?? infos[0] : await c.editableAppInfo(); const remote: Record = {}; for (const l of await c.all(`/v1/appInfos/${info.id}/appInfoLocalizations?limit=200`)) remote[l.attributes.locale] = { name: l.attributes.name ?? undefined, subtitle: l.attributes.subtitle ?? undefined }; for (const l of await c.all(`/v1/appStoreVersions/${v.id}/appStoreVersionLocalizations?limit=200`)) { const at = l.attributes; remote[at.locale] = { ...remote[at.locale], description: at.description ?? undefined, keywords: at.keywords ?? undefined, promotionalText: at.promotionalText ?? undefined, whatsNew: at.whatsNew ?? undefined }; } const file = resolve(config.apple?.listing ?? 'store/appstore.md'); const before = existsSync(file) ? readFileSync(file, 'utf8') : '# App Store listing copy\n\nPulled from App Store Connect by `storebound apple listing pull`.\n'; const r = mergeAppleListing(before, remote); const over = overLimits(r.changes, 'apple'); const diff = compactDiff(before, r.md); if (a.has('json')) printJson({ version: v.attributes.versionString, state: live, file, write: a.has('write') && !a.has('dry'), changes: r.changes, added: r.added, overLimits: over, diff }); else { log('apple', `pull: version ${v.attributes.versionString} (${live}), ${Object.keys(remote).length} locales`); for (const ch of r.changes) log('apple', `${ch.section} ${ch.field}: ${ch.from === null ? 'new' : 'changed'}`); if (diff.length) console.log(diff.join('\n')); } for (const o of over) warn('apple', `over the limit on the store: ${o}`); if (!r.changes.length) { done('apple', `${file} already matches the store`); return; } if (!a.has('write') || a.has('dry')) { done('apple', `${r.changes.length} change(s); nothing written (add --write to update ${file})`); return; } writeFileSync(file, r.md); done('apple', `${file}: ${r.changes.length} change(s) written${r.added.length ? `, new locales ${r.added.join(', ')}` : ''}`); } export async function run(ctx: Ctx): Promise { const { config, root } = ctx.loaded; const a = ctx.args; if (a._[0] === 'pull') return pull(ctx); const version = a.get('version'); if (!version) throw new Error('usage: apple listing --version 1.0.0 [--build N] [--dry]'); const listing = readAppleCopy(ctx); log('apple', `listing: ${Object.keys(listing).length} locales (${Object.keys(listing).join(', ')})`); if (a.has('dry')) return; const app = config.app; const copyright = app.copyright ?? `${new Date().getFullYear()} ${app.name}`; const c = await ascClient(root, need(config.apple?.bundleId, 'apple.bundleId'), config.apple?.appId); const appId = await c.app(); let v = await c.editableVersion(); if (!v) { v = (await c.asc('POST', '/v1/appStoreVersions', { data: { type: 'appStoreVersions', attributes: { platform: 'IOS', versionString: version, copyright, releaseType: 'MANUAL' }, relationships: { app: { data: { type: 'apps', id: appId } } } } })).data; log('apple', `version ${version} created`); } else if (v.attributes.versionString !== version || v.attributes.copyright !== copyright || v.attributes.releaseType !== 'MANUAL') { await c.asc('PATCH', `/v1/appStoreVersions/${v.id}`, { data: { type: 'appStoreVersions', id: v.id, attributes: { versionString: version, copyright, releaseType: 'MANUAL' } } }); log('apple', `version ${v.attributes.versionString} -> ${version}, copyright, release MANUAL`); } const build = a.get('build'); if (build) { const b = (await c.all(`/v1/builds?filter[app]=${appId}&filter[version]=${build}&filter[preReleaseVersion.version]=${version}&filter[preReleaseVersion.platform]=IOS`))[0]; if (!b) throw new Error(`build ${version} (${build}) not found (still uploading? check TestFlight)`); if (b.attributes.processingState !== 'VALID') throw new Error(`build ${build} is ${b.attributes.processingState}; wait until VALID`); await c.asc('PATCH', `/v1/appStoreVersions/${v.id}/relationships/build`, { data: { type: 'builds', id: b.id } }); log('apple', `build ${version} (${build}) attached`); } const info = await c.editableAppInfo(); await upsertLocs(c, `/v1/appInfos/${info.id}/appInfoLocalizations`, 'appInfoLocalizations', 'appInfo', 'appInfos', info.id, Object.fromEntries(Object.entries(listing).map(([l, f]) => [l, { name: f.name ?? app.name, subtitle: f.subtitle, privacyPolicyUrl: app.privacyPolicyUrl }]))); await upsertLocs(c, `/v1/appStoreVersions/${v.id}/appStoreVersionLocalizations`, 'appStoreVersionLocalizations', 'appStoreVersion', 'appStoreVersions', v.id, Object.fromEntries(Object.entries(listing).map(([l, f]) => [l, { description: f.description, keywords: f.keywords, promotionalText: f.promotionalText, whatsNew: f.whatsNew, supportUrl: app.supportUrl, marketingUrl: app.marketingUrl ?? app.website }]))); done('apple', 'listing done'); }