繁体   English   中英

按特定顺序执行Yeoman / nodejs操作

[英]Executing Yeoman/nodejs actions in specific order

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

克隆存储库时,我想删除.git信息(libgit2不支持depth或其他选项,因此我使用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脚本的新知识。

我要去哪里错了? 另外,如何将上下文和变量作为参数传递给函数? 他们不断出现不确定的情况。

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

因此,为了简单起见,我将首先使用rimraf.sync() 这样就可以同步运行。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM