mirror of
https://github.com/trustgraph-ai/trustgraph.git
synced 2026-07-01 09:29:38 +02:00
Use Match for CLI MIME detection
This commit is contained in:
parent
89f9d63b88
commit
a0d98a573b
4 changed files with 51 additions and 22 deletions
|
|
@ -9,7 +9,7 @@
|
|||
"build": "bunx --bun tsc",
|
||||
"dev": "tsc --watch",
|
||||
"clean": "rm -rf dist",
|
||||
"test": "bunx --bun vitest run --passWithNoTests"
|
||||
"test": "bunx --bun vitest run --passWithNoTests --exclude=dist/**"
|
||||
},
|
||||
"dependencies": {
|
||||
"@trustgraph/base": "workspace:*",
|
||||
|
|
|
|||
22
ts/packages/cli/src/__tests__/library.test.ts
Normal file
22
ts/packages/cli/src/__tests__/library.test.ts
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
import { describe, expect, it } from "vitest";
|
||||
import { guessMimeType } from "../commands/library.js";
|
||||
|
||||
describe("library CLI helpers", () => {
|
||||
it("detects known MIME types through the Match-backed extension mapper", () => {
|
||||
expect(guessMimeType("paper.pdf")).toBe("application/pdf");
|
||||
expect(guessMimeType("notes.TXT")).toBe("text/plain");
|
||||
expect(guessMimeType("readme.md")).toBe("text/markdown");
|
||||
expect(guessMimeType("index.html")).toBe("text/html");
|
||||
expect(guessMimeType("partial.htm")).toBe("text/html");
|
||||
expect(guessMimeType("data.json")).toBe("application/json");
|
||||
expect(guessMimeType("table.csv")).toBe("text/csv");
|
||||
expect(guessMimeType("brief.docx")).toBe(
|
||||
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
||||
);
|
||||
});
|
||||
|
||||
it("falls back for unknown or extensionless paths", () => {
|
||||
expect(guessMimeType("archive.bin")).toBe("application/octet-stream");
|
||||
expect(guessMimeType("README")).toBe("application/octet-stream");
|
||||
});
|
||||
});
|
||||
|
|
@ -5,7 +5,7 @@
|
|||
*/
|
||||
|
||||
import type { Command } from "commander";
|
||||
import { Effect } from "effect";
|
||||
import { Effect, Match } from "effect";
|
||||
import { cliCommandError, withSocket, writeJson } from "./util.js";
|
||||
|
||||
function basenamePath(filepath: string): string {
|
||||
|
|
@ -15,27 +15,19 @@ function basenamePath(filepath: string): string {
|
|||
}
|
||||
|
||||
/** Simple MIME-type lookup by file extension. */
|
||||
function guessMimeType(filepath: string): string {
|
||||
export function guessMimeType(filepath: string): string {
|
||||
const ext = filepath.split(".").pop()?.toLowerCase();
|
||||
switch (ext) {
|
||||
case "pdf":
|
||||
return "application/pdf";
|
||||
case "txt":
|
||||
return "text/plain";
|
||||
case "md":
|
||||
return "text/markdown";
|
||||
case "html":
|
||||
case "htm":
|
||||
return "text/html";
|
||||
case "json":
|
||||
return "application/json";
|
||||
case "csv":
|
||||
return "text/csv";
|
||||
case "docx":
|
||||
return "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
|
||||
default:
|
||||
return "application/octet-stream";
|
||||
}
|
||||
return Match.value(ext).pipe(
|
||||
Match.when("pdf", () => "application/pdf"),
|
||||
Match.when("txt", () => "text/plain"),
|
||||
Match.when("md", () => "text/markdown"),
|
||||
Match.when("html", () => "text/html"),
|
||||
Match.when("htm", () => "text/html"),
|
||||
Match.when("json", () => "application/json"),
|
||||
Match.when("csv", () => "text/csv"),
|
||||
Match.when("docx", () => "application/vnd.openxmlformats-officedocument.wordprocessingml.document"),
|
||||
Match.orElse(() => "application/octet-stream"),
|
||||
);
|
||||
}
|
||||
|
||||
export function registerLibraryCommands(program: Command): void {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue