【问题标题】:Executing Yeoman/nodejs actions in specific order按特定顺序执行 Yeoman/nodejs 操作
【发布时间】:2015-11-10 19:31:58
【问题描述】:

第一次开始制作 Yeoman 生成器...我想要做的是从存储库中获取 VM,并且可以选择从另一个存储库中获取现有代码库到同一目录。

当我克隆 repo 时,我想删除 .git 信息(libgit2 不支持深度或其他选项,所以我使用 rimraf 去除 git 历史记录),复制并重命名两个配置文件,并替换字符串在 Yeoman 的“提示”阶段由用户提供输入的那些文件。

在操作完成之前,我无法阻止进度。这是我到目前为止所拥有的。它似乎主要工作......除了 replace() 不像我预期的那样工作:

configuring: function() {
    var done = this.async();
    var vm_repository = "https://github.com/geerlingguy/drupal-vm.git";
    var vm_directory = this.destinationRoot() + '/drupalvm';

    this.log(chalk.yellow('Cloning DrupalVM from ' + vm_repository));

    clone(vm_repository, vm_directory, done)
      .then(function() {
        rimraf(vm_directory + '/.*', function(error) {
          if (error) return console.log(error);
        });
        rimraf(vm_directory + '/docs', function(error) {
          if (error) return console.log(error);
        });
        rimraf(vm_directory + '/examples', function(error) {
          if (error) return console.log(error);
        });
        rimraf(vm_directory + '/mkdocs.yml', function(error) {
          if (error) return console.log(error);
        });
        console.log('Repository cloned successfully.');
        done();
      })
      .catch(function(error) { console.log(error) });
    },

  writing: function() {
    var done = this.async();
    var vm_directory = this.destinationRoot() + '/drupalvm';

    fs.copy(this.destinationRoot() + '/drupalvm/example.config.yml', this.destinationRoot() + '/drupalvm/config.yml', function (error) {
      if (error) return console.log(error);
    });

    fs.copy(this.destinationRoot() + '/drupalvm/example.drupal.make.yml', this.destinationRoot() + '/drupalvm/drupal.make.yml', function (error) {
      if (error) return console.log(error);
    });

    done();
  },

  end: function() {
    this.log('what 4');
    // rewrite values with user input
    replace({
      regex: "/vagrant_machine_name\: drupalvm/",
      replacement: "vagrant_machine_name: " + this.vagrant_machine_name,
      paths: [this.destinationRoot() + '/drupalvm/config.yml'],
      recursive: false,
      silent: false,
    });

    replace({
      regex: "vagrant_ip: 192.168.88.88",
      replacement: "vagrant_ip: " + this.vagrant_ip,
      paths: [this.destinationRoot() + '/drupalvm/config.yml'],
      recursive: false,
      silent: false,
    });
  },

这是否异常有效.. 可能不是。 yeoman/nodejs 脚本非常新。

我哪里出错了?另外,如何通过函数将上下文和变量作为参数传递?它们不断出现未定义。

【问题讨论】:

    标签: javascript node.js yeoman


    【解决方案1】:

    这里有多个问题,很难指出所有问题。基本上,您需要了解 Node 中的异步操作是如何工作的——这不是 Yeoman 特有的。

    所以为了简单起见,我会说从使用rimraf.sync() 开始。所以这将同步运行。

    另外,请查看 Yeoman 文件助手以复制/替换/模板/等等http://yeoman.io/authoring/file-system.html - 这些助手也是同步的,因此您不必手动处理流程。

    【讨论】:

      猜你喜欢
      • 2022-09-27
      • 1970-01-01
      • 2016-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多