diff --git a/scripts/build.js b/scripts/build.js index 8c8d964e1..f931e8249 100644 --- a/scripts/build.js +++ b/scripts/build.js @@ -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 { diff --git a/scripts/tag.js b/scripts/tag.js index e4bbc80e9..650b70c83 100644 --- a/scripts/tag.js +++ b/scripts/tag.js @@ -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 diff --git a/scripts/util.js b/scripts/util.js new file mode 100644 index 000000000..e5de2a830 --- /dev/null +++ b/scripts/util.js @@ -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}; diff --git a/scripts/web.js b/scripts/web.js index 340173ebf..30a04b0fd 100644 --- a/scripts/web.js +++ b/scripts/web.js @@ -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