63 lines
2.4 KiB
JavaScript
63 lines
2.4 KiB
JavaScript
import fs from "node:fs";
|
|
import { dirname, resolve } from "node:path";
|
|
import process from "node:process";
|
|
import { cyan } from "colorette";
|
|
import { consola } from "consola";
|
|
import { createConfigLoader } from "unconfig";
|
|
//#region src/index.ts
|
|
async function loadConfig(cwd = process.cwd(), configOrPath = cwd, extraConfigSources = [], defaults = {}) {
|
|
let inlineConfig = {};
|
|
let hasUserCustomConfig = false;
|
|
if (typeof configOrPath !== "string") {
|
|
hasUserCustomConfig = true;
|
|
inlineConfig = configOrPath;
|
|
if (inlineConfig.configFile === false) return {
|
|
config: inlineConfig,
|
|
sources: []
|
|
};
|
|
else configOrPath = inlineConfig.configFile || process.cwd();
|
|
}
|
|
const resolved = resolve(configOrPath);
|
|
let isFile = false;
|
|
if (fs.existsSync(resolved) && fs.statSync(resolved).isFile()) {
|
|
isFile = true;
|
|
cwd = dirname(resolved);
|
|
} else if (/\.(?:ts|js|mjs|cjs|mts)$/.test(configOrPath) && resolve(configOrPath) !== resolve(cwd)) throw new Error(`[@unocss/config] Custom config file not found: ${cyan(configOrPath)}. Please check the path and try again.`);
|
|
const result = await createConfigLoader({
|
|
sources: isFile ? [{
|
|
files: resolved,
|
|
extensions: []
|
|
}] : [{ files: ["unocss.config", "uno.config"] }, ...extraConfigSources],
|
|
cwd
|
|
}).load();
|
|
if (!hasUserCustomConfig && !isFile && !result.config) consola.error(`[@unocss/config] Config file not found in ${cyan(configOrPath)} - loading default config.`);
|
|
result.config = Object.assign(defaults, inlineConfig, result.config ?? {});
|
|
if (result.config.configDeps) result.sources = [...result.sources, ...result.config.configDeps.map((i) => resolve(cwd, i))];
|
|
return result;
|
|
}
|
|
/**
|
|
* Create a factory function that returns a config loader that recovers from errors.
|
|
*
|
|
* When it fails to load the config, it will return the last successfully loaded config.
|
|
*
|
|
* Mainly used for dev-time where users might have a broken config in between changes.
|
|
*/
|
|
function createRecoveryConfigLoader() {
|
|
let lastResolved;
|
|
return async (cwd = process.cwd(), configOrPath = cwd, extraConfigSources = [], defaults = {}) => {
|
|
try {
|
|
const config = await loadConfig(cwd, configOrPath, extraConfigSources, defaults);
|
|
lastResolved = config;
|
|
return config;
|
|
} catch (e) {
|
|
if (lastResolved) {
|
|
consola.error(`[@unocss/config] Error loading config:`, e);
|
|
return lastResolved;
|
|
}
|
|
throw e;
|
|
}
|
|
};
|
|
}
|
|
//#endregion
|
|
export { createRecoveryConfigLoader, loadConfig };
|