From 2926dcf46a1ca4245458fa8f68b0bb38881a3062 Mon Sep 17 00:00:00 2001 From: Martin Karkowski Date: Mon, 24 Aug 2020 09:36:46 +0200 Subject: [PATCH] adding helpers to handle uncreated dirs --- lib/helpers/fileHelpers.ts | 81 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 lib/helpers/fileHelpers.ts diff --git a/lib/helpers/fileHelpers.ts b/lib/helpers/fileHelpers.ts new file mode 100644 index 0000000..f1d77df --- /dev/null +++ b/lib/helpers/fileHelpers.ts @@ -0,0 +1,81 @@ +import { type } from "os"; +import { mkdir, writeFile} from 'fs/promises' +import { promisify } from "util"; +import { exists, BaseEncodingOptions, lstat } from "fs"; + +const _exists = promisify(exists); +const _lstat = promisify(lstat); + +/** + * Function to create a File + * @param fileName Read the File. + * @param content content which should be stored + * @param options The options to write the file. See original docu: https://nodejs.org/dist/latest-v8.x/docs/api/fs.html#fs_fs_writefile_file_data_options_callback + */ +export async function createFile(fileName: string, content: string, options?: (BaseEncodingOptions & { mode?: string | number; flag?: string | number; }) | "ascii" | "utf8" | "utf-8" | "utf16le" | "ucs2" | "ucs-2" | "base64" | "latin1" | "binary" | "hex") { + const FOLDER_SPLIT_CHAR = type() === 'Linux' ? '/' : '\\' + // Split the Path into different segements. + const pathParts = fileName.split(FOLDER_SPLIT_CHAR); + + // Pop the File. + pathParts.pop(); + + // Create the Path / Directories + await createPath(pathParts.join(FOLDER_SPLIT_CHAR)) + + // Wirte the File. + await writeFile(fileName, content, options) + + return fileName; +} + +/** + * Function to create a File + * @param path Read the File. + * @param content content which should be stored + * @param options The options to write the file. See original docu: https://nodejs.org/dist/latest-v8.x/docs/api/fs.html#fs_fs_writefile_file_data_options_callback + */ +export async function createPath(path: string) { + const FOLDER_SPLIT_CHAR = type() === 'Linux' ? '/' : '\\' + // Split the Path into different segements. + const pathParts = path.split(FOLDER_SPLIT_CHAR); + + + if (pathParts.length > 0) { + /** Check if the given Path Ends with a File Name */ + if (pathParts[pathParts.length - 1].indexOf('.') !== -1) { + pathParts.pop(); + } + + // Reassemble the Segments like that: + // + // C:\\Test\\SubFolder\\AnotherFolder + // Split up: + // => fileParts = [C: , Test , SubFolder, AnotherFolder] + // + // Reassemble: + // 1) C: + // 2) C:\\Test + // 3) C:\\Test\\SubFolder + // 4) ... + // + // Everytime after an assembly Check whether the assembled + // path exists, otherwise create that folder and go on. + + for (const [idx, folder] of pathParts.entries()) { + // Assemble the Path. + const currentPath = pathParts.slice(0, idx + 1).join(FOLDER_SPLIT_CHAR); + // Test and create the Folder + const urlExists = await _exists(currentPath); + // Test if the provided Path exists or not. + if (currentPath && !urlExists) { + await mkdir(currentPath); + } else if (currentPath && urlExists && (await _lstat(currentPath)).isFile()){ + + throw Error('Cant create File at the specified path. The given URL contains a File. See "' + currentPath +'"' ); + } + } + } + + return path; +} \ No newline at end of file