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 inflection = require('inflection');
|
26 | var root_app = process.cwd();
|
27 | var stringHelper = require('../lib/helpers/string_helper.js');
|
28 | var train_generate = require('./train_generate.js');
|
29 |
|
30 | module.exports = function() {
|
31 | var model_name = process.argv[4];
|
32 | var model = model_name.toLowerCase();
|
33 | var controller_name = inflection.pluralize(model_name);
|
34 | var model_plural = inflection.pluralize( stringHelper.CamelCase2Underscore( process.argv[4] ) );
|
35 | var file_model_name = stringHelper.CamelCase2Underscore( process.argv[4] );
|
36 | var model_module = stringHelper.Underscore2CamelCaseWithOutTitleCase( process.argv[4] ) + "Service";
|
37 |
|
38 | var controller_actions = '';
|
39 | var service_resources = '';
|
40 | if ( process.argv.length < 6 ) {
|
41 | controller_actions += '\tthis.index = function(req, res, next) {\n';
|
42 | controller_actions += '\t};\n';
|
43 | controller_actions += '\tthis.create = function(req, res, next) {\n';
|
44 | controller_actions += '\t};\n';
|
45 | controller_actions += '\tthis.show = function(req, res, next) {\n';
|
46 | controller_actions += '\t};\n';
|
47 | controller_actions += '\tthis.update = function(req, res, next) {\n';
|
48 | controller_actions += '\t};\n';
|
49 | controller_actions += '\tthis.destroy = function(req, res, next) {\n';
|
50 | controller_actions += '\t};\n';
|
51 | service_resources += "\t\t'get': {method: 'GET'},\n";
|
52 | service_resources += "\t\t'create': {method: 'POST'},\n";
|
53 | service_resources += "\t\t'update': {method: 'PUT'},\n";
|
54 | service_resources += "\t\t'query': {method: 'GET', isArray:true},\n";
|
55 | service_resources += "\t\t'delete': {method: 'DELETE'},\n";
|
56 | } else {
|
57 | for (var i = 5; i < process.argv.length; i++) {
|
58 | var action = process.argv[i];
|
59 | controller_actions += '\tthis.'+action+' = function(req, res, next) {\n';
|
60 | controller_actions += '\t};\n';
|
61 |
|
62 | if (action == 'show') {
|
63 | service_resources += "\t\t'get': {method: 'GET'},\n";
|
64 | } else if (action == 'create') {
|
65 | service_resources += "\t\t'create': {method: 'POST'},\n";
|
66 | } else if (action == 'update') {
|
67 | service_resources += "\t\t'update': {method: 'PUT'},\n";
|
68 | } else if (action == 'destroy') {
|
69 | service_resources += "\t\t'delete': {method: 'DELETE'},\n";
|
70 | } else if (action == 'index') {
|
71 | service_resources += "\t\t'query': {method: 'GET', isArray:true},\n";
|
72 | }
|
73 | }
|
74 | }
|
75 |
|
76 | var file_templates = {
|
77 | 'app/controllers/controller.js': [
|
78 | {
|
79 | file_path: 'app/controllers/' + model_plural + '_controller.js',
|
80 | info_render: {
|
81 | controller_name: controller_name,
|
82 | model_plural: model_plural,
|
83 | model_name: model_name,
|
84 | model: model,
|
85 | controller_actions: controller_actions
|
86 | }
|
87 | }
|
88 | ],
|
89 | 'public/services/service.js': [
|
90 | {
|
91 | file_path: 'public/services/' + file_model_name + '.js',
|
92 | info_render: {
|
93 | model_plural: model_plural,
|
94 | model_name: model_name,
|
95 | model_module: model_module,
|
96 | service_resources: service_resources
|
97 | }
|
98 | }
|
99 | ]
|
100 | }
|
101 |
|
102 |
|
103 | var routes_file = root_app + "/config/routes.js";
|
104 | var routes = require(routes_file);
|
105 | var is_exist = false;
|
106 | for (var k in routes) {
|
107 | if (routes[k]['resources'] && routes[k]['resources'] == model_plural) {
|
108 | is_exist = true;
|
109 | break;
|
110 | }
|
111 | }
|
112 | if (!is_exist) {
|
113 | var routes_content = fs.readFileSync(routes_file).toString();
|
114 | routes_content = routes_content.replace('module.exports = [', "module.exports = [\n\t{ resources: '"+model_plural+"' },");
|
115 | fs.writeFileSync(routes_file, routes_content);
|
116 | }
|
117 |
|
118 |
|
119 |
|
120 | var app_file = root_app + "/public/app.js";
|
121 | var app_file_content = fs.readFileSync(app_file).toString();
|
122 | if (app_file_content.indexOf(model_module) < 0) {
|
123 | app_file_content = app_file_content.replace("'ui.router'", "'ui.router',\n\t'" + model_module + "'");
|
124 | }
|
125 | fs.writeFileSync(app_file, app_file_content);
|
126 |
|
127 |
|
128 |
|
129 | var index_file = root_app + "/public/index.html";
|
130 | var index_file_content = fs.readFileSync(index_file).toString();
|
131 | if (index_file_content.indexOf('angular-resource.min.js') < 0) {
|
132 | 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>');
|
133 | }
|
134 | if (index_file_content.indexOf('services/'+file_model_name+'.js') < 0) {
|
135 | index_file_content = index_file_content.replace('<script src="app.js"></script>', '<script src="services/'+file_model_name+'.js"></script>\n\t<script src="app.js"></script>');
|
136 | }
|
137 | fs.writeFileSync(index_file, index_file_content);
|
138 |
|
139 |
|
140 |
|
141 | var package_file = root_app + "/package.json";
|
142 | var package_file_content = fs.readFileSync(package_file).toString();
|
143 | if (package_file_content.indexOf('angular-resource') < 0) {
|
144 | package_file_content = package_file_content.replace('"dependencies": {', '"dependencies": {\n\t\t"angular-resource": "1.5.0",');
|
145 | }
|
146 | fs.writeFileSync(package_file, package_file_content);
|
147 |
|
148 |
|
149 | var lib = path.join(path.dirname(fs.realpathSync(__filename)), '../');
|
150 | var path_templ = lib + 'template/service';
|
151 |
|
152 | train_generate(path_templ, null, file_templates);
|
153 | }
|