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

read main from alternatives' main as well #269

Open
wants to merge 2 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
42 changes: 34 additions & 8 deletions lib/detect-dependencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ function detectDependencies(config) {
return config;
}

/**
* does the config object have the proper main we need
*
* @param config
* @returns {Boolean}
*/
function isValidConfig(config) {
return $._.isObject(config) && config.main && config.main.length > 0;
}

/**
* Find the component's JSON configuration file.
Expand All @@ -62,15 +71,32 @@ function findComponentConfigFile(config, component) {
return config.get('bower.json');
}

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

if (!$._.isObject(componentConfigFile) && $.fs.existsSync(configFile)) {
componentConfigFile = JSON.parse($.fs.readFileSync(configFile));
var candidates = ['bower.json', '.bower.json', 'component.json', 'package.json'];
for (var i = 0, len = candidates.length; i < len; i++) {
if (isValidConfig(componentConfigFile)) {
break;
}
var configFile = $.path.join(config.get('bower-directory'), component, candidates[i]);
if ($.fs.existsSync(configFile) === false) {
continue;
} else {
var curConfigFile = JSON.parse($.fs.readFileSync(configFile));
if ($._.isObject(componentConfigFile) === false) {
componentConfigFile = curConfigFile;
//current configFile match what we need
if (isValidConfig(curConfigFile)) {
break;
}
} else {
if (!componentConfigFile.main || componentConfigFile.main.length === 0) {
if (curConfigFile.main && curConfigFile.main.length > 0) {
componentConfigFile.main = curConfigFile.main;
break;
}
}
}
});

}
}
return componentConfigFile;
}

Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "fake-package-only-has-package-json",
"version": "1.0.0",
"main": "fake.js"
}
20 changes: 17 additions & 3 deletions test/wiredep_test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*jshint latedef:false */
/*global after, describe, it, before, beforeEach */
/*global after, describe, it, before, beforeEach, afterEach*/

'use strict';

Expand Down Expand Up @@ -29,10 +29,10 @@ require.uncache = function (moduleName) {
describe('wiredep', function () {
beforeEach(function () {
wiredep = require('../wiredep');
})
});
afterEach(function () {
require.uncache('../wiredep');
})
});
before(function() {
fs.copySync('test/fixture', '.tmp');
process.chdir('.tmp');
Expand Down Expand Up @@ -470,6 +470,20 @@ describe('wiredep', function () {
assert.equal(filePaths.read('actual'), filePaths.read('expected'));
});

it('should read main field from package.json when main is missing from bower.json .bower.json',function(){
var bowerObj = {
name: 'test',
dependencies: {
'fake-package-only-has-package-json': '*'
}
};
var obj = wiredep({
bowerJson: bowerObj
});
assert.isTrue(obj.packages['fake-package-only-has-package-json'].main.length > 0);
assert.isTrue(obj.packages['fake-package-only-has-package-json'].main[0].indexOf('fake.js') > -1);
});

it('should support inclusion of main files from some other dir with manually loaded bower.json', function () {
var filePaths = getFilePaths('index-cwd-include-self', 'html');

Expand Down