1

I am currently working on a yeoman generator that has to copy a bunch of processed ejs files to setup configuration for a new subproject. In order to avoid listing each file individually, I would like to just take the whole directory and bring over every file with the ejs extension removed and templating applied with the same context.

While searching through the documentation, I eventually ended up looking into the tests of mem-fs-editor and found that fs.copyTpl should remove the ejs extensions when using glob patterns https://github.com/SBoudrias/mem-fs-editor/blob/master/tests/copy-tpl.js#L85.

However, even when trying to replicate the test in isolated conditions (but within yeoman), the extensions remained:

  1 'use strict';                                                                                                                                                      
  2 const Generator = require('yeoman-generator');
  3 const chalk = require('chalk');
  4 const yosay = require('yosay');
  5 
  6 module.exports = class extends Generator {
  7   prompting() {
  8     // Have Yeoman greet the user.
  9     this.log(
 10       yosay(`Welcome to the tiptop ${chalk.red('generator-ejs-glob')} generator!`)
 11     );
 12 
 13     const prompts = [
 14       {
 15         type: 'confirm',
 16         name: 'someAnswer',
 17         message: 'Would you like to enable this option?',
 18         default: true
 19       }
 20     ];
 21 
 22     return this.prompt(prompts).then(props => {
 23       // To access props later use this.props.someAnswer;
 24       this.props = props;
 25     });
 26   }
 27 
 28   writing() {
 29     this.fs.copyTpl(
 30       this.templatePath('ejs/'),
 31       this.destinationPath('out/'),
 32     )
 33   }
 34 
 35 };

Where ejs contains file1.txt.ejs and file2.txt.ejs.

The output is

> yo ejs-glob                

     _-----_     ╭──────────────────────────╮
    |       |    │   Welcome to the tiptop  │
    |--(o)--|    │    generator-ejs-glob    │
   `---------´   │        generator!        │
    ( _´U`_ )    ╰──────────────────────────╯
    /___A___\   /
     |  ~  |     
   __'.___.'__   
 ´   `  |° ´ Y ` 

? Would you like to enable this option? Yes
   create out/file1.txt.ejs
   create out/file2.txt.ejs

I tried changing the path to ejs/**.* with no results. When trying to add a options.processDestinationPath function myself, it did not even get called.

Do I have to do anything else in order to make this work?

1
  • the documentation says that yeoman uses vinyl fs, a virtual fs in memory, like gulp, whose plugins you can use, if I remember correctly there was one to rename files.
    – Alex
    Apr 9, 2021 at 2:15

1 Answer 1

0

You can use gulp-rename, but I would suggest to use version 1.2.2, there may be some issue with newer versions, at least I had the problem with the current (2.0.0).

You need to call this.registerTransformStream, see:

I'm using it in a slightly different way, calling my templates something.optional-ext.ejs and then removing .ejs.

  writing() {
    this.registerTransformStream(rename((renamePath) => {
      // remove .ejs extension ans set the real extension if present
      if (renamePath.extname === '.ejs') {
        const lastIndexOfDot = renamePath.basename.lastIndexOf('.');
        if (lastIndexOfDot >= 0) {
          renamePath.extname = renamePath.basename.substr(lastIndexOfDot);
          renamePath.basename = renamePath.basename.substr(0, lastIndexOfDot);
        } else {
          renamePath.extname = '';
        }
      }
      // fix template prefix
      if (renamePath.basename[0] === '_') {
        renamePath.basename = renamePath.basename.slice(1);
      } else {
        renamePath.basename = '.' + renamePath.basename;
      }
    }));
    this.fs.copyTpl(
      this.templatePath(),
      this.destinationPath(), {
          appName: this.props.appName
    });
    // note: write are also affected
    this.fs.write('_props.json', JSON.stringify(this.props));
  }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.