-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathframework.js
More file actions
executable file
·244 lines (203 loc) · 8.42 KB
/
framework.js
File metadata and controls
executable file
·244 lines (203 loc) · 8.42 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
"use strict";
var Path = require('path');
var Async = require('async');
var child_process = require('child_process');
var Fs = require('fs');
var ZipUtil = require('./zip_util');
var DEFAULT_MEM = '256M';
var FRAMEWORKS = {
'Rails' : {id: 'rails', mem: '256M', description: 'Rails Application'},
'Spring' : {id: 'spring', mem: '512M', description: 'Java SpringSource Spring Application'},
'Grails' : {id: 'grails', mem: '512M', description: 'Java SpringSource Grails Application'},
'Lift' : {id: 'lift', mem: '512M', description: 'Scala Lift Application'},
'JavaWeb' : {id: 'java_web', mem: '512M', description: 'Java Web Application'},
'Sinatra' : {id: 'sinatra', mem: '128M', description: 'Sinatra Application'},
'Node' : {id: 'node', mem: '64M', description: 'Node.js Application'},
'PHP' : {id: 'php', mem: '128M', description: 'PHP Application'},
'WSGI' : {id: 'wsgi', mem: '64M', description: 'Python WSGI Application'},
'Django' : {id: 'django', mem: '128M', description: 'Python Django Application'},
'Rack' : {id: 'rack', mem: '128M', description: 'Rack Application'}
};
module.exports = {
DEFAULT_MEM: DEFAULT_MEM,
FRAMEWORKS: FRAMEWORKS,
detect: function(dir, detection_callback) {
var contents;
var files;
// Find files only (ignore directories)
child_process.exec('find . -type f', { cwd: dir }, function (err, stdout, stderr) {
if (err) return detection_callback(err);
contents = stdout || "";
files = contents.split("\n");
var proceed = null;
var checkers = [railsCheck, rackCheck, javaCheck, sinatraCheck, nodeCheck, phpCheck, djangoCheck, WSGI_Check];
var detected = false;
Async.forEachSeries(checkers, function(checker, next) {
if (detected) // skip if already detected
return next();
checker(function(err, which, exec) {
proceed = { error: err, framework: which, exec: exec };
detected = err || which;
next(err, which);
});
}, function (err, result) {
if (err)
return detection_callback(err);
else if (! proceed.framework)
return detection_callback(); // can't determine application type
var framework = clone(FRAMEWORKS[proceed.framework]);
framework.exec = proceed.exec || framework.exec;
detection_callback(null, framework);
});
});
// Rails apps
function railsCheck(callback) {
Path.exists(Path.join(dir, 'config/environment.rb'), function (exists) {
if (exists)
callback(null, 'Rails');
else
callback();
});
}
// Rack apps
function rackCheck(callback) {
Path.exists(Path.join(dir, 'config.ru'), function (exists) {
if (exists)
callback(null, 'Rack');
else
callback();
});
}
// Java apps check
function javaCheck(callback) {
// Utility function to get the java app file
function getJavaAppContents(result, callback) {
if (result.warfile) {
ZipUtil.entry_lines(result.warfile, function(err, war_contents) {
callback(war_contents);
});
} else {
callback(contents);
}
}
// Java
Async.parallel({
warfile: function(callback) {
Fs.readdir(dir, function(err, list) {
if (err) return callback(err);
list = list.filter(function(f) {
return /\.war$/.test(f);
});
if (list.length !== 1)
return callback();
callback(null, Path.join(dir, list[0]));
});
},
webxml: function(callback) {
Path.exists(Path.join(dir, 'WEB-INF/web.xml'), function(exists) {
callback(null, exists);
});
}
}, function(err, result) {
if (result.warfile || result.webxml) {
// This is a java app
// Now we determine what type of java app this is
getJavaAppContents(result, function(app_contents) {
if (/WEB-INF\/lib\/grails-web.*\.jar/.test(app_contents)) {
callback(null, 'Grails');
} else if (/WEB-INF\/lib\/lift-webkit.*\.jar/.test(app_contents)) {
callback(null, 'Lift');
} else if (/WEB-INF\/classes\/org\/springframework/.test(app_contents)
|| /WEB-INF\/lib\/spring-core.*\.jar/.test(app_contents)
|| /WEB-INF\/lib\/org\.springframework\.core.*\.jar/.test(app_contents)) {
callback(null, 'Spring');
} else {
callback(null, 'JavaWeb');
}
});
}
else {
callback();
}
});
}
// Simple Ruby Apps
function sinatraCheck(callback) {
Async.detectSeries(files, function(file, next) {
if (! /\.rb$/.test(file))
return next();
Fs.readFile(Path.join(dir, file), function(err, str) {
if (err || (! /^\s*require[\s\(]*['"]sinatra['"]/.test(str)))
return next();
next(true);
});
}, function(file) {
if (! file) return callback();
callback(null, 'Sinatra', 'ruby ' + file);
});
}
// Node.JS
function nodeCheck(callback) {
// Check all possible detection files
var files = ['server.js', 'app.js', 'index.js', 'main.js'].map(function(file) {
return Path.join(dir, file);
});
Async.detectSeries(files, Path.exists, function (file) {
if (! file) return callback();
callback(null, 'Node', 'node ' + file);
});
}
function phpCheck(callback) {
var found = files.some(function (file) {
return file.toLowerCase().lastIndexOf('.php') === (file.length - 4)
});
if (! found)
return callback();
callback(null, 'PHP');
}
function djangoCheck (callback) {
Path.exists(Path.join(dir, 'manage.py'), function (exists) {
if (exists)
callback(null, 'Django');
else
callback();
});
}
function WSGI_Check (callback) {
Path.exists(Path.join(dir, 'wsgi.py'), function (exists) {
if (exists)
callback(null, 'WSGI');
else
callback();
});
}
}
};
// Utilities
function clone(obj) {
// Handle the 3 simple types, and null or undefined
if (null == obj || "object" != typeof obj) return obj;
// Handle Date
if (obj instanceof Date) {
var copy = new Date();
copy.setTime(obj.getTime());
return copy;
}
// Handle Array
if (obj instanceof Array) {
var copy = [];
for (var i = 0, len = obj.length; i < len; ++i) {
copy[i] = clone(obj[i]);
}
return copy;
}
// Handle Object
if (obj instanceof Object) {
var copy = {};
for (var attr in obj) {
if (obj.hasOwnProperty(attr)) copy[attr] = clone(obj[attr]);
}
return copy;
}
throw new Error("Unable to copy obj! Its type isn't supported.");
}