slushjs / gulp-install

Automatically install npm and bower packages if package.json or bower.json is found in the gulp file stream respectively
MIT License
106 stars 46 forks source link

Callback once modules installed #17

Closed pavlosgi closed 7 years ago

pavlosgi commented 9 years ago

Would be nice if we could provide a callback to run once modules have been installed. I understand it might be difficult because you are spawning a new process for this. But it would be a great addition if possible

dodtsair commented 9 years ago

I ran into this as well.

Currently I have one project which is used to run some sub projects. Like the follow

  1. rootProject/
  2. rootProject/gulpfile.js
  3. rootProject/subProject1/
  4. rootProject/subProject1/gulfile.js
  5. rootProject/subProject2/
  6. rootProject/subProject2/gulfile.js

Running gulp on rootProject runs it on each subproject. I have about three right now, but plan to add more. Before I can run gulp I need to install the npm modules

cd rootProject/
npm install
pushd subProject1
npm install
popd
pushd subProject2
npm install
popd
gulp

I was hoping I could change this to

cd rootProject/
npm install
gulp

This is what most people expect with a gulp project. However when I code gulp-install to call install on all the subprojects I don't see a way to run that serially with the actual gulp of that project.

In the end npm install is running on subProject1 while gulp is called on it. This obviously blows up. Modules are still missing.

dodtsair commented 9 years ago

Looking at the code I see the use of through2. That object is returned from the call to install(). It looks like the code calls callbacks whenever npm install is done executing. So it looks like I am putting my callback in the wrong place.

Here is what my code looks like:

modules.forEach(function(module) {
  gulp.task(module + "-npm-install", function(cb) {
    var installPipe = install()
    gulp.src(['./' + module + '/package.json']).pipe(installPipe)
    installPipe.on("finish", cb);
  });
});

I create a task for each module. I didn't include the code but the gulp call for each module depends on the module-npm-install task. So if the cb gets called at the right time, then the gulp call should not happen until after npm install finished. I put my callback on the "finish" event for the installPipe. It seems to get called because nothing hangs, but it gets called in milliseconds

[12:20:24] Starting 'subProject1-npm-install'...
....
[12:20:24] Finished 'subProject1-npm-install' after 19 ms

Gulp has obviously decided that gulp-install finished well before its calls to npm install finished.

roh85 commented 9 years ago

I have the same issue. Used a timeout as a workaround to bypass this behaviour. A proper fix would be appreciated.

gulp.task('install', function(cb){
    var installPipe = install();
    gulp.src(['./node_modules/subprojects/package.json'])
    .pipe(installPipe);
    installPipe.on("finish", function(){
        if(dirs_exists(['./node_modules/subproject/node_modules'])){
            CONST_TIMEOUT = 1000;
        }
        setTimeout(function() {
            /*
            Put tasks to execute here
             */
            gulp.start('build')
            gulp.start('images')
            gulp.start('scripts')
            gulp.start('styles')
        },CONST_TIMEOUT); //use timeout to give npm install the chance to finish properly
    });
});
maxfrigge commented 9 years ago

Another major use case for this is to restart your application after the modules have been installed.

vtange commented 8 years ago

Would really like this to be possible. Wanted to make a slush generator that does some extra steps after the .pipe(install()) part, but pretty much can't. Tried linking Gulp tasks in serial and using plugins like run-sequence. .pipe(install()) seems to force a hard stop on the process

tliebscher commented 8 years ago

Creating task dependencies in gulp with the intention of blocking the following tasks until gulp-install has finished does not work, as gulp-install returns before e.g. bower even has started. The following tasks then just don't find the directories and files.

Is there a way to make the execution of gulp-install blocking?

swinston1000 commented 7 years ago

Callback support would be very useful - is there any reason that this has not been merged - https://github.com/slushjs/gulp-install/pull/31 ??? Thanks!