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

fix(loadNpmTasks): load tasks relative to package location #1762

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 8 additions & 2 deletions lib/grunt/task.js
Original file line number Diff line number Diff line change
Expand Up @@ -372,16 +372,22 @@ task.loadNpmTasks = function(name) {
var root = path.resolve('node_modules');
var pkgpath = path.join(root, name);
var pkgfile = path.join(pkgpath, 'package.json');
var pkgDistDir = '';
// If package does not exist where grunt expects it to be,
// try to find it using Node's package path resolution mechanism
if (!grunt.file.exists(pkgpath)) {
var nameParts = name.split('/');
// In case name points to directory inside module,
// get real name of the module with respect to scope (if any)
var normailzedName = (name[0] === '@' ? nameParts.slice(0,2).join('/') : nameParts[0]);
// Sometimes a module's task are not at the root of the package, the "name" could contain
// the directory to find the tasks (e.g. grunt-foo/dist/tasks). Therefore, if the name
// contains more than two part, then treat the last part as the directory to find the tasks.
if (nameParts.length > 2) {
pkgDistDir = nameParts.slice(2).join('/');
}
try {
pkgfile = require.resolve(normailzedName + '/package.json');
root = pkgfile.substr(0, pkgfile.length - normailzedName.length - '/package.json'.length);
} catch (err) {
grunt.log.error('Local Npm module "' + normailzedName + '" not found. Is it installed?');
return;
Expand Down Expand Up @@ -409,7 +415,7 @@ task.loadNpmTasks = function(name) {
}

// Process task plugins.
var tasksdir = path.join(root, name, 'tasks');
var tasksdir = path.join(path.dirname(pkgfile), pkgDistDir, 'tasks');
if (grunt.file.exists(tasksdir)) {
loadTasks(tasksdir);
} else {
Expand Down