-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
124 lines (105 loc) · 3.51 KB
/
index.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
const sprite = require('sprity')
const _ = require('lodash')
const Promise = require('bluebird')
const path = require('path')
const exec = require('child_process').exec
const fs = require('fs-extra-promise')
const sharp = require('sharp')
const sizes = [..._.range(10, 32), 32, 64, 128]
// const sizes = [20]
const srcPath = 'bower_components/emojione/assets/png_512x512'
const tmpPath = 'tmp'
const tmpResizePath = path.join(tmpPath, 'resize')
const tmpSpritePath = path.join(tmpPath, 'sprite')
const distPath = 'dist'
const styleName = 'style.scss'
const promisify =
callback =>
new Promise ((resolve, reject) => {
callback(err => {
if (err) {
console.log('Error:', err.message, err)
reject(...arguments)
} else {
resolve(...arguments)
}
})
})
const execPrm =
command =>
promisify(
callback => exec(command, callback)
)
// .then((error, stdout, stderr) => {
// console.log("----------")
// console.log(`finished: ${command}`)
// console.log(`stdout: ${stdout}`);
// console.log(`stderr: ${stderr}`);
// })
console.log("Starting.")
sizes
.reduce((promise, size) => {
return promise
.then(() => fs.ensureDirAsync(tmpResizePath))
.then(() => fs.ensureDirAsync(tmpSpritePath))
.then(() => fs.ensureDirAsync(distPath))
.then(() => {
console.log(`Resizing emoji to size ${size}`)
return fs.readdirAsync(srcPath)
})
.then(fileNames =>
Promise.all(
fileNames.map(fileName => {
const inputFileName = path.join(srcPath, fileName)
const outputFileName = path.join(tmpResizePath, fileName)
return promisify(callback =>
sharp(inputFileName)
.resize(size, size)
.toFile(outputFileName, callback)
)
})
)
)
.then(() => {
console.log(`Generating spritesheet of size ${size}`)
return promisify(
callback =>
sprite
.create({
src: path.join(tmpResizePath, '*.png'),
out: tmpSpritePath,
name: `sprite-${size}`,
style: styleName,
processor: 'emojione-scss',
margin: 0,
prefix: 'emojione',
dimension: [ { ratio: 1, dpi: 1 } ],
orientation: 'binary-tree',
},
callback
)
)
})
.then(() => {
// https://zoompf.com/blog/2014/11/png-optimization
console.log(`Optimizing spritesheet of size ${size}`)
const sourcePath = path.join(tmpSpritePath, `sprite-${size}.png`)
const targetPath = path.join(distPath, `sprite-${size}.png`)
const command = `pngcrush -rem alla -nofilecheck -reduce -m 7 ${sourcePath} ${targetPath}`
return execPrm(command)
})
}, Promise.resolve())
.then(() => {
console.log("Finished generating & optimizing sprites. Moving style file to dist.")
const sourcePath = path.join(tmpSpritePath, styleName)
const targetPath = path.join(distPath, styleName)
return fs.renameAsync(sourcePath, targetPath)
})
.then(() => {
if (tmpPath.slice(0, 1) === '/') return
console.log("Removing tmp folder.")
return fs.removeAsync(tmpPath)
})
.then(() => {
console.log("Finished.")
})