Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

option : Live reload triggering ? #3

Closed
0gust1 opened this issue Apr 15, 2014 · 5 comments
Closed

option : Live reload triggering ? #3

0gust1 opened this issue Apr 15, 2014 · 5 comments

Comments

@0gust1
Copy link

0gust1 commented Apr 15, 2014

Hello,
Thanks a lot for this metalsmith plugin.

I was looking for a livereload functionnality (because it's so useful when writing front-end code), and I have something working now.

I had to tell the livereload server which file have changed, so I added 1line (and one require call for the request lib) in the rebuilder function of the metalsmith-watch plugin (after the rebuilt file has been written on disk).

How do you think this should be implemented ?

  • a callback which can be called after each file rebuild ?
  • an option flag passed to the watch plugin ?
  • ...
var rebuilder = function ( metalsmith ) {
  return function(filepath, name){
    // metalsmith isn't exposing the `read()` function
    // reimplement it on our own.
    fs.readFile(filepath, function(err, buffer){
      var files = {};
      files[name] = { contents : buffer };

      if ( utf8(buffer) ){
        var parsed = front(buffer.toString());
        files[name] = parsed.attributes;
        files[name].contents = new Buffer(parsed.body);
      }

      // Rerun the plugin-chain only for this one file.
      metalsmith.ware.run(files, metalsmith, function(err){
        if ( err ) { throw err; }

        // metalsmith deletes the output directory when writing files, so we
        // need to write the file outselves
        for( var file in files ) {
          var data = files[file];
          var out = path.join(metalsmith.destination(), file);
          return fs.writeFile(out, data.contents, function(err) {
            if ( err ) { throw err; }
            console.log ( chalk.green( '...rebuild' ));
            //tell the livereload server which file have changed  
            request.get("http://localhost:35729/changed?files="+file);
          });
        }
      });
    });
  };
};
@FWeinb
Copy link
Owner

FWeinb commented Apr 15, 2014

Great idea. This should be implemented like in grunt-contrib-watch. Will have a look at it.

@FWeinb
Copy link
Owner

FWeinb commented Apr 15, 2014

I implemented it in fd2f168. It is in the 0.0.4 release.

@FWeinb FWeinb closed this as completed Apr 15, 2014
@0gust1
Copy link
Author

0gust1 commented Apr 15, 2014

Ahah, I was writing a response, and you've already implemented it :-).

Thinking about it, it's maybe 2 congruent needs :

  • Ability to trigger custom code when a metalsmith-watch's rebuild has finished
  • Embedding livreload functionnality with metalsmith-watch

Ok, I saw your code, you have taken the 2nd option. Great, it's less bloated than grunt.

@FWeinb
Copy link
Owner

FWeinb commented Apr 15, 2014

Yeah, I went with option two. This seamed like the right thing to do. Maybe we can build a metalsmith-connect plugin to create a little web server to serve content from the build folder.

@0gust1
Copy link
Author

0gust1 commented Apr 15, 2014

As you see in https://gist.github.com/0gust1/10752096 , setting up connect or tiny-lr is pretty straightforward.

For livereload, I'm wondering if a connect+tiny-lr based plugin could be a good solution. As metalsmith-connect-lr + metalsmith-watch (which would take a callback as parameter, or a flag and a port to activate). However, I may miss something, as I don't know metalsmith very well.

If you make metalsmith-watch extensible to other needs (sending file over network after build, copying it, commit it, etc.), it will greatly leverage all metalsmith's qualities (simplicity, plugability, extensibility).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants