Skip to content

Instantly share code, notes, and snippets.

@SimplGy
Created August 3, 2020 01:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SimplGy/c3d190bb6bab0d7b6a906dbe8ed699c9 to your computer and use it in GitHub Desktop.
Save SimplGy/c3d190bb6bab0d7b6a906dbe8ed699c9 to your computer and use it in GitHub Desktop.
For every website folder that contains a certain file, autogen an index.html landing page for it.
// For every website folder that contains a certain file, autogen an index.html landing page for it.
const { readdirSync, statSync, writeFileSync, existsSync, readFileSync } = require('fs');
const { join } = require('path');
// ----------------------------------------- Config
const DIR = '/home/deploy/www';
const CONFIG_FILE = 'logo/info.txt';
const generate = ({domainName, colorBg = 'white', colorText = 'black'}) => `<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta name="author" content="@simplgy">
<meta name="description" content="">
<meta name="keywords" content="">
<title>${domainName}</title>
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-174127163-1"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-174127163-1');
</script>
<style>
body {
font-family: "San Francisco", sans-serif;
text-align: center;
background: ${colorBg};
}
a {
color: ${colorText};
}
</style>
</head>
<body>
<h1 style="visibility: hidden;">${domainName}</h1>
<img src="/logo/vector/default.svg" alt="${domainName}">
<h2><a href="https://twitter.com/SimplGy">@simplgy</a></h2>
</body>
</html>`;
// ----------------------------------------- Procedure
console.log(`getting all folders inside of '${DIR}'...`);
const dirs = readdirSync(DIR)
.filter(f => statSync(join(DIR, f)).isDirectory())
.filter(f => existsSync(join(DIR, f, CONFIG_FILE)));
console.log(`These folders contain '${CONFIG_FILE}':`);
console.log(' ' + dirs.join('\n ') + '\n');
// Get data from config file(s)
let colorBg;
let colorText;
let domainName;
const data = dirs
.map(subdir => readFileSync(join(DIR, subdir, CONFIG_FILE), 'utf8'))
// Parse json out of each line of the config files
.map(str =>
str.split('\n')
.map(line => {
try {
return JSON.parse(line);
} catch (err) {
return undefined;
}})
.filter(Boolean)
)
// So far only one set of json per config file, and one config file per dir
.map(([{bg, font}], i) => ({
domainName: dirs[i],
colorBg: bg,
colorText: font,
}));
console.log('found this config:');
console.log(data);
console.log();
// Write out
data.forEach(datum => {
const contents = generate(datum)
writeFileSync(join(DIR, datum.domainName, 'index.html'), contents);
});
console.log(`Generated ${data.length} index.html files and wrote to disk.`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment