1
0
Fork 0
mirror of https://github.com/dilllxd/gitfolio.git synced 2024-08-14 22:28:09 +00:00
gitfolio/build.js
Don Isaac e3efba34c1 Added A Suite of Improvements (#25)
* * Created site files are now placed in an output directory (defaults to
dist.)
* Output directory can be specified via the --out flat
* Added a ton of coments 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 image

* Restored config.json to template file

* Changes to the blogging feature

* Moved blog templates and config files to assets/blog
* Created blog pages now go to dist/blog/
* Updated blog.js accordingly

* Make blog.js use the --out setting to specify where blog files should
be put
* create a gitfolio.js file that calls blog/update/populate/build.js
files as commands instead of having to run each script manually
* Make blog/populate.js use bluebird promises

* Updated the README to reflect the changes made by this pull request

* Added link to demo site in README
2019-05-20 13:08:30 +05:30

77 lines
No EOL
2.8 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('-o, --out [directory]', 'the output directory to put the built site')
.parse(process.argv);
const config = 'config.json';
const assetDir = path.resolve('./assets/');
const outDir = path.resolve(program.out || './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) {
updateHTML(('%s', program.name));
} else {
console.error("Error: Please provide a GitHub username.");
}