All files / packages/design-system/scripts/gulp/lint validate-yaml.js

0% Statements 0/30
0% Branches 0/4
0% Functions 0/2
0% Lines 0/30

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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