Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions core/lib/list_item_hunter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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]
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions core/lib/parameter_hunter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions core/lib/pattern_assembler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions core/lib/patternlab.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions core/lib/pseudopattern_hunter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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');
Expand Down
2 changes: 1 addition & 1 deletion core/lib/starterkit_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
}
Expand Down
95 changes: 0 additions & 95 deletions core/lib/utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -158,9 +67,5 @@ module.exports = {
warning,
error,
log,
shuffle,
mergeData,
isObjectEmpty,
emptyDirectory,
reportError
};