7
7
8
8
const ArtifactGenerator = require ( '../../lib/artifact-generator' ) ;
9
9
const debug = require ( '../../lib/debug' ) ( 'model-generator' ) ;
10
+ const inspect = require ( 'util' ) . inspect ;
10
11
const utils = require ( '../../lib/utils' ) ;
11
12
const chalk = require ( 'chalk' ) ;
12
13
const path = require ( 'path' ) ;
13
14
15
+ const PROMPT_BASE_MODEL_CLASS = 'Please select the model base class' ;
16
+ const ERROR_NO_MODELS_FOUND = 'Model was not found in' ;
17
+ const BASE_MODELS = [ 'Entity' , 'Model' ] ;
18
+ const MODEL_TEMPLATE_PATH = 'model.ts.ejs' ;
19
+
14
20
/**
15
21
* Model Generator
16
22
*
@@ -54,6 +60,17 @@ module.exports = class ModelGenerator extends ArtifactGenerator {
54
60
this . artifactInfo . properties = { } ;
55
61
this . propCounter = 0 ;
56
62
63
+ this . artifactInfo . modelDir = path . resolve (
64
+ this . artifactInfo . rootDir ,
65
+ utils . modelsDir ,
66
+ ) ;
67
+
68
+ this . option ( 'base' , {
69
+ type : String ,
70
+ required : false ,
71
+ description : 'A valid based model' ,
72
+ } ) ;
73
+
57
74
return super . _setupGenerator ( ) ;
58
75
}
59
76
@@ -70,15 +87,82 @@ module.exports = class ModelGenerator extends ArtifactGenerator {
70
87
if ( this . shouldExit ( ) ) return ;
71
88
await super . promptArtifactName ( ) ;
72
89
this . artifactInfo . className = utils . toClassName ( this . artifactInfo . name ) ;
73
- this . log ( ) ;
74
- this . log (
75
- `Let's add a property to ${ chalk . yellow ( this . artifactInfo . className ) } ` ,
76
- ) ;
90
+ }
91
+
92
+ // Ask for Model base class
93
+ async promptModelBaseClassName ( ) {
94
+ const availableModelBaseClasses = [ ] ;
95
+
96
+ availableModelBaseClasses . push ( ...BASE_MODELS ) ;
97
+
98
+ try {
99
+ debug ( `model list dir ${ this . artifactInfo . modelDir } ` ) ;
100
+ const modelList = await utils . getArtifactList (
101
+ this . artifactInfo . modelDir ,
102
+ 'model' ,
103
+ ) ;
104
+ debug ( `modelist ${ modelList } ` ) ;
105
+
106
+ if ( modelList && modelList . length > 0 ) {
107
+ availableModelBaseClasses . push ( ...modelList ) ;
108
+ debug ( `availableModelBaseClasses ${ availableModelBaseClasses } ` ) ;
109
+ }
110
+ } catch ( err ) {
111
+ debug ( `error ${ err } ` ) ;
112
+ return this . exit ( err ) ;
113
+ }
114
+
115
+ if (
116
+ this . options . base &&
117
+ availableModelBaseClasses . includes ( this . options . base )
118
+ ) {
119
+ this . artifactInfo . modelBaseClass = utils . toClassName ( this . options . base ) ;
120
+ } else {
121
+ if ( this . options . base ) {
122
+ // the model specified in the command line does not exists
123
+ return this . exit (
124
+ new Error (
125
+ `${ ERROR_NO_MODELS_FOUND } ${
126
+ this . artifactInfo . modelDir
127
+ } .${ chalk . yellow (
128
+ 'Please visit https://loopback.io/doc/en/lb4/Model-generator.html for information on how models are discovered' ,
129
+ ) } `,
130
+ ) ,
131
+ ) ;
132
+ }
133
+ }
134
+
135
+ return this . prompt ( [
136
+ {
137
+ type : 'list' ,
138
+ name : 'modelBaseClass' ,
139
+ message : PROMPT_BASE_MODEL_CLASS ,
140
+ choices : availableModelBaseClasses ,
141
+ when : ! this . artifactInfo . modelBaseClass ,
142
+ default : availableModelBaseClasses [ 0 ] ,
143
+ validate : utils . validateClassName ,
144
+ } ,
145
+ ] )
146
+ . then ( props => {
147
+ Object . assign ( this . artifactInfo , props ) ;
148
+ debug ( `props after model base class prompt: ${ inspect ( props ) } ` ) ;
149
+ this . log (
150
+ `Let's add a property to ${ chalk . yellow (
151
+ this . artifactInfo . className ,
152
+ ) } `,
153
+ ) ;
154
+ return props ;
155
+ } )
156
+ . catch ( err => {
157
+ debug ( `Error during model base class prompt: ${ err } ` ) ;
158
+ return this . exit ( err ) ;
159
+ } ) ;
77
160
}
78
161
79
162
// Prompt for a Property Name
80
163
async promptPropertyName ( ) {
81
- if ( this . shouldExit ( ) ) return ;
164
+ if ( this . shouldExit ( ) ) return false ;
165
+
82
166
this . log ( `Enter an empty property name when done` ) ;
83
167
this . log ( ) ;
84
168
@@ -200,7 +284,11 @@ module.exports = class ModelGenerator extends ArtifactGenerator {
200
284
this . artifactInfo . outFile ,
201
285
) ;
202
286
203
- const modelTemplatePath = this . templatePath ( 'model.ts.ejs' ) ;
287
+ this . artifactInfo . isModelBaseBuiltin = BASE_MODELS . includes (
288
+ this . artifactInfo . modelBaseClass ,
289
+ )
290
+ ? true
291
+ : false ;
204
292
205
293
// Set up types for Templating
206
294
const TS_TYPES = [ 'string' , 'number' , 'object' , 'boolean' , 'any' ] ;
@@ -253,7 +341,11 @@ module.exports = class ModelGenerator extends ArtifactGenerator {
253
341
}
254
342
} ) ;
255
343
256
- this . fs . copyTpl ( modelTemplatePath , tsPath , this . artifactInfo ) ;
344
+ this . fs . copyTpl (
345
+ this . templatePath ( MODEL_TEMPLATE_PATH ) ,
346
+ tsPath ,
347
+ this . artifactInfo ,
348
+ ) ;
257
349
}
258
350
259
351
async end ( ) {
0 commit comments