Renaming single file names while copying entire folder using copyTpl

前端 未结 2 2072
花落未央
花落未央 2021-01-15 18:44

My yeoman generator copies files from template to destination path:

this.fs.copyTpl(
            this.templatePath(),
            this.destinationPath(), {
          


        
相关标签:
2条回答
  • 2021-01-15 18:50

    OK, I found a solution. According to yeoman docs:

    Any generator author can register a transformStream to modify the file path and/or the content.

    Using this method:

    this.registerTransformStream();
    

    What that means is I can pipe all generated files through some script:

    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
                });
        }
    });
    

    This script will pipe all files through gulp-rename, changing 666replacethat666 to something more intelligent.

    0 讨论(0)
  • 2021-01-15 18:55

    registerTransformStream with gulp-rename is still an issue. However, I get it working with glob.

    const glob = require('glob');
    
    writing() {
        const files = glob.sync('**', { dot: true, nodir: true, cwd: this.templatePath() })
        for (let i in files) {
            this.fs.copyTpl(
                this.templatePath(files[i]),
                this.destinationPath( this.props.destinationFolderPath + '\\' + files[i].replace(/__fileName__/g,this.props.fileName)),
                this.props
            )
        }
    }
    
    0 讨论(0)
提交回复
热议问题