回调中的grunt.initConfig不起作用

时间:2013-11-07 08:40:44

标签: deployment callback gruntjs

我想使用grunt进行部署,因此希望根据现有的~/.ssh/config文件读取远程主机的配置。

要加载该配置,我正在使用sshconf,但需要在回调中包含grunt.initConfig()调用,以便在定义环境时进行配置。

var sshconf = require('sshconf');

module.exports = function(grunt) {

  // Read in ssh configuration
  sshconf.read(function(err, sshHosts) {

    if (err)
      console.log(err);

    // SSH config loaded, now init grunt

    grunt.initConfig({
      sshconfig: {
        staging: {
          privateKey: grunt.file.read(sshHosts['project_staging'].properties.IdentityFile),
          host: sshHosts['project_staging'].properties.HostName,
          username: sshHosts['project_staging'].properties.User,
          port: sshHosts['project_staging'].properties.Port || 22,
          path: "/var/www/project"
        },
        production: {
          // ...
        }
      },
      // Tasks to be executed on remote server
      sshexec: {
        example_task: {
          command: 'uptime && hostname'
        }
      },
      sftp: {
        deploy: {
          files: {
            "./": ["*.json", "*.js", "config/**", "controllers/**", "lib/**", "models/**", "public/**", "views/**"]
          },
          options: {
            //srcBasePath: "test/",
            createDirectories: true
          }
        }
      }
      // More tasks
      // ...
    });
    grunt.loadNpmTasks('grunt-ssh');
    // More plugins ...
  });

};

当我致电grunt --help时,它声明:

> grunt --help
Grunt: The JavaScript Task Runner (v0.4.1)
…
Available tasks
(no tasks found)

如果我没有在回调中包含grunt启动(sshconf.read(function(err, sshHosts) {})),一切正常(除了ssh配置未加载或尚未准备好使用)。

我正在尝试甚至可能,如果是这样,怎么样?我错过了一些明显的东西吗

1 个答案:

答案 0 :(得分:2)

Grunt init不能像这样以异步方式使用。要么同步读取sshconf,要么使用任务,如本答案中所述:How can I perform an asynchronous operation before grunt.initConfig()?