-
Notifications
You must be signed in to change notification settings - Fork 1
/
generate-thumbs.js
61 lines (53 loc) · 1.61 KB
/
generate-thumbs.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
const path = require('path')
const fs = require('fs')
const { promisify } = require('util')
const sharp = require('sharp')
const glob = require('glob')
const potrace = require('potrace')
const imgsDir = path.join('./src/assets/', '**/*.+(jpg|png)')
const writeFile = promisify(fs.writeFile)
const globP = promisify(glob)
const trace = promisify(potrace.trace)
const addThumbToName = filePath => {
const parts = filePath.split('.')
return [parts[0], '.thumb.', parts[1]].join('')
}
const replaceExt = (filePath, ext) => filePath.replace(/\.(png|jpg)/g, ext)
globP(imgsDir)
.then(files =>
Promise.all(
files.filter(file => !/\.(thumb|primitive|trace)\./.test(file)).map(proccessFile)
)
)
.then(() => console.info('Finished'))
.catch(err => console.error(err))
function proccessFile(file) {
console.log(`Processing file ${file}`)
const process = (...callers) => Promise.all(callers.map(caller => caller(file)))
return process(generateThumb, generateTrace)
}
function generateThumb(file) {
const thumb = addThumbToName(file)
return sharp(file)
.resize(64, 64)
.max()
.toFile(thumb)
.then(() => console.info(`Outputed file ${thumb}`))
}
function generateTrace(file) {
const tmpFile = replaceExt(path.basename(file), '.trace.tmp')
const tempPath = `/tmp/${tmpFile}`
const out = replaceExt(file, '.trace.svg')
return sharp(file)
.resize(128, 128)
.max()
.toFile(tempPath)
.then(() =>
trace(tempPath, {
background: '#F6F8FA',
color: '#787878'
})
)
.then(svg => writeFile(out, svg))
.then(() => console.info(`Outputed file ${out}`))
}