Skip to content

Commit 4bbe7ad

Browse files
authored
Merge pull request #6 from chipbell4/bugfix/npm-3
Loading grunt tasks manually to work around flattening in npm 3
2 parents bd3e082 + 0423d49 commit 4bbe7ad

File tree

2 files changed

+44
-12
lines changed

2 files changed

+44
-12
lines changed

libs/grunt-loader.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
var fs = require('fs');
2+
var path = require('path');
3+
4+
module.exports = {
5+
/**
6+
* Given a folder, attempts to load any grunt modules included in the child directory node_modules. Due to flattening
7+
* in NPM 3+, grunt-springroll's new modules could actually be installed in the game's node_modules folder rather than
8+
* the node_modules folder underneath grunt-springroll. load-grunt-tasks attempts to connect the package.json with the
9+
* sibling folder node_modules, which is why tasks have failed to load NPM 3 and beyond. This however avoids that and
10+
* simply matches filenames based on convention
11+
*
12+
* @param {grunt} grunt The grunt instance with which to load tasks
13+
* @param {string} folder The folder to load tasks from (will look for a node_modules as a child directory)
14+
* @return void
15+
*/
16+
load: function(grunt, folder) {
17+
var lastDirectory = process.cwd();
18+
19+
process.chdir(folder);
20+
21+
fs.readdirSync(path.join(folder, 'node_modules'))
22+
.filter(function(name) {
23+
return name.indexOf('grunt-') === 0 && name !== 'grunt-springroll';
24+
})
25+
.forEach(function(folder) {
26+
grunt.loadNpmTasks(folder);
27+
});
28+
29+
process.chdir(lastDirectory);
30+
},
31+
};

libs/index.js

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ module.exports = function(grunt, settings, undefined)
44
var _ = require('lodash'),
55
path = require('path'),
66
loader = require('load-grunt-config'),
7+
gruntLoader = require('./grunt-loader'),
78
base = path.dirname(__dirname);
89

910
// Get the settings and options
@@ -26,6 +27,11 @@ module.exports = function(grunt, settings, undefined)
2627

2728
// We need to load the local grunt plugins
2829
var projectDir = process.cwd();
30+
31+
// load both project and this plugin's grunt modules manually to account for NPM 3+ flattening
32+
gruntLoader.load(grunt, projectDir);
33+
gruntLoader.load(grunt, pluginFolder);
34+
2935
process.chdir(pluginFolder);
3036

3137
// The build file which contains all the list of files to build
@@ -70,16 +76,14 @@ module.exports = function(grunt, settings, undefined)
7076

7177
// auto grunt.initConfig()
7278
init: false,
73-
74-
// Load the grunt tasks
75-
loadGruntTasks : { pattern: [
76-
'grunt-*',
77-
'!grunt-springroll'
78-
]},
79+
80+
// don't load grunt tasks, since we'll load them manually (due to NPM 2 vs. NPM 3+ behavioral differences)
81+
loadGruntTasks: false,
7982

8083
// Data based into config
8184
data: data
8285
});
86+
8387
process.chdir(projectDir);
8488

8589
// Project-specific config
@@ -91,11 +95,8 @@ module.exports = function(grunt, settings, undefined)
9195
// Get the config, don't run
9296
init: false,
9397

94-
// We don't want to reload builder
95-
loadGruntTasks: { pattern: [
96-
'grunt-*',
97-
'!grunt-springroll'
98-
]}
98+
// don't load grunt tasks, since we'll load them manually (due to NPM 2 vs. NPM 3+ behavioral differences)
99+
loadGruntTasks: false
99100
});
100101

101102
// Merge the configs
@@ -119,4 +120,4 @@ module.exports = function(grunt, settings, undefined)
119120
}
120121

121122
return config;
122-
};
123+
};

0 commit comments

Comments
 (0)