2019-05-20 07:38:30 +00:00
|
|
|
/* Argument parser */
|
2019-05-08 15:42:29 +00:00
|
|
|
const program = require('commander');
|
2019-05-20 07:38:30 +00:00
|
|
|
/* 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');
|
2019-05-08 15:42:29 +00:00
|
|
|
|
2019-05-20 07:38:30 +00:00
|
|
|
|
|
|
|
/* Specify the options the program uses */
|
2019-05-08 15:42:29 +00:00
|
|
|
program
|
2019-05-20 09:45:12 +00:00
|
|
|
.version('0.1.2')
|
2019-05-20 09:22:19 +00:00
|
|
|
.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);
|
2019-05-20 07:38:30 +00:00
|
|
|
|
2019-05-20 09:17:39 +00:00
|
|
|
const config = './dist/config.json';
|
2019-05-20 07:38:30 +00:00
|
|
|
const assetDir = path.resolve('./assets/');
|
2019-05-20 09:17:39 +00:00
|
|
|
const outDir = path.resolve('./dist/');
|
2019-05-08 15:42:29 +00:00
|
|
|
|
2019-05-20 07:38:30 +00:00
|
|
|
/**
|
|
|
|
* 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');
|
2019-05-08 15:42:29 +00:00
|
|
|
|
2019-05-20 07:38:30 +00:00
|
|
|
try {
|
|
|
|
await fs.accessAsync(outDir, fs.constants.F_OK);
|
|
|
|
} catch (err) {
|
|
|
|
await fs.mkdirAsync(outDir);
|
2019-05-08 15:42:29 +00:00
|
|
|
}
|
2019-05-20 07:38:30 +00:00
|
|
|
/* Copy over the template CSS stylesheet */
|
|
|
|
await fs.copyFileAsync(template, stylesheet);
|
2019-05-08 15:42:29 +00:00
|
|
|
|
2019-05-20 07:38:30 +00:00
|
|
|
/* Get an array of every available theme */
|
|
|
|
let themes = await fs.readdirAsync(path.join(assetDir, 'themes'));
|
2019-05-08 15:42:29 +00:00
|
|
|
|
2019-05-20 07:38:30 +00:00
|
|
|
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({
|
2019-05-20 14:16:32 +00:00
|
|
|
'background': `${program.background || 'https://images.unsplash.com/photo-1553748024-d1b27fb3f960?w=1450'}`
|
2019-05-20 07:38:30 +00:00
|
|
|
})
|
|
|
|
/* 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, ' '));
|
2019-05-12 13:00:22 +00:00
|
|
|
}
|
|
|
|
|
2019-05-20 14:16:32 +00:00
|
|
|
async function populateConfig(sort, order, includeFork) {
|
|
|
|
let data = await fs.readFileAsync(config);
|
|
|
|
data = JSON.parse(data);
|
|
|
|
data[0].sort = sort;
|
|
|
|
data[0].order = order;
|
|
|
|
data[0].includeFork = includeFork;
|
|
|
|
await fs.writeFileAsync(config, JSON.stringify(data, null, ' '));
|
|
|
|
}
|
|
|
|
|
2019-05-20 07:38:30 +00:00
|
|
|
populateCSS();
|
|
|
|
|
2019-05-21 01:37:08 +00:00
|
|
|
if (typeof program.name === 'string' && program.name.trim() !== '') {
|
2019-05-20 09:39:36 +00:00
|
|
|
let sort = program.sort ? program.sort : 'created';
|
2019-05-20 10:25:09 +00:00
|
|
|
let order = "asc";
|
2019-05-20 09:22:19 +00:00
|
|
|
let includeFork = false;
|
2019-05-20 09:17:39 +00:00
|
|
|
if(program.order){
|
2019-05-20 10:25:09 +00:00
|
|
|
order = ('%s', program.order);
|
2019-05-20 09:22:19 +00:00
|
|
|
}
|
|
|
|
if(program.fork){
|
|
|
|
includeFork = true;
|
|
|
|
}
|
2019-05-20 14:16:32 +00:00
|
|
|
populateConfig(sort, order, includeFork);
|
2019-05-20 09:22:19 +00:00
|
|
|
updateHTML(('%s', program.name), sort, order, includeFork);
|
2019-05-08 15:42:29 +00:00
|
|
|
} else {
|
2019-05-20 07:38:30 +00:00
|
|
|
console.error("Error: Please provide a GitHub username.");
|
2019-05-08 15:42:29 +00:00
|
|
|
}
|