Class: InitReact
- Inherits:
-
Object
- Object
- InitReact
- Defined in:
- lib/generators/init_react.rb
Instance Method Summary collapse
- #add_gitignore ⇒ Object
- #add_react_wrapper ⇒ Object
- #create_app_jsx ⇒ Object
- #create_babelrc ⇒ Object
- #create_bootstrap_jsx ⇒ Object
- #create_package_json ⇒ Object
- #create_webpack_config ⇒ Object
- #init ⇒ Object
-
#initialize(app_name) ⇒ InitReact
constructor
A new instance of InitReact.
Constructor Details
#initialize(app_name) ⇒ InitReact
Returns a new instance of InitReact.
4 5 6 7 |
# File 'lib/generators/init_react.rb', line 4 def initialize app_name @app_name = app_name FileUtils::mkdir_p "#{@app_name}/app/jsx" end |
Instance Method Details
#add_gitignore ⇒ Object
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/generators/init_react.rb', line 37 def add_gitignore gitignore = "node_modules\napp/assets/javascripts/react-app.js\n" File.open("#{@app_name}/.gitignore", "a+") do |f| f.write(gitignore) end end |
#add_react_wrapper ⇒ Object
98 99 100 101 102 103 |
# File 'lib/generators/init_react.rb', line 98 def add_react_wrapper file_name = "#{@app_name}/app/views/layouts/application.html.erb" text = File.read(file_name) new_contents = text.gsub(/<%= yield %>/, '<div id="react-wrapper"></div>') File.open(file_name, "w") {|f| f.write(new_contents)} end |
#create_app_jsx ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/generators/init_react.rb', line 48 def create_app_jsx app_jsx = "import getMuiTheme from \"material-ui/styles/getMuiTheme\";\nimport MuiThemeProvider from \"material-ui/styles/MuiThemeProvider\";\n\nexport default class App extends React.Component {\n render() {\n return (\n <MuiThemeProvider muiTheme={getMuiTheme()}>\n <mui.Drawer open={true}>\n <mui.MenuItem>FAQ</mui.MenuItem>\n </mui.Drawer>\n </MuiThemeProvider>\n );\n }\n}\n" File.open("#{@app_name}/app/jsx/app.jsx", "w") do |f| f.write(app_jsx) end end |
#create_babelrc ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/generators/init_react.rb', line 19 def create_babelrc babelrc = "{\npresets: [\n \"es2017\",\n \"react\",\n],\n\"plugins\": [\n \"transform-class-properties\",\n],\n}\n" File.open("#{@app_name}/out.txt", "w") do |f| f.write(babelrc) end end |
#create_bootstrap_jsx ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/generators/init_react.rb', line 71 def create_bootstrap_jsx bootstrap_jsx = "import ReactDOM from \"react-dom\";\nimport {Router, Route, Link, browserHistory} from \"react-router\"\n\nimport injectTapEventPlugin from \"react-tap-event-plugin\";\ninjectTapEventPlugin();\n\nimport App from \"./app\";\n\nconst router = (\n<Router history={browserHistory}>\n <Route path=\"/\" component={App}>\n </Route>\n</Router>\n);\n\n$(document).on(\"ready page:load\", function() {\n ReactDOM.render(router, document.getElementById(\"react-wrapper\"));\n});\n" File.open("#{@app_name}/app/jsx/bootstrap.jsx", "w") do |f| f.write(bootstrap_jsx) end end |
#create_package_json ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/generators/init_react.rb', line 105 def create_package_json package_json = "{\n\"private\": true,\n\"dependencies\": {\n \"babel-core\": \"^6.18.2\",\n \"babel-loader\": \"^6.2.7\",\n \"babel-preset-es2017\": \"^1.4.0\",\n \"babel-preset-react-hmre\": \"^1.1.1\",\n \"babel-preset-react\": \"^6.16.0\",\n \"material-ui\": \"^0.16.0\",\n \"react\": \"^15.4.0\",\n \"react-addons-update\": \"^15.4.0\",\n \"react-dom\": \"^15.4.0\",\n \"react-router\": \"^3.0.0\",\n \"react-tap-event-plugin\": \"^2.0.0\",\n \"react-toastr\": \"^2.8.0\",\n \"webpack\": \"~1.13.3\"\n}\n}\n" File.open("#{@app_name}/package.json", "w") do |f| f.write(package_json) end end |
#create_webpack_config ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/generators/init_react.rb', line 132 def create_webpack_config webpack_config = "var webpack = require(\"webpack\");\n\nconst paths = {\njs: \"./app/assets/javascripts/\",\njsx: \"./app/jsx/\",\n}\n\nvar alias = {\n\"react/lib/CSSPropertyOperations\": \"react-dom/lib/CSSPropertyOperations\"\n};\n\nmodule.exports = {\nentry: paths.jsx + \"bootstrap.jsx\",\noutput: {\n path: paths.js,\n filename: \"react-app.js\",\n},\nresolve: {\n extensions: ['', '.js', '.jsx'],\n alias: alias,\n},\nmodule: {\n loaders: [{\n test: /\\.jsx?$/,\n loaders: ['babel?cacheDirectory'],\n }]\n},\nplugins: [\n new webpack.ProvidePlugin({\n React: \"react\",\n mui: \"material-ui\",\n })\n],\n}\n" File.open("#{@app_name}/webpack.config.js", "w") do |f| f.write(webpack_config) end end |
#init ⇒ Object
9 10 11 12 13 14 15 16 17 |
# File 'lib/generators/init_react.rb', line 9 def init create_babelrc add_gitignore create_app_jsx create_bootstrap_jsx add_react_wrapper create_package_json create_webpack_config end |