Skip to content

Instantly share code, notes, and snippets.

@azappa
Created November 3, 2016 07:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save azappa/b04e36f995ec95ebabb481f0d3a7de70 to your computer and use it in GitHub Desktop.
Save azappa/b04e36f995ec95ebabb481f0d3a7de70 to your computer and use it in GitHub Desktop.
antima written using es6
#!/usr/bin/env node
// -- node modules --
import fs from 'fs';
import fsp from 'fs-promise';
import del from 'del';
import { renderFile } from 'pug';
import { argv } from 'yargs';
import yaml from 'yamljs';
import cons from 'better-console';
const doesItExist = async (path) => {
try {
return fs.realpathSync(`${path}`);
} catch (err) {
cons.error(`No file for ${path}.`);
return false;
}
};
const readFile = async path => {
console.log(`called readFile with param ${path}`);
return await fsp.readFile(`${path}`, 'utf-8');
};
const readDir = async path => {
console.log(`called readDir with param ${path}`);
return await fsp.readdir(`${path}`);
};
const buildHTML = async (path, data) => {
console.log(`called buildHTML with param ${path}, ${JSON.stringify(data)}`);
return renderFile(`${path}`, { data });
};
// -- check parameters --
if (!argv.template || argv.template === '') {
cons.info('> Error, you must pass a --template variable to node process.');
process.exit();
}
// -- variables --
const config = {
template: argv.template,
yamlPath: `./${argv.template}/source`,
htmlPath: `./${argv.template}`,
templatePath: `./${argv.template}/template`,
};
const build = async () => {
// -- check if folder with json files exists --
if (!await doesItExist(config.yamlPath)) {
cons.info(`> Error, data folder into ${config.template} does not exist.`);
process.exit();
}
// -- reading data from json files --
let yamls = await readDir(config.yamlPath);
yamls = yamls.filter(f => (f.indexOf('.yaml') !== -1));
cons.info(`.yamls are: ${yamls}`);
if (!yamls || yamls.length === 0) {
cons.info(`> Error, you have zero yaml files into ${config.yamlPath} for data loading.`);
process.exit();
}
// -- removing old HTML files --
await del([`${config.htmlPath}/*.html`]);
const sitemap = [];
let sitemapConfig = [];
// -- check config file for sitemap --
if (!await doesItExist(`${config.templatePath}/sitemap.config.json`)) {
cons.info('> Error, sitemap.config.json does not exist.');
process.exit();
}
sitemapConfig = JSON.parse(await readFile(`${config.templatePath}/sitemap.config.json`, 'utf-8')).keys;
cons.info(`Sitemap config is: ${sitemapConfig}`);
// -- generating sitemap --
yamls.forEach((file) => {
const yamlSource = yaml.load(`${config.yamlPath}/${file}`);
const sitemapItem = { path: `${config.htmlPath}/${file.replace('.yaml', '.html')}` };
if (sitemapConfig) {
sitemapConfig.forEach((k) => {
sitemapItem[k] = yamlSource[k];
});
}
sitemap.push(sitemapItem);
});
// -- writing sitemap into a pug file --
await fsp.writeFile(`${config.templatePath}/sitemap.pug`, `- var sitemap = ${JSON.stringify(sitemap)}`, 'utf-8');
// -- build html pages from pug template --
for (const file of yamls) {
const yamlSource = yaml.load(`${config.yamlPath}/${file}`);
const html = await buildHTML(`${config.templatePath}/${config.template}.pug`, yamlSource);
await fsp.writeFile(`${config.htmlPath}/${file.replace('.yaml', '.html')}`, html, 'utf-8');
}
cons.info('> Everything is ok now... Exiting.');
process.exit();
};
build();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment