1
0
Fork 0
mirror of https://github.com/dilllxd/gitfolio.git synced 2024-08-14 22:28:09 +00:00
gitfolio/build.js
imfunny 9c3538a387
added commits by @DonIsaac & @Chargnn
* Created site files are now placed in an output directory `dist`
* Added a ton of comments in build.js
* Made functions in build.js use bluebird promises
* Themes are now in the assets/templates folder instead as magic
strings in the source code
* Any number of custom themes (not just light and dark) can be used by creating
a [theme].css file and putting it in assets/themes
* CSS themes are rendered with Handlebars
* CSS themes now rely on Handlebars for specifying the background 
* Restored config.json to template file
* Changes to the blogging 
* Moved blog templates and config files to assets/blog
* Created blog pages now go to dist/blog/
* Updated blogTemplate
* Updated the README to reflect the changes made by this pull request
* Added link to demo site in README
* Added sorting and ordering
2019-05-20 14:52:19 +05:30

92 lines
No EOL
3.2 KiB
JavaScript

/* Argument parser */
const program = require('commander');
/* Filepath utilities */
const path = require('path');
/* Promise library */
const bluebird = require('bluebird');
const hbs = require('handlebars');
/* Creates promise-returning async functions
from callback-passed async functions */
const fs = bluebird.promisifyAll(require('fs'));
const { updateHTML } = require('./populate');
/* Specify the options the program uses */
program
.version('0.1.1')
.option('-n, --name [username]', 'your GitHub username. This will be used to customize your site')
.option('-t, --theme [theme]', 'specify a theme to use')
.option('-b, --background [background]', 'set the background image')
.option('-f, --fork', 'includes forks with repos')
.option('-s, --sort [sort]', 'set default sort for repository')
.option('-o, --order [order]', 'set default order on sort')
.parse(process.argv);
const config = './dist/config.json';
const assetDir = path.resolve('./assets/');
const outDir = path.resolve('./dist/');
/**
* Creates the stylesheet used by the site from a template stylesheet.
*
* Theme styles are added to the new stylesheet depending on command line
* arguments.
*/
async function populateCSS() {
/* Get the theme the user requests. Defaults to 'light' */
let theme = `${program.theme || 'light'}.css`; /* Site theme, defaults to 'light' */
let template = path.resolve(assetDir, 'index.css');
let stylesheet = path.join(outDir, 'index.css');
try {
await fs.accessAsync(outDir, fs.constants.F_OK);
} catch (err) {
await fs.mkdirAsync(outDir);
}
/* Copy over the template CSS stylesheet */
await fs.copyFileAsync(template, stylesheet);
/* Get an array of every available theme */
let themes = await fs.readdirAsync(path.join(assetDir, 'themes'));
if (!themes.includes(theme)) {
console.error('Error: Requested theme not found. Defaulting to "light".');
theme = 'light';
}
/* Read in the theme stylesheet */
let themeSource = await fs.readFileSync(path.join(assetDir, 'themes', theme));
themeSource = themeSource.toString('utf-8');
let themeTemplate = hbs.compile(themeSource);
let styles = themeTemplate({
'background': `${process.background || 'https://images.unsplash.com/photo-1553748024-d1b27fb3f960?w=1450'}`
})
/* Add the user-specified styles to the new stylesheet */
await fs.appendFileAsync(stylesheet, styles);
/* Update the config file with the user's theme choice */
let data = await fs.readFileAsync(config);
data = JSON.parse(data);
data[0].theme = theme;
await fs.writeFileAsync(config, JSON.stringify(data, null, ' '));
}
populateCSS();
if (program.name) {
let sort = program.sort ? program.sort : 'created_at';
let order = -1;
let includeFork = false;
if(program.order){
if(program.order === 'asc')
order = 1;
else if(program.order === 'desc')
order = -1;
}
if(program.fork){
includeFork = true;
}
updateHTML(('%s', program.name), sort, order, includeFork);
} else {
console.error("Error: Please provide a GitHub username.");
}