Refactor PPC to pull out reusable componenets.
This commit is contained in:
21
src/main/util/createDirectoryIfNotExist.ts
Normal file
21
src/main/util/createDirectoryIfNotExist.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import log from "electron-log/main";
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
import errorTypeCheck from "../../util/errorTypeCheck";
|
||||
|
||||
const createdDirectoryIfNotExist = async (dirpath: string) => {
|
||||
try {
|
||||
const directoryPath = path.dirname(dirpath);
|
||||
if (!fs.existsSync(directoryPath)) {
|
||||
log.info(`Directory does not exist. Creating: ${directoryPath}`);
|
||||
fs.mkdirSync(directoryPath, { recursive: true });
|
||||
}
|
||||
} catch (error) {
|
||||
log.error("Error creating directory as needed", errorTypeCheck(error));
|
||||
throw new Error(
|
||||
"Error creating directory: " + errorTypeCheck(error).message,
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
export default createdDirectoryIfNotExist;
|
||||
Reference in New Issue
Block a user