Abstracted snippet loading

Adds a utility script that is used to load the snippets (more coming).
This commit is contained in:
Angelos Chalaris
2018-02-04 19:36:38 +02:00
parent 890cb92f96
commit c7e1d8204c
4 changed files with 33 additions and 52 deletions

View File

@ -6,6 +6,7 @@
const fs = require('fs-extra');
const path = require('path');
const chalk = require('chalk');
const util = require('./util');
// Paths
const SNIPPETS_PATH = './snippets';
const SNIPPETS_ARCHIVE_PATH = './snippets_archive';
@ -68,7 +69,7 @@ These snippets, while useful and interesting, didn\'t quite make it into the rep
console.log(`${chalk.green('SUCCESS!')} README file generated for snippets archive!`);
console.timeEnd('Builder');
}
const snippets = {};
let snippets = {};
const EMOJIS = {
adapter: '🔌',
array: '📚',
@ -98,18 +99,7 @@ const capitalize = (str, lowerRest = false) =>
console.time('Builder');
// Synchronously read all snippets from snippets folder and sort them as necessary (case-insensitive)
try {
const snippetFilenames = fs
.readdirSync(SNIPPETS_PATH)
.sort((a, b) => a.toLowerCase() - b.toLowerCase());
// Store the data read from each snippet in the appropriate object
for (const name of snippetFilenames) {
snippets[name] = fs.readFileSync(path.join(SNIPPETS_PATH, name), 'utf8');
}
} catch (err) {
console.log(`${chalk.red('ERROR!')} During snippet loading: ${err}`);
process.exit(1);
}
snippets = util.readSnippets(SNIPPETS_PATH);
// Load static parts for the README file
try {

View File

@ -6,6 +6,7 @@
const fs = require('fs-extra'),
path = require('path'),
chalk = require('chalk');
const util = require('./util');
// Load helper functions (these are from existing snippets in 30 seconds of code!)
const isTravisCI = () => 'TRAVIS' in process.env && 'CI' in process.env;
if(isTravisCI() && /^Travis build: \d+/g.test(process.env['TRAVIS_COMMIT_MESSAGE'])) {
@ -26,24 +27,7 @@ const countOccurrences = (arr, value) => arr.reduce((a, v) => (v === value ? a +
// Start the timer of the script
console.time('Tagger');
// Synchronously read all snippets and sort them as necessary (case-insensitive)
try {
let snippetFilenames = fs.readdirSync(snippetsPath);
snippetFilenames.sort((a, b) => {
a = a.toLowerCase();
b = b.toLowerCase();
if (a < b) return -1;
if (a > b) return 1;
return 0;
});
// Store the data read from each snippet in the appropriate object
for (let snippet of snippetFilenames)
snippets[snippet] = fs.readFileSync(path.join(snippetsPath, snippet), 'utf8');
} catch (err) {
// Handle errors (hopefully not!)
console.log(`${chalk.red('ERROR!')} During snippet loading: ${err}`);
process.exit(1);
}
// Load tag data from the database
snippets = util.readSnippets(snippetsPath);
try {
tagDbData = objectFromPairs(
fs

26
scripts/util.js Normal file
View File

@ -0,0 +1,26 @@
const fs = require('fs-extra'),
path = require('path'),
chalk = require('chalk');
// Synchronously read all snippets and sort them as necessary (case-insensitive)
const readSnippets = snippetsPath => {
let snippets = {};
try {
let snippetFilenames = fs.readdirSync(snippetsPath);
snippetFilenames.sort((a, b) => {
a = a.toLowerCase();
b = b.toLowerCase();
if (a < b) return -1;
if (a > b) return 1;
return 0;
});
// Store the data read from each snippet in the appropriate object
for (let snippet of snippetFilenames)
snippets[snippet] = fs.readFileSync(path.join(snippetsPath, snippet), 'utf8');
} catch (err) {
// Handle errors (hopefully not!)
console.log(`${chalk.red('ERROR!')} During snippet loading: ${err}`);
process.exit(1);
}
return snippets;
}
module.exports = {readSnippets};

View File

@ -8,6 +8,7 @@ const fs = require('fs-extra'),
chalk = require('chalk'),
md = require('markdown-it')(),
minify = require('html-minifier').minify;
const util = require('./util');
var Prism = require('prismjs');
// Load helper functions (these are from existing snippets in 30 seconds of code!)
const isTravisCI = () => 'TRAVIS' in process.env && 'CI' in process.env;
@ -63,23 +64,7 @@ const capitalize = (str, lowerRest = false) =>
// Start the timer of the script
console.time('Webber');
// Synchronously read all snippets and sort them as necessary (case-insensitive)
try {
let snippetFilenames = fs.readdirSync(snippetsPath);
snippetFilenames.sort((a, b) => {
a = a.toLowerCase();
b = b.toLowerCase();
if (a < b) return -1;
if (a > b) return 1;
return 0;
});
// Store the data read from each snippet in the appropriate object
for (let snippet of snippetFilenames)
snippets[snippet] = fs.readFileSync(path.join(snippetsPath, snippet), 'utf8');
} catch (err) {
// Handle errors (hopefully not!)
console.log(`${chalk.red('ERROR!')} During snippet loading: ${err}`);
process.exit(1);
}
snippets = util.readSnippets(snippetsPath);
// Load static parts for the index.html file
try {
startPart = fs.readFileSync(path.join(staticPartsPath, 'index-start.html'), 'utf8');
@ -107,10 +92,6 @@ try {
console.log(`${chalk.red('ERROR!')} During tag database loading: ${err}`);
process.exit(1);
}
const renderTagAndSnippets = (tag) => {
}
// Create the output for the index.html file
try {
// Add the start static part