diff --git a/src/modules/reportGenerator.ts b/src/modules/reportGenerator.ts index c3229ff36..d6030afec 100644 --- a/src/modules/reportGenerator.ts +++ b/src/modules/reportGenerator.ts @@ -22,7 +22,7 @@ export default class ReportGenerator { async generate(datsStatuses: DATStatus[]): Promise { await this.progressBar.logInfo('Generating report'); - const report = this.options.getOutputReport(); + const report = this.options.getOutputReportPath(); const contents = ( await Promise.all(datsStatuses diff --git a/src/types/options.ts b/src/types/options.ts index ace5fadd7..08eea086e 100644 --- a/src/types/options.ts +++ b/src/types/options.ts @@ -2,7 +2,7 @@ import 'reflect-metadata'; import { Expose, instanceToPlain, plainToInstance } from 'class-transformer'; import fg from 'fast-glob'; -import { promises as fsPromises } from 'fs'; +import fs, { promises as fsPromises } from 'fs'; import { isNotJunk } from 'junk'; import micromatch from 'micromatch'; import moment from 'moment'; @@ -343,8 +343,19 @@ export default class Options implements OptionsProps { return fsPoly.makeLegal(output); } - getOutputReport(): string { - const output = this.shouldWrite() ? this.output : process.cwd(); + getOutputReportPath(): string { + let output = process.cwd(); + if (this.shouldWrite()) { + // Write to the output dir if writing + output = this.output; + } else if (this.input.length === 1) { + // Write to the input dir if there is only one + let [input] = this.input; + while (!fs.existsSync(input)) { + input = path.dirname(input); + } + } + return path.join( output, fsPoly.makeLegal(`${Constants.COMMAND_NAME}_${moment().format()}.csv`), diff --git a/test/igir.test.ts b/test/igir.test.ts index 05f70128e..50d8c5bfc 100644 --- a/test/igir.test.ts +++ b/test/igir.test.ts @@ -41,7 +41,7 @@ async function expectEndToEnd(optionsProps: OptionsProps, expectedFiles: string[ await fsPoly.rm(tempOutput, { force: true, recursive: true }); const reports = await fg(path.join( - path.dirname(options.getOutputReport()), + path.dirname(options.getOutputReportPath()), `${Constants.COMMAND_NAME}_*.csv`, )); await Promise.all(reports.map(async (report) => fsPoly.rm(report)));