Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
}
let functionNode = functions.value && functions.value.items && functions.value.items.find(
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;
}
directives = new Map();
graphQLNode.items.push(new Pair(new Scalar('directives'), directives));
} else {
// Since "directives" is a Pair node, we need to access its value
// 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();
}
);
// 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 not modify the root collection node if already exists', () => {
const config = new Config({});
config._config.contents.items.push(new Pair(
new Scalar('foo'),
new Map()
));
const collectionNode = config._ensureRootCollectionNode('foo', Map);
expect(collectionNode).toBeInstanceOf(Map);
expect(collectionNode.items.length).toBe(0);
expect(config._config.contents.items.length).toBe(1);
expect(config._config.contents.items[0]).toBeInstanceOf(Pair);
expect(config._config.contents.items[0].key).toBeInstanceOf(Scalar);
expect(config._config.contents.items[0].key.value).toBe('foo');
expect(config._config.contents.items[0].value).toBe(collectionNode);
});
addFunction(antFunction, specifyRuntimeVersion = false) {
assert(antFunction, 'Param "antFunction" is required');
assert((antFunction instanceof BinFunction || antFunction instanceof LibFunction),
'Param "antFunction" must be an instance of BinFunction or LibFunction');
const { name, bin, handler, runtime } = antFunction;
let functions = this._config.contents.items.find(
item => item.key.value === 'functions'
);
if (!functions) {
functions = new Pair(
new Scalar('functions'),
new Map()
);
this._config.contents.items.push(functions);
}
let functionNode = functions.value && functions.value.items && functions.value.items.find(
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);
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 find a root collection node by key', () => {
const config = new Config({});
const fooNode = new Pair(
new Scalar('foo'),
new Seq()
);
const barNode = new Pair(
new Scalar('bar'),
new Map()
);
const loremNode = new Pair(
new Scalar('lorem'),
new Pair()
);
config._config.contents.items.push(fooNode);
config._config.contents.items.push(barNode);
config._config.contents.items.push(loremNode);
expect(config._findRootCollectionNode('foo', Seq)).toBe(fooNode.value);
expect(config._findRootCollectionNode('bar', Map)).toBe(barNode.value);
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');
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;
}