Yeoman copy directory with templating

Viewed 78

How can I copy multiple files in a whole folder recursively while applying templating ? I found answers for copying folders without templating, templating individual files but not batch templating.

I currently do :

'use strict';
const Generator = require('yeoman-generator');
module.exports = class extends Generator {
  writing() {
    this.fs.write(
       this.destinationPath('public'),
       ejs.render(this.fs.read(this.templatePath('public')), 
       {
         webPageTitle : 'great web application',
       }),
     );
  );
}

where the public directory contains many files including index.html :

<head>
    <meta charset="utf-8"/>
    <meta content="width=device-width,initial-scale=1" name="viewport"/>
    <title><%= webPageTitle %></title>
</head>
<body></body>
</html>

but yeoman throws an error :

Error generator-name
~/Documents/yo-test/generator-name/generators/app/templates/public doesn't exist

I understand yeoman expects a file and not a folder. I do not want to process each file individually. This would be both cumbersome and more difficult to maintain in the long run.

What is the right approach for both copying a complete folder with its subfolders and in the mean time batch templating all its files with yeoman ?

1 Answers

The right way I found :

    this.fs.copyTpl(
      this.templatePath('public'),
      this.destinationPath('public'),
      {
        webPageTitle : 'great web application',
      }
    );

that copies the whole public folder and applies the templating substitution wherever possible. As a result, the index.html is being applied the expected substitution during the recursive batch copy process of the public folder.

Related