UNPKG

749 BJavaScriptView Raw
1'use strict';
2
3const YAML = require('js-yaml');
4const _ = require('lodash');
5const { schema } = require('./schema');
6
7const loadYaml = (contents, options) => {
8 let data, error;
9 try {
10 data = YAML.load(contents.toString(), options || {});
11 } catch (ex) {
12 error = ex;
13 }
14 return {
15 data,
16 error
17 };
18};
19
20const parseYamlWithCustomTag = (filePath, contents) => {
21 const options = {
22 filename: filePath
23 };
24 let result = loadYaml(contents, options);
25 if (result.error && result.error.name === 'YAMLException') {
26 _.merge(options, { schema });
27 result = loadYaml(contents, options);
28 }
29 if (result.error) {
30 throw result.error;
31 } else {
32 return result.data;
33 }
34};
35
36module.exports = {
37 parseYamlWithCustomTag
38};
\No newline at end of file