1 | var AWS = require('aws-sdk'),
|
2 | gutil = require('gulp-util'),
|
3 | through = require('through2'),
|
4 | backoff = require('backoff'),
|
5 | fs = require('fs-extra'),
|
6 | path = require('path'),
|
7 | PluginError = gutil.PluginError;
|
8 |
|
9 |
|
10 | const PLUGIN_NAME = 'gulp-cf-validate';
|
11 |
|
12 | function validator(opts) {
|
13 | var cloudformation = new AWS.CloudFormation(opts);
|
14 |
|
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 |
|
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 |
|
53 | return stream;
|
54 | }
|
55 |
|
56 |
|
57 | module.exports = validator;
|