UNPKG

1.54 kBJavaScriptView Raw
1var AWS = require('aws-sdk'),
2gutil = require('gulp-util'),
3through = require('through2'),
4backoff = require('backoff'),
5fs = require('fs-extra'),
6path = require('path'),
7PluginError = gutil.PluginError;
8
9
10const PLUGIN_NAME = 'gulp-cf-validate';
11
12function validator(opts) {
13 var cloudformation = new AWS.CloudFormation(opts);
14 // creating a stream through which each file will pass
15 var stream = through.obj(function(file, enc, cb) {
16 var retry = backoff.exponential();
17 retry.failAfter(20);
18
19 retry.on('ready', function(number,delay) {
20 if (file.contents) {
21 cloudformation.validateTemplate({
22 TemplateBody: file.contents.toString()
23 },function(err,data) {
24 if (err && err.code === "Throttling") {
25 //console.log(new PluginError(PLUGIN_NAME,"RETRY VALIDATE (throttled) count:" + number + " delay:" + delay + " file:" + file.path));
26 retry.backoff();
27 }
28 else if (err) {
29 console.log(new PluginError(PLUGIN_NAME, file.path));
30 fs.outputFile(path.join('condensation_errors',file.path),file.contents,function(ofErr) {
31 if (ofErr) {
32 console.log(new PluginError(PLUGIN_NAME, file.contents));
33 }
34 cb(err,file);
35 });
36 }
37 else {
38 cb(err,file);
39 }
40 });
41 }
42 else {
43 cb(null,file);
44 }
45 });
46
47 retry.backoff();
48
49
50 });
51
52 // returning the file stream
53 return stream;
54}
55
56
57module.exports = validator;