forked from wesbos/benjamin-moore-css
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch-colours.js
93 lines (81 loc) · 2.8 KB
/
fetch-colours.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
import fetch from 'isomorphic-fetch';
import { writeFile, readFile, stat } from 'fs/promises';
import slugify from '@sindresorhus/slugify';
import { invertColor } from './utils.js';
async function fetchColors() {
try {
// any json parse error will also be caught
const savedColors = await readFile('colors.json', 'utf8');
console.log('using cached');
return JSON.parse(savedColors);
} catch(err) {}
console.log('Fetching Fresh!')
const res = await fetch('https://www.benjaminmoore.com/api/colors');
const data = await res.json();
// strip the keys and get an array of colors
const colors = Object.values(data.colors);
// write the response to a file so we don't have to nail their API
await writeFile('colors.json', JSON.stringify(colors));
return colors;
}
async function countColors() {
const colors = await fetchColors();
return colors.length;
}
async function familyColors() {
const colors = await fetchColors();
return colors.reduce((fams, color) => {
const { family } = color;
fams[family] = fams[family] || [];
fams[family].push(color);
return fams;
}, {
// Put these here so they go first, they look the best
White: [],
Blue: []
});
}
function makeAGoodName(name) {
return slugify(name.replace('\'',''));
}
async function generateCSSFile() {
const colorsByfamily = await familyColors();
// loop over each family and generate a css variable
const css = Object.entries(colorsByfamily).map(([family, colors]) => {
const header = /*css*/`/* \n\t ${family} \n\t*/`;
const css = colors.map(color => {
const { name, hex } = color;
return `\t --${makeAGoodName(name)}: #${hex};`
}).join('\n');
return `${header}\n${css}`;
}).join('\n\n');
await writeFile('css/all.css', css);
}
async function generateHTMLViewer() {
const colorsByfamily = await familyColors();
const html = Object.entries(colorsByfamily).map(([family, colors]) => {
const header = `<h2 id="${slugify(family)}">${family}</h2>`;
const html = colors.map(color => {
const { name, hex } = color;
return `<div class="color" style="background-color: #${hex}">
<p class="name" style="color: ${invertColor(hex, true)}">${name}</p>
<button type="button" class="hex">--${makeAGoodName(name)}: #${hex};</button>
<button type="button" class="variable">var(--${makeAGoodName(name)})</button>
</div>`
}).join('\n');
return `
<div class="family">
${header}
${html}
</div>
`;
}).join('\n\n');
// get the template
const template = await readFile('./template.html', 'utf8');
// replace the template with the html
const templateHTML = template.replace('<!-- DUMP_ME_HERE -->', html);
// write to disk
await writeFile('./index.html', templateHTML);
}
generateCSSFile();
generateHTMLViewer();