2020-02-02 17:13:00 +00:00
|
|
|
// Filepath utilities
|
2019-11-17 02:27:32 +00:00
|
|
|
const path = require("path");
|
2020-02-02 17:13:00 +00:00
|
|
|
// Promise library
|
2019-11-17 02:27:32 +00:00
|
|
|
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
|
2019-11-17 02:27:32 +00:00
|
|
|
const fs = bluebird.promisifyAll(require("fs"));
|
2020-01-12 09:38:32 +00:00
|
|
|
const { updateHTML } = require("./populate");
|
|
|
|
const { getConfig, outDir } = require("./utils");
|
2019-11-17 02:27:32 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
2019-12-11 15:02:03 +00:00
|
|
|
async function populateCSS({
|
2020-01-12 09:38:32 +00:00
|
|
|
theme = "light",
|
|
|
|
background = "https://source.unsplash.com/1280x720/?wallpaper"
|
2019-12-11 15:02:03 +00:00
|
|
|
} = {}) {
|
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");
|
2019-11-17 02:27:32 +00:00
|
|
|
|
2020-01-12 09:38:32 +00:00
|
|
|
try {
|
|
|
|
await fs.accessAsync(outDir, fs.constants.F_OK);
|
|
|
|
} catch (error) {
|
|
|
|
await fs.mkdirAsync(outDir);
|
|
|
|
}
|
2019-11-17 02:27:32 +00:00
|
|
|
|
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);
|
2019-11-17 02:27:32 +00:00
|
|
|
|
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"));
|
2019-11-17 02:27:32 +00:00
|
|
|
|
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, " "));
|
2019-11-17 02:27:32 +00:00
|
|
|
}
|
|
|
|
|
2019-12-11 15:02:03 +00:00
|
|
|
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, " "));
|
2019-11-17 02:27:32 +00:00
|
|
|
}
|
|
|
|
|
2019-12-11 15:02:03 +00:00
|
|
|
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-02-11 16:42:25 +00:00
|
|
|
medium: program.medium,
|
2020-01-12 09:38:32 +00:00
|
|
|
reddit: program.reddit,
|
2020-02-11 16:42:25 +00:00
|
|
|
stackexchange: program.stackexchange,
|
|
|
|
steam: program.steam,
|
2020-01-12 09:38:32 +00:00
|
|
|
telegram: program.telegram,
|
2020-02-04 17:05:22 +00:00
|
|
|
twitter: program.twitter,
|
|
|
|
xda: program.xda
|
2020-01-12 09:38:32 +00:00
|
|
|
};
|
2019-11-17 02:27:32 +00:00
|
|
|
|
2020-01-12 09:38:32 +00:00
|
|
|
await populateConfig(opts);
|
|
|
|
updateHTML(("%s", username), opts);
|
2019-11-17 02:27:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
2020-01-12 09:38:32 +00:00
|
|
|
buildCommand,
|
|
|
|
populateCSS,
|
|
|
|
populateConfig
|
2019-12-11 15:02:03 +00:00
|
|
|
};
|