1
0
Fork 0
mirror of https://github.com/dilllxd/gitfolio.git synced 2024-08-14 22:28:09 +00:00
gitfolio/build.js

116 lines
3.4 KiB
JavaScript
Raw Normal View History

/* Filepath utilities */
2019-08-12 04:10:00 +00:00
const path = require("path");
/* Promise library */
2019-08-12 04:10:00 +00:00
const bluebird = require("bluebird");
const hbs = require("handlebars");
/* Creates promise-returning async functions
from callback-passed async functions */
2019-08-12 04:10:00 +00:00
const fs = bluebird.promisifyAll(require("fs"));
const { updateHTML } = require("./populate");
const { getConfig, outDir } = require("./utils");
2019-05-08 15:42:29 +00:00
const assetDir = path.resolve(`${__dirname}/assets/`);
2019-08-12 04:10:00 +00:00
const config = path.join(outDir, "config.json");
2019-05-08 15:42:29 +00:00
/**
* Creates the stylesheet used by the site from a template stylesheet.
2019-08-12 04:10:00 +00:00
*
* Theme styles are added to the new stylesheet depending on command line
* arguments.
*/
async function populateCSS({
2019-08-12 04:10:00 +00:00
theme = "light",
background = "https://images.unsplash.com/photo-1553748024-d1b27fb3f960?w=1500&q=80"
} = {}) {
2019-08-12 04:10:00 +00:00
/* Get the theme the user requests. Defaults to 'light' */
theme = `${theme}.css`;
let template = path.resolve(assetDir, "index.css");
let stylesheet = path.join(outDir, "index.css");
2019-05-08 15:42:29 +00:00
2019-08-12 04:10:00 +00:00
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);
2019-08-12 04:10:00 +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-08-12 04:10:00 +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({
background: `${background}`
});
/* Add the user-specified styles to the new stylesheet */
await fs.appendFileAsync(stylesheet, styles);
2019-08-12 04:10:00 +00:00
/* Update the config file with the user's theme choice */
const data = await getConfig();
data[0].theme = theme;
await fs.writeFileAsync(config, JSON.stringify(data, null, " "));
}
2019-08-12 04:10:00 +00:00
async function populateConfig(
sort,
order,
includeFork,
twitter,
linkedin,
medium,
dribbble
) {
const data = await getConfig();
data[0].sort = sort;
data[0].order = order;
data[0].includeFork = includeFork;
data[0].twitter = twitter; // added twitter
data[0].linkedin = linkedin; // added linkedin
data[0].medium = medium; // added medium
data[0].dribbble = dribbble; // added dribbble
await fs.writeFileAsync(config, JSON.stringify(data, null, " "));
}
async function buildCommand(username, program) {
2019-08-12 04:10:00 +00:00
await populateCSS(program);
let sort = program.sort ? program.sort : "created";
let order = program.order ? program.order : "asc";
let includeFork = program.fork ? true : false;
let twitter = program.twitter ? program.twitter : null;
let linkedin = program.linkedin ? program.linkedin : null;
let medium = program.medium ? program.medium : null;
let dribbble = program.dribbble ? program.dribbble : null;
await populateConfig(
sort,
order,
includeFork,
twitter,
linkedin,
medium,
dribbble
);
updateHTML(
("%s", username),
sort,
order,
includeFork,
twitter,
linkedin,
medium,
dribbble
);
}
module.exports = {
2019-08-12 04:10:00 +00:00
buildCommand,
populateCSS,
populateConfig
};