From b74bf133dfc4c8c8884a8579dd6902cb104b4dc4 Mon Sep 17 00:00:00 2001 From: Kirill Yakovenko Date: Fri, 3 Jun 2016 16:47:09 +0300 Subject: [PATCH] add react-component blueprint --- .../files/__root__/__path__/__name__.jsx | 9 ++ blueprints/react-component/index.js | 83 +++++++++++++++++++ index.js | 1 - 3 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 blueprints/react-component/files/__root__/__path__/__name__.jsx create mode 100644 blueprints/react-component/index.js diff --git a/blueprints/react-component/files/__root__/__path__/__name__.jsx b/blueprints/react-component/files/__root__/__path__/__name__.jsx new file mode 100644 index 0000000..c265a8f --- /dev/null +++ b/blueprints/react-component/files/__root__/__path__/__name__.jsx @@ -0,0 +1,9 @@ +import React from 'npm:react'; + +<%= prefix %> <%= camelizedModuleName %> <%= contents %> + +<%= camelizedModuleName %>.propTypes = { + +}; + +export default <%= camelizedModuleName %>; diff --git a/blueprints/react-component/index.js b/blueprints/react-component/index.js new file mode 100644 index 0000000..9a310c1 --- /dev/null +++ b/blueprints/react-component/index.js @@ -0,0 +1,83 @@ +/*jshint node:true*/ + +var stringUtil = require('ember-cli-string-utils'); +var pathUtil = require('ember-cli-path-utils'); +var validComponentName = require('ember-cli-valid-component-name'); +var getPathOption = require('ember-cli-get-component-path-option'); +var path = require('path'); + +var normalizeEntityName = require('ember-cli-normalize-entity-name'); + +var templates = { + func: '= props => {\n\n\treturn (\n\t\t
\n\t);\n};', + class: 'extends React.Component {\n\trender() {\n\t\treturn (\n\t\t\t
\n\t\t);\n\t}\n};', + blockless: '= props => \n\t(\n\t\t
\n\t);' +} + +module.exports = { + description: 'Generates a react component. Name must contain a hyphen.', + + availableOptions: [ + { + name: 'type', + type: String, + default: 'class', + aliases:[ + {'func': 'function'}, + {'no-block': 'blockless-function'} + ] + } + ], + + fileMapTokens: function() { + return { + __path__: function(options) { + return 'components'; + } + }; + }, + + normalizeEntityName: function(entityName) { + entityName = normalizeEntityName(entityName); + + return validComponentName(entityName); + }, + + locals: function(attrs) { + options = attrs.entity.options; + var prefix = 'const'; + if (Object.keys(options).indexOf('func') !== -1) { + return { + prefix: prefix, + contents: templates.func + } + } + + if (Object.keys(options).indexOf('no-block') !== -1) { + return { + prefix: prefix, + contents: templates.blockless + } + } + + if (Object.keys(options).indexOf('type') !== -1) { + if (options.type === 'function' || options.type === 'func') { + return { + prefix: prefix, + contents: templates.func + } + } + if (options.type === 'blockless-function' || options.type === 'no-block') { + return { + prefix: prefix, + contents: templates.blockless + } + } + } + + return { + prefix: 'class', + contents: templates.class + }; + } +}; diff --git a/index.js b/index.js index e52774d..4bf8dc6 100644 --- a/index.js +++ b/index.js @@ -6,7 +6,6 @@ var react = require('broccoli-react'); module.exports = { name: 'ember-cli-react', - preprocessTree: function(type, tree) { if (type === 'js') { tree = react(tree, { transform: { es6module: true } } );