|
| 1 | +// Copyright IBM Corp. 2017,2018. All Rights Reserved. |
| 2 | +// Node module: @loopback/cli |
| 3 | +// This file is licensed under the MIT License. |
| 4 | +// License text available at https://opensource.org/licenses/MIT |
| 5 | + |
| 6 | +'use strict'; |
| 7 | + |
| 8 | +const ArtifactGenerator = require('../../lib/artifact-generator'); |
| 9 | +const debug = require('../../lib/debug')('model-generator'); |
| 10 | +const utils = require('../../lib/utils'); |
| 11 | +const chalk = require('chalk'); |
| 12 | +const path = require('path'); |
| 13 | + |
| 14 | +/** |
| 15 | + * Model Generator |
| 16 | + * |
| 17 | + * Prompts for a Model name, Model Base Class (currently defaults to 'Entity'). |
| 18 | + * Creates the Model Class -- currently a one time process only. |
| 19 | + * |
| 20 | + * Will prompt for properties to add to the Model till a blank property name is |
| 21 | + * entered. Will also ask if a property is required, the default value for the |
| 22 | + * property, if it's the ID (unless one has been selected), etc. |
| 23 | + */ |
| 24 | +module.exports = class ModelGenerator extends ArtifactGenerator { |
| 25 | + constructor(args, opts) { |
| 26 | + super(args, opts); |
| 27 | + } |
| 28 | + |
| 29 | + _setupGenerator() { |
| 30 | + this.artifactInfo = { |
| 31 | + type: 'model', |
| 32 | + rootDir: 'src', |
| 33 | + }; |
| 34 | + |
| 35 | + this.artifactInfo.outDir = path.resolve( |
| 36 | + this.artifactInfo.rootDir, |
| 37 | + 'models', |
| 38 | + ); |
| 39 | + |
| 40 | + // Model Property Types |
| 41 | + this.typeChoices = [ |
| 42 | + 'string', |
| 43 | + 'number', |
| 44 | + 'boolean', |
| 45 | + 'object', |
| 46 | + 'array', |
| 47 | + 'date', |
| 48 | + 'buffer', |
| 49 | + 'geopoint', |
| 50 | + 'any', |
| 51 | + ]; |
| 52 | + |
| 53 | + this.artifactInfo.properties = {}; |
| 54 | + this.propCounter = 0; |
| 55 | + |
| 56 | + return super._setupGenerator(); |
| 57 | + } |
| 58 | + |
| 59 | + setOptions() { |
| 60 | + return super.setOptions(); |
| 61 | + } |
| 62 | + |
| 63 | + checkLoopBackProject() { |
| 64 | + return super.checkLoopBackProject(); |
| 65 | + } |
| 66 | + |
| 67 | + async promptArtifactName() { |
| 68 | + await super.promptArtifactName(); |
| 69 | + this.artifactInfo.className = utils.toClassName(this.artifactInfo.name); |
| 70 | + this.log( |
| 71 | + `Let's add a property to ${chalk.yellow(this.artifactInfo.className)}`, |
| 72 | + ); |
| 73 | + } |
| 74 | + |
| 75 | + // Prompt for a property name |
| 76 | + async promptPropertyName() { |
| 77 | + this.log(`Enter an empty property name when done`); |
| 78 | + |
| 79 | + delete this.propName; |
| 80 | + |
| 81 | + const prompts = [ |
| 82 | + { |
| 83 | + name: 'propName', |
| 84 | + message: 'Enter the property name:', |
| 85 | + validate: function(val) { |
| 86 | + if (val) { |
| 87 | + return utils.checkPropertyName(val); |
| 88 | + } else { |
| 89 | + return true; |
| 90 | + } |
| 91 | + }, |
| 92 | + }, |
| 93 | + ]; |
| 94 | + |
| 95 | + const answers = await this.prompt(prompts); |
| 96 | + debug(`propName => ${JSON.stringify(answers)}`); |
| 97 | + if (answers.propName) { |
| 98 | + this.artifactInfo.properties[answers.propName] = {}; |
| 99 | + this.propName = answers.propName; |
| 100 | + } |
| 101 | + return this._promptPropertyInfo(); |
| 102 | + } |
| 103 | + |
| 104 | + // Internal Method. Called when a new property is entered. |
| 105 | + // Prompts the user for more information about the property to be added. |
| 106 | + async _promptPropertyInfo() { |
| 107 | + if (!this.propName) { |
| 108 | + return true; |
| 109 | + } else { |
| 110 | + const prompts = [ |
| 111 | + { |
| 112 | + name: 'type', |
| 113 | + message: 'Property type:', |
| 114 | + type: 'list', |
| 115 | + choices: this.typeChoices, |
| 116 | + }, |
| 117 | + { |
| 118 | + name: 'arrayType', |
| 119 | + message: 'Type of array items:', |
| 120 | + type: 'list', |
| 121 | + choices: this.typeChoices.filter(choice => { |
| 122 | + return choice !== 'array'; |
| 123 | + }), |
| 124 | + when: answers => { |
| 125 | + return answers.type === 'array'; |
| 126 | + }, |
| 127 | + }, |
| 128 | + { |
| 129 | + name: 'id', |
| 130 | + message: 'Is ID field?', |
| 131 | + type: 'confirm', |
| 132 | + default: false, |
| 133 | + when: answers => { |
| 134 | + return ( |
| 135 | + !this.idFieldSet && |
| 136 | + !['array', 'object', 'buffer'].includes(answers.type) |
| 137 | + ); |
| 138 | + }, |
| 139 | + }, |
| 140 | + { |
| 141 | + name: 'required', |
| 142 | + message: 'Required?:', |
| 143 | + type: 'confirm', |
| 144 | + default: false, |
| 145 | + }, |
| 146 | + { |
| 147 | + name: 'default', |
| 148 | + message: `Default value ${chalk.yellow('[leave blank for none]')}:`, |
| 149 | + when: answers => { |
| 150 | + return ![null, 'buffer', 'any'].includes(answers.type); |
| 151 | + }, |
| 152 | + }, |
| 153 | + ]; |
| 154 | + |
| 155 | + const answers = await this.prompt(prompts); |
| 156 | + debug(`propertyInfo => ${JSON.stringify(answers)}`); |
| 157 | + if (answers.default === '') { |
| 158 | + delete answers.default; |
| 159 | + } |
| 160 | + |
| 161 | + Object.assign(this.artifactInfo.properties[this.propName], answers); |
| 162 | + if (answers.id) { |
| 163 | + this.idFieldSet = true; |
| 164 | + } |
| 165 | + |
| 166 | + this.log( |
| 167 | + `Let's add another property to ${chalk.yellow( |
| 168 | + this.artifactInfo.className, |
| 169 | + )}`, |
| 170 | + ); |
| 171 | + return this.promptPropertyName(); |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + scaffold() { |
| 176 | + if (this.shouldExit()) return false; |
| 177 | + |
| 178 | + debug('scaffolding'); |
| 179 | + |
| 180 | + // Data for templates |
| 181 | + this.artifactInfo.fileName = utils.kebabCase(this.artifactInfo.name); |
| 182 | + this.artifactInfo.outFile = `${this.artifactInfo.fileName}.model.ts`; |
| 183 | + |
| 184 | + // Resolved Output Path |
| 185 | + const tsPath = this.destinationPath( |
| 186 | + this.artifactInfo.outDir, |
| 187 | + this.artifactInfo.outFile, |
| 188 | + ); |
| 189 | + |
| 190 | + const modelTemplatePath = this.templatePath('model.ts.ejs'); |
| 191 | + |
| 192 | + // Set up types for Templating |
| 193 | + const TS_TYPES = ['string', 'number', 'object', 'boolean', 'any']; |
| 194 | + const NON_TS_TYPES = ['geopoint', 'date']; |
| 195 | + Object.entries(this.artifactInfo.properties).forEach(([key, val]) => { |
| 196 | + // Default tsType is the type property |
| 197 | + val.tsType = val.type; |
| 198 | + |
| 199 | + // Override tsType based on certain type values |
| 200 | + if (val.type === 'array') { |
| 201 | + if (TS_TYPES.includes(val.arrayType)) { |
| 202 | + val.tsType = `${val.arrayType}[]`; |
| 203 | + } else if (val.type === 'buffer') { |
| 204 | + val.tsType = `Buffer[]`; |
| 205 | + } else { |
| 206 | + val.tsType = `string[]`; |
| 207 | + } |
| 208 | + } else if (val.type === 'buffer') { |
| 209 | + val.tsType = 'Buffer'; |
| 210 | + } |
| 211 | + |
| 212 | + if (NON_TS_TYPES.includes(val.tsType)) { |
| 213 | + val.tsType = 'string'; |
| 214 | + } |
| 215 | + |
| 216 | + if ( |
| 217 | + val.defaultValue && |
| 218 | + NON_TS_TYPES.concat(['string', 'any']).includes(val.type) |
| 219 | + ) { |
| 220 | + val.defaultValue = `'${val.defaultValue}'`; |
| 221 | + } |
| 222 | + |
| 223 | + // Convert Type to include '' for template |
| 224 | + val.type = `'${val.type}'`; |
| 225 | + |
| 226 | + if (!val.required) { |
| 227 | + delete val.required; |
| 228 | + } |
| 229 | + |
| 230 | + if (!val.id) { |
| 231 | + delete val.id; |
| 232 | + } |
| 233 | + }); |
| 234 | + |
| 235 | + this.fs.copyTpl(modelTemplatePath, tsPath, this.artifactInfo); |
| 236 | + } |
| 237 | + |
| 238 | + async end() { |
| 239 | + await super.end(); |
| 240 | + } |
| 241 | +}; |
0 commit comments