diff --git a/src/lib/logMessages.ts b/src/lib/logMessages.ts index 26dfdb1..c180b8f 100644 --- a/src/lib/logMessages.ts +++ b/src/lib/logMessages.ts @@ -384,15 +384,15 @@ export const logMessages = defineLogList({ chalk`{red There was an error downloading/compressing the url:} {dim ${url}}\n{dim ${error}}` ], CSSWriteError: ({ - fileName, + filepath, error }: { - fileName: string; + filepath: string; error: unknown; }) => [ Log.error, Tag.css, - chalk`{red Could not save the CSS file} {dim ${fileName}}\n${formatError( + chalk`{red Could not save the CSS file} {dim ${filepath}}\n${formatError( error )}` ], diff --git a/src/utils/customFilesHelper.ts b/src/utils/customFilesHelper.ts new file mode 100644 index 0000000..b84cf41 --- /dev/null +++ b/src/utils/customFilesHelper.ts @@ -0,0 +1,32 @@ +import { pathExists } from "fs-extra"; +import { join } from "path"; + +import { createHash } from "@utils/createHash"; +import settings from "@utils/settings"; + +interface CustomFilesPathObject { + filepath: string; + exists: boolean; +} + +/** + * Creates a path to store an url + * Also says if the file already exists + * + * @param url The url to create a filepath for + * @param type The extension and folder of the file + * @returns An Promise containing the filepath and if it the file already exists + */ + +export const customFilesHelper = async ( + url: string, + type: "css" | "js" +): Promise => { + const urlHash = createHash(url); + const filepath = join(settings.customFilesPath, type, `${urlHash}.${type}`); + + return { + filepath, + exists: await pathExists(filepath) + }; +}; diff --git a/src/utils/downloadCSSToFile.ts b/src/utils/downloadCSSToFile.ts index 96bd4c1..bd576c3 100644 --- a/src/utils/downloadCSSToFile.ts +++ b/src/utils/downloadCSSToFile.ts @@ -1,11 +1,9 @@ -import { outputFile, pathExists } from "fs-extra"; -import { join } from "path"; +import { outputFile } from "fs-extra"; import { inspect } from "util"; import { logger } from "@lib/logger"; -import { createHash } from "@utils/createHash"; +import { customFilesHelper } from "@utils/customFilesHelper"; import { downloadCSS } from "@utils/downloadCSS"; -import settings from "@utils/settings"; /** * Uses {@link downloadCSS} to download and minify the CSS @@ -18,23 +16,19 @@ export const downloadCSSToFile = async ( custom_css: string[] ): Promise => { await Promise.all( - custom_css.map(async (cssUrl) => { - const urlHash = createHash(cssUrl); - const fileName = join( - settings.customFilesPath, - "css", - urlHash + ".css" - ); - - if (await pathExists(fileName)) { - logger.cachedCSS({ url: cssUrl }); + custom_css.map(async (url) => { + const { exists, filepath } = await customFilesHelper(url, "css"); + + if (exists) { + logger.cachedCSS({ url }); return; } - logger.downloadCSS({ url: cssUrl }); - await downloadCSS(cssUrl).then(async (output) => { + + logger.downloadCSS({ url }); + await downloadCSS(url).then(async (output) => { if ("errors" in output) { logger.CSSError({ - url: cssUrl, + url: url, error: Array.isArray(output.errors) ? output.errors.join("\n") : inspect(output.errors) @@ -43,12 +37,10 @@ export const downloadCSSToFile = async ( return; } - logger.downloadedCSS({ url: cssUrl }); - - await outputFile(fileName, output.styles).catch((e) => { - logger.CSSWriteError({ fileName, error: e }); + logger.downloadedCSS({ url }); - throw e; + await outputFile(filepath, output.styles).catch((error) => { + logger.CSSWriteError({ filepath, error }); }); }); }) diff --git a/src/utils/downloadJSToFile.ts b/src/utils/downloadJSToFile.ts index e88931e..3315a1c 100644 --- a/src/utils/downloadJSToFile.ts +++ b/src/utils/downloadJSToFile.ts @@ -1,10 +1,6 @@ -import { pathExists } from "fs-extra"; -import { join } from "path"; - import { logger } from "@lib/logger"; -import { createHash } from "@utils/createHash"; +import { customFilesHelper } from "@utils/customFilesHelper"; import { downloadFile } from "@utils/downloadFile"; -import settings from "@utils/settings"; /** * Downloads files and places them in `settings.customFilesPath/js` @@ -13,22 +9,17 @@ import settings from "@utils/settings"; export const downloadJSToFile = async (custom_js: string[]): Promise => { await Promise.all( - custom_js.map(async (jsUrl): Promise => { - const urlHash = createHash(jsUrl); - const filepath = join( - settings.customFilesPath, - "js", - urlHash + ".js" - ); + custom_js.map(async (url): Promise => { + const { exists, filepath } = await customFilesHelper(url, "js"); - if (await pathExists(filepath)) { - logger.cachedJS({ url: jsUrl }); + if (exists) { + logger.cachedJS({ url }); return; } - logger.downloadJS({ url: jsUrl }); - await downloadFile(filepath, jsUrl).catch((err) => - logger.jsError({ error: err }) + logger.downloadJS({ url }); + await downloadFile(filepath, url).catch((error) => + logger.jsError({ error }) ); }) );