transit-departures-widget
Version:
Build a realtime transit departures tool from GTFS and GTFS-Realtime.
386 lines (380 loc) • 12.5 kB
JavaScript
// src/app/index.ts
import { readFileSync } from "node:fs";
import { join as join3 } from "node:path";
import yargs from "yargs";
import { openDb as openDb2 } from "gtfs";
import untildify2 from "untildify";
import express from "express";
import logger from "morgan";
// src/lib/utils.ts
import { join as join2 } from "path";
import { openDb, getDirections, getRoutes, getStops, getTrips } from "gtfs";
import { groupBy, last, maxBy, size, sortBy, uniqBy } from "lodash-es";
// src/lib/file-utils.ts
import { dirname, join, resolve } from "node:path";
import { fileURLToPath } from "node:url";
import beautify from "js-beautify";
import pug from "pug";
import untildify from "untildify";
function getPathToViewsFolder(config2) {
if (config2.templatePath) {
return untildify(config2.templatePath);
}
const __dirname = dirname(fileURLToPath(import.meta.url));
let viewsFolderPath;
if (__dirname.endsWith("/dist/bin") || __dirname.endsWith("/dist/app")) {
viewsFolderPath = resolve(__dirname, "../../views/widget");
} else if (__dirname.endsWith("/dist")) {
viewsFolderPath = resolve(__dirname, "../views/widget");
} else {
viewsFolderPath = resolve(__dirname, "views/widget");
}
return viewsFolderPath;
}
function getPathToTemplateFile(templateFileName, config2) {
const fullTemplateFileName = config2.noHead !== true ? `${templateFileName}_full.pug` : `${templateFileName}.pug`;
return join(getPathToViewsFolder(config2), fullTemplateFileName);
}
async function renderFile(templateFileName, templateVars, config2) {
const templatePath = getPathToTemplateFile(templateFileName, config2);
const html = await pug.renderFile(templatePath, templateVars);
if (config2.beautify === true) {
return beautify.html_beautify(html, { indent_size: 2 });
}
return html;
}
// src/lib/utils.ts
import sqlString from "sqlstring-sqlite";
import toposort from "toposort";
import { I18n } from "i18n";
// src/lib/log-utils.ts
import { noop } from "lodash-es";
import * as colors from "yoctocolors";
function logWarning(config2) {
if (config2.logFunction) {
return config2.logFunction;
}
return (text) => {
process.stdout.write(`
${formatWarning(text)}
`);
};
}
function formatWarning(text) {
const warningMessage = `${colors.underline("Warning")}: ${text}`;
return colors.yellow(warningMessage);
}
// src/lib/utils.ts
var getCalendarsForDateRange = (config2) => {
const db = openDb(config2);
let whereClause = "";
const whereClauses = [];
if (config2.endDate) {
whereClauses.push(`start_date <= ${sqlString.escape(config2.endDate)}`);
}
if (config2.startDate) {
whereClauses.push(`end_date >= ${sqlString.escape(config2.startDate)}`);
}
if (whereClauses.length > 0) {
whereClause = `WHERE ${whereClauses.join(" AND ")}`;
}
return db.prepare(`SELECT * FROM calendar ${whereClause}`).all();
};
function formatRouteName(route) {
let routeName = "";
if (route.route_short_name !== null) {
routeName += route.route_short_name;
}
if (route.route_short_name !== null && route.route_long_name !== null) {
routeName += " - ";
}
if (route.route_long_name !== null) {
routeName += route.route_long_name;
}
return routeName;
}
function getDirectionsForRoute(route, config2) {
const db = openDb(config2);
const directions = getDirections({ route_id: route.route_id }, [
"direction_id",
"direction"
]);
const calendars = getCalendarsForDateRange(config2);
if (directions.length === 0) {
const headsigns = db.prepare(
`SELECT direction_id, trip_headsign, count(*) AS count FROM trips WHERE route_id = ? AND service_id IN (${calendars.map((calendar) => `'${calendar.service_id}'`).join(", ")}) GROUP BY direction_id, trip_headsign`
).all(route.route_id);
for (const group of Object.values(groupBy(headsigns, "direction_id"))) {
const mostCommonHeadsign = maxBy(group, "count");
directions.push({
direction_id: mostCommonHeadsign.direction_id,
direction: config2.__("To {{{headsign}}}", {
headsign: mostCommonHeadsign.trip_headsign
})
});
}
}
return directions;
}
function sortStopIdsBySequence(stoptimes) {
const stoptimesGroupedByTrip = groupBy(stoptimes, "trip_id");
try {
const stopGraph = [];
for (const tripStoptimes of Object.values(stoptimesGroupedByTrip)) {
const sortedStopIds = sortBy(tripStoptimes, "stop_sequence").map(
(stoptime) => stoptime.stop_id
);
for (const [index, stopId] of sortedStopIds.entries()) {
if (index === sortedStopIds.length - 1) {
continue;
}
stopGraph.push([stopId, sortedStopIds[index + 1]]);
}
}
return toposort(
stopGraph
);
} catch {
}
const longestTripStoptimes = maxBy(
Object.values(stoptimesGroupedByTrip),
(stoptimes2) => size(stoptimes2)
);
return longestTripStoptimes.map((stoptime) => stoptime.stop_id);
}
function getStopsForDirection(route, direction, config2) {
const db = openDb(config2);
const calendars = getCalendarsForDateRange(config2);
const whereClause = formatWhereClauses({
direction_id: direction.direction_id,
route_id: route.route_id,
service_id: calendars.map((calendar) => calendar.service_id)
});
const stoptimes = db.prepare(
`SELECT stop_id, stop_sequence, trip_id FROM stop_times WHERE trip_id IN (SELECT trip_id FROM trips ${whereClause}) ORDER BY stop_sequence ASC`
).all();
const sortedStopIds = sortStopIdsBySequence(stoptimes);
const deduplicatedStopIds = sortedStopIds.reduce(
(memo, stopId) => {
if (last(memo) !== stopId) {
memo.push(stopId);
}
return memo;
},
[]
);
deduplicatedStopIds.pop();
const stopFields = ["stop_id", "stop_name", "stop_code", "parent_station"];
if (config2.includeCoordinates) {
stopFields.push("stop_lat", "stop_lon");
}
const stops = getStops({ stop_id: deduplicatedStopIds }, stopFields);
return deduplicatedStopIds.map(
(stopId) => stops.find((stop) => stop.stop_id === stopId)
);
}
function generateTransitDeparturesWidgetHtml(config2) {
const templateVars = {
config: config2,
__: config2.__
};
return renderFile("widget", templateVars, config2);
}
function generateTransitDeparturesWidgetJson(config2) {
const routes = getRoutes();
const stops = [];
const filteredRoutes = [];
const calendars = getCalendarsForDateRange(config2);
for (const route of routes) {
route.route_full_name = formatRouteName(route);
const directions = getDirectionsForRoute(route, config2);
if (directions.length === 0) {
logWarning(config2)(
`route_id ${route.route_id} has no directions - skipping`
);
continue;
}
for (const direction of directions) {
const directionStops = getStopsForDirection(route, direction, config2);
stops.push(...directionStops);
direction.stopIds = directionStops.map((stop) => stop?.stop_id);
const trips = getTrips(
{
route_id: route.route_id,
direction_id: direction.direction_id,
service_id: calendars.map(
(calendar) => calendar.service_id
)
},
["trip_id"]
);
direction.tripIds = trips.map((trip) => trip.trip_id);
}
route.directions = directions;
filteredRoutes.push(route);
}
const sortedRoutes = sortBy(
sortBy(filteredRoutes, (route) => route.route_short_name?.toLowerCase()),
(route) => Number.parseInt(route.route_short_name, 10)
);
const parentStationIds = new Set(stops.map((stop) => stop?.parent_station));
const parentStationStops = getStops(
{ stop_id: Array.from(parentStationIds) },
["stop_id", "stop_name", "stop_code", "parent_station"]
);
stops.push(
...parentStationStops.map((stop) => {
stop.is_parent_station = true;
return stop;
})
);
const sortedStops = sortBy(uniqBy(stops, "stop_id"), "stop_name");
return {
routes: removeNulls(sortedRoutes),
stops: removeNulls(sortedStops)
};
}
function removeNulls(data) {
if (Array.isArray(data)) {
return data.map(removeNulls).filter((item) => item !== null && item !== void 0);
} else if (typeof data === "object" && data !== null) {
return Object.entries(data).reduce((acc, [key, value]) => {
const cleanedValue = removeNulls(value);
if (cleanedValue !== null && cleanedValue !== void 0) {
acc[key] = cleanedValue;
}
return acc;
}, {});
} else {
return data;
}
}
function setDefaultConfig(initialConfig) {
const defaults = {
beautify: false,
noHead: false,
refreshIntervalSeconds: 20,
skipImport: false,
timeFormat: "12hour",
includeCoordinates: false,
overwriteExistingFiles: true,
verbose: true
};
const config2 = Object.assign(defaults, initialConfig);
const viewsFolderPath = getPathToViewsFolder(config2);
const i18n = new I18n({
directory: join2(viewsFolderPath, "locales"),
defaultLocale: config2.locale,
updateFiles: false
});
const configWithI18n = Object.assign(config2, {
__: i18n.__
});
return configWithI18n;
}
function formatWhereClause(key, value) {
if (Array.isArray(value)) {
let whereClause = `${sqlString.escapeId(key)} IN (${value.filter((v) => v !== null).map((v) => sqlString.escape(v)).join(", ")})`;
if (value.includes(null)) {
whereClause = `(${whereClause} OR ${sqlString.escapeId(key)} IS NULL)`;
}
return whereClause;
}
if (value === null) {
return `${sqlString.escapeId(key)} IS NULL`;
}
return `${sqlString.escapeId(key)} = ${sqlString.escape(value)}`;
}
function formatWhereClauses(query) {
if (Object.keys(query).length === 0) {
return "";
}
const whereClauses = Object.entries(query).map(
([key, value]) => formatWhereClause(key, value)
);
return `WHERE ${whereClauses.join(" AND ")}`;
}
// src/app/index.ts
var argv = yargs(process.argv).option("c", {
alias: "configPath",
describe: "Path to config file",
default: "./config.json",
type: "string"
}).parseSync();
var app = express();
var configPath = argv.configPath || join3(process.cwd(), "config.json");
var selectedConfig = JSON.parse(readFileSync(configPath, "utf8"));
var config = setDefaultConfig(selectedConfig);
config.noHead = false;
config.assetPath = "/";
config.logFunction = console.log;
try {
openDb2(config);
} catch (error) {
console.error(
`Unable to open sqlite database "${config.sqlitePath}" defined as \`sqlitePath\` config.json. Ensure the parent directory exists and import GTFS before running this app.`
);
throw error;
}
app.get("/", async (request, response, next) => {
try {
const html = await generateTransitDeparturesWidgetHtml(config);
response.send(html);
} catch (error) {
next(error);
}
});
app.get("/data/routes.json", async (request, response, next) => {
try {
const { routes } = await generateTransitDeparturesWidgetJson(config);
response.json(routes);
} catch (error) {
next(error);
}
});
app.get("/data/stops.json", async (request, response, next) => {
try {
const { stops } = await generateTransitDeparturesWidgetJson(config);
response.json(stops);
} catch (error) {
next(error);
}
});
app.set("views", getPathToViewsFolder(config));
app.set("view engine", "pug");
app.use(logger("dev"));
var staticAssetPath = config.templatePath === void 0 ? getPathToViewsFolder(config) : untildify2(config.templatePath);
app.use(express.static(staticAssetPath));
app.use((req, res) => {
res.status(404).send("Not Found");
});
app.use(
(err, req, res, next) => {
console.error(err.stack);
res.status(500).send("Something broke!");
}
);
var startServer = async (port2) => {
try {
await new Promise((resolve2, reject) => {
const server = app.listen(port2).once("listening", () => {
console.log(`Express server listening on port ${port2}`);
resolve2();
}).once("error", (err) => {
if (err.code === "EADDRINUSE") {
console.log(`Port ${port2} is in use, trying ${port2 + 1}`);
server.close();
resolve2(startServer(port2 + 1));
} else {
reject(err);
}
});
});
} catch (err) {
console.error("Failed to start server:", err);
process.exit(1);
}
};
var port = process.env.PORT ? parseInt(process.env.PORT, 10) : 3e3;
startServer(port);
//# sourceMappingURL=index.js.map