-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
124 lines (103 loc) · 2.47 KB
/
utils.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 fs = require('fs')
const Jimp = require('jimp')
const colorwayVersion = require('@helpscout/colorway/package.json').version
const colorwayData = require('@helpscout/colorway/data/colors.json')
const { ICONS_DIR, ICONS_GENERATED } = require('./constants')
function initialize() {
let generatedFile
try {
generatedFile = fs.readFileSync(ICONS_GENERATED)
} catch (e) {
generatedFile = null
}
if (
!generatedFile ||
colorwayVersion !== JSON.parse(generatedFile).colorwayVersion
) {
clearDir(ICONS_DIR)
createGenerationFile(colorwayVersion)
generateIcons(colorwayData)
return true
}
return false
}
function clearDir(directory) {
try {
const files = fs.readdirSync(directory)
for (const file of files) {
if (file !== '.gitkeep') {
fs.unlink(path.join(directory, file), err => {
if (err) throw err
})
}
}
} catch (error) {
throw error
}
}
function createGenerationFile(colorwayVersion) {
try {
const data = JSON.stringify({
timestamp: Date.now(),
colorwayVersion,
})
fs.writeFileSync(ICONS_GENERATED, data)
} catch (error) {
throw error
}
}
function generateIcons(colors) {
const hues = Object.keys(colors)
hues.forEach(hue => {
const group = colors[hue]
const shades = Object.keys(group)
shades.forEach(shade => {
if (shade != 'default') {
const color = group[shade]
new Jimp(30, 30, color, (err, image) => {
if (err) throw err
image.write(`${ICONS_DIR}/${hue}.${shade}.png`, err => {
if (err) throw err
})
})
}
})
})
}
function hexToRGB(hex) {
let r = 0
let g = 0
let b = 0
// 3 digits
if (hex.length == 4) {
r = '0x' + hex[1] + hex[1]
g = '0x' + hex[2] + hex[2]
b = '0x' + hex[3] + hex[3]
// 6 digits
} else if (hex.length == 7) {
r = '0x' + hex[1] + hex[2]
g = '0x' + hex[3] + hex[4]
b = '0x' + hex[5] + hex[6]
}
return `rgb(${+r}, ${+g}, ${+b})`
}
function flattenColors(colorwayData) {
const flattenColors = {}
Object.keys(colorwayData).forEach(group => {
Object.keys(colorwayData[group]).forEach(shade => {
if (shade !== 'default') {
const shadeColor = colorwayData[group][shade]
flattenColors[`${group}.${shade}`] = shadeColor
}
})
})
return flattenColors
}
module.exports = {
clearDir,
createGenerationFile,
flattenColors,
generateIcons,
initialize,
hexToRGB,
}