|
| 1 | +// Copyright (c) 2015-present, salesforce.com, inc. All rights reserved |
| 2 | +// Licensed under BSD 3-Clause - see LICENSE.txt or git.io/sfdc-license |
| 3 | + |
| 4 | +// Code in this file taken and modified from https://github.com/dtothefp/gulp-yaml-validate/blob/master/task/index.js |
| 5 | + |
| 6 | +import through from 'through2'; |
| 7 | +import path from 'path'; |
| 8 | +import gutil, { PluginError } from 'gulp-util'; |
| 9 | +import jsyaml from 'js-yaml'; |
| 10 | +import extend from 'extend'; |
| 11 | +import BufferStreams from 'bufferstreams'; |
| 12 | +const PLUGIN_NAME = 'validate-yaml'; |
| 13 | + |
| 14 | +const yaml2json = (buffer, options) => { |
| 15 | + const htmlRe = /(<([^>]+)>)/gi; |
| 16 | + let contents = buffer.toString('utf8'); |
| 17 | + if (options.html && htmlRe.test(contents)) { |
| 18 | + throw new Error('YML cannot contain HTML'); |
| 19 | + } else { |
| 20 | + let ymlDocument = options.safe |
| 21 | + ? jsyaml.safeLoad(contents) |
| 22 | + : jsyaml.load(contents); |
| 23 | + |
| 24 | + return Buffer.from( |
| 25 | + JSON.stringify(ymlDocument, options.replacer, options.space) |
| 26 | + ); |
| 27 | + } |
| 28 | +}; |
| 29 | + |
| 30 | +export default function(pluginOptions) { |
| 31 | + var options = extend( |
| 32 | + { |
| 33 | + safe: false, |
| 34 | + html: false, |
| 35 | + replacer: null, |
| 36 | + space: null |
| 37 | + }, |
| 38 | + pluginOptions |
| 39 | + ); |
| 40 | + |
| 41 | + return through.obj(function(file, enc, cb) { |
| 42 | + const self = this; |
| 43 | + if (file.isBuffer()) { |
| 44 | + if (file.contents.length === 0) { |
| 45 | + let msg = `File ${path.dirname(file.path)} is empty`; |
| 46 | + this.emit('error', new PluginError(PLUGIN_NAME, msg)); |
| 47 | + return cb(); |
| 48 | + } |
| 49 | + try { |
| 50 | + file.contents = yaml2json(file.contents, options); |
| 51 | + file.path = gutil.replaceExtension(file.path, '.json'); |
| 52 | + } catch (error) { |
| 53 | + let msg = `${error.message} => ${file.path}`; |
| 54 | + this.emit('error', new PluginError(PLUGIN_NAME, msg)); |
| 55 | + return cb(); |
| 56 | + } |
| 57 | + } else if (file.isStream()) { |
| 58 | + file.contents = file.contents.pipe( |
| 59 | + new BufferStreams((err, buf, cb) => { |
| 60 | + if (err) { |
| 61 | + self.emit('error', new PluginError(PLUGIN_NAME, err.message)); |
| 62 | + } else { |
| 63 | + if (buf.length === 0) { |
| 64 | + let msg = `File ${path.dirname(file.path)} is empty`; |
| 65 | + let error = new PluginError(PLUGIN_NAME, msg); |
| 66 | + self.emit('error', error); |
| 67 | + cb(error); |
| 68 | + } else { |
| 69 | + try { |
| 70 | + file.path = gutil.replaceExtension(file.path, '.json'); |
| 71 | + cb(null, yaml2json(buf, options)); |
| 72 | + } catch (error) { |
| 73 | + let msg = `${error.message} => ${file.path}`; |
| 74 | + self.emit('error', new PluginError(PLUGIN_NAME, msg)); |
| 75 | + cb(error); |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + }) |
| 80 | + ); |
| 81 | + } |
| 82 | + this.push(file); |
| 83 | + cb(); |
| 84 | + }); |
| 85 | +} |
0 commit comments