Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 287
Kiko Beats Avatar asked Jan 11 '23 01:01

Kiko Beats


1 Answers

@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');
});
like image 98
TonyTakeshi Avatar answered Jan 21 '23 15:01

TonyTakeshi