-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gruntfile.js
189 lines (159 loc) · 4.9 KB
/
Gruntfile.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
var config = require('./package.json').config;
var request = require('superagent');
var Promise = require('es6-promise').Promise;
var fs = require('fs');
var _ = require('lodash');
var settings = require('./miccolis.json');
var GtfsMaker = require('gtfs-maker');
var gtfsMaker = new GtfsMaker({
settings:settings,
data:{
miccolis:{
format:'csv',
ext:'.csv',
dir:'./cache/'
},
timetables:{
isDirectory:true,
format:'csv',
ext:'.csv',
dir:'./extracted/timetables/',
transform:function(item){
function lookup(lineName){
return _.result(
_.find( settings.lines, function(line){
return line.number == lineName;
}), 'osmId' );
}
var matches = /MT(.*)\.csv/.exec(item.name);
if ( !matches ){
throw new Error('Timetables file not in correct format.');
}
var name = matches[1];
var osmId = lookup( name );
return {
osmId:osmId,
stopTimes:item.content
};
}
}
}
});
module.exports = function(grunt){
// utility function to parse command line options
function fetchOptions(){
var include = grunt.option( "include" );
if (!include){
throw new Error('No line included!');
}
return {
include:include.toString().split(',')
};
}
grunt.registerTask('cache', function(){
var done = this.async();
function fetchCSV(){
return new Promise(function(resolve, reject){
request.get( config.miccolis.url )
.end(function(err, res){
if(err){
reject(err);
} else {
resolve(res.text);
}
});
});
}
function saveData(data){
fs.writeFileSync('./cache/miccolis.csv', data);
}
gtfsMaker.cache()
.then(
fetchCSV().then(saveData)
).catch(function(err){
console.log(err);
}).then(done);
});
grunt.registerTask('trips', function(){
var done = this.async();
var tripsBuilder = require('./builders/trips');
var trips = tripsBuilder( gtfsMaker.loadData(['masters', 'miccolis']), fetchOptions() );
gtfsMaker.saveDataAsCsv(trips, './gtfs/trips.txt')
.catch(function(err){
console.log(err);
}).then(done);
});
grunt.registerTask('calendar', function(){
var done = this.async();
var calendarBuilder = require('./builders/calendar');
var calendar = calendarBuilder( gtfsMaker.loadData(['miccolis']), fetchOptions() );
gtfsMaker.saveDataAsCsv(calendar, './gtfs/calendar.txt')
.catch(function(err){
console.log(err);
})
.then(function(){
console.log('created ' + calendar.length + ' services.');
console.log('please edit service properties in calendar.txt file.');
}).then(done);
});
grunt.registerTask('frequencies', function(){
var done = this.async();
var frequenciesBuilder = require('./builders/frequencies');
var frequencies = frequenciesBuilder( gtfsMaker.loadData(['miccolis']), fetchOptions() );
gtfsMaker.saveDataAsCsv( frequencies, './gtfs/frequencies.txt' )
.catch(function(err){
console.log(err);
}).then(done);
});
grunt.registerTask('routes', function(){
var done = this.async();
var routes = gtfsMaker.builders.routes( gtfsMaker.loadData(['masters']), fetchOptions() );
gtfsMaker.saveDataAsCsv( routes, './gtfs/routes.txt' )
.catch(function(err){
console.log(err);
}).then(done);
});
grunt.registerTask('stop_times', function(){
var done = this.async();
var stopTimes = gtfsMaker.builders.stopTimes(
gtfsMaker.loadData([ 'masters', 'routes', 'stops', 'timetables', 'trips']),
fetchOptions()
);
gtfsMaker.saveDataAsCsv( stopTimes, './gtfs/stop_times.txt' )
.catch(function(err){
console.log(err);
}).then(done);
});
grunt.registerTask('shapes', function(){
var done = this.async();
var shapes = gtfsMaker.builders.shapes(
gtfsMaker.loadData(['masters', 'routes', 'ways', 'nodes']),
fetchOptions() );
gtfsMaker.saveDataAsCsv( shapes, './gtfs/shapes.txt' )
.catch(function(err){
console.log(err);
}).then(done);
});
grunt.registerTask('stops', function(){
var done = this.async();
var stops = gtfsMaker.builders.stops( gtfsMaker.loadData(['stops']) );
gtfsMaker.saveDataAsCsv( stops, './gtfs/stops.txt' )
.catch(function(err){
console.log(err);
}).then(done);
});
grunt.registerTask('compile', ['routes', 'shapes', 'stops', 'trips', 'frequencies', 'calendar', 'stop_times']);
/**
* validate GTFS
* FIXME: move to GtfsMaker?
*/
grunt.loadNpmTasks('grunt-exec');
grunt.initConfig({
exec: {
validate: {
command: 'python2.7 ./utils/transitfeed/feedvalidator.py gtfs/'
}
}
});
grunt.registerTask('validate', ['exec:validate']);
};