Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
func => func && func.key && func.key.value === name
);
const attributes = new Map();
if (functionNode) {
console.log(`Function "${name}" already found on the configuration file. \
function add command will OVERRIDE the current function`);
functionNode.value = attributes;
} else {
logger.log(`Adding function ${name} into configuration file ${this._path}`);
functionNode = new Pair(new Scalar(name), attributes);
functions.value.items.push(functionNode);
}
if (antFunction instanceof BinFunction) {
attributes.items.push(new Pair(new Scalar('bin'), new Scalar(bin)));
} else {
attributes.items.push(new Pair(new Scalar('handler'), new Scalar(handler)));
attributes.items.push(new Pair(new Scalar('runtime'), new Scalar(
specifyRuntimeVersion ? `${runtime.name} ${runtime.version}` : runtime.name
)));
}
console.log(`Function "${name}" successfully added on configuration file ${this._path}`);
// Document has changed, resets the cached JSON
this._cachedJson = null;
return this;
}
}
let configCategory = templates.value.items.find(item => item.key.value === category);
if (!configCategory) {
configCategory = new Pair(new Scalar(category), new Map());
templates.value.items.push(configCategory);
}
let configTemplate = configCategory.value.items.find(item => item.key.value === template);
logger.log(`Adding template "${template}" with category "${category}" and path \
"${templatePath}" into configuration file ${this._path}`);
if (!configTemplate) {
configTemplate = new Pair(new Scalar(template), new Scalar(templatePath));
configCategory.value.items.push(configTemplate);
} else {
console.log(`Template "${template}" already found on current config. \
template add command will OVERRIDE the current template`);
configTemplate.value = new Scalar(templatePath);
}
console.log(`Template "${template}" successfully added on configuration file ${this._path}`);
// Document has changed, resets the cached JSON
this._cachedJson = null;
return this;
}
// to reach the Map of directives
directives = directives.value;
}
// Given the directives map, we need to find the entry whose key is the name
// of the target directive; either to update it with the new configurations or
// to know if a brand new entry needs to be created.
const directive = directives.items.find(
item => item.key.value === name
);
const resolverAttributes = new Map();
resolverAttributes.items.push(new Pair(new Scalar('handler'), new Scalar(handler)));
resolverAttributes.items.push(new Pair(new Scalar('runtime'), new Scalar(runtime || this.ant.runtimeController.defaultRuntime.name)));
const directiveAttributes = new Map();
directiveAttributes.items.push(new Pair(new Scalar('resolver'), resolverAttributes));
directiveAttributes.items.push(new Pair(new Scalar('definition'), new Scalar(definition)));
if (!directive) {
directives.items.push(new Pair(new Scalar(name), directiveAttributes));
} else {
directive.value = directiveAttributes;
}
return config.save();
}
directives = directives.value;
}
// Given the directives map, we need to find the entry whose key is the name
// of the target directive; either to update it with the new configurations or
// to know if a brand new entry needs to be created.
const directive = directives.items.find(
item => item.key.value === name
);
const resolverAttributes = new Map();
resolverAttributes.items.push(new Pair(new Scalar('handler'), new Scalar(handler)));
resolverAttributes.items.push(new Pair(new Scalar('runtime'), new Scalar(runtime || this.ant.runtimeController.defaultRuntime.name)));
const directiveAttributes = new Map();
directiveAttributes.items.push(new Pair(new Scalar('resolver'), resolverAttributes));
directiveAttributes.items.push(new Pair(new Scalar('definition'), new Scalar(definition)));
if (!directive) {
directives.items.push(new Pair(new Scalar(name), directiveAttributes));
} else {
directive.value = directiveAttributes;
}
return config.save();
}
// If root node was found, but it is an instance of
// an unexpected class, we must replace this node with the
// collection expected
if (rootNode) {
if (!(rootNode instanceof Pair) || !(rootNode.value instanceof collClass)) {
this._config.contents.items = this._config.contents.items.filter(
node => node !== rootNode
);
} else {
return rootNode.value;
}
}
// If root node is unexistant, creates it
const collNode = new collClass();
this._config.contents.items.push(new Pair(
new Scalar(name), collNode
));
return collNode;
}
test('should filter yaml document node by key', () => {
const config = new Config({});
const fooNode = new Pair(
new Scalar('foo'),
new Scalar('/my/foo')
);
const barNode = new Pair(
new Scalar('bar'),
new Scalar('/my/bar')
);
const loremNode = new Pair(
new Scalar('lorem'),
new Scalar('/lorem/ipsum')
);
const map = new Map();
map.items.push(fooNode);
map.items.push(barNode);
map.items.push(loremNode);
const filtered = config._filterNodeFromCollectionByKey(map, 'bar');
expect(filtered).toBe(true);
expect(map.items.length).toBe(2);
expect(map.items.includes(fooNode));
expect(map.items.includes(loremNode));
});
test('should filter yaml document node by key', () => {
const config = new Config({});
const fooNode = new Pair(
new Scalar('foo'),
new Scalar('/my/foo')
);
const barNode = new Pair(
new Scalar('bar'),
new Scalar('/my/bar')
);
const loremNode = new Pair(
new Scalar('lorem'),
new Scalar('/lorem/ipsum')
);
const map = new Map();
map.items.push(fooNode);
map.items.push(barNode);
map.items.push(loremNode);
const filtered = config._filterNodeFromCollectionByKey(map, 'bar');
expect(filtered).toBe(true);
expect(map.items.length).toBe(2);
expect(map.items.includes(fooNode));
expect(map.items.includes(loremNode));
});
_createAttributeMap(attributes) {
const map = new Map();
for(const [key, value] of Object.entries(attributes)) {
if (value) {
let valueNode;
if (value instanceof Array) {
const seq = new Seq();
seq.items = seq.items.concat(value);
valueNode = seq;
} else {
valueNode = new Scalar(value);
}
map.items.push(new Pair(
new Scalar(key),
valueNode
));
}
}
return map;
}
});
expect(map).toBeInstanceOf(Map);
expect(map.items.length).toBe(3);
expect(map.items[0].key).toBeInstanceOf(Scalar);
expect(map.items[0].key.value).toBe('foo');
expect(map.items[0].value).toBeInstanceOf(Scalar);
expect(map.items[0].value.value).toBe('a');
expect(map.items[1].key).toBeInstanceOf(Scalar);
expect(map.items[1].key.value).toBe('bar');
expect(map.items[1].value).toBeInstanceOf(Scalar);
expect(map.items[1].value.value).toBe('b');
expect(map.items[2].key).toBeInstanceOf(Scalar);
expect(map.items[2].key.value).toBe('abc');
expect(map.items[2].value).toBeInstanceOf(Scalar);
expect(map.items[2].value.value).toBe(1);
});