gimm / gulp-live-server

serve your nodejs/static app live
148 stars 71 forks source link

Cannot restart the server #30

Closed cybertino closed 9 years ago

cybertino commented 9 years ago

I have this piece of code:

gulp.task('server', function () {
    var srv = gls.new('./bin/localhost');

    srv.start();

    gulp.watch('./bin/localhost', srv.start.bind(srv));
});

When I launch it the output is:

[16:42:33] Using gulpfile ~/Projects/myproject/gulpfile.js [16:42:33] Starting 'server'... [16:42:33] Finished 'server' after 8.77 ms livereload[tiny-lr] listening on 35729 ...

But if I change the "bin/localhost" file I get this error:

child_process.js:923
  var r = this._handle.spawn(options);
                       ^
TypeError: Bad argument
    at ChildProcess.spawn (child_process.js:923:24)
    at exports.spawn (child_process.js:723:9)
    at Object.exports.start (/home/softomatix/Projects/myproject/node_modules/gulp-live-server/index.js:135:19)
    at Gaze.<anonymous> (/home/softomatix/Projects/myproject/node_modules/gulp/node_modules/vinyl-fs/node_modules/glob-watcher/index.js:18:14)
    at Gaze.EventEmitter.emit (events.js:98:17)
    at Gaze.emit (/home/softomatix/Projects/myproject/node_modules/gulp/node_modules/vinyl-fs/node_modules/glob-watcher/node_modules/gaze/lib/gaze.js:129:32)
    at /home/softomatix/Projects/myproject/node_modules/gulp/node_modules/vinyl-fs/node_modules/glob-watcher/node_modules/gaze/lib/gaze.js:415:16
    at StatWatcher._pollers.(anonymous function) (/home/softomatix/Projects/myproject/node_modules/gulp/node_modules/vinyl-fs/node_modules/glob-watcher/node_modules/gaze/lib/gaze.js:326:7)
    at StatWatcher.EventEmitter.emit (events.js:98:17)
    at StatWatcher._handle.onchange (fs.js:1109:10)

My small investigation showed that the first parameter of ChildProcess.spawn is an object:

{ type: 'changed',
  path: '/home/softomatix/Projects/foodbarn.js/bin/localhost' }

but according to method's documentation it is supposed to be a string:

https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options

Is that an issue?

cybertino commented 9 years ago

I bypassed it with:

gulp.task('server', function () {
    var srv = gls.new('./bin/localhost');

    srv.start();

    gulp.watch('./bin/localhost', function () {
        srv.start();
    });
});

but not sure if it has to be fixed anyway as srv.start.bind() is in documentation's example.

den-mentiei commented 9 years ago

It's still an issue.

Also, can be fixed with:

gulp.watch('server.js', function() {
    server.start.apply(server);
});
dciccale commented 9 years ago

If I change this line https://github.com/gimm/gulp-live-server/blob/master/index.js#L135

execPath -> 'node'

this.server = spawn('node', this.config.args, this.config.options);

it works. but apparently would break something with coffeescript. what should be the correct way of restarting the server?

I have this line but wouldn't work.

gulp.watch(['server/**/*.js'], server.start.bind(server));

@den-mentiei solution works, I will go with that for now.

den-mentiei commented 9 years ago

@douglas-vaz solution works and is better :) Can we have it merged in? (#33)

douglas-vaz commented 9 years ago

Thanks for the merge :)

gimm commented 9 years ago

@douglas-vaz more than welcome