问题描述
所以只是为了给你一些上下文,我正在尝试创建一个生成器来创建一些文件(当然基于用户输入)以及更新项目中的一些现有文件(比如添加一条新路线).
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).
使用 this.template
创建文件没问题...问题是:有没有办法用 Yeoman 做到这一点,而不必使用 Node 读取文件并做一些奇特的查找和替换?
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?
推荐答案
好的,我找到了问题的答案.
Ok, so I found the answer to my question.
Addy Osmani 向我展示了在哪里可以查看 推特上的这个帖子,后来我找到了此链接 这正是我需要的.
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.
它的要点归结为两个函数:readFileAsString
和 write
.用法如下:
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);
我也写了关于这个的博客 在我的博客上.
I've also blogged about this on my blog.
正如 Toilal 在评论中提到的:
As mentionned in comments by Toilal :
write
方法已不存在,必须替换为 writeFileFromString
(参数也颠倒)- Toilal
编辑 2
然后,正如 ivoba 在评论中提到的:
EDIT 2
And then, as mentionned in comments by ivoba:
this.writeFileFromString
&this.readFileAsString
已弃用,现在应该使用 github.com/yeoman/html-wiring,情况有所改变 :) – ivoba
这篇关于Yeoman 生成器可以更新现有文件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!