Search code examples
yeomanyeoman-generator

Can Yeoman generators update existing files?


So just to give you some context, I'm trying to create a generator that will create some files (based on user input of course) as well as update some existing files in the project (like adding a new route for example).

Creating the files using this.template is no problem... the question is: is there any way to do this with Yeoman without having to read the file using Node and doing some fanciful find and replace?


Solution

  • Ok, so I found the answer to my question.

    Addy Osmani showed me where to look in this thread on twitter, and then I later found this link which shows exactly what I need.

    The gist of it boils down to two functions : readFileAsString and write. Usage is as follows:

    var path = "/path/to/file.html",
        file = this.readFileAsString(path);
    
    /* make modifications to the file string here */
    
    this.write(path, file);
    

    Edit: I've also blogged about this on my blog.

     EDIT 1

    As mentionned in comments by Toilal :

    The write method doesn't exists anymore, and must be replaced by writeFileFromString (arguments are also reversed) – Toilal

    EDIT 2

    And then, as mentionned in comments by ivoba:

    this.writeFileFromString & this.readFileAsString are deprecated, github.com/yeoman/html-wiring should be used by now, things change :) – ivoba