Skip to content

Commit d55bbad

Browse files
authored
use unique temp directory (#99)
Co-authored-by: Michael Tsang <michael.tsang@jnction.co.uk>
1 parent dbcba6a commit d55bbad

File tree

4 files changed

+17
-16
lines changed

4 files changed

+17
-16
lines changed

src/cli/Container.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import * as memoize from "memoized-class-decorator";
2+
import * as fs from 'node:fs';
3+
import * as os from 'node:os';
4+
import * as path from 'node:path';
25
import {CLICommand} from "./CLICommand";
36
import {ImportFeedCommand} from "./ImportFeedCommand";
47
import {DatabaseConfiguration, DatabaseConnection} from "../database/DatabaseConnection";
@@ -45,22 +48,22 @@ export class Container {
4548

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

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

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

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

6669

src/cli/ImportFeedCommand.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ export class ImportFeedCommand implements CLICommand {
127127
const file = this.getFeedFile(filename);
128128
const tables = await this.tables(file);
129129
const tableStream = new MySQLStream(filename, file, tables);
130-
const stream = readFile(this.tmpFolder + filename).pipe(tableStream);
130+
const stream = readFile(`${this.tmpFolder}/${filename}`).pipe(tableStream);
131131

132132
try {
133133
await streamToPromise(stream);

src/cli/OutputGTFSCommand.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export class OutputGTFSCommand implements CLICommand {
2525
* Turn the timetable feed into GTFS files
2626
*/
2727
public async run(argv: string[]): Promise<void> {
28-
this.baseDir = argv[3] || "./";
28+
this.baseDir = argv[3] || ".";
2929

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

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

9090
for (const schedule of schedules) {

src/cli/OutputGTFSZipCommand.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11

2+
import * as os from 'node:os';
3+
import * as path from 'node:path';
24
import {CLICommand} from "./CLICommand";
35
import {OutputGTFSCommand} from "./OutputGTFSCommand";
46
import * as fs from "fs";
@@ -19,12 +21,8 @@ export class OutputGTFSZipCommand implements CLICommand {
1921
if (fs.existsSync(filename)) {
2022
fs.unlinkSync(filename);
2123
}
22-
23-
argv[3] = "/tmp/gtfs/";
24-
25-
if (!fs.existsSync(argv[3])) {
26-
fs.mkdirSync(argv[3]);
27-
}
24+
25+
argv[3] = fs.mkdtempSync(path.join(os.tmpdir(), "gtfs"));
2826

2927
await this.command.run(argv);
3028

0 commit comments

Comments
 (0)