复制所有文件,但在 yeoman 中自动更改一些文件的名称

Copy all files but change the name of some automatically in yeoman

我正在尝试创建一个 yeoman 生成器,我必须在其中将一些文件和文件夹从 templatePath 复制到 destinationPath,但我希望其中一些文件带有一个变量,yeoman 可以通过用户输入之一更改该变量.

喜欢:“<%=name%>-plugin.php”-> "hello-plugin.php"

我看到了一些可以做到这一点的参考资料,但我找不到具体方法。

我现在正在做的事情:

//Copy the configuration files
    app: function () {
      this.fs.copyTpl(
        this.templatePath('**'),
        this.destinationPath(), 
        {
          name: this.props.pluginName,
          name_function: this.props.pluginName.replace('-', '_'),
          name_class: this.props.className,
          description: this.props.pluginDescription
        }
      );
    }

我认为使用该代码我的 <%=name%> 会在 copyTpl 上神奇地改变,但它不起作用

这不可能了。该功能过于臃肿,并在 2015 年的某个时候被删除。

现在,只需重命名文件:

this.fs.copy('name.js', 'new-name.js')

我刚找到 the solution:

使用 this.registerTransformStream(); 通过某些 node.js 脚本传输所有文件。

var rename = require("gulp-rename");
//other dependecies...

module.exports = yeoman.Base.extend({

    //some other things generator do...

    writing: function() {
        var THAT = this;
        this.registerTransformStream(rename(function(path) {
            path.basename = path.basename.replace(/(666replacethat666)/g, THAT.props.appName);
            path.dirname = path.dirname.replace(/(666replacethat666)/g, THAT.props.appName);
        }));
        this.fs.copyTpl(
            this.templatePath(),
            this.destinationPath(), {
                appName: this.props.appName
            });
    }
});

我在这里 gulp-rename 将文件名更改为其他名称。

假设this.props.appName == "myFirstApp",这:

666replacethat666-controller.html

将更名为

myFirstApp-controller.html

按照@piotrek 的回答,我做了一个函数来用某种模式替换所有道具(就像 ejs 那样)-> $$[your prop name]$$警告:内含 ES6

var rename = require("gulp-rename");
//other dependecies...

module.exports = yeoman.Base.extend({

//some other things generator do...

  writing: function() {
    this.registerTransformStream(rename((path) => {
      for (let prop in this.props) {
        let regexp = new RegExp('\$\$' + prop + '\$\$', 'g')
        path.basename = path.basename.replace(regexp, this.props[prop]);
        path.dirname = path.dirname.replace(regexp, this.props[prop]);
      }
    }));
    this.fs.copyTpl(
        this.templatePath(),
        this.destinationPath(), {
            appName: this.props.appName
        });
    }
});

示例:

假设您有 this.props.appname = MyAppthis.props.AnotherProps = Test,并且您想要重命名文件或文件夹。

为您的文件或文件夹命名 MyFile$$appname$$.js -> MyFileMyApp.js

为您的文件或文件夹命名 $$appname$$.js -> MyApp.js

为您的文件或文件夹命名 $$AnotherProps$$.js -> Test.js