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

标签 javascript node.js yeoman

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

当我克隆存储库时,我想删除 .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 脚本编写来说相当陌生。

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

最佳答案

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

因此,为了简单起见,我建议从使用 rimraf.sync() 开始。这样就会同步运行。

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

关于javascript - 按特定顺序执行 Yeoman/nodejs 操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33638067/

相关文章:

javascript - meteor 从出版物中返回空光标

javascript - 无法确定在单击按钮时执行了哪个代码

Node.Js AWS SES - 发送电子邮件时 ETIMEDOUT

javascript - 如何在 Nexus 中创建 Yeoman 或 Bower 存储库

javascript - Yeoman:在 grunt 中向现有项目添加 sass/compass 支持

javascript - Vue 和 Vuex 在 store 模块内进行 Axios 操作

鼠标离开浏览器窗口的 Javascript 修改

javascript - 如果在映射数组中,则将对象推送到选中 : true, 的新数组

javascript - AWS Lambda 和 promise : Callbacks not being called

yeoman - 强制覆盖文件