77 lines
4.9 KiB
TypeScript
77 lines
4.9 KiB
TypeScript
// 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
|
|
import { readFileSync } from 'node:fs';
|
|
import { parseAppStoreListing } from '../lib/copy';
|
|
import type { Ctx } from '../lib/ctx';
|
|
import { need } from '../lib/ctx';
|
|
import { done, log } 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<ReturnType<typeof ascClient>>, list: string, type: string, parentRel: string, parentType: string, parentId: string,
|
|
want: Record<string, Record<string, string | undefined>>): Promise<void> {
|
|
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`);
|
|
}
|
|
}
|
|
|
|
export async function run(ctx: Ctx): Promise<void> {
|
|
const { config, root } = ctx.loaded;
|
|
const a = ctx.args;
|
|
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');
|
|
}
|