import * as path from 'path' import * as fs from 'fs' const Font = require('fonteditor-core').Font const woff2 = require('fonteditor-core').woff2 import { wrapper, changeExtension, TypeOnEveryFile, TypeToolProp, TypeToolOptions } from '@asset-toolkit/core' export interface TypeProps extends TypeToolOptions { outputs: ['woff', 'woff2', 'eot', 'ttf', 'svg'] subset: number[] hinting: boolean compound2simple: boolean combinePath: boolean } /** * A tool to ... * @param props - { pathIn, pathOut } * @param options - { } * @returns { Promise(returns) } */ const fontTransform: (_props: TypeToolProp, options?: TypeProps) => Promise = wrapper( ({ pathIn, pathOut, file, options, next }: TypeOnEveryFile) => { const buffer = fs.readFileSync(path.join(pathIn || pathOut, file)) const url = (ext: string) => path.join(pathOut, changeExtension(file, `.${ext}`)) woff2.init().then(() => { const { outputs, ...fontOptions }: TypeOnEveryFile['options'] = { outputs: ['woff', 'woff2'], ...options, } const font = Font.create(buffer, { type: path.extname(file).replace('.', ''), }) outputs.forEach((type: string) => { fs.writeFileSync( url(type), font.write({ type, ...fontOptions, }), ) }) next() }) }, undefined, { exts: ['ttf', 'otf', 'woff', 'woff2', 'eot'], name: 'font-transform' }, ) export default fontTransform