Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swapping code with external library #81

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
73 changes: 35 additions & 38 deletions lib/readconfigdir.js
Original file line number Diff line number Diff line change
@@ -1,54 +1,51 @@
var glob = require('glob');
var async = require('async');
var readfile = require('./readfile');
var path = require('path');
var parseDir = require('parse-dir');
var _ = require('lodash-node');

module.exports = function(dir, grunt, options) {
var files = parseDir(dir + '/' + '*.{js,json,yml,yaml,coffee,ls}');
var result = {};

var getKey = function(file) {
var ext = path.extname(file);
var base = path.basename(file, ext);
return base;
};
files.forEach(function(file) {
var content = file.contents;
var fileName = file.basename;

var files = glob.sync('*.{js,json,yml,yaml,coffee,ls}', { cwd: dir });
if (_.isFunction(content)) {
content = content(grunt, options);
}

var fullPaths = files.map(function(file) {
return path.join(dir, file);
});
// Check if multi config
if (fileName.match(/-tasks$/)) {
var target = fileName.replace(/-tasks$/, '');
var newTarget;
var spl;
var originalKey;

var obj = {};
fullPaths.forEach(function(path) {
var result = readfile(path);
var key = getKey(path);
if (_.isFunction(result)) {
result = result(grunt, options);
}
for (var newKey in content) {
if (content.hasOwnProperty(newKey)) {
newTarget = target;
originalKey = newKey;

//check if multi config
if (key.match(/-tasks$/)) {
var target = key.replace(/-tasks$/, '');
for (var newKey in result) {
var newTarget = target;
var originalKey = newKey;
if (newKey.indexOf('__') != -1) {
var spl = newKey.split('__');
newKey = spl[0];
newTarget = target + '_' + spl[1];
}
if (!obj[newKey]) {
obj[newKey] = {};
if (newKey.indexOf('__') != -1) {
spl = newKey.split('__');
newKey = spl.shift();
newTarget = target + '_' + spl.shift();
}

if (!result[newKey]) {
result[newKey] = {};
}

result[newKey][newTarget] = content[originalKey];
}
obj[newKey][newTarget] = result[originalKey];
}
} else {
if (!obj[key]) {
obj[key] = {};
if (!result[fileName]) {
result[fileName] = {};
}
obj[key] = _.merge(obj[key], result);

result[fileName] = _.merge(result[fileName], content);
}
});
return obj;

return result;
};
28 changes: 0 additions & 28 deletions lib/readfile.js

This file was deleted.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@
"proxyquire": "^0.5.3"
},
"dependencies": {
"async": "~0.2.10",
"glob": "~3.2.6",
"jit-grunt": "~0.7.0",
"js-yaml": "~3.0.1",
"load-grunt-tasks": "~0.3.0",
"jit-grunt": "~0.7.0",
"lodash-node": "~2.4.1",
"async": "~0.2.10"
"parse-dir": "0.0.3"
}
}
60 changes: 0 additions & 60 deletions test/readfile.test.js

This file was deleted.