Skip to content

Commit

Permalink
code style cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephen Sawchuk committed Jun 7, 2014
1 parent 83dc284 commit 6ab3ad3
Show file tree
Hide file tree
Showing 7 changed files with 228 additions and 278 deletions.
1 change: 0 additions & 1 deletion .jshintrc
Expand Up @@ -2,7 +2,6 @@
"curly": true,
"eqeqeq": true,
"immed": true,
"latedef": true,
"newcap": true,
"noarg": true,
"sub": true,
Expand Down
82 changes: 82 additions & 0 deletions lib/default-file-types.js
@@ -0,0 +1,82 @@
module.exports = {
html: {
block: /(([ \t]*)<!--\s*bower:*(\S*)\s*-->)(\n|\r|.)*?(<!--\s*endbower\s*-->)/gi,
detect: {
js: /<script.*src=['"]([^'"]+)/gi,
css: /<link.*href=['"]([^'"]+)/gi
},
replace: {
js: '<script src="{{filePath}}"></script>',
css: '<link rel="stylesheet" href="{{filePath}}" />'
}
},

jade: {
block: /(([ \t]*)\/\/\s*bower:*(\S*))(\n|\r|.)*?(\/\/\s*endbower)/gi,
detect: {
js: /script\(.*src=['"]([^'"]+)/gi,
css: /link\(.*href=['"]([^'"]+)/gi
},
replace: {
js: 'script(src=\'{{filePath}}\')',
css: 'link(rel=\'stylesheet\', href=\'{{filePath}}\')'
}
},

less: {
block: /(([ \t]*)\/\/\s*bower:*(\S*))(\n|\r|.)*?(\/\/\s*endbower)/gi,
detect: {
css: /@import\s['"](.+css)['"]/gi,
less: /@import\s['"](.+less)['"]/gi
},
replace: {
css: '@import "{{filePath}}";',
less: '@import "{{filePath}}";'
}
},

sass: {
block: /(([ \t]*)\/\/\s*bower:*(\S*))(\n|\r|.)*?(\/\/\s*endbower)/gi,
detect: {
css: /@import\s(.+css)/gi,
sass: /@import\s(.+sass)/gi,
scss: /@import\s(.+scss)/gi
},
replace: {
css: '@import {{filePath}}',
sass: '@import {{filePath}}',
scss: '@import {{filePath}}'
}
},

scss: {
block: /(([ \t]*)\/\/\s*bower:*(\S*))(\n|\r|.)*?(\/\/\s*endbower)/gi,
detect: {
css: /@import\s['"](.+css)['"]/gi,
sass: /@import\s['"](.+sass)['"]/gi,
scss: /@import\s['"](.+scss)['"]/gi
},
replace: {
css: '@import "{{filePath}}";',
sass: '@import "{{filePath}}";',
scss: '@import "{{filePath}}";'
}
},

yaml: {
block: /(([ \t]*)#\s*bower:*(\S*))(\n|\r|.)*?(#\s*endbower)/gi,
detect: {
js: /-\s(.+js)/gi,
css: /-\s(.+css)/gi
},
replace: {
js: '- {{filePath}}',
css: '- {{filePath}}'
}
}
};


module.exports['default'] = module.exports.html;
module.exports.htm = module.exports.html;
module.exports.yml = module.exports.yaml;
134 changes: 63 additions & 71 deletions lib/detect-dependencies.js
@@ -1,18 +1,41 @@
/*
* detect-dependencies.js
* https://github.com/stephenplusplus/wiredep
'use strict';

var $ = require('modmod')('fs', 'lodash', 'path', 'propprop');
var _ = $.lodash;


/**
* Detect dependencies of the components from `bower.json`.
*
* Copyright (c) 2013 Stephen Sawchuk
* Licensed under the MIT license.
* @param {object} config the global configuration object.
* @return {object} config
*/
function detectDependencies(config) {
var allDependencies = {};

'use strict';
if (config.get('dependencies')) {
_.assign(allDependencies, config.get('bower.json').dependencies);
}

var fs = require('fs');
var path = require('path');
var _ = require('lodash');
var helpers = require('./helpers');
var prop = helpers.prop;
if (config.get('dev-dependencies')) {
_.assign(allDependencies, config.get('bower.json').devDependencies);
}

_.each(allDependencies, gatherInfo(config));

config.set('global-dependencies-sorted', filterExcludedDependencies(
config.get('detectable-file-types').
reduce(function (acc, fileType) {
if (!acc[fileType]) {
acc[fileType] = prioritizeDependencies(config, '.' + fileType);
}
return acc;
}, {}),
config.get('exclude')
));

return config;
}


/**
Expand All @@ -22,31 +45,31 @@ var prop = helpers.prop;
* @param {string} component the name of the component to dig for
* @return {object} the component's config file
*/
var findComponentConfigFile = function (config, component) {
function findComponentConfigFile(config, component) {
var componentConfigFile;

['bower.json', '.bower.json', 'component.json', 'package.json'].
forEach(function (configFile) {
configFile = path.join(config.get('bower-directory'), component, configFile);
configFile = $.path.join(config.get('bower-directory'), component, configFile);

if (!_.isObject(componentConfigFile) && fs.existsSync(configFile)) {
componentConfigFile = JSON.parse(fs.readFileSync(configFile));
if (!_.isObject(componentConfigFile) && $.fs.existsSync(configFile)) {
componentConfigFile = JSON.parse($.fs.readFileSync(configFile));
}
});

return componentConfigFile;
};
}


/**
* Find the main file the component refers to. It's not always main :(
* Find the main file the component refers to. It's not always `main` :(
*
* @param {object} config the global configuration object
* @param {string} component the name of the component to dig for
* @param {componentConfigFile} the component's config file
* @return {array} the array of paths to the component's primary file(s)
*/
var findMainFiles = function (config, component, componentConfigFile) {
function findMainFiles(config, component, componentConfigFile) {
var filePaths = [];
var file;

Expand All @@ -59,16 +82,16 @@ var findMainFiles = function (config, component, componentConfigFile) {
// still haven't found it. is it stored in config.scripts, then?
filePaths = componentConfigFile.scripts;
} else {
file = path.join(config.get('bower-directory'), component, componentConfigFile.name + '.js');
if (fs.existsSync(file)) {
file = $.path.join(config.get('bower-directory'), component, componentConfigFile.name + '.js');
if ($.fs.existsSync(file)) {
filePaths = [componentConfigFile.name + '.js'];
}
}

return filePaths.map(function (file) {
return path.join(config.get('bower-directory'), component, file);
return $.path.join(config.get('bower-directory'), component, file);
});
};
}


/**
Expand All @@ -77,7 +100,7 @@ var findMainFiles = function (config, component, componentConfigFile) {
* @param {object} config the global configuration object
* @return {function} the iterator function, called on every component
*/
var gatherInfo = function (config) {
function gatherInfo(config) {
/**
* The iterator function, which is called on each component.
*
Expand Down Expand Up @@ -109,22 +132,22 @@ var gatherInfo = function (config) {
}

var mains = findMainFiles(config, component, componentConfigFile);
var fileTypes = _.chain(mains).map(path.extname).unique().value();
var fileTypes = _.chain(mains).map($.path.extname).unique().value();

dep.main = mains;
dep.type = fileTypes;
dep.name = componentConfigFile.name;

var depIsExcluded = _.find(config.get('exclude'), function (pattern) {
return path.join(config.get('bower-directory'), component).match(pattern);
return $.path.join(config.get('bower-directory'), component).match(pattern);
});

if (dep.main.length === 0 && !depIsExcluded) {
// can't find the main file. this config file is useless!
warnings.push(component + ' was not injected in your file.');
warnings.push(
'Please go take a look in "'
+ path.join(config.get('bower-directory'), component)
+ $.path.join(config.get('bower-directory'), component)
+ '" for the file you need, then manually include it in your file.');

config.set('warnings', warnings);
Expand All @@ -139,7 +162,7 @@ var gatherInfo = function (config) {

config.get('global-dependencies').set(component, dep);
};
};
}


/**
Expand All @@ -149,7 +172,7 @@ var gatherInfo = function (config) {
* @param {object} b dependency b
* @return {number} the priority of dependency a in comparison to dependency b
*/
var dependencyComparator = function (a, b) {
function dependencyComparator(a, b) {
var aNeedsB = false;
var bNeedsA = false;

Expand All @@ -174,7 +197,7 @@ var dependencyComparator = function (a, b) {
}

return 0;
};
}


/**
Expand All @@ -185,7 +208,7 @@ var dependencyComparator = function (a, b) {
* @param {array} right
* @return {array} the sorted, merged array
*/
var merge = function (left, right) {
function merge(left, right) {
var result = [];
var leftIndex = 0;
var rightIndex = 0;
Expand All @@ -201,7 +224,7 @@ var merge = function (left, right) {
return result.
concat(left.slice(leftIndex)).
concat(right.slice(rightIndex));
};
}


/**
Expand All @@ -210,7 +233,7 @@ var merge = function (left, right) {
* @param {array} items
* @return {array} the sorted array
*/
var mergeSort = function (items) {
function mergeSort(items) {
if (items.length < 2) {
return items;
}
Expand All @@ -221,7 +244,7 @@ var mergeSort = function (items) {
mergeSort(items.slice(0, middle)),
mergeSort(items.slice(middle))
);
};
}


/**
Expand All @@ -242,7 +265,7 @@ var eliteDependencies = [
* @param {string} fileType the type of file to prioritize
* @return {array} the sorted items of 'path/to/main/files.ext' sorted by type
*/
var prioritizeDependencies = function (config, fileType) {
function prioritizeDependencies(config, fileType) {
var eliteDependenciesCaught = [];

var dependencies = mergeSort(
Expand All @@ -257,7 +280,7 @@ var prioritizeDependencies = function (config, fileType) {
return true;
}
})
).map(prop('main'));
).map($.propprop('main'));

eliteDependenciesCaught.
forEach(function (dependency) {
Expand All @@ -269,9 +292,9 @@ var prioritizeDependencies = function (config, fileType) {
flatten().
value().
filter(function (main) {
return path.extname(main) === fileType;
return $.path.extname(main) === fileType;
});
};
}


/**
Expand All @@ -281,46 +304,15 @@ var prioritizeDependencies = function (config, fileType) {
* @param {array} patterns array of patterns to match against
* @return {array} items that don't match any of the patterns
*/
var filterExcludedDependencies = function (allDependencies, patterns) {
function filterExcludedDependencies(allDependencies, patterns) {
return _.transform(allDependencies, function (result, dependencies, fileType) {
result[fileType] = _.reject(dependencies, function (dependency) {
return _.find(patterns, function (pattern) {
return dependency.match(pattern);
});
});
});
};
}


/**
* Detect dependencies of the components from `bower.json`.
*
* @param {object} config the global configuration object.
* @return {object} config
*/
module.exports = function detect(config) {
var allDependencies = {};

if (config.get('dependencies')) {
_.assign(allDependencies, config.get('bower.json').dependencies);
}

if (config.get('dev-dependencies')) {
_.assign(allDependencies, config.get('bower.json').devDependencies);
}

_.each(allDependencies, gatherInfo(config));

config.set('global-dependencies-sorted', filterExcludedDependencies(
config.get('detectable-file-types').
reduce(function (acc, fileType) {
if (!acc[fileType]) {
acc[fileType] = prioritizeDependencies(config, '.' + fileType);
}
return acc;
}, {}),
config.get('exclude')
));

return config;
};
module.exports = detectDependencies;

0 comments on commit 6ab3ad3

Please sign in to comment.