awesome-docker/build.js

106 lines
2.6 KiB
Markdown
Raw Normal View History

2018-07-21 13:12:59 +00:00
const fs = require('fs-extra');
2018-03-17 15:27:41 +00:00
const cheerio = require('cheerio');
2018-07-21 13:12:59 +00:00
const showdown = require('showdown');
2018-03-17 15:27:41 +00:00
const Parcel = require('parcel-bundler');
2018-06-09 20:59:45 +00:00
const sm = require('sitemap');
2018-03-17 15:27:41 +00:00
process.env.NODE_ENV = 'production';
2018-07-21 13:12:59 +00: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 indexTemplate = `${WEBSITE_FOLDER}/index.tmpl.html`;
const indexDestination = `${WEBSITE_FOLDER}/index.html`;
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',
},
],
};
async function processIndex() {
2018-03-18 13:37:19 +00: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 12:38:24 +00:00
splitAdjacentBlockquotes: true,
2018-03-18 13:37:19 +00:00
});
// converter.setFlavor('github');
2018-07-21 13:12:59 +00: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 13:37:19 +00:00
2018-07-21 13:12:59 +00:00
LOG.debug('Merging files...');
const $ = cheerio.load(template);
$('#md').append(converter.makeHtml(markdown));
2018-03-18 13:37:19 +00:00
2018-07-21 13:12:59 +00:00
LOG.debug('Writing index.html');
await fs.outputFile(indexDestination, $.html(), 'utf8');
LOG.debug('DONE 👍');
} catch (err) {
handleFailure(err);
}
}
2018-03-18 14:01:47 +00:00
2018-07-21 13:12:59 +00:00
const bundle = () => {
LOG.debug('---');
LOG.debug('📦 Bundling with Parcel.js');
LOG.debug('---');
new Parcel(indexDestination, {
2018-03-18 14:01:47 +00:00
name: 'build',
2018-04-07 12:38:24 +00:00
publicURL: '/',
2018-04-22 10:34:38 +00:00
})
.bundle()
2018-07-21 13:12:59 +00:00
.then(() =>
2018-06-09 20:59:45 +00:00
// Creates a sitemap object given the input configuration with URLs
2018-07-21 13:12:59 +00:00
fs.outputFile(
'dist/sitemap.xml',
sm.createSitemap(sitemapOpts).toString(),
),
);
2018-03-18 14:01:47 +00:00
};
2018-03-18 13:37:19 +00:00
2018-07-21 13:12:59 +00:00
async function main() {
await processIndex();
await bundle();
}
2018-03-18 13:37:19 +00:00
main();