Spaces:
Sleeping
Sleeping
Download web/src/sources.js from brianbellx/codehygiene: direct link, hf CLI and curl.
- Browser
- Download file 2.57 kB
-
https://huggingface.co/spaces/brianbellx/codehygiene/resolve/main/web/src/sources.js
- Command line
-
hf download hf://spaces/brianbellx/codehygiene/web/src/sources.js
-
curl -L -o sources.js https://huggingface.co/spaces/brianbellx/codehygiene/resolve/main/web/src/sources.js
2.57 kB
| import presets from "shared:presets"; | |
| import { RepoError, fetchRepoFiles, isReadableText, selectLocalEntries } from "./github.js"; | |
| import { sniffExtension } from "./languages.js"; | |
| const EXAMPLE_ORDER = ["send_receipt.py", "webhook.go", "engine.js", "pricing.ts", "frame.rs"]; | |
| function makeFile(path, content, result) { | |
| return { path, content, result }; | |
| } | |
| function byScore(left, right) { | |
| return left.result.score - right.result.score || left.path.localeCompare(right.path); | |
| } | |
| function scoredFiles(entries, results) { | |
| const files = entries.map((entry, index) => makeFile(entry.path, entry.content, results[index])); | |
| return files.sort(byScore); | |
| } | |
| export async function loadExamples(scoreAll) { | |
| const ordered = EXAMPLE_ORDER.map((name) => presets.find((preset) => preset.filename === name)); | |
| const results = await scoreAll(ordered.map((preset) => preset.before)); | |
| return ordered.map((preset, index) => makeFile(preset.filename, preset.before, results[index])); | |
| } | |
| export async function loadRepo(spec, { signal, scoreAll, onProgress }) { | |
| const fetched = await fetchRepoFiles(spec, { | |
| signal, | |
| focusPath: spec.blobPath, | |
| onProgress: (done, total) => onProgress(`Fetching ${done} of ${total}`), | |
| }); | |
| const results = await scoreAll( | |
| fetched.files.map((entry) => entry.content), | |
| (done, total) => onProgress(`Scoring ${done} of ${total}`), | |
| ); | |
| signal.throwIfAborted(); | |
| return scoredFiles(fetched.files, results); | |
| } | |
| function relativePath(file) { | |
| const parts = (file.webkitRelativePath || file.name).split("/"); | |
| return parts.length > 1 ? parts.slice(1).join("/") : parts[0]; | |
| } | |
| export async function loadFolder(fileList, { scoreAll, onProgress }) { | |
| const byPath = new Map([...fileList].map((file) => [relativePath(file), file])); | |
| const entries = selectLocalEntries([...byPath].map(([path, file]) => ({ path, size: file.size }))); | |
| const texts = await Promise.all(entries.map((entry) => byPath.get(entry.path).text())); | |
| const readable = entries | |
| .map((entry, index) => ({ path: entry.path, content: texts[index] })) | |
| .filter((entry) => isReadableText(entry.content)); | |
| if (readable.length === 0) { | |
| throw new RepoError("no_files", "No code files found"); | |
| } | |
| const results = await scoreAll( | |
| readable.map((entry) => entry.content), | |
| (done, total) => onProgress(`Scoring ${done} of ${total}`), | |
| ); | |
| return scoredFiles(readable, results); | |
| } | |
| export async function loadSnippet(text, scoreAll) { | |
| const [result] = await scoreAll([text]); | |
| return [makeFile(`snippet.${sniffExtension(text)}`, text, result)]; | |
| } | |