forked from 11ty/11ty-website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node-avatars-opencollective.js
81 lines (68 loc) · 2.21 KB
/
node-avatars-opencollective.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
const slugify = require("slugify");
const fs = require("fs-extra");
const eleventyImg = require("@11ty/eleventy-img");
eleventyImg.concurrency = 5;
async function fetch(name, imageUrl, website) {
if(!name) {
return;
}
if(!imageUrl && !website) {
return;
}
// TODO bail if the website pathname is a deep path?
let dir = `./avatars/opencollective/`;
await fs.ensureDir(dir);
// if website exists but no avatar image exists, try to find based on website hostname
if(!imageUrl && website) {
let websiteUrl = new URL(website);
imageUrl = `https://unavatar.now.sh/${websiteUrl.hostname}?fallback=false`;
}
try {
let stats = await eleventyImg(imageUrl, {
// formats: ["webp", "jpeg"],
formats: ["jpeg"],
widths: [90],
urlPath: "/img/avatars/opencollective/",
outputDir: "img/avatars/opencollective/",
cacheOptions: {
duration: "1d",
}
});
let slug = slugify(name).toLowerCase();
let path = `${dir}${slug}.json`;
stats.slug = slug;
await fs.writeFile(path, JSON.stringify(stats, null, 2));
return stats;
} catch(e) {
console.log( `Image error (${name}) with`, imageUrl, e );
}
}
(async function() {
let promises = [];
let avatarPaths = {};
// Deprecated
let redirects = [
"# Careful! This file is generated by node-avatars-opencollective.js for runtime JS for Eleventy Contributor Account avatars",
"# You can add manual entries to the netlify.toml file",
];
let getOpenCollectiveData = require("./_data/opencollective");
let opencollective = await getOpenCollectiveData();
for(let supporter of opencollective.supporters) {
// / https://levelup-styleguide.netlify.com/ 200!
promises.push(fetch(supporter.name, supporter.image, supporter.website));
}
let all = await Promise.all(promises);
for(let stats of all) {
if(stats) {
avatarPaths[stats.slug] = stats.jpeg[0].url;
// Deprecated
redirects.push(`/img/avatars/opencollective/${stats.slug}.jpg ${stats.jpeg[0].url} 200!`);
}
}
let avatarMapPath = "./functions/utils/avatarmap.json";
await fs.writeFile(avatarMapPath, JSON.stringify(avatarPaths, null, 2));
console.log( "Wrote", avatarMapPath );
// Deprecated
await fs.writeFile("./_redirects", redirects.join("\n"));
console.log( "Wrote _redirects" );
})();