From 6d56e0722be629791381b261d53df629b8a37d73 Mon Sep 17 00:00:00 2001 From: Ayush Gupta Date: Fri, 5 Aug 2016 16:27:50 -0400 Subject: [PATCH 1/7] :bug: import plugins for browser --- lib/content-types/merge.js | 10 +++++----- lib/form/scripts.js | 35 ++++++++++++++++++++++++++++------- tests/config/default.js | 3 --- 3 files changed, 33 insertions(+), 15 deletions(-) diff --git a/lib/content-types/merge.js b/lib/content-types/merge.js index a7a5396..915834a 100644 --- a/lib/content-types/merge.js +++ b/lib/content-types/merge.js @@ -14,12 +14,12 @@ 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 diff --git a/lib/form/scripts.js b/lib/form/scripts.js index 07cc2ae..25ad2c1 100644 --- a/lib/form/scripts.js +++ b/lib/form/scripts.js @@ -6,6 +6,7 @@ const fs = require('fs'); const path = require('path'); const _ = require('lodash'); const globalConfig = require('config'); +const exec = require('child_process').execSync; const formJS = fs.readdirSync(path.join(__dirname, 'js')).map(script => { return fs.readFileSync(path.join(__dirname, 'js', script)); }).join('\n\n'); @@ -20,16 +21,36 @@ const rendered = (type) => { const allSettings = {}; const allRepeatables = {}; const inputs = {}; - const dir = _.get(globalConfig, 'content.plugins.directory', []); + let dir = _.get(globalConfig, 'content.plugins.directory', []); const ids = {}; let plugins = type.attributes.map(plugin => { return plugin.type; }); - const existingPlugins = {}; + const existing = {}; + const root = exec('npm root').toString().trim(); + + // 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.length === 0) { + dir = [root]; + } + + // Checks if path to node_modules exists + else if (dir.find(value => { + return value === root; + }).length === 0) { + dir.push(root); + } + + // Adds path of input-plugins to existing dir.forEach((directory) => { fs.readdirSync(directory).forEach((file) => { - if (file.indexOf('input-plugin') >= 0 && !existingPlugins.hasOwnProperty(file)) { - existingPlugins[file] = path.join(directory, file); + if (file.indexOf('input-plugin') >= 0 && !existing.hasOwnProperty(file)) { + existing[file] = path.join(directory, file); } }); }); @@ -40,9 +61,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/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: [], - } } }; From 7bea509c7593a9c69e311dc44f53430adfe6f536 Mon Sep 17 00:00:00 2001 From: Ayush Gupta Date: Fri, 5 Aug 2016 17:39:13 -0400 Subject: [PATCH 2/7] :white_check_mark: adda test for getPlugins --- lib/form/scripts.js | 32 ++------------------------------ lib/util.js | 45 +++++++++++++++++++++++++++++++++++++++++++++ tests/util.js | 15 +++++++++++++++ 3 files changed, 62 insertions(+), 30 deletions(-) create mode 100644 tests/util.js diff --git a/lib/form/scripts.js b/lib/form/scripts.js index 25ad2c1..7728add 100644 --- a/lib/form/scripts.js +++ b/lib/form/scripts.js @@ -5,8 +5,8 @@ const stream = require('stream'); const fs = require('fs'); const path = require('path'); const _ = require('lodash'); +const utils = require('../util'); const globalConfig = require('config'); -const exec = require('child_process').execSync; const formJS = fs.readdirSync(path.join(__dirname, 'js')).map(script => { return fs.readFileSync(path.join(__dirname, 'js', script)); }).join('\n\n'); @@ -21,39 +21,11 @@ const rendered = (type) => { const allSettings = {}; const allRepeatables = {}; const inputs = {}; - let dir = _.get(globalConfig, 'content.plugins.directory', []); const ids = {}; let plugins = type.attributes.map(plugin => { return plugin.type; }); - const existing = {}; - const root = exec('npm root').toString().trim(); - - // 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.length === 0) { - dir = [root]; - } - - // Checks if path to node_modules exists - else if (dir.find(value => { - return value === root; - }).length === 0) { - 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); - } - }); - }); + const existing = utils.getPlugins(globalConfig); plugins = plugins.filter((item, pos, self) => { return self.indexOf(item) === pos; diff --git a/lib/util.js b/lib/util.js index 63628c5..5bee6ff 100644 --- a/lib/util.js +++ b/lib/util.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,47 @@ exports.splitPop = (input, splitter) => { return blocks; }; +/** + * Gets plugins from node_modules and other directories + * + * @param {object} globalConfig - Config 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.length === 0) { + dir = [root]; + } + + // Checks if path to node_modules exists + else if (dir.filter(value => { + return value === root; + }).length === 0) { + 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/util.js b/tests/util.js new file mode 100644 index 0000000..e30cdd9 --- /dev/null +++ b/tests/util.js @@ -0,0 +1,15 @@ +import test from 'ava'; +import util from '../lib/util.js'; +import _ from 'lodash'; +import path from 'path'; + +test('Getplugins works with string', t => { + const input = {}; + _.set(input, 'content.plugins.directory', path.join(process.cwd(), 'fixtures')); + t.is(typeof util.getPlugins(input), 'object'); +}); + +test('Getplugins works with nothing', t => { + const input = {}; + t.is(typeof util.getPlugins(input), 'object'); +}); From aebd14eb619e11cafb80c43df43831f41c2f478e Mon Sep 17 00:00:00 2001 From: Ayush Gupta Date: Mon, 8 Aug 2016 09:33:39 -0400 Subject: [PATCH 3/7] :art: improves structure; changes a condition --- lib/content-types/merge.js | 1 - lib/util.js | 4 ++-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/content-types/merge.js b/lib/content-types/merge.js index 66f44ec..355a682 100644 --- a/lib/content-types/merge.js +++ b/lib/content-types/merge.js @@ -14,7 +14,6 @@ const configPlugins = {}; * * {Object} - [Plugabilly](https://github.com/Snugug/plugabilly) object */ - configPlugins.search = _.get(globalConfig, 'content.plugins.directory', []); if (typeof configPlugins.search === 'string') { configPlugins.search = [configPlugins.search]; diff --git a/lib/util.js b/lib/util.js index 5bee6ff..b8fe543 100644 --- a/lib/util.js +++ b/lib/util.js @@ -55,7 +55,7 @@ exports.splitPop = (input, splitter) => { /** * Gets plugins from node_modules and other directories * - * @param {object} globalConfig - Config object + * @param {object} globalConfig - Configuration object * * @returns {object} - all input plugins with path */ @@ -70,7 +70,7 @@ exports.getPlugins = (globalConfig) => { } // Adds path of node_modules to directory - if (!Array.isArray(dir) || dir.length === 0) { + if (!Array.isArray(dir)) { dir = [root]; } From 1c965df2851499785355a0b8b5eb6af3745189ca Mon Sep 17 00:00:00 2001 From: Ayush Gupta Date: Mon, 8 Aug 2016 11:33:29 -0400 Subject: [PATCH 4/7] :white_check_mark: adds test for getPlugins --- lib/content-types/merge.js | 1 - tests/util.js | 10 ++++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/content-types/merge.js b/lib/content-types/merge.js index 355a682..0385660 100644 --- a/lib/content-types/merge.js +++ b/lib/content-types/merge.js @@ -50,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/tests/util.js b/tests/util.js index e30cdd9..3ab9a74 100644 --- a/tests/util.js +++ b/tests/util.js @@ -3,13 +3,19 @@ import util from '../lib/util.js'; import _ from 'lodash'; import path from 'path'; -test('Getplugins works with string', t => { +test('Getplugins works with directory value as string', t => { const input = {}; _.set(input, 'content.plugins.directory', path.join(process.cwd(), 'fixtures')); t.is(typeof util.getPlugins(input), 'object'); }); -test('Getplugins works with nothing', t => { +test('Getplugins works with directory value as boolean', t => { + const input = {}; + _.set(input, 'content.plugins.directory', true); + t.is(typeof util.getPlugins(input), 'object'); +}); + +test('Getplugins works with directory value as undefined', t => { const input = {}; t.is(typeof util.getPlugins(input), 'object'); }); From 07a111e98c0a9a4028ca5eb90747533d59f3ad89 Mon Sep 17 00:00:00 2001 From: Ayush Gupta Date: Mon, 8 Aug 2016 11:55:14 -0400 Subject: [PATCH 5/7] :art: changes filter to indexOf --- lib/util.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/util.js b/lib/util.js index b8fe543..c1df878 100644 --- a/lib/util.js +++ b/lib/util.js @@ -75,9 +75,7 @@ exports.getPlugins = (globalConfig) => { } // Checks if path to node_modules exists - else if (dir.filter(value => { - return value === root; - }).length === 0) { + else if (dir.indexOf(root) === -1) { dir.push(root); } From 77c7eda973bc26c708be7d06e316b51d40c37d03 Mon Sep 17 00:00:00 2001 From: Ayush Gupta Date: Mon, 8 Aug 2016 11:58:02 -0400 Subject: [PATCH 6/7] :unamused: changes util to utils --- lib/{util.js => utils.js} | 0 tests/{util.js => utils.js} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename lib/{util.js => utils.js} (100%) rename tests/{util.js => utils.js} (100%) diff --git a/lib/util.js b/lib/utils.js similarity index 100% rename from lib/util.js rename to lib/utils.js diff --git a/tests/util.js b/tests/utils.js similarity index 100% rename from tests/util.js rename to tests/utils.js From 7896746f1377badf25fa7483e2fc0ef3a2330c56 Mon Sep 17 00:00:00 2001 From: Ayush Gupta Date: Mon, 8 Aug 2016 12:02:29 -0400 Subject: [PATCH 7/7] :unamused: renaming reference util to utils --- lib/content-types.js | 4 ++-- lib/content-types/load.js | 4 ++-- lib/content-types/only.js | 6 +++--- lib/form/scripts.js | 2 +- lib/form/validate.js | 4 ++-- tests/utils.js | 8 ++++---- 6 files changed, 14 insertions(+), 14 deletions(-) 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/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 7728add..2ccd90b 100644 --- a/lib/form/scripts.js +++ b/lib/form/scripts.js @@ -5,7 +5,7 @@ const stream = require('stream'); const fs = require('fs'); const path = require('path'); const _ = require('lodash'); -const utils = require('../util'); +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)); 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/tests/utils.js b/tests/utils.js index 3ab9a74..b4f1760 100644 --- a/tests/utils.js +++ b/tests/utils.js @@ -1,21 +1,21 @@ import test from 'ava'; -import util from '../lib/util.js'; +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 util.getPlugins(input), 'object'); + 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 util.getPlugins(input), 'object'); + t.is(typeof utils.getPlugins(input), 'object'); }); test('Getplugins works with directory value as undefined', t => { const input = {}; - t.is(typeof util.getPlugins(input), 'object'); + t.is(typeof utils.getPlugins(input), 'object'); });