diff --git a/lib/utils/yamlAstParser.js b/lib/utils/yamlAstParser.js index 91c30404c..6708c70a4 100644 --- a/lib/utils/yamlAstParser.js +++ b/lib/utils/yamlAstParser.js @@ -4,7 +4,6 @@ const fs = require('fs'); const _ = require('lodash'); const os = require('os'); -const astObject = {}; const findKeyChain = (astContent) => { let content = astContent; const chain = [ @@ -19,32 +18,35 @@ const findKeyChain = (astContent) => { return chain.reverse().join('.'); }; -const parseAST = (ymlAstContent) => { +const parseAST = (ymlAstContent, astObject) => { + let newAstObject = astObject || {}; if (ymlAstContent.mappings && _.isArray(ymlAstContent.mappings)) { _.forEach(ymlAstContent.mappings, (v) => { if (v.key.kind === 0 && v.value.kind === 0) { - astObject[findKeyChain(v)] = v.value; + newAstObject[findKeyChain(v)] = v.value; } else if (v.key.kind === 0 && v.value.kind === 2) { - astObject[findKeyChain(v)] = v.value; - parseAST(v.value); + newAstObject[findKeyChain(v)] = v.value; + newAstObject = parseAST(v.value, newAstObject); } else if (v.key.kind === 0 && v.value.kind === 3) { - astObject[findKeyChain(v)] = v.value; - parseAST(v.value); + newAstObject[findKeyChain(v)] = v.value; + newAstObject = parseAST(v.value, newAstObject); } }); } else if (ymlAstContent.items && _.isArray(ymlAstContent.items)) { _.forEach(ymlAstContent.items, (v, i) => { if (v.kind === 0) { const key = `${findKeyChain(ymlAstContent.parent)}[${i}]`; - astObject[key] = v; + newAstObject[key] = v; } }); } + + return newAstObject; }; const addNewArrayItem = (ymlFile, pathInYml, newValue) => { const yamlContent = fs.readFileSync(ymlFile, 'utf8'); - parseAST(yaml.load(yamlContent), astObject); + const astObject = parseAST(yaml.load(yamlContent)); if (astObject[pathInYml] && astObject[pathInYml].items) { let newArray = []; @@ -76,7 +78,7 @@ const addNewArrayItem = (ymlFile, pathInYml, newValue) => { const removeExistingArrayItem = (ymlFile, pathInYml, removeValue) => { const yamlContent = fs.readFileSync(ymlFile, 'utf8'); - parseAST(yaml.load(yamlContent), astObject); + const astObject = parseAST(yaml.load(yamlContent)); if (astObject[pathInYml] && astObject[pathInYml].items) { const newArray = [];