-
Notifications
You must be signed in to change notification settings - Fork 1
/
image.js
95 lines (80 loc) · 3.04 KB
/
image.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
const fs = require('fs')
const execa = require('execa')
const { join, parse: pathParse } = require('path')
const sharp = require('sharp')
const pngquant = require('imagemin-pngquant')()
const webp = require('imagemin-webp')({ quality: 50 })
const { readdir, readFile, writeFile } = fs.promises
const delim = '<!-- ⚡ -->'
const imgTypes = [
{ type: 'webp', handler: webp },
{ type: 'png', handler: pngquant }
]
const filesToAdd = []
const gitAdd = async ({ name, content }) => {
await writeFile(name, content, 'utf8')
filesToAdd.push(name)
}
const buildImage = async (image, w) => {
console.log('sharpening...')
const sharped = sharp(image)
const { width, height } = await sharped.metadata()
const [ buf, bufX2 ] = await Promise.all([
sharped.resize(Math.min(width, w)).toBuffer(),
sharped.resize(Math.min(width, w * 2)).toBuffer()
])
console.log('resized')
return {
width,
height,
version: await Promise.all(imgTypes.map(({ type, handler }) =>
Promise.all([ handler(buf), handler(bufX2) ])
.then(sizes => ({ type, sizes }))))
}
}
const buildArticleHTML = ({ id, ratio }) => `
<div class="article" id="${id}" --data-date="${Date.now()}" style="padding-top: ${ratio}%"></div>`
const exportImage = async file => {
const id = pathParse(file).name
const body = await readFile(join('source', file))
console.log(id, 'reading OK')
const image = await buildImage(body, 702)
console.log(id, 'build OK')
const versions = image.version.flatMap(({ type, sizes: [x1, x2] }) => [
{ name: join('img', `${id}.${type}`), content: x1 },
{ name: join('img', 'x2', `${id}.${type}`), content: x2 },
])
await Promise.all(versions.map(gitAdd))
console.log(id, 'created')
return { id, ratio: ((image.height / image.width) * 100).toFixed(4) }
}
const exec = async () => {
await execa('git', ['pull', '-r', '--autostash'])
const [srcFiles, img] = await Promise.all([ readdir('source'), readdir('img') ])
const available = img.filter(f => f.endsWith('.png')).map(f => f.slice(0, -4))
const addedFiles = srcFiles
.filter(f => f.endsWith('.png') && !available.includes(pathParse(f).name))
if (!addedFiles.length) return console.log('no new files')
console.log(addedFiles.length, 'image to add...')
filesToAdd.push(...srcFiles.map(s => join('source', s)))
const files = await Promise.all(addedFiles.map(exportImage))
console.log('adding images to index...')
const indexBody = await readFile('index.html', 'utf8')
const [left, right] = indexBody.toString('utf8').split(delim)
const articles = files.map(buildArticleHTML)
const content = `${left}${delim}${articles.join('')}${right}`
await gitAdd({ name: 'index.html', content })
await execa('git', ['add', ...filesToAdd])
await execa('git', ['commit', '-m', `add images ${addedFiles.join(',')}`])
console.log('git commit and git add')
const push = execa('git', ['push'])
console.log('git push')
push.stdout.pipe(process.stdout)
await push
console.log('All done !')
}
exec()
.catch(err => {
console.error(err)
process.exit(1)
})