Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 992
Remy Avatar asked Oct 04 '13 10:10

Remy


People also ask

What is yo RC JSON?

The . yo-rc. json file is a JSON file where configuration objects from multiple generators are stored. Each generator configuration is namespaced to ensure no naming conflicts occur between generators. This also means each generator configuration is sandboxed and can only be shared between sub-generators.

How do I update the Yeoman engine and office generator?

To update the Yeoman engine to the latest (or if you find that you don’t have the Yeoman engine installed at all yet), use the following command npm install yo@latest -g To update the Office generator to the latest (or to install it for the first time if you don’t have it yet), use the following command npm install generator-office@latest -g

How does the Yeoman generator work?

The generator system allows you to apply custom filters on every file writes. Automatically beautifying files, normalizing whitespace, etc, is totally possible. Once per Yeoman process, we will write every modified file to disk.

How to update a pre-existing file using Yeoman transform stream?

You can basically use any gulp plugins with the Yeoman transform stream to process generated files during the writing phase. Updating a pre-existing file is not always a simple task. The most reliable way to do so is to parse the file AST ( abstract syntax tree) and edit it.

How do I update my generators?

Glad you asked! Starting with Yeoman RC1, which will be announced later today, there's a brand new way to manage your generators: Just enter yo and enjoy the new wizard: If you select "Update Generators" from the menu, yo yo will take care of bringing them all to the latest version.


2 Answers

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

like image 103
Remy Avatar answered Sep 20 '22 04:09

Remy


Yeoman also provides a more elegant way of fs operations using mem-fs-editor.

You can use this.fs.copy, passing a process function as an option to do any modifications to the content:

this.fs.copy(path, newPath, {     process: function(content) {          /* Any modification goes here. Note that contents is a Buffer object */          var regEx = new RegExp('old string', 'g');         var newContent = content.toString().replace(regEx, 'new string');         return newContent;     } }); 

This way you can also take advantage of mem-fs-editor features.

like image 28
sepans Avatar answered Sep 21 '22 04:09

sepans