All files / lib yaml.js

0% Statements 0/34
0% Branches 0/11
0% Functions 0/8
0% Lines 0/31

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 87 88 89                                                                                                                                                                                 
'use strict';
 
// Modules
const _ = require('lodash');
const fs = require('fs');
const path = require('path');
const tar = require('tar');
const yaml = require('js-yaml');
 
/*
 * Creates a new yaml instance.
 */
module.exports = class PlatformYaml {
  constructor(baseDir = process.cwd()) {
    // @NOTE: We need this for weird reasons eg our custom yaml types are relative
    // paths to files and its not obvious how we easily resolve them to absolute
    // path
    this.base = baseDir;
 
    // Archive tag
    this.ArchiveYamlType = new yaml.Type('!archive', {
      kind: 'scalar',
      resolve: data => {
        // Kill immediately if we have to
        if (!_.isString(data)) return false;
        // Otherwise make sure this is dir that exists
        return fs.existsSync(path.resolve(this.base, data));
      },
      construct: data => {
        const configPath = path.resolve(this.base, data);
        const createOpts = {gzip: true, cwd: configPath, sync: true};
        const archive = tar.create(createOpts, fs.readdirSync(configPath));
        // We need to read it as base64
        archive.setEncoding('base64');
        // Return the archive data
        return archive.read();
      },
    });
 
    // Include tag mapping version
    this.IncludeMappingYamlType = new yaml.Type('!include', {
      kind: 'mapping',
      resolve: data => {
        // Kill immediately if we have to
        if (!_.isString(data.path)) return false;
        // Otherwise check the path exists
        return fs.existsSync(path.resolve(this.base, data.path));
      },
      construct: data => {
        // Get the path resolved
        const filePath = path.resolve(this.base, data.path);
        // Switch based on type
        switch (data.type) {
          case 'binary': return fs.readFileSync(filePath, {encoding: 'base64'});
          case 'string': return fs.readFileSync(filePath, {encoding: 'utf8'});
          case 'yaml': return this.load(filePath);
          default: return this.load(filePath);
        };
      },
    });
    this.IncludeStringYamlType = new yaml.Type('!include', {
      kind: 'scalar',
      resolve: data => {
        // Kill immediately if we have to
        if (!_.isString(data)) return false;
        // Otherwise check the path exists
        return fs.existsSync(path.resolve(this.base, data));
      },
      construct: data => {
        return this.load(path.resolve(this.base, data));
      },
    });
 
    // The new schema
    this.PLATFORM_SCHEMA = yaml.Schema.create([
      this.ArchiveYamlType,
      this.IncludeMappingYamlType,
      this.IncludeStringYamlType,
    ]);
  };
 
  load(file) {
    // Get the basedir from the file
    this.base = path.dirname(file);
    // Load the stuff
    return yaml.load(fs.readFileSync(file), {schema: this.PLATFORM_SCHEMA});
  };
};