Change the background image on every build

This commit is contained in:
K4USTU3H 2019-12-11 20:32:03 +05:30
parent 5e512a6ce5
commit 5d963803be
No known key found for this signature in database
GPG Key ID: C51A67AF09CB9767
2 changed files with 30 additions and 68 deletions

View File

@ -6,15 +6,8 @@ const hbs = require("handlebars");
/* Creates promise-returning async functions
from callback-passed async functions */
const fs = bluebird.promisifyAll(require("fs"));
const
{
updateHTML
} = require("./populate");
const
{
getConfig,
outDir
} = require("./utils");
const { updateHTML } = require("./populate");
const { getConfig, outDir } = require("./utils");
const assetDir = path.resolve(`${__dirname}/assets/`);
const config = path.join(outDir, "config.json");
@ -25,23 +18,18 @@ const config = path.join(outDir, "config.json");
* Theme styles are added to the new stylesheet depending on command line
* arguments.
*/
async function populateCSS(
{
async function populateCSS({
theme = "light",
background = "https://images.unsplash.com/photo-1553748024-d1b27fb3f960?w=1500&q=80"
} = {})
{
background = "https://source.unsplash.com/1600x900/?wallpaper"
} = {}) {
/* 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");
try
{
try {
await fs.accessAsync(outDir, fs.constants.F_OK);
}
catch (err)
{
} catch (err) {
await fs.mkdirAsync(outDir);
}
/* Copy over the template CSS stylesheet */
@ -50,8 +38,7 @@ async function populateCSS(
/* Get an array of every available theme */
let themes = await fs.readdirAsync(path.join(assetDir, "themes"));
if (!themes.includes(theme))
{
if (!themes.includes(theme)) {
console.error('Error: Requested theme not found. Defaulting to "light".');
theme = "light";
}
@ -59,8 +46,7 @@ async function populateCSS(
let themeSource = await fs.readFileSync(path.join(assetDir, "themes", theme));
themeSource = themeSource.toString("utf-8");
let themeTemplate = hbs.compile(themeSource);
let styles = themeTemplate(
{
let styles = themeTemplate({
background: `${background}`
});
/* Add the user-specified styles to the new stylesheet */
@ -72,23 +58,18 @@ async function populateCSS(
await fs.writeFileAsync(config, JSON.stringify(data, null, " "));
}
async function populateConfig(opts)
{
async function populateConfig(opts) {
const data = await getConfig();
Object.assign(data[0], opts);
await fs.writeFileAsync(config, JSON.stringify(data, null, " "));
}
async function buildCommand(username, program)
{
async function buildCommand(username, program) {
await populateCSS(program);
let types;
if (!program.include || !program.include.length)
{
if (!program.include || !program.include.length) {
types = ["all"];
}
else
{
} else {
types = program.include;
}
const opts = {
@ -112,4 +93,4 @@ module.exports = {
buildCommand,
populateCSS,
populateConfig
};
};

51
ui.js
View File

@ -1,31 +1,19 @@
const fs = require("fs");
const express = require("express");
const
{
updateHTML
} = require("./populate");
const
{
populateCSS,
populateConfig
} = require("./build");
const
{
updateCommand
} = require("./update");
const { updateHTML } = require("./populate");
const { populateCSS, populateConfig } = require("./build");
const { updateCommand } = require("./update");
const app = express();
app.set("view engine", "ejs");
app.use(express.static(__dirname + "/views"));
app.set("views", __dirname + "/views");
app.use(
express.json(
{
express.json({
limit: "50mb"
})
);
app.use(
express.urlencoded(
{
express.urlencoded({
limit: "50mb",
extended: true
})
@ -39,17 +27,13 @@ const jsdom = require("jsdom").JSDOM,
};
global.DOMParser = new jsdom().window.DOMParser;
function uiCommand()
{
app.get("/", function(req, res)
{
function uiCommand() {
app.get("/", function(req, res) {
res.render("index.ejs");
});
app.get("/update", function(req, res)
{
if (!fs.existsSync(`${outDir}/config.json`))
{
app.get("/update", function(req, res) {
if (!fs.existsSync(`${outDir}/config.json`)) {
return res.send(
'You need to run build command before using update<br><a href="/">Go Back</a>'
);
@ -58,11 +42,9 @@ function uiCommand()
res.redirect("/");
});
app.post("/build", function(req, res)
{
app.post("/build", function(req, res) {
let username = req.body.username;
if (!username)
{
if (!username) {
return res.send("username can't be empty");
}
let sort = req.body.sort ? req.body.sort : "created";
@ -75,9 +57,9 @@ function uiCommand()
let email = req.body.email ? req.body.email : null;
let instagram = req.body.instagram ? req.body.instagram : null;
let twitter = req.body.twitter ? req.body.twitter : null;
let background = req.body.background ?
req.body.background :
"https://images.unsplash.com/photo-1553748024-d1b27fb3f960?w=1500&q=80";
let background = req.body.background
? req.body.background
: "https://source.unsplash.com/1600x900/?wallpaper";
let theme = req.body.theme == "on" ? "dark" : "light";
const opts = {
sort: sort,
@ -93,8 +75,7 @@ function uiCommand()
};
updateHTML(username, opts);
populateCSS(
{
populateCSS({
background: background,
theme: theme
});
@ -111,4 +92,4 @@ function uiCommand()
module.exports = {
uiCommand
};
};