import * as CLI from 'yargs' import { logger } from '../' import * as path from 'path' import { Helper } from '../lib/process/index' import * as pMap from 'p-map' const fg = require('fast-glob') const defaultOptions = (yargs: CLI.Argv) => { return yargs.option('input', { default: './', describe: 'The sources' }).option('debug', { default: 'false', describe: 'Enable internal debug message' }) }; let options = (yargs: CLI.Argv) => defaultOptions(yargs); async function convertFiles(files){ return pMap(files,(file: any) => { const inParts = path.parse(file); // magick convert leg.pdf -quality 100 -density 250 -trim -flatten -resize 200% -sharpen 0x1.0 leg.jpg const promise = Helper.run(inParts.dir, 'convert', [ `"${inParts.base}[0]"`, '-quality 80', '-density 250', '-trim', '-flatten', '-sharpen 0x1.0', `"${inParts.name}.jpg"` ]); return promise; },{ concurrency: 1 }); } // node ./build/main.js pdf2jpg --input=../drawings export const register = (cli: CLI.Argv) => { return cli.command('pdf2jpg', '', options, async (argv: CLI.Arguments) => { if (argv.help) { return; } const src = path.resolve('' + argv.input); if (argv.debug) { logger.debug(`Begin convert PDF files${src}`); } const files = fg.sync('*.pdf|*.PDF', { dot: true, cwd: src, absolute: true }); await convertFiles(files); if (argv.debug) { logger.debug(`Converted ${files.length} files`); } }) }