-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
86 lines (77 loc) · 2.54 KB
/
index.js
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
/**
* 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'}