forked from toptal/gitignore.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
115 lines (100 loc) · 3.3 KB
/
app.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
/**
* Module dependencies.
*/
var express = require('express')
, routes = require('./routes')
, http = require('http')
, path = require('path')
, fs = require('fs')
, ua = require("universal-analytics");
require('newrelic');
require('uglify-js-middleware');
var app = express();
var gitIgnores = {};
var oneDay = 604800000;
exports.oneDayCache = oneDay;
// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(express.compress());
app.use(express.favicon(path.join(__dirname, 'public/gi/img/favicon.ico'), { maxAge: oneDay }));
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(app.router);
app.use(ua.middleware(process.env.GA_TRACKING_ID, {cookieName: '_ga'}));
app.use(express.static(path.join(__dirname, 'public'), { maxAge: oneDay }));
app.use(require('uglify-js-middleware')({ src: path.join(__dirname,'public') }));
app.use(require('less-middleware')(path.join(__dirname,'public'), [], [], [{compress: true}]));
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
// Main Page
app.get('/', routes.index);
app.get('/dd.json', routes.dropdown);
// API
app.get('/cli', routes.cli);
app.get('/api/list', routes.apiListTypes);
app.get('/api/help', routes.help);
app.get('/api/?', routes.help);
app.get('/api/(:ignore)', routes.apiIgnore);
app.get('/api/f/(:ignore)', routes.apiFile);
app.get('/api/*', routes.help);
function errorHandler(err, req, res, next) {
res.status(500);
res.render('error', { error: err });
}
http.createServer(app).listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});
var walk = function(dir, filter, done) {
var results = [];
fs.readdir(dir, function(err, list) {
if (err) return done(err);
var pending = list.length;
if (!pending) return done(null, results);
list.forEach(function(file) {
file = dir + '/' + file;
fs.stat(file, function(err, stat) {
if (stat && stat.isDirectory()) {
walk(file, filter, function(err, res) {
results = results.concat(res);
if (!--pending) done(null, results);
});
} else {
if (file.indexOf(filter) > -1){
// Strip off file name
var fileName = file.split("/").pop();
var name = fileName.split(".")[0];
var contents = fs.readFileSync(file, 'utf8');
gitIgnores[name.toLowerCase()] = {
name: name,
fileName: fileName,
contents: contents
};
}
if (!--pending) done(null, results);
}
});
});
});
};
walk( __dirname + '/data', ".gitignore", function(err, results) {
if (err) throw err;
var gitIgnoreJSON = [];
var dropdownList = [];
for (var key in gitIgnores){
gitIgnoreJSON.push(gitIgnores[key].name.toLowerCase());
dropdownList.push({
id: gitIgnores[key].name.toLowerCase(),
text: gitIgnores[key].name
})
}
exports.gitIgnoreDropdownList = dropdownList;
exports.gitIgnoreJSONObject = gitIgnores;
exports.gitIgnoreJSONString = gitIgnoreJSON.sort().join(',')+"\n";
exports.gitIgnoreFileCount = gitIgnoreJSON.length;
});