-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
Copy pathneedle-client-angular.js
250 lines (224 loc) · 11.4 KB
/
needle-client-angular.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
/**
* Copyright 2013-2020 the original author or authors from the JHipster project.
*
* This file is part of the JHipster project, see https://www.jhipster.tech/
* for more information.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const chalk = require('chalk');
const _ = require('lodash');
const needleClientBase = require('./needle-client');
const constants = require('../../generator-constants');
const jhipsterUtils = require('../../utils');
module.exports = class extends needleClientBase {
addGlobalSCSSStyle(style, comment) {
const filePath = `${this.CLIENT_MAIN_SRC_DIR}content/scss/global.scss`;
this.addStyle(style, comment, filePath, 'jhipster-needle-scss-add-main');
}
addVendorSCSSStyle(style, comment) {
const filePath = `${this.CLIENT_MAIN_SRC_DIR}content/scss/vendor.scss`;
super.addStyle(style, comment, filePath, 'jhipster-needle-scss-add-vendor');
}
addModule(appName, angularName, folderName, fileName, enableTranslation, clientFramework) {
const modulePath = `${this.CLIENT_MAIN_SRC_DIR}app/app.module.ts`;
const importNeedle = 'jhipster-needle-angular-add-module-import';
const moduleNeedle = 'jhipster-needle-angular-add-module';
this._genericAddModule(
appName,
angularName,
folderName,
fileName,
enableTranslation,
clientFramework,
modulePath,
importNeedle,
moduleNeedle
);
}
addToAdminModule(appName, adminAngularName, adminFolderName, adminFileName, enableTranslation, clientFramework) {
const adminModulePath = `${this.CLIENT_MAIN_SRC_DIR}app/admin/admin-routing.module.ts`;
const importNeedle = 'jhipster-needle-add-admin-module-import';
const moduleNeedle = 'jhipster-needle-add-admin-module';
this._genericAddModule(
appName,
adminAngularName,
adminFolderName,
adminFileName,
enableTranslation,
clientFramework,
adminModulePath,
importNeedle,
moduleNeedle
);
}
_genericAddModule(
appName,
angularName,
folderName,
fileName,
enableTranslation,
clientFramework,
modulePath,
importNeedle,
moduleNeedle
) {
const errorMessage = `${
chalk.yellow('Reference to ') + angularName + folderName + fileName + enableTranslation + clientFramework
} ${chalk.yellow(`not added to ${modulePath}.\n`)}`;
const importRewriteFileModel = this._generateRewriteFileModelWithImportStatement(
appName,
angularName,
folderName,
fileName,
modulePath,
importNeedle
);
this.addBlockContentToFile(importRewriteFileModel, errorMessage);
const moduleRewriteFileModel = this._generateRewriteFileModelAddModule(appName, angularName, modulePath, moduleNeedle);
this.addBlockContentToFile(moduleRewriteFileModel, errorMessage);
}
_generateRewriteFileModelWithImportStatement(appName, angularName, folderName, fileName, modulePath, needle) {
const importStatement = this._generateImportStatement(appName, angularName, folderName, fileName);
return this.generateFileModel(modulePath, needle, this.generator.stripMargin(importStatement));
}
_generateImportStatement(appName, angularName, folderName, fileName) {
let importStatement = `|import { ${appName}${angularName}Module } from './${folderName}/${fileName}.module';`;
if (importStatement.length > constants.LINE_LENGTH) {
// prettier-ignore
importStatement = `|import {
| ${appName}${angularName}Module
|} from './${folderName}/${fileName}.module';`;
}
return importStatement;
}
_generateRewriteFileModelAddModule(appName, angularName, modulePath, needle) {
return this.generateFileModel(modulePath, needle, this.generator.stripMargin(`|${appName}${angularName}Module,`));
}
addIcon(iconName) {
const iconsPath = `${this.CLIENT_MAIN_SRC_DIR}app/config/font-awesome-icons.ts`;
const iconImport = `fa${this.generator.upperFirstCamelCase(iconName)}`;
if (!jhipsterUtils.checkRegexInFile(iconsPath, new RegExp(`\\b${iconImport}\\b`), this.generator)) {
try {
jhipsterUtils.replaceContent(
{
file: iconsPath,
pattern: /(\r?\n)(\s*)\/\/ jhipster-needle-add-icon-import/g,
content: `\n ${iconImport},\n // jhipster-needle-add-icon-import`,
},
this.generator
);
} catch (e) {
this.generator.log(
chalk.yellow('\nUnable to find ') +
iconsPath +
chalk.yellow(' or other error. Icon imports not updated with icon ') +
iconImport +
chalk.yellow('.\n')
);
this.generator.debug('Error:', e);
}
}
}
addEntityToMenu(
routerName,
enableTranslation,
entityTranslationKeyMenu,
entityTranslationValue = _.startCase(routerName),
jhiPrefix = 'jhi'
) {
const errorMessage = `${chalk.yellow('Reference to ') + routerName} ${chalk.yellow('not added to menu.\n')}`;
const entityMenuPath = `${this.CLIENT_MAIN_SRC_DIR}app/layouts/navbar/navbar.component.html`;
const entityEntry =
// prettier-ignore
this.generator.stripMargin(`|<li>
| <a class="dropdown-item" routerLink="${routerName}" routerLinkActive="active" [routerLinkActiveOptions]="{ exact: true }" (click)="collapseNavbar()">
| <fa-icon icon="asterisk" [fixedWidth]="true"></fa-icon>
| <span${enableTranslation ? ` ${jhiPrefix}Translate="global.menu.entities.${entityTranslationKeyMenu}"` : ''}>${entityTranslationValue}</span>
| </a>
| </li>`);
const rewriteFileModel = this.generateFileModel(entityMenuPath, 'jhipster-needle-add-entity-to-menu', entityEntry);
this.addBlockContentToFile(rewriteFileModel, errorMessage);
}
addElementToMenu(routerName, iconName, enableTranslation, translationKeyMenu = routerName, jhiPrefix = 'jhi') {
const errorMessage = `${chalk.yellow('Reference to ') + routerName} ${chalk.yellow('not added to menu.\n')}`;
const entityMenuPath = `${this.CLIENT_MAIN_SRC_DIR}app/layouts/navbar/navbar.component.html`;
// prettier-ignore
const entityEntry = `<li class="nav-item" routerLinkActive="active" [routerLinkActiveOptions]="{exact: true}">
<a class="nav-link" routerLink="${routerName}" (click)="collapseNavbar()">
<fa-icon icon="${iconName}" [fixedWidth]="true"></fa-icon>
<span${enableTranslation ? ` ${jhiPrefix}Translate="global.menu.${translationKeyMenu}"` : ''}>${_.startCase(routerName)}</span>
</a>
</li>`;
const rewriteFileModel = this.generateFileModel(entityMenuPath, 'jhipster-needle-add-element-to-menu', entityEntry);
this.addBlockContentToFile(rewriteFileModel, errorMessage);
this.addIcon(iconName);
}
addElementToAdminMenu(routerName, iconName, enableTranslation, translationKeyMenu = routerName, jhiPrefix = 'jhi') {
const errorMessage = `${chalk.yellow('Reference to ') + routerName} ${chalk.yellow('not added to admin menu.\n')}`;
const navbarAdminPath = `${this.CLIENT_MAIN_SRC_DIR}app/layouts/navbar/navbar.component.html`;
// prettier-ignore
const entityEntry = `<li>
<a class="dropdown-item" routerLink="${routerName}" routerLinkActive="active" (click)="collapseNavbar()">
<fa-icon icon="${iconName}" [fixedWidth]="true"></fa-icon>
<span${enableTranslation ? ` ${jhiPrefix}Translate="global.menu.admin.${translationKeyMenu}"` : ''}>${_.startCase(routerName)}</span>
</a>
</li>`;
const rewriteFileModel = this.generateFileModel(navbarAdminPath, 'jhipster-needle-add-element-to-admin-menu', entityEntry);
this.addBlockContentToFile(rewriteFileModel, errorMessage);
this.addIcon(iconName);
}
_addRoute(route, modulePath, moduleName, needleName, filePath, pageTitle) {
const isRouteAlreadyAdded = jhipsterUtils.checkStringInFile(filePath, `path: '${route}'`, this.generator);
if (isRouteAlreadyAdded) {
return;
}
const errorMessage = `${chalk.yellow('Route ') + route + chalk.yellow(` not added to ${filePath}.\n`)}`;
let pageTitleTemplate = '';
if (pageTitle) {
pageTitleTemplate = `
| data: { pageTitle: '${pageTitle}' },`;
}
const routingEntry = this.generator.stripMargin(
`{
| path: '${route}',${pageTitleTemplate}
| loadChildren: () => import('${modulePath}').then(m => m.${moduleName}),
| },`
);
const rewriteFileModel = this.generateFileModel(filePath, needleName, routingEntry);
this.addBlockContentToFile(rewriteFileModel, errorMessage);
}
addEntityToModule(entityAngularName, entityFolderName, entityFileName, entityUrl, microserviceName, pageTitle) {
const entityModulePath = `${this.CLIENT_MAIN_SRC_DIR}app/entities/entity-routing.module.ts`;
try {
const isSpecificEntityAlreadyGenerated = jhipsterUtils.checkStringInFile(
entityModulePath,
`path: '${entityUrl}'`,
this.generator
);
if (!isSpecificEntityAlreadyGenerated) {
const modulePath = `./${entityFolderName}/${entityFileName}.module`;
const moduleName = microserviceName
? `${this.generator.upperFirstCamelCase(microserviceName)}${entityAngularName}Module`
: `${entityAngularName}Module`;
this._addRoute(entityUrl, modulePath, moduleName, 'jhipster-needle-add-entity-route', entityModulePath, pageTitle);
}
} catch (e) {
this.generator.debug('Error:', e);
}
}
addAdminRoute(route, modulePath, moduleName, pageTitle) {
const adminModulePath = `${this.CLIENT_MAIN_SRC_DIR}app/admin/admin-routing.module.ts`;
this._addRoute(route, modulePath, moduleName, 'jhipster-needle-add-admin-route', adminModulePath, pageTitle);
}
};