UNPKG

sails-generate-forestay

Version:

Build dynamic user interfaces quickly and easily! Use the forestay generator to scaffold complete CRUD interfaces using just your SailsJS model attributes.

87 lines (77 loc) 2.61 kB
/** * Module dependencies */ const util = require('util') const path = require('path') const _ = require('lodash') _.str = require('underscore.string') const colors = require('colors') const ncp = require('ncp').ncp /** * sails-generate-forestay * * Usage: * `sails generate forestay (modelname)` * * @description Generates a forestay. * @docs https://sailsjs.com/docs/concepts/extending-sails/generators/custom-generators */ module.exports = { forestay: require('./lib/forestay.js'), before: function (scope, done) { if (_.isUndefined(scope.args[0])) { return done(new Error('Please provide a name for this forestay.')) } if (!_.isString(scope.args[0])) { return done(new Error('Expected a string for `scope.args[0]`, but instead got: ' + util.inspect(scope.args[0], { depth: null }))) } var globalID = _.str.capitalize(scope.args[0]) scope.controllerfile = globalID + 'Controller.js' scope.modelfile = globalID + '.js' scope.upperForestay = globalID scope.lowerForestay = globalID.toLowerCase() console.log('Copying Forestay files (this will not overwrite anything, only create files that do not exist)') ncp(path.resolve(__dirname, './templates/assets/forestay'), './assets/forestay', {clobber: false}, function (err) { if (err) throw err ncp(path.resolve(__dirname, './templates/config'), './config', {clobber: false}, function (err) { if (err) throw err console.log('Creating ' + scope.controllerfile.blue + ' controller and ' + scope.modelfile.blue + ' model') return done() }) }) }, after: function (scope, done) { console.log('That\'s done!') console.log('You\'ll want to add the following code to the '.red + 'config/routes.js'.yellow + ' file.'.red) /* Template for routes.js */ console.log(` "/${scope.lowerForestay}/*": { controller: "${scope.lowerForestay}", action: "forestay", forestay:{ linkName:"${scope.upperForestay}", model:"${scope.upperForestay}", } }, `.blue) return done() }, targets: { './api/controllers/:controllerfile': { template: 'api/controller.js' }, './api/models/:modelfile': { template: 'api/model.js' } }, templatesDirectory: path.resolve(__dirname, './templates') } // './config/forestay.js': { // template: 'config/forestay.js' // }, // './assets/forestay': {folder: {}}, // './assets/forestay/img': {folder: {}}, // './assets/forestay/js': {folder: {}}, // './assets/forestay/img/logo.png': {copy: 'assets/forestay/img/logo.png'}