wealth/tools/generate-splash-screens.ts

61 lines
2.4 KiB
TypeScript

// Writes the iOS startup images (the pwa lib's APPLE_STARTUP_IMAGES) into each app's public/: the
// app's 512px icon centered on the manifest's background, as Android draws its splash.
//
// pnpm exec jiti tools/generate-splash-screens.ts apps/bull apps/bear
//
// Run it again and commit the images when an app's icon, MANIFEST_BACKGROUND_COLOR, or APPLE_DEVICES
// changes. ImageResponse ships with Next.js, so no dependency is added.
import { mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
import path from 'node:path';
import { ImageResponse } from 'next/og';
import { createElement } from 'react';
import { APPLE_SPLASH_DIR } from '../packages/libs/src/pwa/constants';
import { APPLE_STARTUP_IMAGES } from '../packages/libs/src/pwa/splash-screens';
import { MANIFEST_BACKGROUND_COLOR } from '../platforms/market/src/app/manifest/constants';
const ICON = 'pwa/icon-512.png';
async function writeSplashScreens(appDir: string): Promise<void> {
const publicDir = path.resolve(appDir, 'public');
const icon = `data:image/png;base64,${readFileSync(path.join(publicDir, ICON)).toString('base64')}`;
// Starts empty, so a size removed from APPLE_DEVICES leaves no image behind.
const splashDir = path.join(publicDir, APPLE_SPLASH_DIR);
rmSync(splashDir, { recursive: true, force: true });
mkdirSync(splashDir, { recursive: true });
for (const { url, width, height } of APPLE_STARTUP_IMAGES) {
const size = Math.round(Math.min(width, height) / 4);
const page = createElement(
'div',
{
style: {
width: '100%',
height: '100%',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
background: MANIFEST_BACKGROUND_COLOR,
},
},
createElement('img', { src: icon, width: size, height: size }),
);
const image = new ImageResponse(page, { width, height });
writeFileSync(path.join(publicDir, url), Buffer.from(await image.arrayBuffer()));
}
console.log(`${appDir}: ${APPLE_STARTUP_IMAGES.length} startup images in public${APPLE_SPLASH_DIR}`);
}
async function main(): Promise<void> {
const appDirs = process.argv.slice(2);
if (appDirs.length === 0) {
throw new Error('Usage: pnpm exec jiti tools/generate-splash-screens.ts apps/<app>...');
}
for (const appDir of appDirs) await writeSplashScreens(appDir);
}
main().catch((error: unknown) => {
console.error(error);
process.exitCode = 1;
});