【问题标题】:Call yeoman generator from code with options使用选项从代码中调用 yeoman 生成器
【发布时间】:2018-12-16 19:37:29
【问题描述】:

我创建了一个带有用户交互的yeoman generator,可以在终端中调用它(运行npm link之后):

yo mygenerator --name test --path /test/path --project testproject

现在我想在我的 vscode 扩展中包含这个生成器。

当生成器被添加为 package.json 依赖项时,如何从我的打字稿代码中调用 yo 生成器?

类似(伪代码)

import { yo } from 'yeoman';
import mygenerator; // added as a dependency via package.json

const options = {
   name: 'test',
   path: '/test/path',
   project: 'testproject',
};
yo.exec(mygenerator, options, () => {
    console.log('yeoman finished')
});

这样的事情可能吗?

【问题讨论】:

    标签: yeoman yeoman-generator


    【解决方案1】:

    这是一个解决方案:

    const env = yeoman.createEnv();
    const generatorPath = '../node_modules/generator-name/generators/app/index.js';
    env.getByPath(generatorDir);
    env.on('error', (err: any) => {
      // handle error
    });
    const options = {
      env,
      'option1': option1,
      'option2': option2,
    };
    try {
      await env.run('name', options);
    } catch (err) {
      // handle error
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-05
      • 2011-05-04
      • 2018-07-18
      • 1970-01-01
      相关资源
      最近更新 更多