import * as fs from 'fs' import * as path from 'path' const packer = require('../packer/') import { wrapper, TypeOnBody, TypeToolProp, TypeToolOptions } from '@asset-toolkit/core' export interface TypeProps extends TypeToolOptions { format?: string customFormat?: string name?: string height?: number width?: number fullpath?: boolean prefix?: string trim?: boolean square?: boolean powerOfTwo?: boolean scale?: string fuzz?: string algorithm?: string padding?: number sort?: string maxAtlases?: number gutter?: number group?: string[] resizeWidth?: number resizeHeight?: number } /** * A tool to create image spritesheets. * https://github.com/Gamefroot/Gamefroot-Texture-Packer * Install ImageMagick if not present: brew install imagemagick * * @param props - { pathIn, pathOut } * @param options - default { * format: "pixi.js", name: "atlas", size: 2048, path: pathOut, trim: false, square: true, padding: 2, width: size, height: size, powerOfTwo: true, } see https://github.com/Gamefroot/Gamefroot-Texture-Packer * @returns { Promise(returns) } */ const imageSprite: (_props: TypeToolProp, options?: TypeProps) => Promise = wrapper( ({ file, pathIn, pathOut, wrapperProps, next }) => { const filePath = path.resolve(pathIn || pathOut, file) wrapperProps.files.push(filePath) next() }, ({ pathOut, options, wrapperProps }: TypeOnBody): Promise => { return new Promise(resolve => { const opts = { format: 'pixi.js', name: 'atlas', path: pathOut, trim: false, square: true, padding: 2, width: 2048, height: 2048, powerOfTwo: true, ...options, } packer(wrapperProps.files, opts, (err: any) => { if (err) { console.log(err) } else { const files: string[] = [] fs.readdirSync(pathOut).forEach(file => { if (file.startsWith(`${opts.name}-`) && file.includes('.json')) { files.push(file) } }) fs.writeFileSync( path.resolve(pathOut, `${opts.name}.json`), JSON.stringify( { files, }, null, ' ', ), ) resolve() } }) }) }, { exts: ['jpg', 'JPG', 'jpeg', 'JPEG', 'png', 'PNG'], name: 'Image Sprite', files: [] }, ) export default imageSprite