I'm trying to use Grunt to clean up a large project. For this specific example, I am trying to run unit tests and want to do so only for paths under the current grunt
execution directory (i.e., the result of pwd
).
I want one Gruntfile at the project root. I know grunt
will find and execute this with no problem from any subdirectory. If I define my test runner options to look in "test/"
, it only runs tests under {project root/}test/
. Is there a way to tell a project-level Gruntfile to make its paths (in all or in part) relative to the executing location?
Notes:
- I don't need to be told "Why would you do this? Grunt should manage your whole project!" This is a retrofit, and until that halcyon day when it all works, I want/need it piecemeal.
- To reiterate,
"**/test/"
isn't the answer, because I want only the tests under the currentgrunt
execution directory. --base
also won't work, because Grunt will look for the Node packages at the base location.- I have, for similar situations, used a shared configuration JSON file that I've imported with
grunt.config.merge(grunt.file.readJSON("../grunt-shared.json"));
. However, that requires Gruntfiles in subfolders, as well as a hard-coded path to the shared file (e.g.,../
), which seems tenuous. - I could write code to do some directory climbing and path building, but I'd like to make that a last resort.
Here's the solution I came up with (H/T to @firstdoit, https://stackoverflow.com/a/28763634/356016):
Gruntfile.js
.node_modules
source.Gruntfile.js
Using a
var
forPATH_TO_ROOT
is largely unnecessary, but it provides a single focus point for using this boilerplate file across sub-projects.{ROOT}/grunt-shared.js