|
| 1 | +/** |
| 2 | + * Sync highlight queries from pinned upstream tree-sitter repos. |
| 3 | + * |
| 4 | + * Usage: |
| 5 | + * bun run scripts/sync-upstream-queries.ts |
| 6 | + * bun run scripts/sync-upstream-queries.ts --check |
| 7 | + */ |
| 8 | + |
| 9 | +import { readFile, writeFile } from "node:fs/promises"; |
| 10 | +import { existsSync } from "node:fs"; |
| 11 | +import { join, resolve } from "node:path"; |
| 12 | + |
| 13 | +type Replacement = { |
| 14 | + find: string; |
| 15 | + replace: string; |
| 16 | +}; |
| 17 | + |
| 18 | +type QuerySourceEntry = { |
| 19 | + repository: string; |
| 20 | + revision: string; |
| 21 | + queryPath: string; |
| 22 | + targetPath: string; |
| 23 | + overridePath?: string; |
| 24 | + replacements?: Replacement[]; |
| 25 | +}; |
| 26 | + |
| 27 | +type QuerySources = Record<string, QuerySourceEntry>; |
| 28 | + |
| 29 | +const ROOT = resolve(import.meta.dirname, ".."); |
| 30 | +const SOURCES_PATH = join(ROOT, "query-sources.json"); |
| 31 | +const CHECK_MODE = process.argv.includes("--check"); |
| 32 | + |
| 33 | +function normalizeNewlines(input: string): string { |
| 34 | + return input.replace(/\r\n/g, "\n"); |
| 35 | +} |
| 36 | + |
| 37 | +function ensureTrailingNewline(input: string): string { |
| 38 | + return input.endsWith("\n") ? input : `${input}\n`; |
| 39 | +} |
| 40 | + |
| 41 | +function applyReplacements(content: string, replacements: Replacement[] | undefined): string { |
| 42 | + if (!replacements || replacements.length === 0) { |
| 43 | + return content; |
| 44 | + } |
| 45 | + |
| 46 | + let next = content; |
| 47 | + for (const replacement of replacements) { |
| 48 | + if (!next.includes(replacement.find)) { |
| 49 | + throw new Error(`Replacement target not found: ${replacement.find}`); |
| 50 | + } |
| 51 | + next = next.split(replacement.find).join(replacement.replace); |
| 52 | + } |
| 53 | + |
| 54 | + return next; |
| 55 | +} |
| 56 | + |
| 57 | +function buildRawUrl(entry: QuerySourceEntry): string { |
| 58 | + return `https://raw.githubusercontent.com/${entry.repository}/${entry.revision}/${entry.queryPath}`; |
| 59 | +} |
| 60 | + |
| 61 | +function buildGeneratedHeader(name: string, entry: QuerySourceEntry): string { |
| 62 | + return [ |
| 63 | + "; AUTO-GENERATED FILE - DO NOT EDIT DIRECTLY.", |
| 64 | + `; Source: https://github.com/${entry.repository}/blob/${entry.revision}/${entry.queryPath}`, |
| 65 | + `; Generator: scripts/sync-upstream-queries.ts (${name})`, |
| 66 | + "; Local customizations belong in highlights.override.scm.", |
| 67 | + "", |
| 68 | + ].join("\n"); |
| 69 | +} |
| 70 | + |
| 71 | +function buildGeneratedQuery( |
| 72 | + name: string, |
| 73 | + entry: QuerySourceEntry, |
| 74 | + upstreamContent: string, |
| 75 | + overrideContent: string | null, |
| 76 | +): string { |
| 77 | + const header = buildGeneratedHeader(name, entry); |
| 78 | + const upstream = ensureTrailingNewline(normalizeNewlines(upstreamContent)).trimEnd(); |
| 79 | + |
| 80 | + if (!overrideContent || overrideContent.trim().length === 0) { |
| 81 | + return `${header}${upstream}\n`; |
| 82 | + } |
| 83 | + |
| 84 | + const normalizedOverride = ensureTrailingNewline(normalizeNewlines(overrideContent)).trimEnd(); |
| 85 | + return `${header}${upstream}\n\n; --- Athas overrides ---\n${normalizedOverride}\n`; |
| 86 | +} |
| 87 | + |
| 88 | +async function fetchText(url: string): Promise<string> { |
| 89 | + const response = await fetch(url); |
| 90 | + if (!response.ok) { |
| 91 | + throw new Error(`Failed to fetch ${url}: ${response.status} ${response.statusText}`); |
| 92 | + } |
| 93 | + return await response.text(); |
| 94 | +} |
| 95 | + |
| 96 | +async function syncEntry(name: string, entry: QuerySourceEntry): Promise<{ |
| 97 | + name: string; |
| 98 | + changed: boolean; |
| 99 | +}> { |
| 100 | + const rawUrl = buildRawUrl(entry); |
| 101 | + const targetPath = join(ROOT, entry.targetPath); |
| 102 | + const overridePath = entry.overridePath ? join(ROOT, entry.overridePath) : null; |
| 103 | + |
| 104 | + const upstreamRaw = await fetchText(rawUrl); |
| 105 | + const patchedUpstream = applyReplacements(upstreamRaw, entry.replacements); |
| 106 | + const overrideContent = |
| 107 | + overridePath && existsSync(overridePath) ? await readFile(overridePath, "utf8") : null; |
| 108 | + |
| 109 | + const generated = buildGeneratedQuery(name, entry, patchedUpstream, overrideContent); |
| 110 | + const existing = existsSync(targetPath) ? await readFile(targetPath, "utf8") : ""; |
| 111 | + const changed = normalizeNewlines(existing) !== normalizeNewlines(generated); |
| 112 | + |
| 113 | + if (CHECK_MODE) { |
| 114 | + if (changed) { |
| 115 | + throw new Error( |
| 116 | + `${name}: ${entry.targetPath} is out of date. Run: bun run scripts/sync-upstream-queries.ts`, |
| 117 | + ); |
| 118 | + } |
| 119 | + return { name, changed: false }; |
| 120 | + } |
| 121 | + |
| 122 | + if (changed) { |
| 123 | + await writeFile(targetPath, generated, "utf8"); |
| 124 | + } |
| 125 | + |
| 126 | + return { name, changed }; |
| 127 | +} |
| 128 | + |
| 129 | +async function main() { |
| 130 | + const rawConfig = await readFile(SOURCES_PATH, "utf8"); |
| 131 | + const sources = JSON.parse(rawConfig) as QuerySources; |
| 132 | + |
| 133 | + const names = Object.keys(sources).sort(); |
| 134 | + if (names.length === 0) { |
| 135 | + console.log("No query sources configured."); |
| 136 | + return; |
| 137 | + } |
| 138 | + |
| 139 | + const results = []; |
| 140 | + for (const name of names) { |
| 141 | + const result = await syncEntry(name, sources[name]); |
| 142 | + results.push(result); |
| 143 | + const label = CHECK_MODE ? "checked" : result.changed ? "updated" : "unchanged"; |
| 144 | + console.log(`${name}: ${label}`); |
| 145 | + } |
| 146 | + |
| 147 | + if (CHECK_MODE) { |
| 148 | + console.log(`\nQuery sources check passed (${results.length} entries).`); |
| 149 | + } else { |
| 150 | + const updated = results.filter((entry) => entry.changed).length; |
| 151 | + console.log(`\nQuery sync complete (${updated}/${results.length} updated).`); |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +await main(); |
0 commit comments