forked from jhipster/generator-jhipster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script-base.js
307 lines (269 loc) · 12.7 KB
/
script-base.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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
97
98
99
100
101
102
103
104
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
131
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
'use strict';
var path = require('path'),
util = require('util'),
yeoman = require('yeoman-generator'),
jhipsterUtils = require('./util.js'),
shelljs = require('shelljs'),
Insight = require('insight'),
html = require("html-wiring"),
ejs = require('ejs');
module.exports = Generator;
function Generator() {
yeoman.generators.NamedBase.apply(this, arguments);
this.env.options.appPath = this.config.get('appPath') || 'src/main/webapp';
}
util.inherits(Generator, yeoman.generators.NamedBase);
Generator.prototype.addJavaScriptToIndex = function (script) {
try {
var fullPath = 'src/main/webapp/index.html';
jhipsterUtils.rewriteFile({
file: fullPath,
needle: '<!-- endbuild -->',
splicable: [
'<script src="scripts/' + script + '"></script>'
]
});
} catch (e) {
console.log('\nUnable to find '.yellow + fullPath + '. Reference to '.yellow + script + '.js ' + 'not added.\n'.yellow);
}
};
Generator.prototype.addComponentsScriptToIndex = function (script) {
try {
var appPath = this.env.options.appPath;
var fullPath = path.join(appPath, 'index.html');
jhipsterUtils.rewriteFile({
file: fullPath,
needle: '<!-- endbuild -->',
splicable: [
'<script src="scripts/components/entities/' + script + '"></script>'
]
});
} catch (e) {
console.log('\nUnable to find '.yellow + fullPath + '. Reference to '.yellow + script + '.js ' + 'not added.\n'.yellow);
}
};
Generator.prototype.addMessageformatLocaleToIndex = function (script) {
try {
var appPath = this.env.options.appPath;
var fullPath = path.join(appPath, 'index.html');
jhipsterUtils.rewriteFile({
file: fullPath,
needle: '<!-- endbuild -->',
splicable: [
'<script src="bower_components/messageformat/locale/' + script + '"></script>'
]
});
} catch (e) {
console.log('\nUnable to find '.yellow + fullPath + '. Reference to '.yellow + script + '.js ' + 'not added.\n'.yellow);
}
};
Generator.prototype.addRouterToMenu = function (routerName, enableTranslation) {
try {
var fullPath = 'src/main/webapp/scripts/components/navbar/navbar.html';
jhipsterUtils.rewriteFile({
file: fullPath,
needle: '<!-- JHipster will add entities to the menu here -->',
splicable: [
'<li ui-sref-active="active" ><a ui-sref="' + routerName + '" data-toggle="collapse" data-target=".navbar-collapse.in"><span class="glyphicon glyphicon-asterisk"></span>\n' +
'  <span ' + ( enableTranslation ? 'translate="global.menu.entities.' + routerName + '"':'' ) + '>' + routerName + '</span></a></li>'
]
});
} catch (e) {
console.log('\nUnable to find '.yellow + fullPath + '. Reference to '.yellow + routerName + '.js ' + 'not added.\n'.yellow);
}
};
Generator.prototype.addChangelogToLiquibase = function (changelogName) {
console.log("add changelog");
try {
var fullPath = 'src/main/resources/config/liquibase/master.xml';
jhipsterUtils.rewriteFile({
file: fullPath,
needle: '<!-- JHipster will add liquibase changelogs here -->',
splicable: [
'<include file="classpath:config/liquibase/changelog/' + changelogName + '.xml" relativeToChangelogFile="false"/>'
]
});
} catch (e) {
console.log('\nUnable to find '.yellow + fullPath + '. Reference to '.yellow + changelogName + '.js ' + 'not added.\n'.yellow);
}
};
// This generates a date to be used by Liquibase changelogs
Generator.prototype.dateFormatForLiquibase = function () {
var now = new Date();
var now_utc = new Date(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(), now.getUTCHours(), now.getUTCMinutes(), now.getUTCSeconds());
var year = "" + now_utc.getFullYear();
var month = "" + (now_utc.getMonth() + 1); if (month.length == 1) { month = "0" + month; }
var day = "" + now_utc.getDate(); if (day.length == 1) { day = "0" + day; }
var hour = "" + now_utc.getHours(); if (hour.length == 1) { hour = "0" + hour; }
var minute = "" + now_utc.getMinutes(); if (minute.length == 1) { minute = "0" + minute; }
var second = "" + now_utc.getSeconds(); if (second.length == 1) { second = "0" + second; }
return year + "" + month + "" + day + "" + hour + "" + minute + "" + second;
};
Generator.prototype.installI18nFilesByLanguage = function (_this, webappDir, resourceDir, lang) {
this.copyI18nFilesByName(_this, webappDir, 'activate.json', lang);
this.copyI18nFilesByName(_this, webappDir, 'audits.json', lang);
this.copyI18nFilesByName(_this, webappDir, 'configuration.json', lang);
this.copyI18nFilesByName(_this, webappDir, 'error.json', lang);
this.copyI18nFilesByName(_this, webappDir, 'login.json', lang);
this.copyI18nFilesByName(_this, webappDir, 'logs.json', lang);
this.copyI18nFilesByName(_this, webappDir, 'main.json', lang);
this.copyI18nFilesByName(_this, webappDir, 'metrics.json', lang);
this.copyI18nFilesByName(_this, webappDir, 'password.json', lang);
this.copyI18nFilesByName(_this, webappDir, 'register.json', lang);
this.copyI18nFilesByName(_this, webappDir, 'sessions.json', lang);
this.copyI18nFilesByName(_this, webappDir, 'settings.json', lang);
this.copyI18nFilesByName(_this, webappDir, 'reset.json', lang);
this.copyI18nFilesByName(_this, webappDir, 'user.management.json', lang);
// tracker.json for Websocket
if (this.websocket == 'spring-websocket') {
this.copyI18nFilesByName(_this, webappDir, 'tracker.json', lang);
}
if (this.enableSocialSignIn) {
this.copyI18nFilesByName(_this, webappDir, 'social.json', lang);
}
// Templates
_this.template(webappDir + '/i18n/' + lang + '/_global.json', webappDir + 'i18n/' + lang + '/global.json', this, {});
_this.template(webappDir + '/i18n/' + lang + '/_health.json', webappDir + 'i18n/' + lang + '/health.json', this, {});
// Template the message server side properties
var lang_prop = lang.replace(/-/g, "_");
_this.template(resourceDir + '/i18n/_messages_' + lang_prop + '.properties', resourceDir + 'i18n/messages_' + lang_prop + '.properties', this, {});
};
Generator.prototype.copyI18nFilesByName = function(_this, webappDir, fileToCopy, lang) {
_this.copy(webappDir + '/i18n/' + lang + '/' + fileToCopy, webappDir + '/i18n/' + lang + '/' + fileToCopy);
};
Generator.prototype.installNewLanguage = function(language) {
var appPath = this.env.options.appPath;
var fullPath = path.join(appPath, 'scripts/components/language/language.service.js');
try {
jhipsterUtils.rewriteFile({
file: fullPath,
needle: '//JHipster will add new languages here',
splicable: [
',\'' + language + '\''
]
});
} catch (e) {
console.log('\nUnable to find '.yellow + fullPath + '. Reference to '.yellow + language + 'not added as a new language. Check if you have enabled translation support.\n'.yellow);
}
};
Generator.prototype.addNewEntityToMenu = function(language, key, value) {
var appPath = this.env.options.appPath;
var fullPath = path.join(appPath, 'i18n/' + language + '/global.json');
try {
jhipsterUtils.rewriteFile({
file: fullPath,
needle: '"additionalEntity": "JHipster will add additional entities here (do not translate!)"',
splicable: [
'"' + key + '": "' + value + '",'
]
});
} catch (e) {
console.log('\nUnable to find '.yellow + fullPath + '. Reference to '.yellow + language + 'not added as a new entity in the menu.\n'.yellow);
}
};
Generator.prototype.insight = function () {
var pkg = require('./package.json');
var insight = new Insight({
trackingCode: 'UA-46075199-2',
packageName: pkg.name,
packageVersion: pkg.version
});
return insight;
}
Generator.prototype.copyHtml = function (source, dest, data, _opt, template) {
_opt = _opt !== undefined ? _opt : {};
data = data !== undefined ? data : this;
if (this.enableTranslation) {
// uses template method instead of copy if template boolean is set as true
template ? this.template(source, dest, data, _opt) : this.copy(source, dest);
} else {
var regex = '( translate\="([a-zA-Z0-9](\.)?)+")|( translate-values\="\{([a-zA-Z]|\d|\:|\{|\}|\[|\]|\-|\'|\s|\.)*?\}")';
//looks for something like translate="foo.bar.message" and translate-values="{foo: '{{ foo.bar }}'}"
var body = this.stripContent(source, regex, data, _opt);
body = this.replacePlaceholders(body, data);
this.write(dest, body);
}
}
Generator.prototype.copyJs = function (source, dest, data, _opt, template) {
_opt = _opt !== undefined ? _opt : {};
data = data !== undefined ? data : this;
if (this.enableTranslation) {
// uses template method instead of copy if template boolean is set as true
template ? this.template(source, dest, data, _opt) : this.copy(source, dest);
} else {
var regex = '[a-zA-Z]+\:(\s)?\[[ \'a-zA-Z0-9\$\,\(\)\{\}\n\.\<\%\=\>\;\s]*\}\]';
//looks for something like mainTranslatePartialLoader: [*]
var body = this.stripContent(source, regex, data, _opt);
body = this.replaceTitle(body, data, template);
this.write(dest, body);
}
}
Generator.prototype.stripContent = function (source, regex, data, _opt) {
var re = new RegExp(regex, 'g');
var that=this;
var body = html.readFileAsString(path.join(that.sourceRoot(), source));
this.engine = require('ejs').render;
//temp hack to fix error thrown by ejs during entity creation, this needs a permanent fix when we add more .ejs files
_opt.filename = path.join(that.sourceRoot(), "src/main/webapp/app/ng_validators.ejs");
body = this.engine(body, data, _opt);
body = body.replace(re, '');
return body;
}
Generator.prototype.replaceTitle = function (body, data, template) {
var re = /pageTitle[\s]*:[\s]*[\'|\"]([a-zA-Z0-9\.\-\_]+)[\'|\"]/g;
var match;
while (match = re.exec(body)) {
// match is now the next match, in array form and our key is at index 1, index 1 is replace target.
var key = match[1], target = key;
var jsonData = this.geti18nJson(key, data);
var keyValue = jsonData !== undefined ? this.deepFind(jsonData, key) : undefined;
body = body.replace(target, keyValue!== undefined ? keyValue : this.baseName);
}
return body;
}
Generator.prototype.replacePlaceholders = function (body, data) {
var re = /placeholder=[\'|\"]([\{]{2}[\'|\"]([a-zA-Z0-9\.\-\_]+)[\'|\"][\s][\|][\s](translate)[\}]{2})[\'|\"]/g;
var match;
while (match = re.exec(body)) {
// match is now the next match, in array form and our key is at index 2, index 1 is replace target.
var key = match[2], target = match[1];
var jsonData = this.geti18nJson(key, data);
var keyValue = jsonData !== undefined ? this.deepFind(jsonData, key, true) : undefined; // dirty fix to get placeholder as it is not in proper json format, name has a dot in it. Assuming that all placeholders are in similar format
body = body.replace(target, keyValue!== undefined ? keyValue : '');
}
return body;
}
Generator.prototype.geti18nJson = function (key, data, template) {
var that = this,
i18nDirectory = 'src/main/webapp/i18n/en/',
filename = i18nDirectory + key.split('.')[0] + '.json',
keyValue, render = template;
if (!shelljs.test('-f', path.join(that.sourceRoot(), filename))) {
filename = i18nDirectory + '_' +key.split('.')[0] + '.json';
render = true;
}
try {
var file = html.readFileAsString(path.join(that.sourceRoot(), filename));
this.engine = require('ejs').render;
file = render ? this.engine(file, data, {}) : file;
return JSON.parse(file);
} catch (err) {
// 'Error reading translation file!'
return undefined;
}
}
Generator.prototype.deepFind = function (obj, path, placeholder) {
var paths = path.split('.'), current=obj, i;
if(placeholder){// dirty fix for placeholders, the json files needs to be corrected
paths[paths.length-2] = paths[paths.length-2] + '.' + paths[paths.length-1];
paths.pop();
}
for (i = 0; i < paths.length; ++i) {
if (current[paths[i]] == undefined) {
return undefined;
} else {
current = current[paths[i]];
}
}
return current;
}