Skip to content

Instantly share code, notes, and snippets.

@brekk
Created November 18, 2020 07:17
Show Gist options
  • Save brekk/83ea48f73cbf9a82e4d66443a0a6d168 to your computer and use it in GitHub Desktop.
Save brekk/83ea48f73cbf9a82e4d66443a0a6d168 to your computer and use it in GitHub Desktop.
Convert globs into max 800 x 800 png files
const fs = require('fs')
const path = require('path')
const sharp = require('sharp')
const globby = require('globby')
const { trace } = require('xtrace')
const { futurizeWithCancel, tacit } = require('ensorcel')
const { parallel, fork: rawFork, Future, chain } = require('fluture')
const { reduce, mergeRight, objOf, pipe, curryN, __: $, map } = require('ramda')
const yargsParser = require('yargs-parser')
const { writeFile, copyFile } = require('torpor')
const globWithCancel = futurizeWithCancel($, 1, globby)
const fork = tacit(3, rawFork)
const relativePath = curryN(2, path.resolve)
const IMAGE_DEFAULTS = {
fit: 'outside',
width: 800,
height: 800,
withoutEnlargement: true
}
const imageFuture = (image) => {
const { dir, name } = path.parse(image)
const output = path.join(dir, name + '.png')
return new Future((bad, good) => {
sharp(image)
.resize(IMAGE_DEFAULTS)
.png()
.toBuffer()
.catch(bad)
.then((data) => {
pipe(map(objOf(output)), fork(bad)(good))(writeFile(output, data, { flag: 'w' }))
})
return () => {}
})
}
const run = (raw) =>
pipe(
trace('processing location:'),
globWithCancel(() => {}),
chain(pipe(map(imageFuture), parallel(10))),
map(reduce(mergeRight, {})),
fork(console.warn, console.log)
)(raw)
const opts = yargsParser(process.argv.slice(2))
const { _: directoryWithAsterisks } = opts
run(`${directoryWithAsterisks}`)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment