Skip to content

Commit

Permalink
use unique temp directory (#99)
Browse files Browse the repository at this point in the history
Co-authored-by: Michael Tsang <[email protected]>
  • Loading branch information
miklcct and miklcct authored Sep 13, 2024
1 parent dbcba6a commit d55bbad
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 16 deletions.
11 changes: 7 additions & 4 deletions src/cli/Container.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import * as memoize from "memoized-class-decorator";
import * as fs from 'node:fs';
import * as os from 'node:os';
import * as path from 'node:path';
import {CLICommand} from "./CLICommand";
import {ImportFeedCommand} from "./ImportFeedCommand";
import {DatabaseConfiguration, DatabaseConnection} from "../database/DatabaseConnection";
Expand Down Expand Up @@ -45,22 +48,22 @@ export class Container {

@memoize
public async getFaresImportCommand(): Promise<ImportFeedCommand> {
return new ImportFeedCommand(await this.getDatabaseConnection(), config.fares, "/tmp/dtd/fares/");
return new ImportFeedCommand(await this.getDatabaseConnection(), config.fares, fs.mkdtempSync(path.join(os.tmpdir(), "dtd")));
}

@memoize
public async getRouteingImportCommand(): Promise<ImportFeedCommand> {
return new ImportFeedCommand(await this.getDatabaseConnection(), config.routeing, "/tmp/dtd/routeing/");
return new ImportFeedCommand(await this.getDatabaseConnection(), config.routeing, fs.mkdtempSync(path.join(os.tmpdir(), "dtd")));
}

@memoize
public async getTimetableImportCommand(): Promise<ImportFeedCommand> {
return new ImportFeedCommand(await this.getDatabaseConnection(), config.timetable, "/tmp/dtd/timetable/");
return new ImportFeedCommand(await this.getDatabaseConnection(), config.timetable, fs.mkdtempSync(path.join(os.tmpdir(), "dtd")));
}

@memoize
public async getNFM64ImportCommand(): Promise<ImportFeedCommand> {
return new ImportFeedCommand(await this.getDatabaseConnection(), config.nfm64, "/tmp/dtd/nfm64/");
return new ImportFeedCommand(await this.getDatabaseConnection(), config.nfm64, fs.mkdtempSync(path.join(os.tmpdir(), "dtd")));
}


Expand Down
2 changes: 1 addition & 1 deletion src/cli/ImportFeedCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ export class ImportFeedCommand implements CLICommand {
const file = this.getFeedFile(filename);
const tables = await this.tables(file);
const tableStream = new MySQLStream(filename, file, tables);
const stream = readFile(this.tmpFolder + filename).pipe(tableStream);
const stream = readFile(`${this.tmpFolder}/${filename}`).pipe(tableStream);

try {
await streamToPromise(stream);
Expand Down
10 changes: 5 additions & 5 deletions src/cli/OutputGTFSCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export class OutputGTFSCommand implements CLICommand {
* Turn the timetable feed into GTFS files
*/
public async run(argv: string[]): Promise<void> {
this.baseDir = argv[3] || "./";
this.baseDir = argv[3] || ".";

if (!fs.existsSync(this.baseDir)) {
throw new Error(`Output path ${this.baseDir} does not exist.`);
Expand Down Expand Up @@ -68,7 +68,7 @@ export class OutputGTFSCommand implements CLICommand {
*/
private async copy(results: object[] | Promise<object[]>, filename: string): Promise<void> {
const rows = await results;
const output = this.output.open(this.baseDir + filename);
const output = this.output.open(`${this.baseDir}/filename`);

console.log("Writing " + filename);
rows.forEach(row => output.write(row));
Expand All @@ -82,9 +82,9 @@ export class OutputGTFSCommand implements CLICommand {
*/
private copyTrips(schedules: Schedule[], serviceIds: ServiceIdIndex): Promise<any> {
console.log("Writing trips.txt, stop_times.txt and routes.txt");
const trips = this.output.open(this.baseDir + "trips.txt");
const stopTimes = this.output.open(this.baseDir + "stop_times.txt");
const routeFile = this.output.open(this.baseDir + "routes.txt");
const trips = this.output.open(`${this.baseDir}/trips.txt`);
const stopTimes = this.output.open(`${this.baseDir}/stop_times.txt`);
const routeFile = this.output.open(`${this.baseDir}/routes.txt`);
const routes = {};

for (const schedule of schedules) {
Expand Down
10 changes: 4 additions & 6 deletions src/cli/OutputGTFSZipCommand.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

import * as os from 'node:os';
import * as path from 'node:path';
import {CLICommand} from "./CLICommand";
import {OutputGTFSCommand} from "./OutputGTFSCommand";
import * as fs from "fs";
Expand All @@ -19,12 +21,8 @@ export class OutputGTFSZipCommand implements CLICommand {
if (fs.existsSync(filename)) {
fs.unlinkSync(filename);
}

argv[3] = "/tmp/gtfs/";

if (!fs.existsSync(argv[3])) {
fs.mkdirSync(argv[3]);
}

argv[3] = fs.mkdtempSync(path.join(os.tmpdir(), "gtfs"));

await this.command.run(argv);

Expand Down

0 comments on commit d55bbad

Please sign in to comment.