From 1a0d40013d74ce34c983d7fe0e986d0ac7140878 Mon Sep 17 00:00:00 2001 From: Daniel Franklin Date: Mon, 5 Aug 2024 11:47:01 +0100 Subject: [PATCH] Improve gtfs zip output robustness - Clear /tmp/gtfs before running OutputGTFSCommand - Log errors from running zip command --- src/cli/OutputGTFSZipCommand.ts | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/src/cli/OutputGTFSZipCommand.ts b/src/cli/OutputGTFSZipCommand.ts index c604ba63..b2adfe52 100644 --- a/src/cli/OutputGTFSZipCommand.ts +++ b/src/cli/OutputGTFSZipCommand.ts @@ -2,13 +2,11 @@ import {CLICommand} from "./CLICommand"; import {OutputGTFSCommand} from "./OutputGTFSCommand"; import * as fs from "fs"; -import {execSync} from "child_process"; +import { execSync } from "child_process"; +import * as path from "path"; export class OutputGTFSZipCommand implements CLICommand { - - constructor( - private readonly command: OutputGTFSCommand - ) { } + constructor(private readonly command: OutputGTFSCommand) {} /** * Create the text files and then zip them up using a CLI command that hopefully exists. @@ -22,7 +20,11 @@ export class OutputGTFSZipCommand implements CLICommand { argv[3] = "/tmp/gtfs/"; - if (!fs.existsSync(argv[3])) { + if (fs.existsSync(argv[3])) { + for (const entry of fs.readdirSync(argv[3])) { + fs.rmSync(path.join(argv[3], entry), { recursive: true }); + } + } else { fs.mkdirSync(argv[3]); } @@ -31,8 +33,16 @@ export class OutputGTFSZipCommand implements CLICommand { // when node tells you it's finished writing a file, it's lying. setTimeout(() => { console.log("Writing " + filename); - execSync(`zip -j ${filename} ${argv[3]}/*.txt`); + try { + execSync(`zip -j ${filename} ${argv[3]}/*.txt`); + } catch (err) { + const dec = new TextDecoder(); + console.error("STDOUT:"); + console.error(dec.decode(err.stdout)); + console.error("STDERR:"); + console.error(dec.decode(err.stderr)); + throw err; + } }, 1000); } - -} \ No newline at end of file +}