使用gulp-awspublish

时间:2016-04-01 05:11:43

标签: amazon-web-services amazon-s3 gulp yeoman-generator

我有一个简单的单页应用,使用gulp-awspublish部署到S3存储桶。我们使用inquirer.js(通过gulp-prompt)向开发人员询问要部署到哪个存储区。

有时,应用可能会部署到多个S3存储桶。目前,我们只允许选择一个存储桶,因此开发人员必须依次为每个存储桶gulp deploy。这很乏味,容易出错。

我希望能够选择多个存储桶并为每个存储桶部署相同的内容。使用inquirer.js / gulp-prompt选择多个存储桶很简单,但从单个流生成任意多个S3目标并不简单。

我们的deploy任务基于generator-webapp' S3 recipe。该配方建议gulp-rename重写写入特定存储桶的路径。目前我们的任务如下:

gulp.task('deploy', ['build'], () => {
  // get AWS creds
  if (typeof(config.awsCreds) !== 'object') {
    return console.error('No config.awsCreds settings found. See README');
  }

  var dirname;
  const publisher = $.awspublish.create({
    key: config.awsCreds.key,
    secret: config.awsCreds.secret,
    bucket: config.awsCreds.bucket
  });

  return gulp.src('dist/**/*.*')
    .pipe($.prompt.prompt({
      type: 'list',
      name: 'dirname',
      message: 'Using the ‘' + config.awsCreds.bucket + '’ bucket. Which hostname would you like to deploy to?',
      choices: config.awsCreds.dirnames,
      default: config.awsCreds.dirnames.indexOf(config.awsCreds.dirname)
    }, function (res) {
      dirname = res.dirname;
    }))
    .pipe($.rename(function(path) {
        path.dirname = dirname + '/dist/' + path.dirname;
    }))
    .pipe(publisher.publish())
    .pipe(publisher.cache())
    .pipe($.awspublish.reporter());
});

希望很明显,但config.awsCreds可能看起来像:

awsCreds: {
  dirname: 'default-bucket',
  dirnames: ['default-bucket', 'other-bucket', 'another-bucket']
}

Gulp-rename重写目标路径以使用正确的存储桶。

我们可以使用"复选框"选择多个存储桶而不是" list"对于gulp-prompt选项,但我不确定如何将其传送到多个存储桶。

简而言之,如果$.prompt返回字符串数组而不是字符串,那么如何将源写入多个目标(存储桶)而不是单个存储桶?

请注意,gulp.dest()未使用 - 仅限gulp.awspublish() - 而且我们不知道可以选择多少个桶。

1 个答案:

答案 0 :(得分:1)

从未使用过S3,但如果我正确理解了您的问题,那么当复选框js/foo.jsdefault-bucket/dist/js/foo.js时,文件other-bucket/dist/js/foo.js应重命名为default-bucketother-bucket被选中了吗?

然后这应该可以解决问题:

// additionally required modules
var path = require('path');
var through = require('through2').obj;

gulp.task('deploy', ['build'], () => {
  if (typeof(config.awsCreds) !== 'object') {
    return console.error('No config.awsCreds settings found. See README');
  }

  var dirnames = []; // array for selected buckets
  const publisher = $.awspublish.create({
    key: config.awsCreds.key,
    secret: config.awsCreds.secret,
    bucket: config.awsCreds.bucket
  });

  return gulp.src('dist/**/*.*')
    .pipe($.prompt.prompt({
      type: 'checkbox', // use checkbox instead of list
      name: 'dirnames', // use different result name
      message: 'Using the ‘' + config.awsCreds.bucket + 
               '’ bucket. Which hostname would you like to deploy to?',
      choices: config.awsCreds.dirnames,
      default: config.awsCreds.dirnames.indexOf(config.awsCreds.dirname)
    }, function (res) {
      dirnames = res.dirnames; // store array of selected buckets
    }))
    // use through2 instead of gulp-rename
    .pipe(through(function(file, enc, done) {
      dirnames.forEach((dirname) => {
        var f = file.clone();
        f.path = path.join(f.base, dirname, 'dist',
                           path.relative(f.base, f.path));
        this.push(f);
      });
      done();
    }))
    .pipe(publisher.cache())
    .pipe($.awspublish.reporter());
});

请注意我对您发布的代码进行更改的评论。

这样做是使用through2来克隆通过流的每个文件。每个文件都被克隆了多次,因为选中了存储桶复选框,并且每个克隆都被重命名为最终位于不同的存储桶中。