forked from angular-fullstack/generator-angular-fullstack
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
118 lines (104 loc) · 3.16 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
'use strict';
var path = require('path');
var yeoman = require('yeoman-generator');
var util = require('util');
var ngUtil = require('../util');
var ScriptBase = require('../script-base.js');
var Generator = module.exports = function Generator() {
ScriptBase.apply(this, arguments);
};
util.inherits(Generator, ScriptBase);
Generator.prototype.prompting = function askFor() {
var done = this.async();
var name = this.name;
var base = this.config.get('routesBase') || '/api/';
if(base.charAt(base.length-1) !== '/') {
base = base + '/';
}
// pluralization defaults to true for backwards compat
if (this.config.get('pluralizeRoutes') !== false) {
name = name + 's';
}
var self = this;
var prompts = [
{
name: 'route',
message: 'What will the url of your endpoint be?',
default: base + name
},
{
type: 'list',
name: 'models',
message: 'What would you like to use for the endpoint\'s models?',
choices: [ 'Mongoose', 'Sequelize' ],
filter: function( val ) {
return val.toLowerCase();
},
when: function() {
return self.filters.mongoose && self.filters.sequelize;
}
}
];
this.prompt(prompts, function (props) {
if(props.route.charAt(0) !== '/') {
props.route = '/' + props.route;
}
this.route = props.route;
if (props.models) {
delete this.filters.mongoose;
delete this.filters.mongooseModels;
delete this.filters.sequelize;
delete this.filters.sequelizeModels;
this.filters[props.models] = true;
this.filters[props.models + 'Models'] = true;
}
done();
}.bind(this));
};
Generator.prototype.writing = function createFiles() {
var dest = this.config.get('endpointDirectory') || 'server/api/' + this.name;
this.sourceRoot(path.join(__dirname, './templates'));
ngUtil.processDirectory(this, '.', dest);
};
Generator.prototype.end = function registerEndpoint() {
if(this.config.get('insertRoutes')) {
var routeConfig = {
file: this.config.get('registerRoutesFile'),
needle: this.config.get('routesNeedle'),
splicable: [
"app.use(\'" + this.route +"\', require(\'./api/" + this.name + "\'));"
]
};
ngUtil.rewriteFile(routeConfig);
}
if (this.filters.socketio) {
if(this.config.get('insertSockets')) {
var socketConfig = {
file: this.config.get('registerSocketsFile'),
needle: this.config.get('socketsNeedle'),
splicable: [
"require(\'../api/" + this.name + '/' + this.name + ".socket\').register(socket);"
]
};
ngUtil.rewriteFile(socketConfig);
}
}
if (this.filters.sequelize) {
if (this.config.get('insertModels')) {
var modelConfig = {
file: this.config.get('registerModelsFile'),
needle: this.config.get('modelsNeedle'),
splicable: [
"db." + this.classedName + " = db.sequelize.import(path.join(\n" +
" config.root,\n" +
" 'server',\n" +
" 'api',\n" +
" '" + this.name + "',\n" +
" '" + this.name + ".model'\n" +
"));"
]
};
ngUtil.rewriteFile(modelConfig);
}
}
};