| 24 | } |
| 25 | |
| 26 | function runValidation(file, encoding, callback) { |
| 27 | if (file.isNull()) { |
| 28 | return callback(null, file); |
| 29 | } |
| 30 | if (file.isStream()) { |
| 31 | this.emit('error', new PluginError(PLUGIN_NAME, |
| 32 | 'Streams not supported!')); |
| 33 | } |
| 34 | if (file.isBuffer()) { |
| 35 | validator.getInstance() |
| 36 | .then(function(validatorInstance) { |
| 37 | const inputString = file.contents.toString(); |
| 38 | file.ampValidationResult = |
| 39 | validatorInstance.validateString(inputString); |
| 40 | return callback(null, file); |
| 41 | }) |
| 42 | .catch(function(err) { |
| 43 | // This happens if the validator download failed. We don't fail the |
| 44 | // build, but map the exception to an validation error instead. This |
| 45 | // makes it possible to configure via failAfterError whether this |
| 46 | // should fail the build or not. |
| 47 | console.log(colors.red(err.message)); |
| 48 | file.ampValidationResult = { |
| 49 | status: STATUS_UNKNOWN, |
| 50 | }; |
| 51 | return callback(null, file); |
| 52 | }); |
| 53 | } |
| 54 | } |
| 55 | return through.obj(runValidation); |
| 56 | }; |
| 57 | |