diff --git a/lib/content-types.js b/lib/content-types.js index 9af5a14..16e0cb4 100644 --- a/lib/content-types.js +++ b/lib/content-types.js @@ -5,7 +5,7 @@ const _ = require('lodash'); const loader = require('./content-types/load'); const merge = require('./content-types/merge'); const only = require('./content-types/only'); -const util = require('./util'); +const utils = require('./utils'); /** * Get single Content Type @@ -16,7 +16,7 @@ const util = require('./util'); * @returns {object} content type object */ const findCT = (id, types) => { - const ct = util.singleItem('id', id, types); + const ct = utils.singleItem('id', id, types); if (ct === false) { throw new Error(`Content type ${id} not found `); diff --git a/lib/content-types/load.js b/lib/content-types/load.js index b5c4bc9..cd3d370 100644 --- a/lib/content-types/load.js +++ b/lib/content-types/load.js @@ -5,7 +5,7 @@ const yaml = require('js-yaml'); const slugify = require('lodash/kebabCase'); const globalConfig = require('config'); const path = require('path'); -const util = require('../util'); +const utils = require('../utils'); /** @@ -111,7 +111,7 @@ const names = types => { * @returns {array} array of content type's attribute objects */ const attributes = (id, types) => { - const filtered = util.singleItem('id', id, types); + const filtered = utils.singleItem('id', id, types); return filtered ? filtered[0].attributes : false; }; diff --git a/lib/content-types/merge.js b/lib/content-types/merge.js index b573b03..0385660 100644 --- a/lib/content-types/merge.js +++ b/lib/content-types/merge.js @@ -14,12 +14,11 @@ const configPlugins = {}; * * {Object} - [Plugabilly](https://github.com/Snugug/plugabilly) object */ -if (globalConfig.hasOwnProperty('content')) { - if (globalConfig.content.hasOwnProperty('plugins')) { - configPlugins.search = globalConfig.content.plugins.directory; - } +configPlugins.search = _.get(globalConfig, 'content.plugins.directory', []); +if (typeof configPlugins.search === 'string') { + configPlugins.search = [configPlugins.search]; } -const plugins = plugabilly(configPlugins).name().containsSync('input-plugin-'); +const plugins = plugabilly(_.cloneDeep(configPlugins)).name().containsSync('input-plugin-'); /* * Determine required level @@ -51,7 +50,6 @@ const squish = (types) => { if (!Array.isArray(types)) { reject(new Error('Content types must be an array')); } - const configured = types.map(type => { const mergedType = type; const ids = []; diff --git a/lib/content-types/only.js b/lib/content-types/only.js index c370515..f16b860 100644 --- a/lib/content-types/only.js +++ b/lib/content-types/only.js @@ -1,7 +1,7 @@ 'use strict'; const merge = require('deepmerge'); -const util = require('../util'); +const utils = require('../utils'); const only = (type, config) => { return new Promise((resolve) => { @@ -30,8 +30,8 @@ const only = (type, config) => { inputs.forEach(input => { instance[input].validation = attribute.inputs[0][input].validation; instance[input].type = attribute.inputs[0][input].type; - instance[input].id = `${util.splitPop(attribute.inputs[0][input].id, '--')}--${index}`; - instance[input].name = `${util.splitPop(attribute.inputs[0][input].name, '--')}--${index}`; + instance[input].id = `${utils.splitPop(attribute.inputs[0][input].id, '--')}--${index}`; + instance[input].name = `${utils.splitPop(attribute.inputs[0][input].name, '--')}--${index}`; }); return instance; diff --git a/lib/form/scripts.js b/lib/form/scripts.js index 07cc2ae..2ccd90b 100644 --- a/lib/form/scripts.js +++ b/lib/form/scripts.js @@ -5,6 +5,7 @@ const stream = require('stream'); const fs = require('fs'); const path = require('path'); const _ = require('lodash'); +const utils = require('../utils'); const globalConfig = require('config'); const formJS = fs.readdirSync(path.join(__dirname, 'js')).map(script => { return fs.readFileSync(path.join(__dirname, 'js', script)); @@ -20,19 +21,11 @@ const rendered = (type) => { const allSettings = {}; const allRepeatables = {}; const inputs = {}; - const dir = _.get(globalConfig, 'content.plugins.directory', []); const ids = {}; let plugins = type.attributes.map(plugin => { return plugin.type; }); - const existingPlugins = {}; - dir.forEach((directory) => { - fs.readdirSync(directory).forEach((file) => { - if (file.indexOf('input-plugin') >= 0 && !existingPlugins.hasOwnProperty(file)) { - existingPlugins[file] = path.join(directory, file); - } - }); - }); + const existing = utils.getPlugins(globalConfig); plugins = plugins.filter((item, pos, self) => { return self.indexOf(item) === pos; @@ -40,9 +33,9 @@ const rendered = (type) => { plugins.forEach(plugin => { // Require Plugins for Browserify - if (existingPlugins.hasOwnProperty(`input-plugin-${plugin}`)) { - b.require(existingPlugins[`input-plugin-${plugin}`]); - allPlugins[plugin] = existingPlugins[`input-plugin-${plugin}`]; + if (existing.hasOwnProperty(`input-plugin-${plugin}`)) { + b.require(existing[`input-plugin-${plugin}`]); + allPlugins[plugin] = existing[`input-plugin-${plugin}`]; } }); const resolvedPlugins = []; diff --git a/lib/form/validate.js b/lib/form/validate.js index 02c1ae6..1c17954 100644 --- a/lib/form/validate.js +++ b/lib/form/validate.js @@ -1,6 +1,6 @@ 'use strict'; -const util = require('../util'); +const utils = require('../utils'); /* * @typedef FormInputValues @@ -230,7 +230,7 @@ const validate = (raw, type) => { const values = buildValues(working); working = working.map(value => { const result = value; - const plugin = util.singleItem('id', value.plugin, type.attributes); + const plugin = utils.singleItem('id', value.plugin, type.attributes); const input = {}; const settings = {}; diff --git a/lib/util.js b/lib/utils.js similarity index 55% rename from lib/util.js rename to lib/utils.js index 63628c5..c1df878 100644 --- a/lib/util.js +++ b/lib/utils.js @@ -6,6 +6,10 @@ 'use strict'; const util = require('util'); +const exec = require('child_process').execSync; +const fs = require('fs'); +const path = require('path'); +const _ = require('lodash'); /** * Filters an array of objects based on the value of a key in the objects @@ -48,6 +52,45 @@ exports.splitPop = (input, splitter) => { return blocks; }; +/** + * Gets plugins from node_modules and other directories + * + * @param {object} globalConfig - Configuration object + * + * @returns {object} - all input plugins with path + */ +exports.getPlugins = (globalConfig) => { + const existing = {}; + const root = exec('npm root').toString().trim(); + let dir = _.get(globalConfig, 'content.plugins.directory', []); + + // Check if configuration is set to string + if (typeof dir === 'string') { + dir = [dir]; + } + + // Adds path of node_modules to directory + if (!Array.isArray(dir)) { + dir = [root]; + } + + // Checks if path to node_modules exists + else if (dir.indexOf(root) === -1) { + dir.push(root); + } + + // Adds path of input-plugins to existing + dir.forEach((directory) => { + fs.readdirSync(directory).forEach((file) => { + if (file.indexOf('input-plugin') >= 0 && !existing.hasOwnProperty(file)) { + existing[file] = path.join(directory, file); + } + }); + }); + + return existing; +}; + /** * Pretty prints big objects * diff --git a/tests/config/default.js b/tests/config/default.js index a12f2ca..a0f05e5 100644 --- a/tests/config/default.js +++ b/tests/config/default.js @@ -4,8 +4,5 @@ const path = require('path'); module.exports = { content: { directory: './fixtures/content-types', - plugins: { - directory: [], - } } }; diff --git a/tests/utils.js b/tests/utils.js new file mode 100644 index 0000000..b4f1760 --- /dev/null +++ b/tests/utils.js @@ -0,0 +1,21 @@ +import test from 'ava'; +import utils from '../lib/utils.js'; +import _ from 'lodash'; +import path from 'path'; + +test('Getplugins works with directory value as string', t => { + const input = {}; + _.set(input, 'content.plugins.directory', path.join(process.cwd(), 'fixtures')); + t.is(typeof utils.getPlugins(input), 'object'); +}); + +test('Getplugins works with directory value as boolean', t => { + const input = {}; + _.set(input, 'content.plugins.directory', true); + t.is(typeof utils.getPlugins(input), 'object'); +}); + +test('Getplugins works with directory value as undefined', t => { + const input = {}; + t.is(typeof utils.getPlugins(input), 'object'); +});