UNPKG

sandhog

Version:

A virtual Open Source project maintainer

200 lines (195 loc) 5.69 kB
import { findPackage } from "./chunk-4KN5GL56.js"; import { CONSTANT } from "./chunk-VYUMLP7D.js"; // src/logger/logger.ts import color from "ansi-colors"; var Logger = class { constructor(logLevel) { this.logLevel = logLevel; } VERBOSE_COLOR = "yellow"; WARNING_COLOR = "yellow"; DEBUG_COLOR = "cyan"; /** * Logs info-related messages */ info(...messages) { if (this.logLevel < 1 /* INFO */) return; console.log(...messages); } /** * Logs verbose-related messages */ verbose(...messages) { if (this.logLevel < 2 /* VERBOSE */) return; console.log(color[this.VERBOSE_COLOR]("[VERBOSE]"), ...messages); } /** * Logs debug-related messages */ debug(...messages) { if (this.logLevel < 3 /* DEBUG */) return; console.log(color[this.DEBUG_COLOR]("[DEBUG]"), ...messages); } /** * Logs warning-related messages */ warn(...messages) { console.log(color[this.WARNING_COLOR](`(!)`), ...messages); } }; // src/config/find-config/find-config.ts import path from "crosspath"; import _fs from "fs"; import json5 from "json5"; import yaml from "yaml"; async function findConfig({ filename, logger, root = process.cwd(), fs = { existsSync: _fs.existsSync, readFileSync: _fs.readFileSync }, pkg }) { if (pkg == null) { pkg = (await findPackage({ root, logger, fs })).pkg; } logger.debug(`Checking package.json for config`); if (pkg.sandhog != null) { logger.debug(`Matched config in package.json`); return pkg.sandhog; } return await findConfigRecursiveStep(root, logger, fs, filename); } async function findConfigRecursiveStep(root, logger, fs, filename) { const absolutePaths = filename != null ? [path.isAbsolute(filename) ? path.normalize(filename) : path.join(root, filename)] : [ path.join(root, CONSTANT.configFilenameJs), path.join(root, CONSTANT.configFilenameCjs), path.join(root, CONSTANT.configFilenameMjs), path.join(root, CONSTANT.configFilenameJson), path.join(root, CONSTANT.configFilenameJson5), path.join(root, CONSTANT.configFilenameYaml), path.join(root, CONSTANT.configFilenameYml), path.join(root, CONSTANT.configFilenameRc) ]; for (const absolutePath of absolutePaths) { const nativeAbsolutePath = path.native.normalize(absolutePath); logger.debug(`Checking path for config: ${nativeAbsolutePath}`); if (fs.existsSync(nativeAbsolutePath)) { logger.debug(`Matched config at path: ${nativeAbsolutePath}`); const ext = path.extname(absolutePath); switch (ext) { case "": case ".json5": case ".json": return json5.parse(fs.readFileSync(nativeAbsolutePath, "utf8")); case ".yml": case ".yaml": return yaml.parse(fs.readFileSync(nativeAbsolutePath, "utf8")); default: { const result = await import(`file://${absolutePath}`); return "default" in result ? result.default : result; } } } } const newRoot = path.join(root, "../"); if (newRoot === root || newRoot === "/" || newRoot === "") return void 0; return await findConfigRecursiveStep(newRoot, logger, fs, filename); } // src/config/default-sandhog-config.ts import prettier from "prettier"; var DEFAULT_SANDHOG_CONFIG = (async () => ({ isDevelopmentPackage: false, logo: { height: 80 }, featureImage: { height: 180 }, donate: { other: { donors: [] }, patreon: {}, openCollective: {} }, readme: { badges: { exclude: [] }, sections: { exclude: [] } }, prettier: await (async () => { const defaultConfig = { // Tabs don't print well in Github Flavored Markdown Syntax highlighting useTabs: false, semi: true, tabWidth: 2, singleQuote: false, trailingComma: "none", bracketSpacing: false, printWidth: 90, arrowParens: "avoid" }; try { const prettierConfig = await prettier.resolveConfig(process.cwd()); if (prettierConfig != null) { return prettierConfig; } return defaultConfig; } catch { return defaultConfig; } })() }))(); // src/config/get-config/get-config.ts async function getConfig(options) { let config = await findConfig(options); if (config == null) { config = {}; } const defaultConfig = await DEFAULT_SANDHOG_CONFIG; return { isDevelopmentPackage: config.isDevelopmentPackage ?? defaultConfig.isDevelopmentPackage, logo: { height: config.logo?.height ?? defaultConfig.logo.height, url: config.logo?.url ?? defaultConfig.logo.url }, featureImage: { height: config.featureImage?.height ?? defaultConfig.featureImage.height, url: config.featureImage?.url ?? defaultConfig.featureImage.url }, prettier: config.prettier ?? defaultConfig.prettier, donate: { other: { donors: config.donate?.other?.donors ?? defaultConfig.donate.other.donors }, patreon: { username: config.donate?.patreon?.username ?? defaultConfig.donate.patreon.username, userId: config.donate?.patreon?.userId ?? defaultConfig.donate.patreon.userId }, openCollective: { project: config.donate?.openCollective?.project ?? defaultConfig.donate.openCollective.project } }, readme: { badges: { exclude: config.readme?.badges?.exclude ?? defaultConfig.readme.badges.exclude }, sections: { exclude: config.readme?.sections?.exclude ?? defaultConfig.readme.sections.exclude } } }; } export { Logger, findConfig, getConfig }; //# sourceMappingURL=chunk-SDA7SBLT.js.map