57 lines
1.6 KiB
TypeScript
57 lines
1.6 KiB
TypeScript
import fs from 'fs'
|
|
|
|
import sharp from 'sharp'
|
|
|
|
import { BaseCommand } from '#application/base/baseCommand'
|
|
import { getPublicPath } from '#application/storage'
|
|
|
|
export default class TilesCommand extends BaseCommand {
|
|
public async execute(): Promise<void> {
|
|
// Get all tiles
|
|
const tilesDir = 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(getPublicPath('tiles', tile)).metadata()
|
|
if (metadata.width === 66 && metadata.height === 34) {
|
|
this.logger.info(`Tile ${tile} already processed`)
|
|
continue
|
|
}
|
|
|
|
const inputPath = getPublicPath('tiles', tile)
|
|
const tempPath = 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.')
|
|
}
|
|
}
|