const fs = require('fs-extra');
const fetch = require('node-fetch');
const cheerio = require('cheerio');
const dayjs = require('dayjs');
const showdown = require('showdown');
const Parcel = require('parcel-bundler');
const sm = require('sitemap');
process.env.NODE_ENV = 'production';
const LOG = {
error: (...args) => console.error('❌ ERROR', { ...args }),
debug: (...args) => {
if (process.env.DEBUG) console.log('💡 DEBUG: ', { ...args });
},
};
const handleFailure = err => {
LOG.error(err);
process.exit(1);
};
process.on('unhandledRejection', handleFailure);
// --- FILES
const README = 'README.md';
const WEBSITE_FOLDER = 'website';
const DATA_FOLDER = 'data';
const LATEST_FILENAME = `${DATA_FOLDER}/latest`;
const MAPPING = `${DATA_FOLDER}/mapping.json`;
const CATEGORY = `${DATA_FOLDER}/category.json`;
const indexTemplate = `${WEBSITE_FOLDER}/index.tmpl.html`;
const indexDestination = `${WEBSITE_FOLDER}/index.html`;
const tableTemplate = `${WEBSITE_FOLDER}/table.tmpl.html`;
const tableDestination = `${WEBSITE_FOLDER}/table.html`;
// --- CONFIG
const valueNames = [
'name',
'description',
'homepage',
'star',
'updated',
'language',
'license',
'author',
];
const sitemapOpts = {
hostname: 'https://awesome-docker.netlify.com/',
cacheTime: 6000000, // 600 sec (10 min) cache purge period
urls: [
{
url: '/',
changefreq: 'daily',
priority: 0.8,
lastmodrealtime: true,
lastmodfile: 'dist/index.html',
},
{
url: '/table.html',
changefreq: 'daily',
priority: 0.8,
lastmodrealtime: true,
lastmodfile: 'dist/table.html',
},
],
};
// --- FORMAT
const loadEmoji = () =>
fetch('https://api.github.com/emojis')
.then(r => r.json())
.catch(handleFailure);
let emojiMapURL = {};
const emojify = text => {
if (!text) return text;
const colonWrapped = /(:[\w\-+]+:)/g;
const result = text.replace(colonWrapped, match => {
const name = match.replace(/:/g, '');
const url = emojiMapURL[name];
return url ? `` : match;
});
return result || text;
};
const getLastUpdate = updated => {
const updt = Number(dayjs(updated).diff(dayjs(), 'days'));
if (updt < 0) {
if (Math.abs(updt) === 1) return `1 day ago`;
return `${Math.abs(updt)} days ago`;
} else if (updt === 0) return 'today';
return updated;
};
const mapHomePage = h => {
if (h === 'manageiq.org') return 'https://manageiq.org';
else if (h === 'dev-sec.io') return 'https://dev-sec.io';
return h;
};
const mapLicense = l => {
if (l === 'GNU Lesser General Public License v3.0') return 'GNU LGPL v3.0';
else if (l === 'GNU General Public License v2.0') return 'GNU GPL v2.0';
else if (l === 'GNU General Public License v3.0') return 'GNU GPL v3.0';
else if (l === 'BSD 3-Clause "New" or "Revised" License')
return 'BSD 3-Clause';
else if (l === 'BSD 2-Clause "Simplified" License') return 'BSD 2-Clause';
return l;
};
const formatEntry = (
{
name,
html_url: repoURL,
description,
homepage,
stargazers_count: stargazers,
pushed_at: updated,
language,
license,
owner,
categoryName,
},
i,
) =>
[
`
${emojify(description) || '-'}
`, ` `, (homepage && `website`) || '', ` `, (language && `${language}
`) || '', (license && license.url !== null && `${mapLicense( license.name, )}`) || '', `${categoryName}
`, owner && `${ owner.login }`, 'Sort by
${buttonHTLM}