import fs from 'fs' ; import path from 'path' ; import sharp from 'sharp' ; import Logger from './logger' ; /** Image file information. */ export type ImageInfo = { /** Image size (width/height). */ size : number ; /** Path of an image file. */ filePath : string ; } ; param images param sizes return export const filterImagesBySizes = ( images , sizes ) => { return images . filter ( ( image ) => { return sizes . some ( ( size ) => { return image . size === size ; } ) ; } ) . sort ( ( a , b ) => { return a . size - b . size ; } ) ; } ; param svg param size param dir param logger return const generate = async ( svg , size , dir , logger ) : Promise < ImageInfo > => { const dest = path . join ( dir , size + '.png' ) ; logger . log ( '  Create: ' + dest ) ; await sharp ( svg ) . png ( { compressionLevel : 9 } ) . resize ( size , size , { fit : 'contain' , background : { r : 0 , g : 0 , b : 0 , alpha : 0 } } ) . toFile ( dest ) ; return { size : size , filePath : dest } ; } ; param src param dir param sizes param logger export const generatePNG = async ( src , dir , sizes , logger ) : Promise < ImageInfo [ ] > => { logger . log ( 'SVG to PNG:' ) ; const svg = fs . readFileSync ( src ) ; const images = [ ] ; for ( const size of sizes ) { images . push ( await generate ( svg , size , dir , logger ) ) ; } return images ; } ; export default generatePNG ;