serverless/lib/classes/YamlParser.js
Mariusz Nowak 7f1b507bce
Prevent change of current directory
Yaml parser was introducing side effects, that affected test results.
Refactor so it doesn't depend on current directory
2019-05-30 17:19:23 +02:00

33 lines
749 B
JavaScript

'use strict';
const path = require('path');
const YAML = require('js-yaml');
const resolve = require('json-refs').resolveRefs;
class YamlParser {
constructor(serverless) {
this.serverless = serverless;
}
parse(yamlFilePath) {
let parentDir = yamlFilePath.split(path.sep);
parentDir.pop();
parentDir = parentDir.join('/');
const root = this.serverless.utils.readFileSync(yamlFilePath);
const options = {
filter: ['relative', 'remote'],
loaderOptions: {
processContent: (res, callback) => {
callback(null, YAML.load(res.text));
},
},
relativeBase: parentDir
};
return resolve(root, options).then((res) => (res.resolved));
}
}
module.exports = YamlParser;