-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
133 lines (105 loc) · 2.97 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
require('shelljs/global');
var util = require("util");
var mkdirp = require('mkdirp');
var Inflector = require('inflected');
var tpl = require('tpl_apply');
// util.inherits(MyStream, events.EventEmitter);
// var file_path = __dirname;
// var current_path = process.cwd();
function getUserHome() {
return process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME'];
}
var cache_path = getUserHome() + '/.express-g/';
mkdirp(cache_path, function (err) {
if (err) console.error(err)
else console.log('pow! create cache_path')
});
var _default_options = function (base_path){
return {
controller_path : base_path + '/controllers',
model_path : base_path + '/models',
view_path : base_path + '/views',
route_path : base_path + '/routes'
}
}
function _cp(des, src) {
if (!des) {
des = {};
}
if (src) {
for (var i in src) {
des[i] = src[i];
}
}
return des;
}
function g (obj, opts) {
this.model = obj;
this.root_path = __dirname;
this.base_path = obj.base_path;
this.option = _default_options(this.base_path);
_cp(this.option, opts);
_cp(this, this.option);
_mkdir(this);
}
function _mkdir(t){
mkdirp(t.controller_path, function (err) {
if (err) console.error(err)
else console.log('pow! create controller_path')
});
mkdirp(t.model_path, function (err) {
if (err) console.error(err)
else console.log('pow! create model_path')
});
mkdirp(t.view_path, function (err) {
if (err) console.error(err)
else console.log('pow! create view_path')
});
mkdirp(t.route_path, function (err) {
if (err) console.error(err)
else console.log('pow! create route_path')
});
mkdirp(t.route_path + '/api', function (err) {
if (err) console.error(err)
else console.log('pow! create route_api_path')
});
}
g.prototype.c = function () {
require('./lib/controller')(this)
}
g.prototype.m = function () {
require('./lib/model')(this)
}
g.prototype.v = function () {
require('./lib/view')(this)
}
g.prototype.r = function () {
require('./lib/route')(this)
}
g.prototype.r_api = function () {
require('./lib/route_api')(this)
}
g.prototype.all = function () {
this.c();
this.m();
this.v();
this.r();
this.r_api();
}
g.prototype.destroy = function () {
var entity = this.model.entity;
var cache_path = getUserHome() +'/.express-g/' + Date.now();
mkdirp(cache_path, function (err) {
if (err) console.error(err)
else console.log('pow! create controller_path')
});
var c = this.controller_path +'/'+ Inflector.pluralize(entity) + "_controller.js";
var m = this.model_path +'/'+ entity + ".js";
var v = this.view_path +'/'+ Inflector.pluralize(entity) + "/";
var r = this.route_path +'/'+ Inflector.pluralize(entity) + ".js";
var a = this.route_path +'/api/'+ Inflector.pluralize(entity) + ".js";
[c,m,v,r,a].forEach(function(file){
mv('-f', file, cache_path + '/');
});
}
module.exports = g;