Search code examples
node.jsyeomanyeoman-generator

Using Yeoman programmatically inside nodejs project


I want to use an yeoman generator inside a NodeJS project

I installed yeoman-generatorand generator-git (the generator that I want use) as locally dependency, and, at this moment my code is like this:

var env = require('yeoman-generator')();
var path = require('path');
var gitGenerator = require('generator-git');
var workingDirectory = path.join(process.cwd(), 'install_here/');
generator = env.create(gitGenerator);

obviously the last line doesn't work and doesn't generate the scaffold.

The question: How to?

Importantly, I want to stay in local dependency level!


Solution

  • @simon-boudrias's solution does work, but after I changed the process.chdir(), this.templatePath() and this.destinationPath() returns same path.

    I could have use this.sourcePath() to tweak the template path, but having to change this to each yeoman generator is not so useful. After digging to yo-cli, I found the following works without affecting the path.

    var env = require('yeoman-environment').createEnv();
    
    env.lookup(function() {
        env.run('generator-name');
    });