【问题标题】:Create/update file(s) post dependency install, with Yeoman generator使用 Yeoman 生成器在依赖项安装后创建/更新文件
【发布时间】:2015-04-27 05:02:39
【问题描述】:

我想在安装依赖项后创建/更新文件,同时执行生成器(generator-custom)。

$ yo custom

任何指针都会很有帮助。


例如,在安装依赖项后,我正在尝试针对依赖包devDependencies 更新package.jsondevDependencies 部分。但是我无法实现它。请找到下面的代码sn-p。

install: function () {
  this.installDependencies({
    skipInstall: this.options['skip-install'],
    callback: function () {
      var pkgPath = process.cwd() + '/package.json';
      var pkg = require(pkgPath);
      pkg.devDependencies = {
        "grunt": "~0.4.2"
      };
      this.write(pkgPath, JSON.stringify(pkg));
    }.bind(this)
  });
}

更新 package.json 背后的想法是重新运行 installDependencies 函数。所以当核心包更新它的依赖时,生成器不需要更新它的模板。

正如@SimonBoudrias 所建议的,上述方法不是执行操作的有效方法。可以使用主依赖包的 peerDependencies 来安装依赖包。


【问题讨论】:

  • 为什么要在安装运行后添加devDependencies?这只会让您的用户感到困惑。
  • 其实,我想从其中一个依赖包的 package.json 中复制 devDependencies 并重新运行安装。
  • 背后的原因是,当主依赖的依赖发生变化时,我可以避免更新生成器。请建议是否有更好的方法。
  • 然后使用可组合性。无需等待组合生成器安装运行,您将能够编写相同的文件而无需实际敲击硬盘驱动器。 yeoman.io/authoring/composability.htmlyeoman.io/authoring/file-system.html
  • 谢谢@SimonBoudrias。我想我可以通过主依赖包的 peerDependencies 实现同样的效果。

标签: yeoman yeoman-generator writefile post-install


【解决方案1】:

我想在安装依赖项后创建/更新文件,同时执行生成器(generator-custom)。

以下 sn-p 服务于所需的用例。

this.npmInstall(['npm-module'], {}, function() {
  this.write('path/to/file', 'file-content');
}.bind(this));

【讨论】:

  • 是的,但不是。这不适用于最新版本的yeoman-generator
  • 实际上,我收回了这一点。我们最终修复了代码以使此类模式起作用。不过,我认为这是一个有缺陷的逻辑,你应该以不同的方式处理。
  • @SimonBoudrias 是用例有缺陷还是实现有缺陷?你能建议一个更好的方法吗?
猜你喜欢
  • 2016-04-19
  • 1970-01-01
  • 1970-01-01
  • 2015-06-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-27
相关资源
最近更新 更多