Yeoman:修改子生成器创建的文件时如何避免文件覆盖冲突消息?

时间:2015-10-01 06:57:07

标签: yeoman yeoman-generator

概述

我正在创建一个生成自定义角应用程序的自动生成器。它将生成自定义角度控制器和服务。

虽然我可以简单地获取由angular generator生成的当前角度模板,添加我的代码,然后将其保存为我的生成器中的模板,我更喜欢让生成器调用“官方”角度生成器(以前由用户安装)作为运行时的子生成器,因此每次角度生成器模板更改时,我都不必同步模板。

问题

我有这个工作,但唯一的问题是,从我的生成器,我将我的代码添加到由子生成器(例如控制器)生成的文件时,我收到消息:

 conflict app/scripts/controllers/mycontroller.js
? Overwrite app/scripts/controllers/mycontroller.js? overwrite
    force app/scripts/controllers/mycontroller.js

有没有办法可以在将子文件写入磁盘之前获取子生成器生成的文件,这样我就可以编辑它,而不会生成覆盖提示?

我认为提示有关覆盖首次创建的文件会让人感到困惑。

数据

在阅读yeoman doc时,它听起来像生成器和子生成器共享相同的“软”内存文件系统:

“由于异步API更难使用,Yeoman提供了一个同步文件系统API,其中每个文件都写入内存文件系统,并且只有在Yeoman运行完毕后才会写入磁盘。此内存文件系统在所有组合的生成器之间共享。“

但似乎子生成器将文件写入磁盘而不是内存中的文件系统。不幸的是,我无法控制子发电机,因为它是标准的角度yeoman发电机。

这是我的发电机(向下滚动到标有“相关部分”的部分):

'use strict';   
var yeoman = require('yeoman-generator');
var chalk = require('chalk');
var yosay = require('yosay');

module.exports = yeoman.generators.Base.extend({
 prompting: function () {
    var done = this.async();

    // Have Yeoman greet the user.
    this.log(yosay(
      'Welcome to the epic ' + chalk.red('AngularVr') + ' generator!'
    ));

    var prompts = [{
      type: 'confirm',
      name: 'someOption',
      message: 'Would you like to enable this option?',
      default: true
    }];

    this.prompt(prompts, function (props) {
      this.props = props;
      done();
    }.bind(this));
  },

  writing: { 
    app: function () {
          this.fs.copy(
        this.templatePath('_vt_marker.json'),
        this.destinationPath('vt_marker.json')
      );
    },

====================== Relevant Portion ===============
  subgenerators: function () {
      this.log("now executing subgenerators");
      this.composeWith('angular:controller',  {args: ['mycontroller']} );

      this.log("now done with subgeneration");
  }, 

  subgenerators_read: function () {
// add a new line to generated file..this generates overwrite prompt
    var fp = this.destinationPath('app/scripts/controllers/mycontroller.js');
    var fc = this.fs.read(fp);
    fc += '\nhello there\n';
    this.fs.write(fp, fc);
  },

});

这是我第一次尝试编写自然生成器。

非常感谢。

1 个答案:

答案 0 :(得分:0)

问题可能只是您的生成器或生成器角度在yeoman-generator的旧版本上运行。

此时的最新版本为0.20.3 https://www.npmjs.com/package/yeoman-generator

相关问题