Gruntjs:我可以在不同的ftpush任务之间共享"auth"部分吗?



我的Gruntfile中有一个任务,它通过ftp将更改的文件推送到我的服务器。这些任务都使用相同的身份验证,我想删除所有重复的"身份验证"部分并只定义一次。

当前工作版本(包含大量重复项)如下所示(为便于阅读而缩短):

ftpush: {
  css_prod: {
    auth: {
      host: 'xxx.xx.xx.x',
      port: 21,
      authKey: 'authKey'
    },
    src: '<%= dirs.dev_css_build %>',
    dest: '<%= dirs.prod_css %>',
    simple: true,
    useList: false
  },
  css_wordpress: {
    auth: {
      host: 'xxx.xx.xx.x',
      port: 21,
      authKey: 'authKey'
    },
    src: '<%= dirs.dev_css %>/style.css',
    dest: '<%= dirs.prod_theme_current %>',
    simple: true,
    useList: false
  },
  js_standalone: {
    auth: {
      host: 'xxx.xx.xx.x',
      port: 21,
      authKey: 'authKey'
    },
    src: '<%= dirs.dev_js_standalone %>',
    dest: '<%= dirs.prod_js_standalone %>',
    simple: false,
    useList: false
  }
}

我尝试在"选项"区域中定义"身份验证"部分,如下所示:

ftpush: {
  options: {
    auth: {
      host: 'xxx.xx.xx.x',
      port: 21,
      authKey: 'authKey'
    }      
  },
  css_prod: {
    src: '<%= dirs.dev_css_build %>',
    dest: '<%= dirs.prod_css %>',
    simple: true,
    useList: false
  },
  css_wordpress: {
    src: '<%= dirs.dev_css %>/style.css',
    dest: '<%= dirs.prod_theme_current %>',
    simple: true,
    useList: false
  },
  js_standalone: {
    src: '<%= dirs.dev_js_standalone %>',
    dest: '<%= dirs.prod_js_standalone %>',
    simple: false,
    useList: false
  }
}

但这行不通。我怎样才能做到这一点?

不是一个完美的解决方案,但您可以使用 grunt 配置模板来引用仅在一个地方定义的实际值。

例如,您可以尝试使第二个解决方案在每个"真实"部分添加下一行:

auth: '<%= ftpush.options.auth %>'

这样,身份验证部分将入到所有小节中。

最新更新