Skip to content

Instantly share code, notes, and snippets.

@g4rcez
Created August 8, 2019 04:06
Show Gist options
  • Save g4rcez/c6bb44e9dca7d2b401dc4c46467b0cc2 to your computer and use it in GitHub Desktop.
Save g4rcez/c6bb44e9dca7d2b401dc4c46467b0cc2 to your computer and use it in GitHub Desktop.
const FS = require("fs");
const PATH = require("path");
const signale = require("signale");
const { transparentize, lighten, darken } = require("polished");
const dirname = `${__dirname}/../themes/`;
const createContent = async (path, filename) => {
FS.readFile(`${path}${filename}`, "utf8", (err, contents) => {
const json = JSON.parse(contents);
const { theme } = json;
const tenant = filename.replace(/.json$/, "");
signale.start(`Generate ${tenant} theme`);
const colors = Object.keys(theme).reduce((acc, x) => {
const color = theme[x];
if (!`${color}`.startsWith("#")) {
return acc;
}
const name = x;
return {
...acc,
[name]: color,
[`${name}Alpha`]: transparentize(0.5, color),
[`${name}Light`]: lighten(0.2, color),
[`${name}Lightest`]: lighten(0.6, color),
[`${name}Dark`]: darken(0.2, color),
[`${name}Darkest`]: darken(0.6, color)
};
}, {});
const prefixBuild = PATH.join(__dirname, "..", "build");
const themeJS = PATH.join(prefixBuild, "js", `${tenant}.js`);
const fullFile = { ...JSON.parse(contents), theme: colors };
FS.writeFile(themeJS, `window.$__CONFIG__.config = ${JSON.stringify(fullFile)}`, (err) => {
signale.complete(`Settings.js created for tenant: ${themeJS}`);
const folderName = PATH.join(prefixBuild, tenant);
const manifestJson = PATH.join(folderName, "manifest.json");
if (json.tenant) {
FS.mkdir(folderName, () => {
const manifestContent = {
short_name: json.tenant,
name: json.tenant,
icons: [
{
src: json.icon,
sizes: "64x64 32x32 24x24 16x16",
type: "image/x-icon"
},
{
src: json.icon,
sizes: "512x512",
type: "image/x-icon"
}
],
start_url: ".",
orientation: "landscape",
display: "standalone",
theme_color: colors.primary,
background_color: "#000"
};
FS.writeFile(manifestJson, JSON.stringify(manifestContent, null, 4), "utf-8", (err) => {
signale.success(`Manifest.json for tenant: ${tenant}`);
});
});
}
});
});
};
const createFiles = async () =>
FS.readdir(dirname, (err, files) => {
files.forEach((file) => {
createContent(dirname, file);
});
});
module.exports = createFiles;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment