复制所有文件,但在自耕农中自动更改某些文件的名称

问题描述 投票:2回答:3

我正在尝试创建一个yeoman生成器,我必须从templatePath复制到destinationPath一些文件和文件夹,但我想要一些这样的文件,其中一个变量可以通过用户的输入之一改变。

喜欢:“<%= 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上神奇地改变,但它不起作用

yeoman yeoman-generator
3个回答
1
投票

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

现在,只需重命名文件:

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


3
投票

我刚刚找到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


1
投票

在@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

© www.soinside.com 2019 - 2024. All rights reserved.