Skip to content

Commit

Permalink
Close #44 PR: Add option for Node.js require resolution algorithm. Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
kirbysayshi authored and sindresorhus committed Dec 19, 2015
1 parent d8decab commit 0c4c0e7
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 6 deletions.
5 changes: 3 additions & 2 deletions Gruntfile.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
'use strict';
module.exports = function (grunt) {
require('./')(grunt, {
pattern: ['*'],
pattern: ['grunt*'],
config: require('./package'),
scope: 'devDependencies'
scope: 'devDependencies',
requireResolution: true
});

grunt.initConfig({
Expand Down
15 changes: 14 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var path = require('path');
var pkgUp = require('pkg-up');
var multimatch = require('multimatch');
var arrify = require('arrify');
var resolveCwd = require('resolve-cwd');

module.exports = function (grunt, opts) {
opts = opts || {};
Expand All @@ -22,5 +23,17 @@ module.exports = function (grunt, opts) {
return result.concat(Array.isArray(deps) ? deps : Object.keys(deps));
}, []);

multimatch(names, pattern).forEach(grunt.loadNpmTasks);
multimatch(names, pattern).forEach(function (pkgName) {
if (opts.requireResolution === true) {
// This resolution is complicated because most grunt plugins are written
// in violation of package.json conventions. And example is not having a
// `main` field defined, which will cause `require` or `resolve`
// to fail. So better to lookup a guaranteed file, such as package.json.
var pkg = resolveCwd(path.join(pkgName, 'package.json'));
var root = path.dirname(pkg);
pkgName = path.join(root, 'tasks');
}

grunt.loadTasks(pkgName);
});
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
"dependencies": {
"arrify": "^1.0.0",
"multimatch": "^2.0.0",
"pkg-up": "^1.0.0"
"pkg-up": "^1.0.0",
"resolve-cwd": "^1.0.0"
},
"devDependencies": {
"grunt": "^0.4.2",
Expand Down
16 changes: 14 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ Usually you would have to load each task one by one, which is unnecessarily cumb

This module will read the `dependencies`/`devDependencies`/`peerDependencies`/`optionalDependencies` in your package.json and load grunt tasks that match the provided patterns.


#### Before

```js
Expand Down Expand Up @@ -102,13 +101,20 @@ require('load-grunt-tasks')(grunt, {scope: 'devDependencies'});
require('load-grunt-tasks')(grunt, {scope: ['devDependencies', 'dependencies']});
```

### Load from all `node_modules` in the current hierarchy

```js
require('load-grunt-tasks')(grunt, {requireResolution: true});
```

### All options in use

```js
require('load-grunt-tasks')(grunt, {
pattern: 'grunt-contrib-*',
config: '../package.json',
scope: 'devDependencies'
scope: 'devDependencies',
requireResolution: false
});
```

Expand All @@ -131,6 +137,12 @@ Type: `string`, `array`
Default: `['dependencies', 'devDependencies', 'peerDependencies', 'optionalDependencies']`
Values: `'dependencies'`, `'devDependencies'`, `'peerDependencies'`, `'optionalDependencies'`, `'bundledDependencies'`

### requireResolution

Traverse the file hierarchy, just like node does when requiring modules.

Type: `boolean`
Default: `false`

## License

Expand Down

0 comments on commit 0c4c0e7

Please sign in to comment.