-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
173 lines (143 loc) · 3.48 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
'use strict';
(function()
{
// Extensions
var fs = require('fs')
, arg = require('./lib/argument-parser')
, JSONcleaner = require('./lib/comment-cut-out')
, normalize = require('./lib/normalize');
// Constants
var ct = require('./lib/constants');
var Scaffold = function()
{
this.init();
this.config;
}
Scaffold.prototype =
{
init : function()
{
this.parse();
},
parse : function()
{
// Configuration
var config = arg.parse(process.argv);
if(config.help)
{
this.help();
}
else if(config.file == null)
{
this.shell();
}
else
{
var err = false
, file;
try
{
file = fs.readFileSync(config.file, 'utf8');
}
catch(e)
{
err = true;
console.log('Couldn\'t access \''+config.file+'\'!');
}
if(!err)
{
this.config = config;
this.validate(file);
}
}
},
validate : function(file)
{
var validateConfig = require('./lib/validate-config');
if(validateConfig.isValid(this))
{
this.job(file);
}
},
cleanJSON : function(json)
{
for(var index in json.models)
{
json.models[index] = normalize.do(json.models[index]);
}
return json;
},
job : function(file)
{
var json = JSON.parse(JSONcleaner.clean(file));
if(!fs.existsSync('./models'))
{
fs.mkdirSync('./models');
}
if(!fs.existsSync('./controllers'))
{
fs.mkdirSync('./controllers');
}
if(!fs.existsSync('./views'))
{
fs.mkdirSync('./views');
}
json = this.cleanJSON(json);
var dissectModel = require('./lib/dissect-model')
, dissectController = require('./lib/dissect-controller')
, dissectView = require('./lib/dissect-view')
, dissectServer = require('./lib/dissect-server');
dissectServer.dissect(this, json.models);
for(var index in json.models)
{
dissectModel.dissect(this, json.models[index]);
dissectController.dissect(this, json.models[index]);
dissectView.dissect(this, json.models[index]);
}
},
help : function()
{
console.log('Usage: node xx.js [options argument]\n');
console.log('Options:');
console.log(' -h, --http-framework name\tHttp framework to use (default: express).');
console.log(' -de, --db-engine name\t\tDB engine to use (default: mongodb).');
console.log(' -df, --db-framework name\tDB framework to use (default: mongoose).');
console.log(' -f, --file filepath\t\tFile to read (required).');
console.log(' -F, --force-overwrite\t\tForce overwrite of existing files.');
console.log('\nExample:');
console.log(' node scaffold.js --file data.json --http-framework koa -de mysql -F');
console.log('\nDocumentation can be found at http://github.com/mauriciogior/node-scaffold');
},
shell : function()
{
this.message('Please give me a file through -file! (ie. -file data.json)', ct.MSG_ERROR);
},
message : function(message, type)
{
if(type == ct.MSG_ERROR)
{
console.log('\x1b[1;97;101m%s\x1b[0m %s', '!ERROR!', message);
}
else if(type == ct.MSG_WARNING)
{
console.log('\x1b[1;41;103m%s\x1b[0m %s', '!WARNING!', message);
}
else if(type == ct.MSG_SUCCESS)
{
console.log('\x1b[1;97;42m%s\x1b[0m %s', ' SUCCESS ', message);
}
else if(type == ct.MSG_FAILED)
{
console.log('\x1b[1;97;101m%s\x1b[0m %s', '!FAIL!', message);
}
},
finalize : function()
{
this.message('Finished scaffolding!', ct.MSG_SUCCESS);
}
}
exports.exec = function()
{
new Scaffold();
}
})();