Skip to content

Commit

Permalink
docs things
Browse files Browse the repository at this point in the history
  • Loading branch information
catplvsplus committed Jun 18, 2024
1 parent ce925fd commit 131fbc2
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 0 deletions.
25 changes: 25 additions & 0 deletions packages/reciple/src/classes/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,20 @@ export interface RecipleConfigJS {
export class ConfigReader {
public static defaultConfigFile = path.join(path.dirname(fileURLToPath(import.meta.url)), '../../static/config.mjs');

/**
* Reads the default configuration data, processes it, and returns the resulting configuration string.
*
* @return {Promise<string>} The default configuration data as a string.
*/
public static async readDefaultConfig(): Promise<RecipleConfigJS> {
return recursiveDefaults<RecipleConfigJS>(await import(this.defaultConfigFile))!;
}

/**
* Reads the default configuration data, processes it, and returns the resulting configuration string.
*
* @return {Promise<string>} The default configuration data as a string.
*/
public static async getDefaultConfigData(): Promise<string> {
let defaultConfig = (await readFile(this.defaultConfigFile, 'utf-8')).replaceAll('\r\n', '\n');

Expand All @@ -51,6 +61,15 @@ export class ConfigReader {

public static async readConfigJS(config: string|{ paths: string[]; default?: string; }, createIfNotExists?: true): Promise<RecipleConfigJS>;
public static async readConfigJS(config: string|{ paths: string[]; default?: string; }, createIfNotExists?: false): Promise<RecipleConfigJS|null>;
/**
* Reads a JavaScript configuration file and returns its contents as a `RecipleConfigJS` object.
* If the file does not exist, it can optionally create the file and return a default configuration.
*
* @param {string|{ paths: string[]; default?: string; }} config - The path to the configuration file or an object specifying multiple paths and a default path.
* @param {boolean} [createIfNotExists=true] - Whether to create the configuration file if it does not exist.
* @return {Promise<RecipleConfigJS|null>} A promise that resolves to the contents of the configuration file as a `RecipleConfigJS` object, or `null` if the file does not exist and `createIfNotExists` is `false`.
* @throws {RecipleError} If the configuration file does not contain valid data.
*/
public static async readConfigJS(config: string|{ paths: string[]; default?: string; }, createIfNotExists: boolean = true): Promise<RecipleConfigJS|null> {
if (typeof config !== 'string') {
let data: RecipleConfigJS|null = null;
Expand Down Expand Up @@ -78,6 +97,12 @@ export class ConfigReader {
return data;
}

/**
* Creates a configuration file if it doesn't exist and returns the file path.
*
* @param {string} file - The path to the configuration file.
* @return {Promise<string>} The path of the created or existing configuration file.
*/
public static async createConfigJS(file: string): Promise<string> {
if (await existsAsync(file)) return file;

Expand Down
21 changes: 21 additions & 0 deletions packages/reciple/src/utils/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ import { type RecipleClient } from '../index.js';
import { cli } from './cli.js';
import path from 'node:path';

/**
* Formats a log message with optional prefix, colored messages, and thread information.
*
* @param {string} message - The log message to format.
* @param {Logger} logger - The logger instance used to get the logger name.
* @param {PartialDeep<Exclude<RecipleConfig['logger'], Logger|undefined>> & { shards?: boolean; }} config - The logger configuration.
* @param {LoggerLevel} level - The log level.
* @return {string} The formatted log message.
*/
export function formatLogMessage(message: string, logger: Logger, config: PartialDeep<Exclude<RecipleConfig['logger'], Logger|undefined>> & { shards?: boolean; }, level: LoggerLevel): string {
const color = (msg: string) => {
if (!config.coloredMessages || level === LoggerLevel.INFO) return msg;
Expand All @@ -30,6 +39,12 @@ export function formatLogMessage(message: string, logger: Logger, config: Partia
) + ` ${message}`;
}

/**
* Creates a logger with the specified configuration.
*
* @param {Omit<PartialDeep<Exclude<RecipleConfig['logger'], Logger|undefined>>, 'enabled'> & { shards?: boolean; }} config - The configuration for the logger.
* @return {Promise<Logger>} The created logger instance.
*/
export async function createLogger(config?: Omit<PartialDeep<Exclude<RecipleConfig['logger'], Logger|undefined>>, 'enabled'> & { shards?: boolean; }): Promise<Logger> {
const logger = new Logger({
enableDebugmode: (cli.options.debugmode || config?.debugmode) ?? null,
Expand All @@ -55,6 +70,12 @@ export async function createLogger(config?: Omit<PartialDeep<Exclude<RecipleConf
return logger;
}

/**
* Adds event listeners to the client for various module events and reciple events.
*
* @param {RecipleClient} client - The client to add the event listeners to.
* @return {void} This function does not return anything.
*/
export function addEventListenersToClient(client: RecipleClient): void {
client.on('recipleDebug', debug => client.logger?.debug(debug));

Expand Down
7 changes: 7 additions & 0 deletions packages/reciple/src/utils/modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ import { existsAsync } from '@reciple/utils';
import micromatch from 'micromatch';
import path from 'node:path';

/**
* Recursively finds modules in specified directories based on the given configuration.
*
* @param {RecipleConfig['modules']} config - The configuration object containing directories and filters.
* @param {((filename: string) => Awaitable<boolean>)?} filter - An optional function to filter files.
* @return {Promise<string[]>} A promise that resolves to an array of module file paths.
*/
export async function findModules(config: RecipleConfig['modules'], filter?: (filename: string) => Awaitable<boolean>): Promise<string[]> {
const modules: string[] = [];
const { globby, isDynamicPattern } = await import('globby');
Expand Down
37 changes: 37 additions & 0 deletions packages/utils/src/classes/PackageUpdateChecker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ export class PackageUpdateChecker extends StrictTypedEmitter<PackageUpdateChecke
public packages: Collection<string, string> = new Collection();
public interval?: NodeJS.Timeout;

/**
* Initializes a new instance of the PackageUpdateChecker class.
*
* @param {PackageUpdateCheckerOptions} options - The options for initializing the class.
*/
constructor(options: PackageUpdateCheckerOptions) {
super();

Expand All @@ -56,6 +61,12 @@ export class PackageUpdateChecker extends StrictTypedEmitter<PackageUpdateChecke
if (options.updatecheckIntervalMs) this.startCheckInterval(options.updatecheckIntervalMs);
}

/**
* Sets up an interval to check for available updates.
*
* @param {number} ms - The interval time in milliseconds
* @return {void}
*/
public startCheckInterval(ms?: number): void {
if (this.interval) {
clearInterval(this.interval);
Expand All @@ -65,10 +76,20 @@ export class PackageUpdateChecker extends StrictTypedEmitter<PackageUpdateChecke
if (ms) this.interval = setInterval(() => this.checkForAvailableUpdates(), ms).unref();
}

/**
* Stops the interval for checking for available updates.
*
* @return {void}
*/
public stopCheckInterval(): void {
this.startCheckInterval(0);
}

/**
* Asynchronously checks for available updates for each package in the `packages` collection.
*
* @return {Promise<PackageUpdateCheckerUpdateData[]>} A promise that resolves to an array of `PackageUpdateCheckerUpdateData` objects representing the available updates for each package.
*/
public async checkForAvailableUpdates(): Promise<PackageUpdateCheckerUpdateData[]> {
const packageUpdates: PackageUpdateCheckerUpdateData[] = [];

Expand All @@ -87,6 +108,15 @@ export class PackageUpdateChecker extends StrictTypedEmitter<PackageUpdateChecke
return packageUpdates;
}

/**
* Asynchronously checks the latest available version of a package that satisfies the given version constraint.
*
* @param {string} pkg - The name of the package to check for updates.
* @param {string} version - The current version of the package.
* @param {boolean} [allowMajor=false] - Whether to allow major version updates.
* @return {Promise<PackageUpdateCheckerUpdateData>} A promise that resolves to an object containing information about the latest update, including the package name, data, current version, updated version, latest version, and update type.
* @throws {Error} If no version of the package satisfies the given version constraint.
*/
public static async checkLatestUpdate(pkg: string, version: string, allowMajor: boolean = false): Promise<PackageUpdateCheckerUpdateData> {
const currentSemver = new SemVer(version);

Expand Down Expand Up @@ -117,6 +147,13 @@ export class PackageUpdateChecker extends StrictTypedEmitter<PackageUpdateChecke

public static async fetchPackageData<T extends AbbreviatedMetadata = AbbreviatedMetadata>(pkg: string, options?: Options): Promise<T>;
public static async fetchPackageData<T extends FullMetadata = FullMetadata>(pkg: string, options?: FullMetadata): Promise<T>;
/**
* Asynchronously fetches package data for the given package name.
*
* @param {string} pkg - The name of the package to fetch data for.
* @param {FullMetadata|Options} [options] - Optional options for fetching package data.
* @return {Promise<T>} A promise that resolves to the fetched package data.
*/
public static async fetchPackageData<T extends FullMetadata|AbbreviatedMetadata = FullMetadata|AbbreviatedMetadata>(pkg: string, options?: FullMetadata|Options): Promise<T> {
return packageJson(pkg, { ...options, allVersions: true }) as Promise<T>;
}
Expand Down

0 comments on commit 131fbc2

Please sign in to comment.