diff --git a/core/lib/list_item_hunter.js b/core/lib/list_item_hunter.js index c13078f2f..7ffe450de 100644 --- a/core/lib/list_item_hunter.js +++ b/core/lib/list_item_hunter.js @@ -2,9 +2,9 @@ const list_item_hunter = function () { const extend = require('util')._extend; + const _ = require('lodash'); const pa = require('./pattern_assembler'); const smh = require('./style_modifier_hunter'); - const plutils = require('./utilities'); const jsonCopy = require('./json_copy'); const Pattern = require('./object_factory').Pattern; @@ -47,7 +47,7 @@ const list_item_hunter = function () { console.log(err); } - listData = plutils.mergeData(listData, pattern.listitems); + listData = _.merge(listData, pattern.listitems); listData = pattern_assembler.parse_data_links_specific(patternlab, listData, 'listitems.json + any pattern listitems.json'); //iterate over each copied block, rendering its contents along with pattenlab.listitems[i] @@ -68,8 +68,8 @@ const list_item_hunter = function () { console.log(err); } - let allData = plutils.mergeData(globalData, localData); - allData = plutils.mergeData(allData, itemData !== undefined ? itemData[i] : {}); //itemData could be undefined if the listblock contains no partial, just markup + let allData = _.merge(globalData, localData); + allData = _.merge(allData, itemData !== undefined ? itemData[i] : {}); //itemData could be undefined if the listblock contains no partial, just markup allData.link = extend({}, patternlab.data.link); //check for partials within the repeated block diff --git a/core/lib/parameter_hunter.js b/core/lib/parameter_hunter.js index 07c20a167..c5a5d9ce5 100644 --- a/core/lib/parameter_hunter.js +++ b/core/lib/parameter_hunter.js @@ -2,10 +2,10 @@ const parameter_hunter = function () { const extend = require('util')._extend; + const _ = require('lodash'); const jsonCopy = require('./json_copy'); const pa = require('./pattern_assembler'); const smh = require('./style_modifier_hunter'); - const plutils = require('./utilities'); const style_modifier_hunter = new smh(); const pattern_assembler = new pa(); @@ -267,8 +267,8 @@ const parameter_hunter = function () { console.log(err); } - let allData = plutils.mergeData(globalData, localData); - allData = plutils.mergeData(allData, paramData); + let allData = _.merge(globalData, localData); + allData = _.merge(allData, paramData); //if partial has style modifier data, replace the styleModifier value if (pattern.stylePartials && pattern.stylePartials.length > 0) { diff --git a/core/lib/pattern_assembler.js b/core/lib/pattern_assembler.js index d63b1be11..d83a0f7c3 100644 --- a/core/lib/pattern_assembler.js +++ b/core/lib/pattern_assembler.js @@ -62,7 +62,7 @@ const pattern_assembler = function () { list.push(container.listitems[item]); } } - container.listItemArray = plutils.shuffle(list); + container.listItemArray = _.shuffle(list); for (var i = 1; i <= container.listItemArray.length; i++) { var tempItems = []; @@ -149,7 +149,7 @@ const pattern_assembler = function () { var markdownFileContents = fs.readFileSync(markdownFileName, 'utf8'); var markdownObject = markdown_parser.parse(markdownFileContents); - if (!plutils.isObjectEmpty(markdownObject)) { + if (!_.isEmpty(markdownObject)) { //set keys and markdown itself currentPattern.patternDescExists = true; currentPattern.patternDesc = markdownObject.markdown; diff --git a/core/lib/patternlab.js b/core/lib/patternlab.js index 3d1272967..aef5520ae 100644 --- a/core/lib/patternlab.js +++ b/core/lib/patternlab.js @@ -401,7 +401,7 @@ const patternlab_engine = function (config) { console.log('There was an error parsing JSON for ' + pattern.relPath); console.log(err); } - allData = plutils.mergeData(allData, pattern.jsonFileData); + allData = _.merge(allData, pattern.jsonFileData); allData.cacheBuster = patternlab.cacheBuster; //re-rendering the headHTML each time allows pattern-specific data to influence the head of the pattern @@ -452,7 +452,7 @@ const patternlab_engine = function (config) { console.log('There was an error parsing JSON for ' + pattern.relPath); console.log(err); } - allFooterData = plutils.mergeData(allFooterData, pattern.jsonFileData); + allFooterData = _.merge(allFooterData, pattern.jsonFileData); allFooterData.patternLabFoot = footerPartial; const footerHTML = pattern_assembler.renderPattern(patternlab.userFoot, allFooterData); diff --git a/core/lib/pseudopattern_hunter.js b/core/lib/pseudopattern_hunter.js index 3c7c14edc..debae2d8c 100644 --- a/core/lib/pseudopattern_hunter.js +++ b/core/lib/pseudopattern_hunter.js @@ -3,9 +3,9 @@ const ch = require('./changes_hunter'); const glob = require('glob'); const fs = require('fs-extra'); +const _ = require('lodash'); const lh = require('./lineage_hunter'); const Pattern = require('./object_factory').Pattern; -const plutils = require('./utilities'); const path = require('path'); const lineage_hunter = new lh(); const changes_hunter = new ch(); @@ -43,7 +43,7 @@ pseudopattern_hunter.prototype.find_pseudopatterns = function (currentPattern, p } //extend any existing data with variant data - variantFileData = plutils.mergeData(currentPattern.jsonFileData, variantFileData); + variantFileData = _.merge(currentPattern.jsonFileData, variantFileData); let variantName = pseudoPatterns[i].substring(pseudoPatterns[i].indexOf('~') + 1).split('.')[0]; let variantFilePath = path.join(currentPattern.subdir, currentPattern.fileName + '~' + variantName + '.json'); @@ -89,7 +89,7 @@ pseudopattern_hunter.prototype.find_pseudopatterns = function (currentPattern, p } //extend any existing data with variant data - variantFileData = plutils.mergeData(currentPattern.jsonFileData, variantFileData); + variantFileData = _.merge(currentPattern.jsonFileData, variantFileData); variantName = pseudoPatterns[i].substring(pseudoPatterns[i].indexOf('~') + 1).split('.')[0]; variantFilePath = path.join(currentPattern.subdir, currentPattern.fileName + '~' + variantName + '.json'); diff --git a/core/lib/starterkit_manager.js b/core/lib/starterkit_manager.js index f0741ce37..2498580b9 100644 --- a/core/lib/starterkit_manager.js +++ b/core/lib/starterkit_manager.js @@ -31,7 +31,7 @@ const starterkit_manager = function (config) { if (clean) { console.log('Deleting contents of', paths.source.root, 'prior to starterkit load.'); - util.emptyDirectory(paths.source.root); + fs.emptyDirSync(paths.source.root); } else { console.log('Overwriting contents of', paths.source.root, 'during starterkit load.'); } diff --git a/core/lib/utilities.js b/core/lib/utilities.js index e464ec909..b271ac14c 100644 --- a/core/lib/utilities.js +++ b/core/lib/utilities.js @@ -49,97 +49,6 @@ const warning = log.warning.bind(log); */ const error = log.error.bind(log); - /** - * Shuffles an array in place. - * http://stackoverflow.com/questions/6274339/how-can-i-shuffle-an-array-in-javascript - * - * @param {Array} o - * @returns {Array} o - */ -const shuffle = function (o) { - /*eslint-disable curly*/ - for (var j, x, i = o.length; i; j = Math.floor(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x); - return o; -}; - -/** - * Recursively merge properties of two objects. - * - * @param {Object} obj1 If obj1 has properties obj2 doesn't, add to obj2. - * @param {Object} obj2 This object's properties have priority over obj1. - * @returns {Object} obj2 - */ -const mergeData = function (obj1, obj2) { - /*eslint-disable no-param-reassign, guard-for-in*/ - if (typeof obj2 === 'undefined') { - obj2 = {}; - } - for (var p in obj1) { - try { - // Only recurse if obj1[p] is an object. - if (obj1[p].constructor === Object) { - // Requires 2 objects as params; create obj2[p] if undefined. - if (typeof obj2[p] === 'undefined') { - obj2[p] = {}; - } - obj2[p] = mergeData(obj1[p], obj2[p]); - - // Pop when recursion meets a non-object. If obj1[p] is a non-object, - // only copy to undefined obj2[p]. This way, obj2 maintains priority. - } else if (typeof obj2[p] === 'undefined') { - obj2[p] = obj1[p]; - } - } catch (e) { - // Property in destination object not set; create it and set its value. - if (typeof obj2[p] === 'undefined') { - obj2[p] = obj1[p]; - } - } - } - return obj2; -}; - -/** - * Determines whether or not an object is empty. - * - * @param {Object} obj - * @returns {Boolean} - */ -const isObjectEmpty = function (obj) { - for (var prop in obj) { - if (obj.hasOwnProperty(prop)) { return false; } - } - return true; -}; - -/** - * Recursively delete the contents of directory. - * Adapted from https://gist.github.com/tkihira/2367067 - * - * @param {string} dir - directory to empty - * @param {string} cleanDir - already empty directory - * @returns {undefined} - */ -const emptyDirectory = function (dir, cleanDir) { - var list = fs.readdirSync(dir); - for (var i = 0; i < list.length; i++) { - var filename = path.join(dir, list[i]); - var stat = fs.statSync(filename); - - if (filename === "." || filename === "..") { - // pass these files - } else if (stat.isDirectory()) { - this.emptyDirectory(filename); - } else { - // rm fiilename - fs.unlinkSync(filename); - } - } - if (cleanDir) { - fs.rmdirSync(dir); - } -}; - /** * Useful for reporting errors in .catch() on Promises * @param {string} - a message to report @@ -158,9 +67,5 @@ module.exports = { warning, error, log, - shuffle, - mergeData, - isObjectEmpty, - emptyDirectory, reportError };