UNPKG

14.9 kBJavaScriptView Raw
1/*
2
3 This file is a part of node-on-train project.
4
5 Copyright (C) Thanh D. Dang <thanhdd.it@gmail.com>
6
7 node-on-train is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 node-on-train is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20*/
21
22
23var fs = require('fs');
24var path = require('path');
25var inflection = require('inflection');
26var root_app = process.cwd();
27var stringHelper = require('../lib/helpers/string_helper.js');
28var migrationTime = require('../lib/helpers/migration_time.js');
29var train_generate = require('./train_generate.js');
30
31module.exports = function() {
32 var model_name = process.argv[4];
33 var model = model_name.toLowerCase();
34 var controller_name = inflection.pluralize(model_name);
35 var model_plural = inflection.pluralize(model);
36 var migration_file_name = migrationTime() + '_create_' + model_plural;
37
38 var model_attrs = "";
39 var migration_attrs = "";
40 var form_html = "";
41 var index_th_html = "";
42 var index_td_html = "";
43 var show_html = "";
44
45 for (var i = 5; i < process.argv.length; i++) {
46 var attr_str = process.argv[i].split(':');
47 var attr_name = stringHelper.toTitleCase(attr_str[0]);
48
49 // model.js
50 model_attrs += '\t' + attr_str[0] + ': {\n';
51 model_attrs += '\t\ttype: Sequelize.' + attr_str[1].toUpperCase() + ',\n';
52 model_attrs += '\t},\n';
53
54 // form.html
55 form_html += '\t<div class="field">\n';
56 var type = 'text';
57 if (attr_str[0] == 'email') {
58 type = 'email';
59 } else if (attr_str[0] == 'password') {
60 type = 'password';
61 } else if (attr_str[1] == 'integer') {
62 type = 'number';
63 }
64
65 var multiline = '';
66 if ( attr_str[1].toLowerCase() == 'text' ) {
67 multiline = 'multiline';
68 }
69
70 form_html += '\t\t<text-field attribute="'+attr_str[0]+'" label="'+attr_name+'" type="'+type+'" '+multiline+'></text-field>\n';
71 form_html += '\t</div>\n';
72
73 // index.html
74 index_th_html += '\t\t<th>' + attr_name + '</th>\n';
75 index_td_html += '\t\t<td>{{ '+ model +'.'+ attr_str[0] +' }}</td>\n';
76
77 // show.html
78 show_html += '<p>\n';
79 show_html += '\t<strong>'+ attr_name +':</strong>\n';
80 show_html += '\t{{ '+ model +'.'+ attr_str[0] +' }}\n';
81 show_html += '</p>\n';
82
83 // migration
84 migration_attrs += "\t\t\t" + attr_str[0] + ": DataTypes." + attr_str[1].toUpperCase() + ",\n";
85 }
86
87 var dir_templates = {
88 'public/partials/controller_name': [
89 { dir_path: 'public/partials/' + model_plural }
90 ]
91 }
92
93 var file_templates = {
94 'app/controllers/controller.js': [
95 {
96 file_path: 'app/controllers/' + model_plural + '_controller.js',
97 info_render: {
98 controller_name: controller_name,
99 model_plural: model_plural,
100 model_name: model_name,
101 model: model
102 }
103 }
104 ],
105 'app/models/model.js': [
106 {
107 file_path: 'app/models/' + model + '.js',
108 info_render: {
109 model_name: model_name,
110 model_attrs: model_attrs,
111 model: model
112 }
113 }
114 ],
115 'db/migrate/migration.js': [
116 {
117 file_path: 'db/migrate/' + migration_file_name + '.js',
118 info_render: {
119 model: model,
120 migration_attrs: migration_attrs
121 }
122 }
123 ],
124 'public/controllers/controller.js': [
125 {
126 file_path: 'public/controllers/' + model_plural + '_controller.js',
127 info_render: {
128 controller_name: controller_name,
129 model_plural: model_plural,
130 model_name: model_name,
131 model: model
132 }
133 }
134 ],
135 'public/services/service.js': [
136 {
137 file_path: 'public/services/' + model + '.js',
138 info_render: {
139 model_plural: model_plural,
140 model_name: model_name,
141 model: model
142 }
143 }
144 ],
145 'public/partials/controller_name/form.html': [
146 {
147 file_path: 'public/partials/' + model_plural + '/form.html',
148 info_render: {
149 model: model,
150 model_name: model_name,
151 model_plural: model_plural,
152 form_html: form_html
153 }
154 }
155 ],
156 'public/partials/controller_name/index.html': [
157 {
158 file_path: 'public/partials/' + model_plural + '/index.html',
159 info_render: {
160 model: model,
161 model_plural: model_plural,
162 model_name: model_name,
163 controller_name: controller_name,
164 index_td_html: index_td_html,
165 index_th_html: index_th_html
166 }
167 }
168 ],
169 'public/partials/controller_name/show.html': [
170 {
171 file_path: 'public/partials/' + model_plural + '/show.html',
172 info_render: {
173 model: model,
174 model_plural: model_plural,
175 show_html: show_html
176 }
177 }
178 ]
179 }
180
181 //--- Edit routes ---//
182 var routes_file = root_app + "/config/routes.js";
183 var routes = require(routes_file);
184 var is_exist = false;
185 for (var k in routes) {
186 if (routes[k]['resources'] && routes[k]['resources'] == model_plural) {
187 is_exist = true;
188 break;
189 }
190 }
191 if (!is_exist) {
192 var routes_content = fs.readFileSync(routes_file).toString();
193 routes_content = routes_content.replace('module.exports = [', "module.exports = [\n\t{ resources: '"+model_plural+"' },");
194 fs.writeFileSync(routes_file, routes_content);
195 }
196 //--- Edit routes ---//
197
198
199 //--- Edit public/app.js ---//
200 var app_file = root_app + "/public/app.js";
201 var app_file_content = fs.readFileSync(app_file).toString();
202 if (app_file_content.indexOf('formFor') < 0) {
203 app_file_content = app_file_content.replace("'ui.router'", "'ui.router',\n\t'formFor',\n\t'formFor.defaultTemplates'");
204 }
205 if (app_file_content.indexOf(model_plural + 'Controller') < 0) {
206 app_file_content = app_file_content.replace("'ui.router'", "'ui.router',\n\t'" + model_plural + "Controller'");
207 }
208 if (app_file_content.indexOf(model + 'Service') < 0) {
209 app_file_content = app_file_content.replace("'ui.router'", "'ui.router',\n\t'" + model + "Service'");
210 }
211 if (app_file_content.indexOf('alertHelper') < 0) {
212 app_file_content = app_file_content.replace("'ui.router'", "'ui.router',\n\t'alertHelper'");
213 }
214 if (app_file_content.indexOf('alertDirective') < 0) {
215 app_file_content = app_file_content.replace("'ui.router'", "'ui.router',\n\t'alertDirective'");
216 }
217 if (app_file_content.indexOf('partials/'+model_plural+'/index.html') < 0) {
218 var state_content = "\t.state('"+model_plural+"', {\n"
219 state_content += "\t\turl: '/"+model_plural+"',\n"
220 state_content += "\t\ttemplateUrl: 'partials/"+model_plural+"/index.html',\n"
221 state_content += "\t\tresolve: {\n"
222 state_content += "\t\t\t"+model_plural+": ['$q', '"+model_name+"', function($q, "+model_name+"){\n"
223 state_content += "\t\t\t\tvar deferred = $q.defer();\n"
224 state_content += "\t\t\t\t"+model_name+".query({}, function("+model_plural+") {\n"
225 state_content += "\t\t\t\t\tdeferred.resolve("+model_plural+");\n"
226 state_content += "\t\t\t\t}, function(error) {\n"
227 state_content += "\t\t\t\t\tdeferred.reject();\n"
228 state_content += "\t\t\t\t});\n"
229 state_content += "\t\t\t\treturn deferred.promise;\n"
230 state_content += "\t\t\t}]\n"
231 state_content += "\t\t},\n"
232 state_content += "\t\tcontroller: '"+controller_name+"Ctrl'\n"
233 state_content += "\t})"
234 app_file_content = app_file_content.replace('\t$stateProvider', '\t$stateProvider\n' + state_content);
235 }
236 if (app_file_content.indexOf('partials/'+model_plural+'/show.html') < 0) {
237 var state_content = "\t.state('"+model+"_detail', {\n"
238 state_content += "\t\turl: '/"+model_plural+"/:id',\n"
239 state_content += "\t\ttemplateUrl: 'partials/"+model_plural+"/show.html',\n"
240 state_content += "\t\tresolve: {\n"
241 state_content += "\t\t\t"+model+": ['$q', '$stateParams', '"+model_name+"', function($q, $stateParams, "+model_name+"){\n"
242 state_content += "\t\t\t\tvar deferred = $q.defer();\n"
243 state_content += "\t\t\t\t"+model_name+".get({id: $stateParams.id}, function("+model+") {\n"
244 state_content += "\t\t\t\t\tdeferred.resolve("+model+");\n"
245 state_content += "\t\t\t\t}, function(error) {\n"
246 state_content += "\t\t\t\t\tdeferred.reject();\n"
247 state_content += "\t\t\t\t});\n"
248 state_content += "\t\t\t\treturn deferred.promise;\n"
249 state_content += "\t\t\t}]\n"
250 state_content += "\t\t},\n"
251 state_content += "\t\tcontroller: '"+controller_name+"DetailCtrl'\n"
252 state_content += "\t})"
253 app_file_content = app_file_content.replace('\t$stateProvider', '\t$stateProvider\n' + state_content);
254 }
255 if (app_file_content.indexOf('partials/'+model_plural+'/form.html') < 0) {
256 var state_content = "\t.state('"+model+"_form', {\n"
257 state_content += "\t\turl: '/"+model_plural+"/save/:id',\n"
258 state_content += "\t\ttemplateUrl: 'partials/"+model_plural+"/form.html',\n"
259 state_content += "\t\tresolve: {\n"
260 state_content += "\t\t\t"+model+": ['$q', '$stateParams', '"+model_name+"', function($q, $stateParams, "+model_name+"){\n"
261 state_content += "\t\t\t\tif ( $stateParams == 'new' ) {\n"
262 state_content += "\t\t\t\t\treturn {"
263 for (var i = 5; i < process.argv.length; i++) {
264 var attr_str = process.argv[i].split(':');
265 if (i == 5)
266 state_content += attr_str[0] + ": ''";
267 else
268 state_content += ", " + attr_str[0] + ": ''";
269 }
270 state_content += "};\n"
271 state_content += "\t\t\t\t} else {\n"
272 state_content += "\t\t\t\t\tvar deferred = $q.defer();\n"
273 state_content += "\t\t\t\t\t"+model_name+".get({id: $stateParams.id}, function("+model+") {\n"
274 state_content += "\t\t\t\t\t\tdeferred.resolve("+model+");\n"
275 state_content += "\t\t\t\t\t}, function(error) {\n"
276 state_content += "\t\t\t\t\t\tdeferred.reject();\n"
277 state_content += "\t\t\t\t\t});\n"
278 state_content += "\t\t\t\t\treturn deferred.promise;\n"
279 state_content += "\t\t\t\t}\n"
280 state_content += "\t\t\t}]\n"
281 state_content += "\t\t},\n"
282 state_content += "\t\tcontroller: '"+model_name+"FormCtrl'\n"
283 state_content += "\t})"
284 app_file_content = app_file_content.replace('\t$stateProvider', '\t$stateProvider\n' + state_content);
285 }
286 fs.writeFileSync(app_file, app_file_content);
287 //--- Edit public/app.js ---//
288
289
290 //--- Edit public/index.html ---//
291 var index_file = root_app + "/public/index.html";
292 var index_file_content = fs.readFileSync(index_file).toString();
293 if (index_file_content.indexOf('form-for.css') < 0) {
294 index_file_content = index_file_content.replace('</title>', '</title>\n\t<link rel="stylesheet" href="../node_modules/angular-form-for/dist/form-for.css">');
295 }
296 if (index_file_content.indexOf('assets/stylesheets/scaffolds.css') < 0) {
297 index_file_content = index_file_content.replace('</title>', '</title>\n\t<link rel="stylesheet" href="assets/stylesheets/scaffolds.css">');
298 }
299 if (index_file_content.indexOf('form-for.default-templates.js') < 0) {
300 index_file_content = index_file_content.replace('<script src="../node_modules/angular-ui-router/release/angular-ui-router.min.js"></script>', '<script src="../node_modules/angular-ui-router/release/angular-ui-router.min.js"></script>\n\t<script src="../node_modules/angular-form-for/dist/form-for.default-templates.js"></script>');
301 }
302 if (index_file_content.indexOf('form-for.min.js') < 0) {
303 index_file_content = index_file_content.replace('<script src="../node_modules/angular-ui-router/release/angular-ui-router.min.js"></script>', '<script src="../node_modules/angular-ui-router/release/angular-ui-router.min.js"></script>\n\t<script src="../node_modules/angular-form-for/dist/form-for.min.js"></script>');
304 }
305 if (index_file_content.indexOf('angular-resource.min.js') < 0) {
306 index_file_content = index_file_content.replace('<script src="../node_modules/angular-ui-router/release/angular-ui-router.min.js"></script>', '<script src="../node_modules/angular-ui-router/release/angular-ui-router.min.js"></script>\n\t<script src="../node_modules/angular-resource/angular-resource.min.js"></script>');
307 }
308 if (index_file_content.indexOf('helpers/alert.js') < 0) {
309 index_file_content = index_file_content.replace('<script src="app.js"></script>', '<script src="helpers/alert.js"></script>\n\t<script src="app.js"></script>');
310 }
311 if (index_file_content.indexOf('controllers/'+model_plural+'_controller.js') < 0) {
312 index_file_content = index_file_content.replace('<script src="app.js"></script>', '<script src="controllers/'+model_plural+'_controller.js"></script>\n\t<script src="app.js"></script>');
313 }
314 if (index_file_content.indexOf('services/'+model+'.js') < 0) {
315 index_file_content = index_file_content.replace('<script src="app.js"></script>', '<script src="services/'+model+'.js"></script>\n\t<script src="app.js"></script>');
316 }
317 if (index_file_content.indexOf('directives/alert.js') < 0) {
318 index_file_content = index_file_content.replace('<script src="app.js"></script>', '<script src="directives/alert.js"></script>\n\t<script src="app.js"></script>');
319 }
320 fs.writeFileSync(index_file, index_file_content);
321 //--- Edit public/index.html ---//
322
323
324 //--- Edit app.js ---//
325 var app_file = root_app + "/app.js";
326 var app_file_content = fs.readFileSync(app_file).toString();
327 if (app_file_content.indexOf('bodyParser.json()') < 0) {
328 if (app_file_content.indexOf('body-parser') < 0) {
329 app_file_content = app_file_content.replace("require('connect');", "require('connect');\nvar bodyParser = require('body-parser');");
330 }
331 app_file_content = app_file_content.replace("connect();", "connect();\napp.use(bodyParser.json());");
332 fs.writeFileSync(app_file, app_file_content);
333 }
334 //--- Edit app.js ---//
335
336
337 //--- Edit package.json ---//
338 var package_file = root_app + "/package.json";
339 var package_file_content = fs.readFileSync(package_file).toString();
340 if (package_file_content.indexOf('body-parser') < 0) {
341 package_file_content = package_file_content.replace('"dependencies": {', '"dependencies": {\n\t\t"body-parser": "*",');
342 }
343 if (package_file_content.indexOf('sequelize') < 0) {
344 package_file_content = package_file_content.replace('"dependencies": {', '"dependencies": {\n\t\t"sequelize": "*",');
345 }
346 if (package_file_content.indexOf('sqlite3') < 0) {
347 package_file_content = package_file_content.replace('"dependencies": {', '"dependencies": {\n\t\t"sqlite3": "*",');
348 }
349 if (package_file_content.indexOf('angular-form-for') < 0) {
350 package_file_content = package_file_content.replace('"dependencies": {', '"dependencies": {\n\t\t"angular-form-for": "4.1.10",');
351 }
352 if (package_file_content.indexOf('angular-resource') < 0) {
353 package_file_content = package_file_content.replace('"dependencies": {', '"dependencies": {\n\t\t"angular-resource": "1.5.0",');
354 }
355 fs.writeFileSync(package_file, package_file_content);
356 //--- Edit package.json ---//
357
358
359 var lib = path.join(path.dirname(fs.realpathSync(__filename)), '../');
360 var path_templ = lib + 'template/scaffold';
361
362 train_generate(path_templ, dir_templates, file_templates);
363}