import { logger } from '../../../logger/index.ts'; import { getRepoContents } from '../../../modules/platform/forgejo/forgejo-helper.ts'; import type { RepoContents } from '../../../modules/platform/forgejo/schema.ts'; import { ExternalHostError } from '../../../types/errors/external-host-error.ts'; import type { Nullish } from '../../../types/index.ts'; import type { Preset, PresetConfig } from '../types.ts'; import { PRESET_DEP_NOT_FOUND, PRESET_INVALID, fetchPreset, parsePreset, } from '../util.ts'; export const Endpoint = 'https://code.forgejo.org/'; export async function fetchJSONFile( repo: string, fileName: string, endpoint: string, tag?: string | null, ): Promise> { let res: RepoContents; try { res = await getRepoContents(repo, fileName, tag, { baseUrl: endpoint, }); } catch (err) { if (err instanceof ExternalHostError) { throw err; } logger.debug( `Preset file ${fileName} not found in ${repo}: ${err.message}`, ); throw new Error(PRESET_DEP_NOT_FOUND); } let contentString: string; if (res.type === 'file') { contentString = res.contentString; } else { logger.debug( `Preset ${fileName} has unexpected type '${res.type}'. Only \`file\` is supported`, ); throw new Error(PRESET_INVALID); } return parsePreset(contentString, fileName); } export function getPresetFromEndpoint( repo: string, filePreset: string, presetPath?: string, endpoint = Endpoint, tag?: string, ): Promise> { return fetchPreset({ repo, filePreset, presetPath, endpoint, tag, fetch: fetchJSONFile, }); } export function getPreset({ repo, presetName = 'default', presetPath, tag, }: PresetConfig): Promise> { return getPresetFromEndpoint(repo, presetName, presetPath, Endpoint, tag); }