awesome-docker/build.js

296 lines
7.7 KiB
Markdown
Raw Normal View History

2018-07-21 09:12:59 -04:00
const fs = require('fs-extra');
2018-08-08 06:22:43 -04:00
const fetch = require('node-fetch');
2018-03-17 11:27:41 -04:00
const cheerio = require('cheerio');
2018-07-21 09:12:59 -04:00
const dayjs = require('dayjs');
const showdown = require('showdown');
2018-03-17 11:27:41 -04:00
const Parcel = require('parcel-bundler');
2018-06-09 16:59:45 -04:00
const sm = require('sitemap');
2018-03-17 11:27:41 -04:00
process.env.NODE_ENV = 'production';
2018-07-21 09:12:59 -04:00
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`;
2018-08-08 06:22:43 -04:00
const MAPPING = `${DATA_FOLDER}/mapping.json`;
const CATEGORY = `${DATA_FOLDER}/category.json`;
2018-07-21 09:12:59 -04:00
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`;
2018-08-08 06:22:43 -04:00
// --- CONFIG
2018-07-21 09:12:59 -04:00
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',
},
],
};
2018-08-08 06:22:43 -04:00
// --- 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];
2018-08-13 03:54:14 -04:00
return url ? `<img src="${url}" class="emoji" alt="${name}" />` : match;
2018-08-08 06:22:43 -04:00
});
return result || text;
};
2018-07-21 09:12:59 -04:00
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,
2018-08-08 06:22:43 -04:00
categoryName,
2018-07-21 09:12:59 -04:00
},
i,
) =>
[
`<li data-id="${i}">`,
`<a href="${repoURL}" class="link ${valueNames[0]}">${name}</a>`,
2018-08-08 06:22:43 -04:00
`<p class="${valueNames[1]}">${emojify(description) || '-'}</p>`,
2018-07-21 09:12:59 -04:00
`<p class="${
valueNames[4]
} timestamp" data-timestamp="${updated}">Last code update: ${getLastUpdate(
updated,
)}</p>`,
(homepage &&
`<a href="${mapHomePage(homepage)}" class="link ${
valueNames[2]
}">website</a>`) ||
'<p></p>',
`<p class="${
valueNames[3]
2018-08-13 09:51:21 -04:00
} timestamp" title="Stars on GitHub" data-stars="${stargazers}">⭐️${stargazers}</p>`,
2018-07-21 09:12:59 -04:00
(language && `<p class="${valueNames[5]}">${language}</p>`) || '<p></p>',
(license &&
license.url !== null &&
`<a href="${license.url}" class="link ${valueNames[6]}">${mapLicense(
license.name,
)}</a>`) ||
'<p></p>',
2018-08-08 06:22:43 -04:00
`<p title="Category">${categoryName}</p>`,
2018-07-21 09:12:59 -04:00
owner &&
2018-08-08 06:22:43 -04:00
`<a href="${owner.html_url}" class="link ${valueNames[7]}">${
owner.login
}</a>`,
2018-07-21 09:12:59 -04:00
'</li>',
].join('');
2018-08-08 06:22:43 -04:00
const buttonHTLM = valueNames
.filter(x => !['description', 'homepage'].includes(x))
.map(v => `<button class="sort" data-sort="${v}">${v} </button>`)
.join('');
const processMetadata = metaData =>
[
`<div class="container">`,
2018-08-13 03:54:14 -04:00
`<div class="searchbar"><input class="search" placeholder="Search" /></div>`,
`<div class="sortbtn"><p>Sort by</p>${buttonHTLM}</div>`,
2018-08-08 06:22:43 -04:00
`</div>`,
'<ul class="list">',
Object.values(metaData)
.map(formatEntry)
.join(''),
'</ul>',
].join('');
const normalizedMetadata = ([mapping, category, data]) =>
data.reduce((acc, repo) => {
const m = mapping[repo.html_url];
if (!m) {
console.log('MISSING:', { repo: repo.html_url });
return acc;
}
const c = m && category[m.category];
if (!c) {
console.log('CATEGORY MISSING', { mapping: m });
return acc;
}
return {
...acc,
...{
[repo.html_url.toLowerCase()]: {
...repo,
ownerType: repo.owner && repo.owner.type,
categoryName: c.name,
categoryDescription: c.description,
status: m.status,
},
},
};
}, {});
2018-07-21 09:12:59 -04:00
async function processTable() {
try {
LOG.debug('Loading files...', { LATEST_FILENAME, tableTemplate });
const latestFilename = await fs.readFile(LATEST_FILENAME, 'utf8');
LOG.debug({ latestFilename });
2018-04-07 08:38:24 -04:00
2018-08-08 06:22:43 -04:00
const data = await Promise.all([
fs.readJSON(MAPPING),
fs.readJSON(CATEGORY),
fs.readJSON(latestFilename),
]);
const metaData = normalizedMetadata(data);
LOG.debug({ metaData });
2018-07-21 09:12:59 -04:00
const template = await fs.readFile(tableTemplate, 'utf8');
2018-08-08 06:22:43 -04:00
LOG.debug('Processing template');
2018-07-21 09:12:59 -04:00
const $ = cheerio.load(template);
2018-08-08 06:22:43 -04:00
$('#md').append(processMetadata(metaData));
2018-07-21 09:12:59 -04:00
LOG.debug('Writing table.html');
await fs.outputFile(tableDestination, $.html(), 'utf8');
LOG.debug('✅ DONE 👍');
} catch (err) {
handleFailure(err);
}
}
async function processIndex() {
2018-03-18 09:37:19 -04:00
const converter = new showdown.Converter({
omitExtraWLInCodeBlocks: true,
simplifiedAutoLink: true,
excludeTrailingPunctuationFromURLs: true,
literalMidWordUnderscores: true,
strikethrough: true,
tables: true,
tablesHeaderId: true,
ghCodeBlocks: true,
tasklists: true,
disableForced4SpacesIndentedSublists: true,
simpleLineBreaks: true,
requireSpaceBeforeHeadingText: true,
ghCompatibleHeaderId: true,
ghMentions: true,
backslashEscapesHTMLTags: true,
emoji: true,
2018-04-07 08:38:24 -04:00
splitAdjacentBlockquotes: true,
2018-03-18 09:37:19 -04:00
});
// converter.setFlavor('github');
2018-07-21 09:12:59 -04:00
try {
LOG.debug('Loading files...', { indexTemplate, README });
const template = await fs.readFile(indexTemplate, 'utf8');
const markdown = await fs.readFile(README, 'utf8');
2018-03-18 09:37:19 -04:00
2018-07-21 09:12:59 -04:00
LOG.debug('Merging files...');
const $ = cheerio.load(template);
$('#md').append(converter.makeHtml(markdown));
2018-03-18 09:37:19 -04:00
2018-07-21 09:12:59 -04:00
LOG.debug('Writing index.html');
await fs.outputFile(indexDestination, $.html(), 'utf8');
LOG.debug('DONE 👍');
} catch (err) {
handleFailure(err);
}
}
2018-03-18 10:01:47 -04:00
2018-07-21 09:12:59 -04:00
const bundle = () => {
LOG.debug('---');
LOG.debug('📦 Bundling with Parcel.js');
LOG.debug('---');
new Parcel(indexDestination, {
2018-03-18 10:01:47 -04:00
name: 'build',
2018-04-07 08:38:24 -04:00
publicURL: '/',
2018-04-22 06:34:38 -04:00
})
.bundle()
2018-07-21 09:12:59 -04:00
.then(() =>
2018-06-09 16:59:45 -04:00
// Creates a sitemap object given the input configuration with URLs
2018-07-21 09:12:59 -04:00
fs.outputFile(
'dist/sitemap.xml',
sm.createSitemap(sitemapOpts).toString(),
),
);
2018-03-18 10:01:47 -04:00
};
2018-03-18 09:37:19 -04:00
2018-07-21 09:12:59 -04:00
async function main() {
2018-08-08 06:22:43 -04:00
emojiMapURL = await loadEmoji();
2018-07-21 09:12:59 -04:00
await processTable();
await processIndex();
await bundle();
}
2018-03-18 09:37:19 -04:00
main();