therealklanni / git-guppy

Simple git-hook integration for your gulp workflows.
http://therealklanni.github.io/git-guppy
MIT License
104 stars 14 forks source link

Add configuration for gulp/gulpfile location #84

Open therealklanni opened 8 years ago

therealklanni commented 8 years ago

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.

robfentress commented 8 years ago

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 commented 8 years ago

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 commented 8 years ago

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 commented 8 years ago

Okay, maybe now?

robfentress commented 8 years ago

Grr. . . nevermind.

therealklanni commented 8 years ago

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

ChrisMBarr commented 8 years ago

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.

therealklanni commented 8 years ago

@robfentress any luck finding a solution? :)

robfentress commented 8 years ago

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.

wheller commented 7 years ago

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

arminrosu commented 7 years ago

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.:

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.