Skip to content

Commit

Permalink
fix: auto create output dir in CLI to make more tests run without set…
Browse files Browse the repository at this point in the history
…ting up directory structure
  • Loading branch information
modesty committed May 11, 2024
1 parent d962b8e commit b0b8ea3
Showing 1 changed file with 25 additions and 16 deletions.
41 changes: 25 additions & 16 deletions src/cli/p2jcli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ class PDFProcessor {
}

//public methods
validateParams() {
async validateParams() {
let retVal = '';

if (!fs.existsSync(this.inputDir))
Expand All @@ -177,9 +177,14 @@ class PDFProcessor {
else if (!fs.existsSync(this.inputPath))
retVal =
`Input error: input file doesn't exist - ${this.inputPath}.`;
else if (!fs.existsSync(this.outputDir))
retVal =
`Input error: output directory doesn't exist - ${this.outputDir}.`;
else if (!fs.existsSync(this.outputDir)) {
try {
await fs.promises.mkdir(this.outputDir, { recursive: true });
} finally {
if (!fs.existsSync(this.outputDir))
retVal = `Input error: output directory doesn't exist and fails to create - ${this.outputDir}.`;
}
}

if (retVal !== '') {
this.curCLI.addResultCount(retVal);
Expand Down Expand Up @@ -225,18 +230,22 @@ class PDFProcessor {

processFile() {
return new Promise((resolve, reject) => {
const validateMsg = this.validateParams();
if (validateMsg !== '') {
reject(validateMsg);
} else {
const parserFunc = PROCESS_WITH_STREAM
? this.parseOnePDFStream
: this.parseOnePDF;
parserFunc
.call(this)
.then((value) => resolve(value))
.catch((err) => reject(err));
}
this.validateParams()
.then((validateMsg) => {
if (validateMsg !== '') {
reject(validateMsg);
}
else {
const parserFunc = PROCESS_WITH_STREAM
? this.parseOnePDFStream
: this.parseOnePDF;
parserFunc
.call(this)
.then((value) => resolve(value))
.catch((err) => reject(err));
}
})
.catch((err) => reject(err));
});
}

Expand Down

0 comments on commit b0b8ea3

Please sign in to comment.