mirror of
https://github.com/sharevb/it-tools.git
synced 2026-11-27 16:09:01 +03:00
Offline mode uses the snapshot only; online, it replaces the hardcoded fallback list when GitHub can't be reached. Refresh it with pnpm script:update:git-templates. Refs sharevb/it-tools#571 Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
103 lines
3.5 KiB
TypeScript
103 lines
3.5 KiB
TypeScript
// Template loading shared by the gitignore and gitattributes generators: live from GitHub, with a bundled snapshot
|
|
// (generated by scripts/update-git-templates.mjs) used in offline mode or whenever GitHub can't be reached.
|
|
|
|
export interface GitTemplatesSnapshot {
|
|
repository: string;
|
|
ref: string;
|
|
commit: string;
|
|
date: string;
|
|
templates: Record<string, string>;
|
|
}
|
|
|
|
export interface GitTemplatesSource {
|
|
/** GitHub `owner/repo` */
|
|
repository: string;
|
|
/** Branch the live templates are read from */
|
|
ref: string;
|
|
/** Template file extension, e.g. `.gitignore` */
|
|
extension: string;
|
|
/** Lazily loads the bundled snapshot, so it stays out of the bundle until needed */
|
|
loadSnapshot: () => Promise<GitTemplatesSnapshot>;
|
|
}
|
|
|
|
type Fetch = (url: string) => Promise<Response>;
|
|
|
|
export function liveTemplateUrl({ repository, ref, extension }: GitTemplatesSource, name: string) {
|
|
return `https://raw.githubusercontent.com/${repository}/${ref}/${name}${extension}`;
|
|
}
|
|
|
|
async function fetchOk(fetchFn: Fetch, url: string) {
|
|
const response = await fetchFn(url);
|
|
if (!response.ok) {
|
|
throw new Error(`${url}: ${response.status} ${response.statusText}`);
|
|
}
|
|
return response;
|
|
}
|
|
|
|
/** Template names (paths without extension) from a GitHub recursive tree listing */
|
|
export function templateNamesFromTree(tree: { path: string }[], extension: string) {
|
|
return tree
|
|
.filter(({ path }) => path.endsWith(extension))
|
|
.map(({ path }) => path.slice(0, -extension.length))
|
|
.filter(Boolean);
|
|
}
|
|
|
|
/** Snapshot commit date as YYYY-MM-DD, for display */
|
|
export function snapshotDay(snapshot: GitTemplatesSnapshot) {
|
|
return snapshot.date.slice(0, 10);
|
|
}
|
|
|
|
/**
|
|
* Lists the available templates: live from the GitHub API, or from the snapshot when `offline` or when the live
|
|
* listing fails. `snapshot` is set when the snapshot was used.
|
|
*/
|
|
export async function loadTemplateNames(
|
|
source: GitTemplatesSource,
|
|
{ offline, fetchFn = fetch }: { offline: boolean; fetchFn?: Fetch },
|
|
): Promise<{ names: string[]; snapshot?: GitTemplatesSnapshot }> {
|
|
if (!offline) {
|
|
try {
|
|
const response = await fetchOk(
|
|
fetchFn,
|
|
`https://api.github.com/repos/${source.repository}/git/trees/${source.ref}?recursive=true`,
|
|
);
|
|
const { tree } = await response.json();
|
|
const names = templateNamesFromTree(tree, source.extension);
|
|
if (names.length) {
|
|
return { names };
|
|
}
|
|
} catch {}
|
|
}
|
|
const snapshot = await source.loadSnapshot();
|
|
return { names: Object.keys(snapshot.templates), snapshot };
|
|
}
|
|
|
|
/**
|
|
* Fetches one template live, or reads it from the snapshot when `offline` or when the live fetch fails.
|
|
* `url` is where the content comes from (pinned to the snapshot commit when the snapshot was used).
|
|
*/
|
|
export async function loadTemplate(
|
|
source: GitTemplatesSource,
|
|
name: string,
|
|
{ offline, fetchFn = fetch }: { offline: boolean; fetchFn?: Fetch },
|
|
): Promise<{ content: string; url: string; snapshot?: GitTemplatesSnapshot }> {
|
|
let liveError: unknown;
|
|
if (!offline) {
|
|
const url = liveTemplateUrl(source, name);
|
|
try {
|
|
return { content: await (await fetchOk(fetchFn, url)).text(), url };
|
|
} catch (e) {
|
|
liveError = e;
|
|
}
|
|
}
|
|
const snapshot = await source.loadSnapshot();
|
|
if (!Object.hasOwn(snapshot.templates, name)) {
|
|
throw liveError ?? new Error(`Unknown template: ${name}`);
|
|
}
|
|
return {
|
|
content: snapshot.templates[name],
|
|
url: `https://raw.githubusercontent.com/${source.repository}/${snapshot.commit}/${name}${source.extension}`,
|
|
snapshot,
|
|
};
|
|
}
|