From 2d269fc3500e6964abe663f7adbc0e777ac85204 Mon Sep 17 00:00:00 2001 From: Michael Tsang Date: Thu, 26 Sep 2024 12:36:32 +0100 Subject: [PATCH] fix errors when specifying zip path with spaces --- src/cli/CLICommand.ts | 13 +++++++++++++ src/cli/OutputGTFSZipCommand.ts | 8 ++++---- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/cli/CLICommand.ts b/src/cli/CLICommand.ts index 3d59e90f..1ee9e5c5 100644 --- a/src/cli/CLICommand.ts +++ b/src/cli/CLICommand.ts @@ -1,3 +1,4 @@ +import * as child_process from 'node:child_process'; export interface CLICommand { @@ -7,3 +8,15 @@ export interface CLICommand { run(argv: string[]): Promise; } + +export function processSpawnResult(result : child_process.SpawnSyncReturns) { + if (result.error !== undefined) { + throw result.error; + } + if (result.signal !== null) { + throw Error(`Child process has been killed by signal ${result.signal}`); + } + if (result.status !== null && result.status !== 0) { + throw Error(`Child process exited with non-zero status ${result.status}`); + } +} diff --git a/src/cli/OutputGTFSZipCommand.ts b/src/cli/OutputGTFSZipCommand.ts index b1acd20a..fad777f9 100644 --- a/src/cli/OutputGTFSZipCommand.ts +++ b/src/cli/OutputGTFSZipCommand.ts @@ -1,10 +1,10 @@ import * as os from 'node:os'; import * as path from 'node:path'; -import {CLICommand} from "./CLICommand"; +import {CLICommand, processSpawnResult} from "./CLICommand"; import {OutputGTFSCommand} from "./OutputGTFSCommand"; import * as fs from "fs"; -import {execSync} from "child_process"; +import {spawnSync} from "child_process"; export class OutputGTFSZipCommand implements CLICommand { @@ -29,8 +29,8 @@ 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`); + processSpawnResult(spawnSync('zip', ['-jr', filename, argv[3]])); }, 1000); } -} \ No newline at end of file +}