From 12502fdd22472642faadd12f319ed3c6d340a26c Mon Sep 17 00:00:00 2001 From: Stephen Carter Date: Thu, 4 Jan 2024 13:52:38 -0500 Subject: [PATCH] @W-14689342@: (Part 10) implement review feedback --- src/lib/output/OutfileResultsProcessor.ts | 4 +-- src/lib/output/OutputUtils.ts | 13 ---------- src/lib/output/RunResultsProcessor.ts | 4 +-- src/lib/util/FileHandler.ts | 31 ++++++++++++++++------- 4 files changed, 26 insertions(+), 26 deletions(-) delete mode 100644 src/lib/output/OutputUtils.ts diff --git a/src/lib/output/OutfileResultsProcessor.ts b/src/lib/output/OutfileResultsProcessor.ts index 515b4f889..0da855785 100644 --- a/src/lib/output/OutfileResultsProcessor.ts +++ b/src/lib/output/OutfileResultsProcessor.ts @@ -1,8 +1,8 @@ import {ResultsProcessor} from "./ResultsProcessor"; import {Results} from "./Results"; import {OutputFormat} from "./OutputFormat"; -import {writeToFile} from "./OutputUtils"; import {FormattedOutput} from "../../types"; +import {FileHandler} from "../util/FileHandler"; /** * Processes results to produce an output file @@ -19,6 +19,6 @@ export class OutfileResultsProcessor implements ResultsProcessor { public async processResults(results: Results): Promise { const fileContents: FormattedOutput = await results.toFormattedOutput(this.outputFormat, this.verboseViolations); - writeToFile(this.outfile, fileContents as string); + (new FileHandler()).writeFileSync(this.outfile, fileContents as string); } } diff --git a/src/lib/output/OutputUtils.ts b/src/lib/output/OutputUtils.ts deleted file mode 100644 index 907f42bad..000000000 --- a/src/lib/output/OutputUtils.ts +++ /dev/null @@ -1,13 +0,0 @@ -import {SfError} from "@salesforce/core"; -import {INTERNAL_ERROR_CODE} from "../../Constants"; -import fs = require('fs'); - -export function writeToFile(file: string, fileContents: string): void { - try { - fs.writeFileSync(file, fileContents); - } catch (e) { - // Rethrow any errors as SfError. - const message: string = e instanceof Error ? e.message : e as string; - throw new SfError(message, null, null, INTERNAL_ERROR_CODE); - } -} diff --git a/src/lib/output/RunResultsProcessor.ts b/src/lib/output/RunResultsProcessor.ts index a938e86e8..58267b02c 100644 --- a/src/lib/output/RunResultsProcessor.ts +++ b/src/lib/output/RunResultsProcessor.ts @@ -7,8 +7,8 @@ import {INTERNAL_ERROR_CODE} from "../../Constants"; import {OutputFormat} from "./OutputFormat"; import {Results} from "./Results"; import {ResultsProcessor} from "./ResultsProcessor"; -import {writeToFile} from "./OutputUtils"; import {JsonReturnValueHolder} from "./JsonReturnValueHolder"; +import {FileHandler} from "../util/FileHandler"; export type RunOutputOptions = { format: OutputFormat; @@ -134,7 +134,7 @@ export class RunResultsProcessor implements ResultsProcessor { private writeToOutfile(results: string | {columns; rows}): string { // At this point, we can cast `results` to a string, since it being an object would indicate that the format // is `table`, and we already have validations preventing tables from being written to files. - writeToFile(this.opts.outfile, results as string); + (new FileHandler()).writeFileSync(this.opts.outfile, results as string); // Return a message indicating the action we took. return getMessage(BundleName.RunOutputProcessor, 'output.writtenToOutFile', [this.opts.outfile]); diff --git a/src/lib/util/FileHandler.ts b/src/lib/util/FileHandler.ts index e0adaa8b7..498e7cfb3 100644 --- a/src/lib/util/FileHandler.ts +++ b/src/lib/util/FileHandler.ts @@ -1,5 +1,8 @@ -import {Stats, promises as fs, constants as fsConstants} from 'fs'; +import {Stats, promises as fsp, constants as fsConstants} from 'fs'; +import fs = require('fs'); import tmp = require('tmp'); +import {SfError} from "@salesforce/core"; +import {INTERNAL_ERROR_CODE} from "../../Constants"; type DuplicationFn = (src: string, target: string) => Promise; @@ -10,7 +13,7 @@ type DuplicationFn = (src: string, target: string) => Promise; export class FileHandler { async exists(filename: string): Promise { try { - await fs.access(filename, fsConstants.F_OK); + await fsp.access(filename, fsConstants.F_OK); return true; } catch (e) { return false; @@ -18,7 +21,7 @@ export class FileHandler { } stats(filename: string): Promise { - return fs.stat(filename); + return fsp.stat(filename); } async isDir(filename: string): Promise { @@ -30,24 +33,34 @@ export class FileHandler { } readDir(filename: string): Promise { - return fs.readdir(filename); + return fsp.readdir(filename); } readFileAsBuffer(filename: string): Promise { - return fs.readFile(filename); + return fsp.readFile(filename); } readFile(filename: string): Promise { - return fs.readFile(filename, 'utf-8'); + return fsp.readFile(filename, 'utf-8'); } async mkdirIfNotExists(dir: string): Promise { - await fs.mkdir(dir, {recursive: true}); + await fsp.mkdir(dir, {recursive: true}); return; } writeFile(filename: string, fileContent: string): Promise { - return fs.writeFile(filename, fileContent); + return fsp.writeFile(filename, fileContent); + } + + writeFileSync(filename: string, fileContent: string): void { + try { + fs.writeFileSync(filename, fileContent); + } catch (e) { + // Rethrow any errors as SfError. + const message: string = e instanceof Error ? e.message : e as string; + throw new SfError(message, null, null, INTERNAL_ERROR_CODE); + } } // Create a temp file that will automatically be cleaned up when the process exits. @@ -86,7 +99,7 @@ export class FileHandler { // at the moment. So we'll go with this semi-naive implementation, aware that it performs SLIGHTLY worse than // an optimal one, and prepared to address it if there's somehow a problem. // These are the file duplication functions available to us, in order of preference. - const dupFns: DuplicationFn[] = [fs.symlink, fs.link, fs.copyFile]; + const dupFns: DuplicationFn[] = [fsp.symlink, fsp.link, fsp.copyFile]; const errMsgs: string[] = []; // Iterate over the potential duplication methods....