polymech-astro/packages/imagetools_3/utils/runtimeChecks.js
2025-12-30 09:58:29 +01:00

109 lines
3.0 KiB
JavaScript

// @ts-check
import fs from "node:fs";
import path from "node:path";
import findCacheDir from "find-cache-dir";
import filterConfigs from "./filterConfigs.js";
import { findUpSync } from "find-up"
import { get_var } from "@polymech/commons/config"
import { resolve } from "@polymech/commons"
import { sync as mkdir } from "@polymech/fs/dir"
import { sync as exists } from "@polymech/fs/exists"
import os from "node:os";
// Boost libuv threadpool size on Windows for better sharp performance
// See: https://sharp.pixelplumbing.com/performance/
if (os.platform() === "win32" && !process.env.UV_THREADPOOL_SIZE) {
process.env.UV_THREADPOOL_SIZE = "8";
}
// Sharp related checks
export const sharp = await (async () => {
try {
if (await import("sharp")) {
return true;
}
} catch (error) {
return false;
}
})();
export const supportedImageTypes = [
"avif",
"jpeg",
"jpg",
"png",
"webp",
...(sharp ? ["heic", "heif", "tiff", "gif"] : ["jxl", "wp2"]),
];
// prettier-ignore
export const supportedConfigs = [
"src", "alt", "tag", "content", "sizes", "preload", "loading", "decoding", "attributes",
"layout", "placeholder", "breakpoints", "objectFit", "objectPosition", "backgroundSize",
"backgroundPosition", "format", "fallbackFormat", "includeSourceFormat", "formatOptions",
"fadeInTransition", "artDirectives", "flip", "flop", "invert", "flatten", "normalize",
"grayscale", "hue", "saturation", "brightness", "w", "h", "ar", "width", "height", "aspect",
"background", "tint", "blur", "median", "rotate", "quality", "fit", "kernel", "position",
"cacheDir", "assetFileNames", "cacheRoot"
];
const configFile = findUpSync([
"astro-imagetools.config.js",
"astro-imagetools.config.mjs",
]);
/* @vite-ignore */
const configFunction = configFile ? await import(configFile).catch(async () => await import("/" + configFile)) : null;
const rawGlobalConfigOptions = configFunction?.default ?? {};
const NonGlobalConfigOptions = ["src", "alt", "content"];
const GlobalConfigs = supportedConfigs.filter(
(key) => !NonGlobalConfigOptions.includes(key)
);
const GlobalConfigOptions = filterConfigs(
"Global",
rawGlobalConfigOptions,
GlobalConfigs
);
export { GlobalConfigOptions };
// CWD
export const cwd = process.cwd().split(path.sep).join(path.posix.sep);
const { cacheDir } = GlobalConfigOptions;
// FS Cache related checks
let fsCachePath =
(cacheDir
? cwd + cacheDir
: findCacheDir({
name: "astro-imagetools",
})) + "/";
fs.existsSync(fsCachePath) || fs.mkdirSync(fsCachePath, { recursive: true });
const cache_dir = () => {
if (!GlobalConfigOptions.cacheRoot) return false;
let dir;
if (!exists(GlobalConfigOptions.cacheRoot)) {
dir = path.resolve(resolve(GlobalConfigOptions.cacheRoot, false, {
"POLYMECH-CACHE": get_var("POLYMECH-CACHE")
}))
if (!exists(dir)) {
mkdir(dir);
}
} else {
dir = GlobalConfigOptions.cacheRoot
}
return dir + "/";
}
// @todo : cache dir
const pm_cache_dir = cache_dir();
fsCachePath = pm_cache_dir ? pm_cache_dir : fsCachePath;
export { fsCachePath };