import * as path from 'path' import * as fs from 'fs' import { wrapper, TypeOnEveryFile, TypeToolOptions, TypeToolProp } from '@asset-toolkit/core' const { execSync } = require('child_process') export interface TypeProps extends TypeToolOptions { settings?: string preset?: 'low' | 'medium' | 'high' | 'max' mipmap?: boolean linear?: boolean } /** * A tool to create a super compressed image, KTX2. * @param props - { pathIn, pathOut } * @param options - { } * @returns { Promise(returns) } */ const imageSuperCompression: (_props: TypeToolProp, options?: TypeProps) => Promise = wrapper( ({ pathIn, pathOut, file, options, next, log }: TypeOnEveryFile) => { const opts = { settings: '-uastc -uastc_level 2 -uastc_rdo_l 1.0', preset: 'high', mipmap: true, linear: true, ...options, } if (opts.preset === 'max') opts.settings = '-uastc -uastc_level 3 -uastc_rdo_l 1.0' if (opts.preset === 'medium') opts.settings = '-comp_level 5 -max_endpoints 16128 -max_selectors 16128' if (opts.mipmap) opts.settings += ' -mipmap' if (opts.linear) opts.settings += ' -linear' log(opts.settings) execSync( `"${path.resolve(__dirname, '../bin/basisu')}" ${opts.settings} -ktx2 ${path.resolve(pathIn || pathOut, file)}`, ) const extFile = path.extname(file) const newFile = file.replace(extFile, '.ktx2') // move image file from root to destination folder fs.renameSync(path.resolve(process.cwd(), newFile), path.resolve(pathOut, `${file}.ktx2`)) next() }, undefined, { exts: ['png', 'jpg'], name: 'Image Supercompression' }, ) export default imageSuperCompression