import * as path from 'path' import * as bluebird from 'bluebird' import * as pMap from 'p-map' import * as sharp from 'sharp' import { logger } from '../../../index' import { IOptions, IResizeOptions } from '../../../types' import { OSR_CACHE } from '@plastichub/osr-commons' import { sync as exists } from "@plastichub/fs/exists" import { sync as dir } from "@plastichub/fs/dir" import { get_cached } from '@plastichub/osr-cache/lib' import { targets } from '../../' import { MODULE_NAME } from '../../../constants' export const convertFile = async (file, target, onNode: (data: any) => void = () => { }, options: IResizeOptions) => { const osr_cache = OSR_CACHE() const ca_options = JSON.parse(JSON.stringify({ ...options, target, skip: null })) const cached = await get_cached(file, ca_options, MODULE_NAME) let image = sharp(file) const srcParts = path.parse(file) const dstParts = path.parse(target) if(!exists(dstParts.dir)){ dir(dstParts.dir) } let ret = await image.withMetadata().toFile(target) return ret } export async function _convert(file, targets: string[], onNode: (data: any) => void = () => { }, options: IOptions) { return pMap(targets, async (target) => { logger.debug(`Convert ${file} to ${target}`); if (options.dry) { return bluebird.resolve(); } return convertFile(file, target, onNode, options); }, { concurrency: 1 }); } export const convert = async (options: IOptions) => { let reports = [] const onNode = (data) => { reports.push(data) } logger.info(`Convert ${options.srcInfo.FILES.length} files `) await pMap(options.srcInfo.FILES,(f) => { const outputs = targets(f, options) options.verbose && logger.info(`Convert ${f} to `, outputs) return _convert(f, outputs, onNode, options) }, { concurrency: 1 }) }