Can Yeoman generators update existing files?

前端 未结 5 1979
旧巷少年郎
旧巷少年郎 2021-01-30 10:57

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

5条回答
  •  遇见更好的自我
    2021-01-30 11:35

    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.

提交回复
热议问题