Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
addPlugin(plugin) {
assert(plugin, 'Could not add plugin: param "plugin" is required');
assert(
typeof plugin === 'string',
'Could not add plugin: param "plugin" should be String'
);
let plugins = this._config.contents.items.find(
item => item.key.value === 'plugins'
);
if (!plugins) {
plugins = new Pair(new Scalar('plugins'), new Seq());
this._config.contents.items.push(plugins);
}
if (plugins.value && plugins.value.items && plugins.value.items.find(
item => item.value === plugin
)) {
console.log(`Plugin "${plugin}" already found on current config. \
plugin add command should do nothing`);
return this;
}
logger.log(`Adding plugin ${plugin} into configuration file ${this._path}`);
plugins.value.items.push(new Scalar(plugin));
console.log(`Plugin "${plugin}" successfully added on configuration file ${this._path}`);
// Document has changed, resets the cached JSON
this._cachedJson = null;
return this;
// we must replace it with a Map
pluginSeqItems.splice(pluginSeqItems.indexOf(pluginNode), 1);
const configurationEntry = new Map();
pluginNode = new Map();
pluginNode.items.push(new Pair(new Scalar(plugin), configurationEntry));
pluginSeqItems.push(pluginNode);
return configurationEntry;
}
}
}
// If "force" flag is true, we need to ensure the plugin
// configuration entry
if (force) {
// If "plugins" entry was not found, we must create it
if (!plugins) {
plugins = this._ensureRootCollectionNode('plugins', Seq);
}
// Creates the plugin configuration entry and returns
// the configuration map
const configurationEntry = new Map();
const pluginEntry = new Map();
pluginEntry.items.push(new Pair(
new Scalar(plugin), configurationEntry
));
plugins.items.push(pluginEntry);
return configurationEntry;
}
return null;
}
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);
expect(config._findRootCollectionNode('lorem', Map)).toBe(null);
});
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);
expect(config._findRootCollectionNode('lorem', Map)).toBe(null);
});
_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;
}