-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·170 lines (156 loc) · 4.71 KB
/
Copy pathindex.js
File metadata and controls
executable file
·170 lines (156 loc) · 4.71 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
#! /usr/bin/env node
// Dependencies
const fs = require('fs');
const rimraf = require('rimraf');
const async = require('async');
const homeDir = require('home-dir');
// Modules
const Package = require('./package.json');
const Core = require('./src/core.js')();
const Stop = require('./src/stop.js')(Core);
const Trip = require('./src/trip.js')(Core, Stop);
// Store migration from 0.2 to 1.0
// Migration script to migrate the old store data to the new folder and accommodate for the new IDs from Resrobot.
if (fs.existsSync(homeDir('/store')) && fs.existsSync(homeDir('/store/store.json'))) {
const oldData = JSON.parse(fs.readFileSync(homeDir('/store/store.json')));
const migrationData = [];
// Migrate stops
if (oldData.stops) {
const stopNames = Object.keys(oldData.stops);
stopNames.forEach((name, i) => {
const stop = oldData.stops[name];
migrationData.push({
key: name,
name: stop.name,
type: 'stop'
});
});
}
// Migrate trips
if (oldData.trips) {
const tripNames = Object.keys(oldData.trips);
tripNames.forEach((name, i) => {
const trip = oldData.trips[name];
migrationData.push({
key: name,
origin: { name: trip.origin.name, type: 'origin'},
destination: { name: trip.destination.name, type: 'destination' },
via: { name: ((trip.via) ? trip.via.name:false), type: 'via' },
type: 'trip'
});
});
}
rimraf.sync(homeDir('/store'));
if (migrationData.length) {
console.log('');
console.log('Migrating your old store data. We apologize for the inconvenience.');
console.log('');
Core.getPlanKey()
.then((key) => {
async.eachSeries(migrationData, (obj, callback) => {
if (obj.type === 'stop') {
// Migrate stop.
Stop.search(obj.name, key).then((stop) => {
delete stop.weight;
delete stop.products;
delete stop.extId;
Core.addStop(stop, obj.key);
callback();
});
} else {
// Migrate trip.
const trip = {};
async.eachSeries([ obj.origin, obj.destination, obj.via ], (stop, cb) => {
if (stop.type === 'via' && !stop.name) {
trip.via = false;
return cb();
}
Stop.search(stop.name, key).then((newStop) => {
delete newStop.weight;
delete newStop.products;
delete newStop.extId;
trip[stop.type] = newStop;
cb();
});
}, () => {
Core.addTrip(trip, obj.key);
callback();
});
}
}, () => {
console.log('');
console.log('Store data migrated, thank you for your patience! :)');
console.log('');
});
})
.catch(error => console.log(error));
}
}
const args = process.argv.splice(2, process.argv.length);
if (!args.length) {
console.log('');
console.log('Tramz is a CLI client for public transit in Sweden.');
console.log('');
console.log(' > Type [tramz -h] to see how to use it.');
console.log('');
return;
} else if (args[0] === '-h' || args[0] === '--help') {
return Core.showHelp();
} else if (args[0] === '-v') {
return console.log(Package.version);
} else if (args[0] === 'stops') {
if (args[1] === 'add') {
if (args[2]) {
return Stop.add(args[2]);
}
} else if (args[1] === 'remove') {
if (args[2]) {
return Stop.remove(args[2]);
}
} else {
return Stop.list();
}
} else if (args[0] === 'trips') {
if (args[1] === 'add') {
if (args[2] && args[3]) {
const orig = args[2];
const dest = args[3];
const via = args[4] ? args[4]:false;
return Trip.add([
{ name: 'origin', string: orig },
{ name: 'destination', string: dest },
{ name: 'via', string: via }
]);
}
} else if (args[1] === 'remove') {
if (args[2]) {
return Trip.remove(args[2]);
}
} else {
return Trip.list();
}
} else {
if (args[0]) {
const value = args[0];
if (args[1]) {
const orig = value;
const dest = args[1];
const via = (args[2]) ? args[2]:false;
return Trip.search([
{ name: 'origin', string: orig },
{ name: 'destination', string: dest },
{ name: 'via', string: via }
]);
} else {
if (Trip.get(value)) {
const trip = Trip.get(value);
return Trip.search([
{ name: 'origin', stop: trip.origin },
{ name: 'destination', stop: trip.destination },
{ name: 'via', stop: ((trip.via) ? trip.via:false) }
]);
}
}
}
}
console.log('Invalid command.');