// Play store listing text + app details in one edit. // storebound play listing [--dry] [--only en-US,de-DE] // --dry is read-only: a throwaway edit is opened, the current details and listings are read and compared with the file, // and the edit is deleted. Nothing is written, not even into that edit. // App details: default language, contact email, website (must be the domain that serves app-ads.txt). // Listings: title, short and full description of every `` section of the listing file, plus the promo // video (play.video, a YouTube URL) on every language. Play has no video upload: make the video with // `storebound shots video --target youtube`, upload it to YouTube (public or unlisted, ads off, embeddable), paste the URL. // And the other way round (read-only on Play, one throwaway edit that is deleted): // storebound play listing pull [--write] [--json] live listings -> diff -> (with --write) the Play listing file import { existsSync, readFileSync, writeFileSync } from 'node:fs'; import { parsePlayListing } from '../lib/copy'; import { compactDiff } from '../lib/diff'; import { mergePlayListing, overLimits } from '../lib/listing-pull'; import type { Ctx } from '../lib/ctx'; import { need } from '../lib/ctx'; import { done, log, printJson, step, warn } from '../lib/log'; import { playClient } from './client'; export function readPlayCopy(ctx: Ctx) { const { config, resolve } = ctx.loaded; const file = resolve(config.play?.listing ?? 'store/listing.md'); const only = ctx.args.list('only'); return parsePlayListing(readFileSync(file, 'utf8'), config.app.name).filter(c => !only.length || only.includes(c.lang)); } /** Body of one Play listing PUT. The PUT replaces the whole listing, so a promo video set in Play Console is cleared * unless its URL is in play.video. Pure. */ export function listingBody(c: { lang: string; title: string; short: string; full: string }, video?: string): Record { return { language: c.lang, title: c.title, shortDescription: c.short, fullDescription: c.full, ...(video ? { video } : {}) }; } export interface PlayListing { language: string; title?: string; shortDescription?: string; fullDescription?: string; video?: string } export interface ListingChange { lang: string; change: 'new' | 'update' | 'same'; fields: string[] } const LISTING_FIELDS = ['title', 'shortDescription', 'fullDescription', 'video'] as const; /** What a listing run would change, from the listings Play has now (GET edits/{id}/listings). An empty and a missing * value are the same; a promo video on Play that play.video leaves out is cleared by the PUT. Pure. */ export function listingChanges(current: PlayListing[], wanted: Record[]): ListingChange[] { const now = new Map(current.map(l => [l.language, l])); return wanted.map(w => { const cur = now.get(w.language!); if (!cur) return { lang: w.language!, change: 'new', fields: [] }; const fields = LISTING_FIELDS.filter(f => (cur[f] ?? '') !== (w[f] ?? '')).map(f => (f === 'video' && !w.video ? 'video (cleared)' : f)); return { lang: w.language!, change: fields.length ? 'update' : 'same', fields }; }); } /** App details fields a run would change (defaultLanguage, contactEmail, contactWebsite). Pure. */ export function detailsChanges(current: Record, wanted: Record): string[] { return Object.keys(wanted).filter(k => (current[k] ?? '') !== (wanted[k] ?? '')); } async function pull(ctx: Ctx): Promise { const { config, root, resolve } = ctx.loaded; const a = ctx.args; const play = await playClient(root, need(config.play?.packageName, 'play.packageName')); const now = await play.read(async id => (await play.call<{ listings?: PlayListing[] }>('GET', `${play.API}/edits/${id}/listings`)).listings ?? []); const file = resolve(config.play?.listing ?? 'store/listing.md'); const before = existsSync(file) ? readFileSync(file, 'utf8') : '# Google Play listing copy\n\nPulled from Google Play by `storebound play listing pull`.\n'; const r = mergePlayListing(before, now, config.app.name); const over = overLimits(r.changes, 'play'); const diff = compactDiff(before, r.md); if (a.has('json')) printJson({ file, write: a.has('write') && !a.has('dry'), languages: now.map(l => l.language), changes: r.changes, added: r.added, conflicts: r.conflicts, overLimits: over, diff }); else { log('play', `pull: ${now.length} listing(s) on Play`); for (const ch of r.changes) log('play', `${ch.section} ${ch.field}: ${ch.from === null ? 'new' : 'changed'}`); if (diff.length) console.log(diff.join('\n')); } for (const c of r.conflicts) warn('play', c); for (const o of over) warn('play', `over the limit on the store: ${o}`); if (!r.changes.length) { done('play', `${file} already matches the store`); return; } if (!a.has('write') || a.has('dry')) { done('play', `${r.changes.length} change(s); nothing written (add --write to update ${file})`); return; } writeFileSync(file, r.md); done('play', `${file}: ${r.changes.length} change(s) written${r.added.length ? `, new languages ${r.added.join(', ')}` : ''}`); } export async function run(ctx: Ctx): Promise { const { config, root } = ctx.loaded; if (ctx.args._[0] === 'pull') return pull(ctx); const pkg = need(config.play?.packageName, 'play.packageName'); const copy = readPlayCopy(ctx); const details = { defaultLanguage: config.play?.defaultLanguage ?? 'en-US', contactEmail: config.app.contactEmail, contactWebsite: config.app.website }; if (!copy.some(c => c.lang === details.defaultLanguage) && !ctx.args.has('only')) throw new Error(`listing has no section for the default language ${details.defaultLanguage}`); log('play', `${copy.length} listings: ${copy.map(c => c.lang).join(', ')}${config.play?.video ? ` · promo video ${config.play.video}` : ''}`); const play = await playClient(root, pkg); if (ctx.args.has('dry')) { await play.read(async id => { const cur = await play.call>('GET', `${play.API}/edits/${id}/details`); const dc = detailsChanges(cur, details); log('play', `details: ${dc.length ? 'would change ' + dc.join(', ') : 'unchanged'}`); const now = (await play.call<{ listings?: PlayListing[] }>('GET', `${play.API}/edits/${id}/listings`)).listings ?? []; const changes = listingChanges(now, copy.map(c => listingBody(c, config.play?.video))); for (const c of changes) log('play', `${c.lang}: ${c.change === 'update' ? 'would update ' + c.fields.join(', ') : c.change === 'new' ? 'would add (new language)' : 'unchanged'}`); const other = now.filter(l => !copy.some(c => c.lang === l.language)).map(l => l.language); if (other.length && !ctx.args.has('only')) log('play', `${other.length} languages on Play not in the listing file (left as they are): ${other.join(', ')}`); const n = changes.filter(c => c.change !== 'same').length + (dc.length ? 1 : 0); done('play', `dry run: ${n ? n + ' change(s)' : 'nothing to change'}; read-only, nothing written`); }); return; } await play.edit(async id => { await play.call('PUT', `${play.API}/edits/${id}/details`, details); log('play', `details: ${details.defaultLanguage} · ${details.contactEmail} · ${details.contactWebsite}`); for (const [i, c] of copy.entries()) { await play.call('PUT', `${play.API}/edits/${id}/listings/${c.lang}`, listingBody(c, config.play?.video)); log('play', step(i + 1, copy.length, c.lang)); } }); done('play', 'listing done'); }