git-guppy icon indicating copy to clipboard operation
git-guppy copied to clipboard

Add configuration for gulp/gulpfile location

Open therealklanni opened this issue 8 years ago • 11 comments

There seems to be an increasing need for complex project structures where gulp and/or gulpfile.js are not in the project root.

Example use case:

.
├── .git
├── foo
│   ├── gulpfile.js
│   └── node_modules
└── bar

The goal would be to add a guppy entry in package.json with a property that sets an override for the gulpfile.js location or override the project path in such a way that guppy executes in the correct directory for gulp.

Suggestions for better/best approaches for this are welcomed.

therealklanni avatar Jul 22 '16 17:07 therealklanni

I think I have this issue. I am executing pre-commit from foo essentially and am getting an empty array for the changed files when I know they are there.

gulp.task('pre-commit', guppy.src('pre-commit', function (files) {
  console.log(files);
 }));

This results in [] being printed.

Executing the pre-commit task directly does return the changed files though. It also works if I move the gulpfile to the root of the project, but I don't want to do that.

Do you have a workaround?

robfentress avatar Aug 10 '16 19:08 robfentress

I submitted a pull request/s. Sorry for the multiple commits. I'm still trying to get the hang of this and realized some issues I had to fix. I tried to follow the guidelines in CONTRIBUTING.md--hope I did that right.

robfentress avatar Aug 11 '16 15:08 robfentress

Actually, looking at it now, I see there are significant problems with what I'm doing. I just quickly hacked something and it seemed to work in one limited case, but I didn't really test it out enough. If you've got any ideas, let me know.

robfentress avatar Aug 11 '16 17:08 robfentress

Okay, maybe now?

robfentress avatar Aug 12 '16 13:08 robfentress

Grr. . . nevermind.

robfentress avatar Aug 12 '16 13:08 robfentress

Thanks for your efforts. Keep at it, I'm sure you'll get it! 👍

therealklanni avatar Aug 15 '16 21:08 therealklanni

After struggling with git-guppy for a bit I realized that this issue is why I can't get it working. We have a large & complex codebase with multiple projects, each one has its own gulpfile that does linting and runs unit tests

.
├── .git
├── source
│   ├── project-1
│   │   ├── gulpfile.js
│   │   └── node_modules
│   ├── project-2
│   │   ├── gulpfile.js
│   │   └── node_modules
│   ├── project-3
│   └── project-4
├── tools
└── docs

I'd love to use this project, but until I have the ability to define the location of the gulpfile(s) I can't.

ChrisMBarr avatar Aug 30 '16 18:08 ChrisMBarr

@robfentress any luck finding a solution? :)

therealklanni avatar Aug 31 '16 22:08 therealklanni

Sorry. Been on vacation. For the moment, I've decided to just work around it in the gulpfiles themselves using the lodash, find-parent-dir, and path modules.

That looks something like this:

var _ = require('lodash');
var findParentDir = require('find-parent-dir');
var path = require('path');

var gitParent;

try {
  gitParent = findParentDir.sync(__dirname, '.git');
} catch(err) {
  console.error('error', err);
}

var gulpParent = path.resolve();
var gulpRelToGit = path.relative(gitParent, gulpParent);

gulp.task('pre-commit', guppy.src('pre-commit', function (files) {
      files = _.map(files, function(file) {
        var fileDir = path.resolve(path.dirname(file));
        var relFileDir = path.relative(gulpRelToGit, fileDir);

        return path.format({
          dir: relFileDir,
          base: path.basename(file)
        });
      });
      return gulp.src(files).pipe(doWhatever());
 }));

I had to edit what I actually had in my code a bit to make it clearer for this example. Hopefully, I didn't introduce any typos. Either way, this is the principle I used. Please let me know if I've overlooked anything.

robfentress avatar Sep 06 '16 17:09 robfentress

It seems to me since git will tell you where it's operating from, as well as where you need to "cd" to, to find the root... it could be as simple as this... #114

wheller avatar Dec 09 '16 07:12 wheller

I don't know if it's the case for you as well, but I am running gulp (ie. gulp build) in /foo . In this case, you can edit the hookfile to cd into that directory first, since cwd will be /foo

E.g.:

  • Open .git/hooks/pre-commit
  • Change
var dir = execSync('npm bin gulp').toString().trim();

to

var dir = execSync(`cd ${cwd}; npm bin gulp`).toString().trim();

This should not affect projects which run gulp from the root, because cwd will be root.

arminrosu avatar May 16 '17 11:05 arminrosu