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

104 lines
2.9 KiB
JavaScript
Raw Normal View History

2020-02-02 17:13:00 +00:00
// Filepath utilities
const path = require("path");
2020-02-02 17:13:00 +00:00
// Promise library
const bluebird = require("bluebird");
const hbs = require("handlebars");
2020-02-02 17:13:00 +00:00
// Creates promise-returning async functions from callback-passed async functions
const fs = bluebird.promisifyAll(require("fs"));
2020-01-12 09:38:32 +00:00
const { updateHTML } = require("./populate");
const { getConfig, outDir } = require("./utils");
const assetDir = path.resolve(`${__dirname}/assets/`);
const config = path.join(outDir, "config.json");
/**
* 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({
2020-01-12 09:38:32 +00:00
theme = "light",
background = "https://source.unsplash.com/1280x720/?wallpaper"
} = {}) {
2020-02-02 17:13:00 +00:00
// Get the theme the user requests. Defaults to 'light'
2020-01-12 09:38:32 +00:00
theme = `${theme}.css`;
const template = path.resolve(assetDir, "index.css");
const stylesheet = path.join(outDir, "index.css");
2020-01-12 09:38:32 +00:00
try {
await fs.accessAsync(outDir, fs.constants.F_OK);
} catch (error) {
await fs.mkdirAsync(outDir);
}
2020-02-02 17:13:00 +00:00
// Copy over the template CSS stylesheet
2020-01-12 09:38:32 +00:00
await fs.copyFileAsync(template, stylesheet);
2020-02-02 17:13:00 +00:00
// Get an array of every available theme
2020-01-12 09:38:32 +00:00
const themes = await fs.readdirAsync(path.join(assetDir, "themes"));
2020-01-12 09:38:32 +00:00
if (!themes.includes(theme)) {
console.error('Error: Requested theme not found. Defaulting to "light".');
theme = "light";
}
2020-01-11 16:48:56 +00:00
2020-02-02 17:13:00 +00:00
// Read in the theme stylesheet
2020-01-12 09:38:32 +00:00
let themeSource = await fs.readFileSync(path.join(assetDir, "themes", theme));
themeSource = themeSource.toString("utf-8");
const themeTemplate = hbs.compile(themeSource);
const styles = themeTemplate({
background: `${background}`
});
2020-02-02 17:13:00 +00:00
// Add the user-specified styles to the new stylesheet
2020-01-12 09:38:32 +00:00
await fs.appendFileAsync(stylesheet, styles);
2020-01-11 16:48:56 +00:00
2020-02-02 17:13:00 +00:00
// Update the config file with the user's theme choice
2020-01-12 09:38:32 +00:00
const data = await getConfig();
data[0].theme = theme;
await fs.writeFileAsync(config, JSON.stringify(data, null, " "));
}
async function populateConfig(opts) {
2020-01-12 09:38:32 +00:00
const data = await getConfig();
Object.assign(data[0], opts);
await fs.writeFileAsync(config, JSON.stringify(data, null, " "));
}
async function buildCommand(username, program) {
2020-01-12 09:38:32 +00:00
await populateCSS(program);
let types;
if (!program.include || !program.include.length) {
types = ["all"];
} else {
types = program.include;
}
2020-01-11 16:48:56 +00:00
2020-01-12 09:38:32 +00:00
const opts = {
sort: program.sort,
order: program.order,
includeFork: Boolean(program.fork),
types,
codepen: program.codepen,
dev: program.dev,
dribbble: program.dribbble,
email: program.email,
2020-01-14 12:08:00 +00:00
facebook: program.facebook,
2020-01-12 09:38:32 +00:00
instagram: program.instagram,
2020-01-12 10:25:54 +00:00
keybase: program.keybase,
2020-01-12 09:38:32 +00:00
reddit: program.reddit,
telegram: program.telegram,
twitter: program.twitter,
xda: program.xda
2020-01-12 09:38:32 +00:00
};
2020-01-12 09:38:32 +00:00
await populateConfig(opts);
updateHTML(("%s", username), opts);
}
module.exports = {
2020-01-12 09:38:32 +00:00
buildCommand,
populateCSS,
populateConfig
};