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?
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.
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
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.
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.
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.
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.
As mentionned in comments by Toilal :
The
write
method doesn't exists anymore, and must be replaced bywriteFileFromString
(arguments are also reversed) – Toilal
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With