generator-swiftserver
Version:
Generator for Kitura REST webservice servers
130 lines (113 loc) • 3.77 kB
JavaScript
/*
* Copyright IBM Corporation 2016-2017
*
* 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
*
* http://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.
*/
'use strict'
var debug = require('debug')('generator-swiftserver:model')
var Generator = require('yeoman-generator')
var actions = require('../lib/actions')
var helpers = require('../lib/helpers')
var validateRequiredName = helpers.validateRequiredName
var validateNewModel = helpers.validateNewModel
var convertModelNametoSwiftClassname = helpers.convertModelNametoSwiftClassname
module.exports = Generator.extend({
constructor: function () {
Generator.apply(this, arguments)
// Allow user to pass the model name into the generator directly
this.argument('name', {
desc: 'Name of the model to create.',
required: false,
type: String
})
this.option('skip-build', {
type: Boolean,
desc: 'Skip building the generated application',
defaults: false
})
},
initializing: {
ensureInProject: actions.ensureInProject,
ensureProjectIsCrud: actions.ensureProjectIsCrud,
initModelName: function () {
this.skipPromptingAppName = false
if (this.options.name) {
// User passed a desired model name as an argument
var validation = validateRequiredName(this.options.name)
if (validation === true) {
// Desired model name is valid, skip prompting for it later
debug('Valid model name provided via command-line argument:', this.options.name)
this.name = this.options.name
this.skipPromptingModelName = true
} else {
// Log reason for validation failure, if provided
validation = validation || 'Model name not valid'
this.log(validation)
}
}
}
},
prompting: {
promptModelName: function () {
if (this.skipPromptingModelName) { return }
var prompts = [
{
name: 'name',
message: 'Enter the model name:',
default: this.name,
validate: validateNewModel
}
]
return this.prompt(prompts).then((props) => {
this.name = props.name
})
},
promptPlural: function () {
var prompts = [
{
name: 'plural',
message: 'Custom plural form (used to build REST URL):',
default: this.name + 's'
}
]
// TODO needs to handle plurals properly (check for function)
return this.prompt(prompts).then((props) => {
this.plural = props.plural
})
}
},
property: function () {
this.classname = convertModelNametoSwiftClassname(this.name)
// Create a model object
this.model = {
name: this.name,
plural: this.plural,
classname: this.classname,
properties: {
'id': {
'type': 'string',
'id': true
}
}
}
this.log('Let\'s add some ' + this.name + ' properties now.\n')
this.log('Enter an empty property name when done.')
var propertyGenerator = this.options.testmode ? 'swiftserver:property' : require.resolve('../property')
this.composeWith(propertyGenerator, {
apic: this.options.apic,
repeatMultiple: true,
model: this.model,
'skip-build': this.options['skip-build']
})
}
})