Skip to content

Commit

Permalink
Extract code used by both custom files to one function
Browse files Browse the repository at this point in the history
  • Loading branch information
Netfloex committed May 10, 2022
1 parent 071a6bc commit f2ba478
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 42 deletions.
6 changes: 3 additions & 3 deletions src/lib/logMessages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
)}`
],
Expand Down
32 changes: 32 additions & 0 deletions src/utils/customFilesHelper.ts
Original file line number Diff line number Diff line change
@@ -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<object> containing the filepath and if it the file already exists
*/

export const customFilesHelper = async (
url: string,
type: "css" | "js"
): Promise<CustomFilesPathObject> => {
const urlHash = createHash(url);
const filepath = join(settings.customFilesPath, type, `${urlHash}.${type}`);

return {
filepath,
exists: await pathExists(filepath)
};
};
36 changes: 14 additions & 22 deletions src/utils/downloadCSSToFile.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -18,23 +16,19 @@ export const downloadCSSToFile = async (
custom_css: string[]
): Promise<void> => {
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)
Expand All @@ -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 });
});
});
})
Expand Down
25 changes: 8 additions & 17 deletions src/utils/downloadJSToFile.ts
Original file line number Diff line number Diff line change
@@ -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`
Expand All @@ -13,22 +9,17 @@ import settings from "@utils/settings";

export const downloadJSToFile = async (custom_js: string[]): Promise<void> => {
await Promise.all(
custom_js.map(async (jsUrl): Promise<void> => {
const urlHash = createHash(jsUrl);
const filepath = join(
settings.customFilesPath,
"js",
urlHash + ".js"
);
custom_js.map(async (url): Promise<void> => {
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 })
);
})
);
Expand Down

0 comments on commit f2ba478

Please sign in to comment.