import fs from 'fs' import sharp from 'sharp' import { BaseCommand } from '#application/base/baseCommand' import Storage from '#application/storage' export default class TilesCommand extends BaseCommand { public async execute(): Promise { // Get all tiles const tilesDir = Storage.getPublicPath('tiles') const tiles = fs.readdirSync(tilesDir).filter((file) => file.endsWith('.png')) // Create output directory if it doesn't exist if (!fs.existsSync(tilesDir)) { fs.mkdirSync(tilesDir, { recursive: true }) } for (const tile of tiles) { // Check if tile is already 66x34 const metadata = await sharp(Storage.getPublicPath('tiles', tile)).metadata() if (metadata.width === 66 && metadata.height === 34) { this.logger.info(`Tile ${tile} already processed`) continue } const inputPath = Storage.getPublicPath('tiles', tile) const tempPath = Storage.getPublicPath('tiles', `temp_${tile}`) try { await sharp(inputPath) .resize({ width: 66, height: 34, fit: 'fill', kernel: 'nearest' }) .toFile(tempPath) // Replace original file with processed file fs.unlinkSync(inputPath) fs.renameSync(tempPath, inputPath) this.logger.info(`Processed and replaced: ${tile}`) } catch (error) { console.error(`Error processing ${tile}:`, error) // Clean up temp file if it exists if (fs.existsSync(tempPath)) { fs.unlinkSync(tempPath) } } } this.logger.info('Tile processing completed.') } }