Skip to content

Commit

Permalink
Merge pull request #81 from ayush2k/bugfix/modules
Browse files Browse the repository at this point in the history
fixes import plugins for browser
  • Loading branch information
scottnath authored Aug 8, 2016
2 parents 79ea5ba + 7896746 commit ad71a26
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 30 deletions.
4 changes: 2 additions & 2 deletions lib/content-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 `);
Expand Down
4 changes: 2 additions & 2 deletions lib/content-types/load.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');


/**
Expand Down Expand Up @@ -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;
};
Expand Down
10 changes: 4 additions & 6 deletions lib/content-types/merge.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = [];
Expand Down
6 changes: 3 additions & 3 deletions lib/content-types/only.js
Original file line number Diff line number Diff line change
@@ -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) => {
Expand Down Expand Up @@ -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;
Expand Down
17 changes: 5 additions & 12 deletions lib/form/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -20,29 +21,21 @@ 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;
});

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 = [];
Expand Down
4 changes: 2 additions & 2 deletions lib/form/validate.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const util = require('../util');
const utils = require('../utils');

/*
* @typedef FormInputValues
Expand Down Expand Up @@ -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 = {};

Expand Down
43 changes: 43 additions & 0 deletions lib/util.js → lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
*
Expand Down
3 changes: 0 additions & 3 deletions tests/config/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,5 @@ const path = require('path');
module.exports = {
content: {
directory: './fixtures/content-types',
plugins: {
directory: [],
}
}
};
21 changes: 21 additions & 0 deletions tests/utils.js
Original file line number Diff line number Diff line change
@@ -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');
});

0 comments on commit ad71a26

Please sign in to comment.