#!/usr/bin/env node import { readdir, mkdir } from 'node:fs/promises' import path from 'node:path' import { fileURLToPath } from 'node:url' import sharp from 'sharp' const __filename = fileURLToPath(import.meta.url) const __dirname = path.dirname(__filename) const rootDir = path.resolve(__dirname, '..') const args = process.argv.slice(2) function readArg(flag, fallback = '') { const idx = args.indexOf(flag) if (idx === -1) return fallback return args[idx + 1] || fallback } const inputDirArg = readArg('--input-dir', 'android-app/playstore-assets/anon') const outputDirArg = readArg('--output-dir', 'android-app/playstore-assets/final') const inputDir = path.resolve(rootDir, inputDirArg) const outputDir = path.resolve(rootDir, outputDirArg) const profiles = [ { key: 'phone', width: 1080, height: 1920 }, { key: 'tablet-7', width: 1200, height: 1920 }, { key: 'tablet-10', width: 1600, height: 2560 }, ] function isImageFile(name) { const lower = name.toLowerCase() return lower.endsWith('.png') || lower.endsWith('.jpg') || lower.endsWith('.jpeg') } async function processFile(fileName) { const inputPath = path.join(inputDir, fileName) const parsed = path.parse(fileName) for (const profile of profiles) { const profileDir = path.join(outputDir, profile.key) await mkdir(profileDir, { recursive: true }) const outputPath = path.join( profileDir, `${parsed.name}-${profile.width}x${profile.height}.png`, ) // Use contain to preserve all UI content and add solid bars only if needed. await sharp(inputPath) .resize(profile.width, profile.height, { fit: 'contain', background: { r: 0, g: 0, b: 0, alpha: 1 }, }) .png() .toFile(outputPath) console.log(`Created: ${path.relative(rootDir, outputPath)}`) } } async function run() { const files = await readdir(inputDir) const images = files.filter(isImageFile) if (images.length === 0) { console.error(`No PNG/JPG files found in: ${path.relative(rootDir, inputDir)}`) process.exitCode = 1 return } await mkdir(outputDir, { recursive: true }) for (const image of images) { await processFile(image) } console.log(`Done. Output dir: ${path.relative(rootDir, outputDir)}`) } run().catch((error) => { console.error('Failed to generate screenshot profiles:', error) process.exitCode = 1 })