76 lines
2.9 KiB
JavaScript
76 lines
2.9 KiB
JavaScript
#!/usr/bin/env node
|
|
"use strict";
|
|
|
|
const fs = require("fs");
|
|
const path = require("path");
|
|
const { createRequire } = require("module");
|
|
|
|
const [pkgDirArg, ...converterArgs] = process.argv.slice(2);
|
|
|
|
if (!pkgDirArg || converterArgs.length < 2) {
|
|
console.error(
|
|
"Usage: transxchange2gtfs_shim.js <package-dir> <input...> <output>",
|
|
);
|
|
process.exit(2);
|
|
}
|
|
|
|
const pkgDir = path.resolve(pkgDirArg);
|
|
|
|
function replaceOnce(relativePath, before, after) {
|
|
const file = path.join(pkgDir, relativePath);
|
|
const original = fs.readFileSync(file, "utf8");
|
|
if (original.includes(before)) {
|
|
fs.writeFileSync(file, original.replace(before, after));
|
|
} else if (original.includes(after)) {
|
|
return;
|
|
} else {
|
|
throw new Error(`Could not patch ${relativePath}: expected text not found`);
|
|
}
|
|
}
|
|
|
|
// The published 1.12.0 package has a few compatibility issues with current
|
|
// TfL TransXChange exports:
|
|
// - the bin script points at dist/src/cli.js, but the package ships dist/cli.js
|
|
// - the compiled date-holidays import expects a synthetic default export
|
|
// - some TfL journeys reference timing links without matching route-link geometry
|
|
//
|
|
// GTFS shapes are optional for R5 routing. Clear shape references and omit
|
|
// shapes.txt so missing route geometry does not drop otherwise usable trips.
|
|
function patchPackage() {
|
|
replaceOnce(
|
|
"dist/transxchange/TransXChangeJourneyStream.js",
|
|
"distanceSoFarM += routeLink.Distance;",
|
|
"distanceSoFarM += routeLink ? routeLink.Distance : 0;",
|
|
);
|
|
replaceOnce(
|
|
"dist/gtfs/TripsStream.js",
|
|
"(0, crypto_1.createHash)('md5').update(JSON.stringify({ routeId: journey.route, routeLinkSeq: journey.routeLinkIds })).digest(\"hex\"));",
|
|
"\"\");",
|
|
);
|
|
replaceOnce(
|
|
"dist/gtfs/StopTimesStream.js",
|
|
"stop.shapeDistTraveled, stop.exactTime ? \"1\" : \"0\");",
|
|
"\"\", stop.exactTime ? \"1\" : \"0\");",
|
|
);
|
|
replaceOnce(
|
|
"dist/Container.js",
|
|
"\"stops.txt\": transxchange.pipe(new StopsStream_1.StopsStream(naptanIndex)),\n \"shapes.txt\": journeyStream.pipe(new ShapesStream_1.ShapesStream())",
|
|
"\"stops.txt\": transxchange.pipe(new StopsStream_1.StopsStream(naptanIndex))",
|
|
);
|
|
replaceOnce(
|
|
"dist/Container.js",
|
|
"\"routes.txt\": transxchange.pipe(new RoutesStream_1.RoutesStream()),\n \"transfers.txt\": transxchange.pipe(new TransfersStream_1.TransfersStream(naptanIndex, locationIndex)),\n \"stops.txt\": transxchange.pipe(new StopsStream_1.StopsStream(naptanIndex))",
|
|
"\"routes.txt\": transxchange.pipe(new RoutesStream_1.RoutesStream()),\n \"stops.txt\": transxchange.pipe(new StopsStream_1.StopsStream(naptanIndex))",
|
|
);
|
|
}
|
|
|
|
patchPackage();
|
|
|
|
const pkgRequire = createRequire(path.join(pkgDir, "package.json"));
|
|
const Holidays = pkgRequire("date-holidays");
|
|
if (!Holidays.default) {
|
|
Holidays.default = Holidays;
|
|
}
|
|
|
|
process.argv = [process.argv[0], "transxchange2gtfs", ...converterArgs];
|
|
require(path.join(pkgDir, "dist", "cli.js"));
|