1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | var fs = require('fs');
|
24 | var path = require('path');
|
25 | var root_app = process.cwd();
|
26 | var stringHelper = require('../lib/helpers/string_helper.js');
|
27 | var train_generate = require('./train_generate.js');
|
28 |
|
29 | module.exports = function() {
|
30 | var controller_module = stringHelper.Underscore2CamelCaseWithOutTitleCase( process.argv[4] ) + "Controller";
|
31 | var controller_module_name = stringHelper.Underscore2CamelCaseWithTitleCase( process.argv[4] );
|
32 | var controller_name = stringHelper.CamelCase2Underscore( process.argv[4] );
|
33 |
|
34 | var controller_js = '';
|
35 | var controller_test_js = '';
|
36 | var action_templates = [];
|
37 | for (var i = 5; i < process.argv.length; i++) {
|
38 | var action = process.argv[i];
|
39 | if (action != '--no-test-framework') {
|
40 | var controller_action = controller_module_name + stringHelper.toTitleCase(action) + "Ctrl";
|
41 |
|
42 | controller_js += controller_module + '.controller(\n';
|
43 | controller_js += "\t'"+controller_action+"',\n";
|
44 | controller_js += "\t['$scope', function ($scope) {\n";
|
45 | controller_js += "\t}]\n";
|
46 | controller_js += ");\n";
|
47 |
|
48 | var state_url = controller_name + "/" + action.toLowerCase();
|
49 | controller_test_js += "\tit('should get "+action.toLowerCase()+"', function() {\n";
|
50 | controller_test_js += "\t\tvar current_url = 'http://localhost:1337/#/" + state_url + "';\n";
|
51 | controller_test_js += "\t\tbrowser.get(current_url);\n";
|
52 | controller_test_js += "\t\texpect(browser.getCurrentUrl()).toContain('#/" + state_url + "');\n";
|
53 | controller_test_js += "\t\texpect( element(by.css('body')).getText() ).not.toEqual('');\n";
|
54 | controller_test_js += "\t});\n";
|
55 |
|
56 | action_templates.push({
|
57 | file_path: 'public/partials/' + controller_name + '/' + action +'.html',
|
58 | info_render: {
|
59 | controller_module_name: controller_module_name,
|
60 | action: action,
|
61 | controller_name: controller_name
|
62 | }
|
63 | });
|
64 |
|
65 |
|
66 | var app_file = root_app + "/public/app.js";
|
67 | var app_file_content = fs.readFileSync(app_file).toString();
|
68 | if (app_file_content.indexOf(controller_module) < 0) {
|
69 | app_file_content = app_file_content.replace("'ui.router'", "'ui.router',\n\t'" + controller_module + "'");
|
70 | }
|
71 | if (app_file_content.indexOf('partials/'+controller_name+'/'+action+'.html') < 0) {
|
72 | var state_content = "\t.state('"+controller_name+"_"+action+"', {\n"
|
73 | state_content += "\t\turl: '/"+controller_name+"/"+action+"',\n"
|
74 | state_content += "\t\ttemplateUrl: 'partials/"+controller_name+"/"+action+".html',\n"
|
75 | state_content += "\t\tcontroller: '"+controller_action+"'\n"
|
76 | state_content += "\t})"
|
77 | app_file_content = app_file_content.replace('\t$stateProvider', '\t$stateProvider\n' + state_content);
|
78 | }
|
79 |
|
80 | fs.writeFileSync(app_file, app_file_content);
|
81 |
|
82 | }
|
83 | }
|
84 |
|
85 | var dir_templates = {
|
86 | 'public/partials/controller_name': [
|
87 | { dir_path: 'public/partials/' + controller_name }
|
88 | ]
|
89 | };
|
90 |
|
91 | var file_templates = {
|
92 | 'public/controllers/controller.js': [
|
93 | {
|
94 | file_path: 'public/controllers/' + controller_name + '_controller.js',
|
95 | info_render: {
|
96 | controller_js: controller_js,
|
97 | controller_module: controller_module
|
98 | }
|
99 | }
|
100 | ],
|
101 | 'public/partials/controller_name/action.html': action_templates,
|
102 | 'public/test/e2e_test/controllers/controller_test.js': []
|
103 | };
|
104 |
|
105 | if (process.argv.indexOf('--no-test-framework') < 0) {
|
106 | file_templates['public/test/e2e_test/controllers/controller_test.js'] = [
|
107 | {
|
108 | file_path: 'public/test/e2e_test/controllers/' + controller_name + '_controller_test.js',
|
109 | info_render: {
|
110 | controller_test_js: controller_test_js,
|
111 | controller_module: controller_module
|
112 | }
|
113 | }
|
114 | ];
|
115 | }
|
116 |
|
117 |
|
118 | var index_file = root_app + "/public/index.html";
|
119 | var index_file_content = fs.readFileSync(index_file).toString();
|
120 | if (index_file_content.indexOf('controllers/'+controller_name+'_controller.js') < 0) {
|
121 | index_file_content = index_file_content.replace('<script src="app.js"></script>', '<script src="controllers/'+controller_name+'_controller.js"></script>\n\t<script src="app.js"></script>');
|
122 | }
|
123 | fs.writeFileSync(index_file, index_file_content);
|
124 |
|
125 |
|
126 |
|
127 | var lib = path.join(path.dirname(fs.realpathSync(__filename)), '../');
|
128 | var path_templ = lib + 'template/controller';
|
129 |
|
130 | train_generate(path_templ, dir_templates, file_templates);
|
131 | }
|