/
generator-entity-to-application.ts
342 lines (330 loc) · 13.6 KB
/
generator-entity-to-application.ts
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
import { Command, flags } from '@oclif/command';
import { CLIError } from '@oclif/errors';
import * as inquirer from 'inquirer';
import { join, resolve } from 'path';
import { loadJson, loadPackageJson, runCommand, schematicsCommandBuilder } from '../utils/schematics-command-builder';
import { Prepare } from './prepare';
export class EntityToApplication extends Command {
static aliases = ['entity-to-application', 'entity-to-app', 'entity2app'];
static description = 'Linking the entity to the application, based on the Rucken template';
static flags = {
type: flags.string({ char: 't', description: 'Type(s) of applications.', multiple: true, options: ['web', 'ionic', 'all'] }),
lib: flags.string({ description: 'The name of the library with entity.' }),
coreLib: flags.string({ description: 'The name of the core library with entity.' }),
webLib: flags.string({ description: 'The name of the web library with entity.' }),
ionicLib: flags.string({ description: 'The name of the ionic library with entity.' }),
app: flags.string({ description: 'The name of the application.' }),
webApp: flags.string({ description: 'The name of the web application.' }),
ionicApp: flags.string({ description: 'The name of the ionic application.' }),
webTemplate: flags.string({ description: 'Binding of components for editing an entity to a frontend application on Angular7+ with Bootstrap3', default: '@rucken/schematics:rucken-entity-web-to-app' }),
ionicTemplate: flags.string({ description: 'Binding components for editing an entity to a mobile frontend application on Angular7+ with Ionic4', default: '@rucken/schematics:rucken-entity-ionic-to-app' }),
workspace: flags.string({ char: 'w', description: 'The workspace directory name.' }),
help: flags.help({ char: 'h' })
};
static args = [{ name: 'name' }];
async run() {
process.setMaxListeners(0);
require('events').EventEmitter.defaultMaxListeners = 100;
const { args, flags } = this.parse(EntityToApplication);
const workspace = flags.workspace;
const packageJson: any = loadPackageJson(join(workspace || process.cwd(), 'package.json'));
const angularJson: any = loadJson(join(workspace || process.cwd(), 'angular.json'));
const nxJson: any = loadJson(join(workspace || process.cwd(), 'nx.json'));
if (packageJson === undefined || angularJson === undefined || nxJson === undefined) {
throw new CLIError(`Not found exists nx workspace in "${join(workspace || process.cwd())}"`);
}
try {
let {
name,
type
} = await inquirer.prompt(
[
...(
!args.name ? [{
name: 'name',
message: 'Enter name entity for link:',
type: 'input',
validate: (value: string) => Boolean(value)
}] : []
),
...(
!flags.type ? [{
name: 'type',
message: 'What type(s) of entity would you like to link for the application(s)?',
type: 'checkbox',
choices: ['web', 'ionic', 'all'],
validate: (value: string) => value.length > 0
}] : []
)
]
);
type = type || flags.type;
name = name || args.name;
let result: any;
let coreLibOrg = '';
let webLibOrg = '';
let ionicLibOrg = '';
const lib = flags.lib;
let coreLib = flags.coreLib;
let webLib = flags.webLib;
let ionicLib = flags.ionicLib;
const app = flags.app;
let webApp = flags.webApp;
let ionicApp = flags.ionicApp;
const webTemplate = flags.webTemplate;
const ionicTemplate = flags.ionicTemplate;
// core
if (type.indexOf('web') !== -1 || type.indexOf('ionic') !== -1 || type.indexOf('all') !== -1) {
({ coreLib, result, coreLibOrg } = await this.prepareCore(
name,
(type.indexOf('all') !== -1 && !coreLib && lib) ? lib : coreLib,
angularJson,
nxJson,
result,
coreLibOrg
));
}
// web
if (type.indexOf('web') !== -1 || type.indexOf('all') !== -1) {
({ webLib, webApp, result, webLibOrg } = await this.prepareWeb(
name,
(type.indexOf('all') !== -1 && !webLib && lib) ? lib + '-web' : webLib,
(type.indexOf('all') !== -1 && !webApp && app) ? app : webApp,
angularJson,
nxJson,
result,
webLibOrg,
webTemplate,
coreLib,
coreLibOrg,
workspace
));
({ webLib, webApp, result, webLibOrg } = await this.prepareWebApp(
name,
webLib,
webApp,
angularJson,
nxJson,
result,
webLibOrg,
webTemplate,
coreLib,
coreLibOrg,
workspace
));
}
// ionic
if (type.indexOf('ionic') !== -1 || type.indexOf('all') !== -1) {
({ ionicLib, ionicApp, result, ionicLibOrg } = await this.prepareIonic(
name,
(type.indexOf('all') !== -1 && !ionicLib && lib) ? lib + '-ionic' : ionicLib,
(type.indexOf('all') !== -1 && !ionicApp && app) ? app + '-ionic' : ionicApp,
angularJson,
nxJson,
result,
ionicLibOrg,
ionicTemplate,
coreLib,
coreLibOrg,
workspace
));
({ ionicLib, ionicApp, result, ionicLibOrg } = await this.prepareIonicApp(
name,
ionicLib,
ionicApp,
angularJson,
nxJson,
result,
ionicLibOrg,
ionicTemplate,
coreLib,
coreLibOrg,
workspace
));
}
} catch (error) {
throw new CLIError(error);
}
try {
await Prepare.run([
resolve(join(workspace || process.cwd()))
]);
} catch (error) {
throw new CLIError(error);
}
}
private async prepareIonic(name: string, ionicLib: string | undefined, ionicApp: string | undefined, angularJson: any, nxJson: any, result: any, ionicLibOrg: string, ionicTemplate: string | undefined, coreLib: string | undefined, coreLibOrg: string, workspace: string | undefined) {
if (
!ionicLib ||
!(angularJson['projects'] && angularJson['projects'][ionicLib] ||
nxJson['projects'] && angularJson['projects'][ionicLib])
) {
const choices = nxJson.projects && Object.keys(nxJson.projects).filter(key => nxJson.projects[key].tags && nxJson.projects[key].tags.indexOf('client') !== -1 &&
nxJson.projects[key].tags.indexOf('library') !== -1);
if (choices.length === 0) {
throw new CLIError('Library for link to application with ionic entity not founded!');
}
try {
result = await inquirer.prompt([{
name: 'ionicLib',
message: `Select library used for store linked ionic entity "${name}":`,
type: 'list',
choices,
validate: (value: string) => value.length > 0
}]);
ionicLib = result.ionicLib;
} catch (error) {
throw new CLIError(error);
}
}
ionicLibOrg = ionicLib &&
angularJson['projects'] &&
angularJson['projects'][ionicLib] &&
angularJson['projects'][ionicLib]['root'] &&
angularJson['projects'][ionicLib]['root'].split('/').length > 2 &&
angularJson['projects'][ionicLib]['root'].split('/').filter((key: string, index: number) => index === 1)[0];
return { ionicLib, ionicApp, result, ionicLibOrg };
}
private async prepareIonicApp(name: string, ionicLib: string | undefined, ionicApp: string | undefined, angularJson: any, nxJson: any, result: any, ionicLibOrg: string, ionicTemplate: string | undefined, coreLib: string | undefined, coreLibOrg: string, workspace: string | undefined) {
if (
!ionicApp ||
!(angularJson['projects'] && angularJson['projects'][ionicApp] ||
nxJson['projects'] && angularJson['projects'][ionicApp])
) {
const choices = nxJson.projects && Object.keys(nxJson.projects).filter(key => nxJson.projects[key].tags && nxJson.projects[key].tags.indexOf('client') !== -1 &&
nxJson.projects[key].tags.indexOf('application') !== -1);
if (choices.length === 0) {
throw new CLIError('Application for link to library with ionic entity not founded!');
}
try {
result = await inquirer.prompt([{
name: 'ionicApp',
message: `Select ionic application for link library "${ionicLib}" with entity "${name}"`,
type: 'list',
choices,
validate: (value: string) => value.length > 0
}]);
ionicApp = result.ionicApp;
} catch (error) {
throw new CLIError(error);
}
}
const command = schematicsCommandBuilder(process.cwd(), ionicTemplate || '', [name], {
lib: ionicLib,
org: ionicLibOrg,
entitiesLib: coreLib,
entitiesLibOrg: coreLibOrg,
app: ionicApp,
workspace
});
try {
await runCommand(command, (...args: any[]) => this.debug(...args));
} catch (error) {
throw new CLIError(error);
}
return { ionicLib, ionicApp, result, ionicLibOrg };
}
private async prepareWeb(name: string, webLib: string | undefined, webApp: string | undefined, angularJson: any, nxJson: any, result: any, webLibOrg: string, webTemplate: string | undefined, coreLib: string | undefined, coreLibOrg: string, workspace: string | undefined) {
if (
!webLib ||
!(angularJson['projects'] && angularJson['projects'][webLib] ||
nxJson['projects'] && angularJson['projects'][webLib])
) {
const choices = nxJson.projects && Object.keys(nxJson.projects).filter(key => nxJson.projects[key].tags && nxJson.projects[key].tags.indexOf('client') !== -1 &&
nxJson.projects[key].tags.indexOf('library') !== -1);
if (choices.length === 0) {
throw new CLIError('Library for link to application with web entity not founded!');
}
try {
result = await inquirer.prompt([{
name: 'webLib',
message: `Select web application for link web entity "${name}"`,
type: 'list',
choices,
validate: (value: string) => value.length > 0
}]);
webLib = result.webLib;
} catch (error) {
throw new CLIError(error);
}
}
webLibOrg = webLib &&
angularJson['projects'] &&
angularJson['projects'][webLib] &&
angularJson['projects'][webLib]['root'] &&
angularJson['projects'][webLib]['root'].split('/').length > 2 &&
angularJson['projects'][webLib]['root'].split('/').filter((key: string, index: number) => index === 1)[0];
return { webLib, webApp, result, webLibOrg };
}
private async prepareWebApp(name: string, webLib: string | undefined, webApp: string | undefined, angularJson: any, nxJson: any, result: any, webLibOrg: string, webTemplate: string | undefined, coreLib: string | undefined, coreLibOrg: string, workspace: string | undefined) {
if (
!webApp ||
!(angularJson['projects'] && angularJson['projects'][webApp] ||
nxJson['projects'] && angularJson['projects'][webApp])
) {
const choices = nxJson.projects && Object.keys(nxJson.projects).filter(key => nxJson.projects[key].tags && nxJson.projects[key].tags.indexOf('client') !== -1 &&
nxJson.projects[key].tags.indexOf('application') !== -1);
if (choices.length === 0) {
throw new CLIError('Application for link to library with web entity not founded!');
}
try {
result = await inquirer.prompt([{
name: 'webApp',
message: `Select web application for link library "${webLib}" with entity "${name}"`,
type: 'list',
choices,
validate: (value: string) => value.length > 0
}]);
webApp = result.webApp;
} catch (error) {
throw new CLIError(error);
}
}
const command = schematicsCommandBuilder(process.cwd(), webTemplate || '', [name], {
lib: webLib,
org: webLibOrg,
entitiesLib: coreLib,
entitiesLibOrg: coreLibOrg,
app: webApp,
workspace
});
try {
await runCommand(command, (...args: any[]) => this.debug(...args));
} catch (error) {
throw new CLIError(error);
}
return { webLib, webApp, result, webLibOrg };
}
private async prepareCore(name: string, coreLib: string | undefined, angularJson: any, nxJson: any, result: any, coreLibOrg: string) {
if (
!coreLib ||
!(angularJson['projects'] && angularJson['projects'][coreLib] ||
nxJson['projects'] && angularJson['projects'][coreLib])
) {
const choices = nxJson.projects && Object.keys(nxJson.projects).filter(key => nxJson.projects[key].tags && nxJson.projects[key].tags.indexOf('client') !== -1 &&
nxJson.projects[key].tags.indexOf('library') !== -1);
if (choices.length === 0) {
throw new CLIError('Library for frontend entities not founded!');
}
try {
result = await inquirer.prompt([{
name: 'coreLib',
message: `Select library used for store of entity ${name} for frontend application`,
type: 'list',
choices,
validate: (value: string) => value.length > 0
}]);
coreLib = result.coreLib;
} catch (error) {
throw new CLIError(error);
}
}
coreLibOrg = coreLib &&
angularJson['projects'] &&
angularJson['projects'][coreLib] &&
angularJson['projects'][coreLib]['root'] &&
angularJson['projects'][coreLib]['root'].split('/').length > 2 &&
angularJson['projects'][coreLib]['root'].split('/').filter((key: string, index: number) => index === 1)[0];
return { coreLib, result, coreLibOrg };
}
}