1 | 'use strict';
|
2 |
|
3 | const YAML = require('js-yaml');
|
4 | const _ = require('lodash');
|
5 | const { schema } = require('./schema');
|
6 |
|
7 | const 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 |
|
20 | const 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 |
|
36 | module.exports = {
|
37 | parseYamlWithCustomTag
|
38 | }; |
\ | No newline at end of file |