Abstracted snippet loading
Adds a utility script that is used to load the snippets (more coming).
This commit is contained in:
@ -6,6 +6,7 @@
|
|||||||
const fs = require('fs-extra');
|
const fs = require('fs-extra');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const chalk = require('chalk');
|
const chalk = require('chalk');
|
||||||
|
const util = require('./util');
|
||||||
// Paths
|
// Paths
|
||||||
const SNIPPETS_PATH = './snippets';
|
const SNIPPETS_PATH = './snippets';
|
||||||
const SNIPPETS_ARCHIVE_PATH = './snippets_archive';
|
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.log(`${chalk.green('SUCCESS!')} README file generated for snippets archive!`);
|
||||||
console.timeEnd('Builder');
|
console.timeEnd('Builder');
|
||||||
}
|
}
|
||||||
const snippets = {};
|
let snippets = {};
|
||||||
const EMOJIS = {
|
const EMOJIS = {
|
||||||
adapter: '🔌',
|
adapter: '🔌',
|
||||||
array: '📚',
|
array: '📚',
|
||||||
@ -98,18 +99,7 @@ const capitalize = (str, lowerRest = false) =>
|
|||||||
console.time('Builder');
|
console.time('Builder');
|
||||||
|
|
||||||
// Synchronously read all snippets from snippets folder and sort them as necessary (case-insensitive)
|
// Synchronously read all snippets from snippets folder and sort them as necessary (case-insensitive)
|
||||||
try {
|
snippets = util.readSnippets(SNIPPETS_PATH);
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Load static parts for the README file
|
// Load static parts for the README file
|
||||||
try {
|
try {
|
||||||
|
|||||||
@ -6,6 +6,7 @@
|
|||||||
const fs = require('fs-extra'),
|
const fs = require('fs-extra'),
|
||||||
path = require('path'),
|
path = require('path'),
|
||||||
chalk = require('chalk');
|
chalk = require('chalk');
|
||||||
|
const util = require('./util');
|
||||||
// Load helper functions (these are from existing snippets in 30 seconds of code!)
|
// Load helper functions (these are from existing snippets in 30 seconds of code!)
|
||||||
const isTravisCI = () => 'TRAVIS' in process.env && 'CI' in process.env;
|
const isTravisCI = () => 'TRAVIS' in process.env && 'CI' in process.env;
|
||||||
if(isTravisCI() && /^Travis build: \d+/g.test(process.env['TRAVIS_COMMIT_MESSAGE'])) {
|
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
|
// Start the timer of the script
|
||||||
console.time('Tagger');
|
console.time('Tagger');
|
||||||
// Synchronously read all snippets and sort them as necessary (case-insensitive)
|
// Synchronously read all snippets and sort them as necessary (case-insensitive)
|
||||||
try {
|
snippets = util.readSnippets(snippetsPath);
|
||||||
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
|
|
||||||
try {
|
try {
|
||||||
tagDbData = objectFromPairs(
|
tagDbData = objectFromPairs(
|
||||||
fs
|
fs
|
||||||
|
|||||||
26
scripts/util.js
Normal file
26
scripts/util.js
Normal 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};
|
||||||
@ -8,6 +8,7 @@ const fs = require('fs-extra'),
|
|||||||
chalk = require('chalk'),
|
chalk = require('chalk'),
|
||||||
md = require('markdown-it')(),
|
md = require('markdown-it')(),
|
||||||
minify = require('html-minifier').minify;
|
minify = require('html-minifier').minify;
|
||||||
|
const util = require('./util');
|
||||||
var Prism = require('prismjs');
|
var Prism = require('prismjs');
|
||||||
// Load helper functions (these are from existing snippets in 30 seconds of code!)
|
// Load helper functions (these are from existing snippets in 30 seconds of code!)
|
||||||
const isTravisCI = () => 'TRAVIS' in process.env && 'CI' in process.env;
|
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
|
// Start the timer of the script
|
||||||
console.time('Webber');
|
console.time('Webber');
|
||||||
// Synchronously read all snippets and sort them as necessary (case-insensitive)
|
// Synchronously read all snippets and sort them as necessary (case-insensitive)
|
||||||
try {
|
snippets = util.readSnippets(snippetsPath);
|
||||||
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 static parts for the index.html file
|
// Load static parts for the index.html file
|
||||||
try {
|
try {
|
||||||
startPart = fs.readFileSync(path.join(staticPartsPath, 'index-start.html'), 'utf8');
|
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}`);
|
console.log(`${chalk.red('ERROR!')} During tag database loading: ${err}`);
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
const renderTagAndSnippets = (tag) => {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create the output for the index.html file
|
// Create the output for the index.html file
|
||||||
try {
|
try {
|
||||||
// Add the start static part
|
// Add the start static part
|
||||||
|
|||||||
Reference in New Issue
Block a user