fix(core): text files missclassified as binary

This commit is contained in:
Adam
2026-02-19 07:23:37 -06:00
parent 38f7071da9
commit 8ebdbe0ea2
2 changed files with 130 additions and 3 deletions

View File

@@ -166,7 +166,6 @@ export namespace File {
"efi",
"rom",
"com",
"bat",
"cmd",
"ps1",
"sh",
@@ -203,11 +202,77 @@ export namespace File {
"x3f",
])
const textExtensions = new Set([
"ts",
"tsx",
"mts",
"cts",
"mtsx",
"ctsx",
"js",
"jsx",
"mjs",
"cjs",
"sh",
"bash",
"zsh",
"fish",
"ps1",
"psm1",
"cmd",
"bat",
"json",
"jsonc",
"json5",
"yaml",
"yml",
"toml",
"md",
"mdx",
"txt",
"xml",
"html",
"htm",
"css",
"scss",
"sass",
"less",
"graphql",
"gql",
"sql",
"ini",
"cfg",
"conf",
"env",
])
const textNames = new Set([
"dockerfile",
"makefile",
".gitignore",
".gitattributes",
".editorconfig",
".npmrc",
".nvmrc",
".prettierrc",
".eslintrc",
])
function isImageByExtension(filepath: string): boolean {
const ext = path.extname(filepath).toLowerCase().slice(1)
return imageExtensions.has(ext)
}
function isTextByExtension(filepath: string): boolean {
const ext = path.extname(filepath).toLowerCase().slice(1)
return textExtensions.has(ext)
}
function isTextByName(filepath: string): boolean {
const name = path.basename(filepath).toLowerCase()
return textNames.has(name)
}
function getImageMimeType(filepath: string): string {
const ext = path.extname(filepath).toLowerCase().slice(1)
const mimeTypes: Record<string, string> = {
@@ -445,7 +510,9 @@ export namespace File {
return { type: "text", content: "" }
}
if (isBinaryByExtension(file)) {
const text = isTextByExtension(file) || isTextByName(file)
if (isBinaryByExtension(file) && !text) {
return { type: "binary", content: "" }
}
@@ -454,7 +521,7 @@ export namespace File {
}
const mimeType = Filesystem.mimeType(full)
const encode = await shouldEncode(mimeType)
const encode = text ? false : await shouldEncode(mimeType)
if (encode && !isImage(mimeType)) {
return { type: "binary", content: "", mimeType }