From 7c753ee518d4a89606a6cfc05ae7b6b97ea575d2 Mon Sep 17 00:00:00 2001 From: Belen Curcio Date: Wed, 5 Jul 2017 16:41:52 -0300 Subject: [PATCH] create-plugin --- bin/cli-plugins | 51 ++++++++++++++++++- client/coral-framework/helpers/plugins.js | 5 +- package.json | 1 + scripts/templates/plugin/client/.babelrc | 14 +++++ .../templates/plugin/client/.eslintrc.json | 23 +++++++++ scripts/templates/plugin/client/index.js | 20 ++++++++ .../templates/plugin/client/translations.yml | 15 ++++++ scripts/templates/plugin/index.js | 1 + 8 files changed, 127 insertions(+), 3 deletions(-) create mode 100644 scripts/templates/plugin/client/.babelrc create mode 100644 scripts/templates/plugin/client/.eslintrc.json create mode 100644 scripts/templates/plugin/client/index.js create mode 100644 scripts/templates/plugin/client/translations.yml create mode 100644 scripts/templates/plugin/index.js diff --git a/bin/cli-plugins b/bin/cli-plugins index b1dee381b..6dc127697 100755 --- a/bin/cli-plugins +++ b/bin/cli-plugins @@ -8,13 +8,14 @@ // https://yarnpkg.com/ const program = require('./commander'); +const inquirer = require('inquirer'); // Make things colorful! require('colors'); const emoji = require('node-emoji'); const dir = process.cwd(); -const fs = require('fs'); +const fs = require('fs-extra'); const path = require('path'); const spawn = require('cross-spawn'); const semver = require('semver'); @@ -272,10 +273,58 @@ async function reconcilePluginDeps({skipLocal, skipRemote, dryRun, upgradeRemote console.log(`✨ Done in ${totalTime}s.`); } +async function createSeedPlugin() { + const pluginsDir = path.join(dir, 'plugins'); + + function pluginNameExists(pluginName) { + const pluginNames = fs.readdirSync(pluginsDir); + return !!pluginNames + .filter((pn) => pn === pluginName).length; + } + + let answers = await inquirer.prompt([ + { + type: 'input', + name: 'pluginName', + message: 'Plugin Name: i.e talk-plugin-permalink', + validate: (input) => { + + if (pluginNameExists(input)) { + return 'Please, choose another name. That name already exists'; + } + + if (input && input.length > 0) { + return true; + } + + return 'Plugin Name is required.'; + } + } + ]); + + // Creating plugin seed + + const seedTemplatePlugin = path.join(dir, 'scripts/templates/plugin'); + const newPluginPath = path.join(pluginsDir, answers.pluginName); + + try { + fs.copySync(seedTemplatePlugin, newPluginPath); + + console.log('Success!'); + } catch (err) { + console.error(err); + } +} + //============================================================================== // Setting up the program command line arguments. //============================================================================== +program + .command('create') + .description('creates a seed plugin') + .action(createSeedPlugin); + program .command('list') .description('') diff --git a/client/coral-framework/helpers/plugins.js b/client/coral-framework/helpers/plugins.js index ab5e3080e..fcd0e6db6 100644 --- a/client/coral-framework/helpers/plugins.js +++ b/client/coral-framework/helpers/plugins.js @@ -15,9 +15,9 @@ function getSlotComponents(slot) { // Filter out components that have been disabled in `plugin_config` return flatten(plugins - + // Filter out components that have been disabled in `plugin_config` - .filter((o) => !pluginConfig || !pluginConfig[o.plugin] || !pluginConfig[o.plugin].disable_components) + .filter((o) => o.module.slots && (!pluginConfig || !pluginConfig[o.plugin] || !pluginConfig[o.plugin].disable_components)) .filter((o) => o.module.slots[slot]) .map((o) => o.module.slots[slot])); @@ -78,6 +78,7 @@ export function getSlotsFragments(slots) { } const components = uniq(flattenDeep(slots.map((slot) => { return plugins + .filter((o) => o.module.slots) .filter((o) => o.module.slots[slot]) .map((o) => o.module.slots[slot]); }))); diff --git a/package.json b/package.json index 382846cef..218381896 100644 --- a/package.json +++ b/package.json @@ -78,6 +78,7 @@ "express": "^4.15.2", "express-session": "^1.15.1", "form-data": "^2.1.2", + "fs-extra": "^3.0.1", "gql-merge": "^0.0.4", "graphql": "^0.9.1", "graphql-errors": "^2.1.0", diff --git a/scripts/templates/plugin/client/.babelrc b/scripts/templates/plugin/client/.babelrc new file mode 100644 index 000000000..60be246eb --- /dev/null +++ b/scripts/templates/plugin/client/.babelrc @@ -0,0 +1,14 @@ +{ + "presets": [ + "es2015" + ], + "plugins": [ + "add-module-exports", + "transform-class-properties", + "transform-decorators-legacy", + "transform-object-assign", + "transform-object-rest-spread", + "transform-async-to-generator", + "transform-react-jsx" + ] +} \ No newline at end of file diff --git a/scripts/templates/plugin/client/.eslintrc.json b/scripts/templates/plugin/client/.eslintrc.json new file mode 100644 index 000000000..9fe56bd14 --- /dev/null +++ b/scripts/templates/plugin/client/.eslintrc.json @@ -0,0 +1,23 @@ +{ + "env": { + "browser": true, + "es6": true, + "mocha": true + }, + "parserOptions": { + "sourceType": "module", + "ecmaFeatures": { + "experimentalObjectRestSpread": true, + "jsx": true + } + }, + "parser": "babel-eslint", + "plugins": [ + "react" + ], + "rules": { + "react/jsx-uses-react": "error", + "react/jsx-uses-vars": "error", + "no-console": ["warn", { "allow": ["warn", "error"] }] + } +} diff --git a/scripts/templates/plugin/client/index.js b/scripts/templates/plugin/client/index.js new file mode 100644 index 000000000..931cd722c --- /dev/null +++ b/scripts/templates/plugin/client/index.js @@ -0,0 +1,20 @@ + +/** + This is a client index example file and it should look like this: + + ``` + import LoveButton from './components/LoveButton'; + + export default { + slots: { + commentReactions: [LoveButton] + }, + reducer, + translations + }; + ``` + + To read more info on how to build client plugins. Please, go to: https://coralproject.github.io/talk/plugins-client.html + */ + +export default {}; diff --git a/scripts/templates/plugin/client/translations.yml b/scripts/templates/plugin/client/translations.yml new file mode 100644 index 000000000..d8407b801 --- /dev/null +++ b/scripts/templates/plugin/client/translations.yml @@ -0,0 +1,15 @@ +# +# This file is for translations and they should look like this: +# +# +# ``` +# en: +# coral-plugin-respect: +# respect: Respect +# respected: Respected +# es: +# coral-plugin-respect: +# respect: Respetar +# respected: Respetado +# ``` +# diff --git a/scripts/templates/plugin/index.js b/scripts/templates/plugin/index.js new file mode 100644 index 000000000..f053ebf79 --- /dev/null +++ b/scripts/templates/plugin/index.js @@ -0,0 +1 @@ +module.exports = {};