mirror of
https://github.com/sourcebot-dev/sourcebot.git
synced 2025-12-12 04:15:30 +00:00
Use zoekt-webserver /print route to get file contents
This commit is contained in:
parent
0bfbf5a257
commit
9af8696a6d
6 changed files with 28 additions and 40 deletions
|
|
@ -14,7 +14,6 @@ export async function GET(request: NextRequest) {
|
||||||
["num", numResults],
|
["num", numResults],
|
||||||
["format", "json"],
|
["format", "json"],
|
||||||
);
|
);
|
||||||
console.log(url);
|
|
||||||
const res = await fetch(url);
|
const res = await fetch(url);
|
||||||
const data = await res.json();
|
const data = await res.json();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,11 @@
|
||||||
"use server";
|
"use server";
|
||||||
|
|
||||||
import { ErrorCode } from "@/lib/errorCodes";
|
import { missingQueryParam } from "@/lib/serviceError";
|
||||||
import { serviceError, missingQueryParam } from "@/lib/serviceError";
|
|
||||||
import { StatusCodes } from "http-status-codes";
|
import { StatusCodes } from "http-status-codes";
|
||||||
import { NextRequest } from "next/server";
|
import { NextRequest } from "next/server";
|
||||||
import path from "path";
|
import { GetSourceResponse, pathQueryParamName, repoQueryParamName, ZoektPrintResponse } from "@/lib/types";
|
||||||
import fs from "fs";
|
import { ZOEKT_WEBSERVER_URL } from "@/lib/environment";
|
||||||
import { GetSourceResponse, pathQueryParamName, repoQueryParamName } from "@/lib/types";
|
import { createPathWithQueryParams } from "@/lib/utils";
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the content of a source file at the given path.
|
* Returns the content of a source file at the given path.
|
||||||
|
|
@ -28,44 +26,23 @@ export async function GET(request: NextRequest) {
|
||||||
return missingQueryParam(repoQueryParamName);
|
return missingQueryParam(repoQueryParamName);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the contents of the path
|
const url = createPathWithQueryParams(
|
||||||
const repoPath = getRepoPath(repo);
|
`${ZOEKT_WEBSERVER_URL}/print`,
|
||||||
if (!repoPath) {
|
["f", filepath],
|
||||||
return serviceError({
|
["r", repo],
|
||||||
statusCode: StatusCodes.NOT_FOUND,
|
["format", "json"],
|
||||||
errorCode: ErrorCode.REPOSITORY_NOT_FOUND,
|
);
|
||||||
message: `Could not find repository '${repo}'.`
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const fullPath = path.join(repoPath, filepath);
|
const res = await fetch(url);
|
||||||
if (!fs.existsSync(fullPath)) {
|
const data = await res.json() as ZoektPrintResponse;
|
||||||
return serviceError({
|
|
||||||
statusCode: StatusCodes.NOT_FOUND,
|
|
||||||
errorCode: ErrorCode.FILE_NOT_FOUND,
|
|
||||||
message: `Could not find file '${filepath}' in repository '${repo}'.`
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// @todo : some error handling here would be nice
|
|
||||||
const content = fs.readFileSync(fullPath, "utf8");
|
|
||||||
|
|
||||||
return Response.json(
|
return Response.json(
|
||||||
{
|
{
|
||||||
content,
|
content: data.Content,
|
||||||
|
encoding: data.Encoding,
|
||||||
} satisfies GetSourceResponse,
|
} satisfies GetSourceResponse,
|
||||||
{
|
{
|
||||||
status: StatusCodes.OK
|
status: StatusCodes.OK
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// @todo : we will need to figure out a more sophisticated system for this..
|
|
||||||
const getRepoPath = (repo: string): string | undefined => {
|
|
||||||
switch (repo) {
|
|
||||||
case "monorepo":
|
|
||||||
return "/Users/brendan/monorepo"
|
|
||||||
}
|
|
||||||
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
@ -151,6 +151,7 @@ export const CodePreview = ({
|
||||||
extensions={extensions}
|
extensions={extensions}
|
||||||
/>
|
/>
|
||||||
<Scrollbar orientation="vertical" />
|
<Scrollbar orientation="vertical" />
|
||||||
|
<Scrollbar orientation="horizontal" />
|
||||||
</ScrollArea>
|
</ScrollArea>
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -105,9 +105,14 @@ export default function Home() {
|
||||||
fetch(url)
|
fetch(url)
|
||||||
.then(response => response.json())
|
.then(response => response.json())
|
||||||
.then((body: GetSourceResponse) => {
|
.then((body: GetSourceResponse) => {
|
||||||
|
if (body.encoding !== "base64") {
|
||||||
|
throw new Error("Expected base64 encoding");
|
||||||
|
}
|
||||||
|
|
||||||
|
const content = atob(body.content);
|
||||||
setSelectedMatchIndex(0);
|
setSelectedMatchIndex(0);
|
||||||
setPreviewFile({
|
setPreviewFile({
|
||||||
content: body.content,
|
content: content,
|
||||||
filepath: match.FileName,
|
filepath: match.FileName,
|
||||||
matches: match.Matches,
|
matches: match.Matches,
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ export const repoQueryParamName = "repo";
|
||||||
|
|
||||||
export type GetSourceResponse = {
|
export type GetSourceResponse = {
|
||||||
content: string;
|
content: string;
|
||||||
|
encoding: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ZoektMatch {
|
export interface ZoektMatch {
|
||||||
|
|
@ -39,4 +40,9 @@ export interface ZoektSearchResponse {
|
||||||
result: ZoektResult,
|
result: ZoektResult,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface ZoektPrintResponse {
|
||||||
|
Content: string,
|
||||||
|
Encoding: string,
|
||||||
|
}
|
||||||
|
|
||||||
export type KeymapType = "default" | "vim";
|
export type KeymapType = "default" | "vim";
|
||||||
2
vendor/zoekt
vendored
2
vendor/zoekt
vendored
|
|
@ -1 +1 @@
|
||||||
Subproject commit 96970313fe9081d5e176341a85172287402d5f44
|
Subproject commit b0fd0b52e8c6d01b5f13ba283602f291ac34b95a
|
||||||
Loading…
Reference in a new issue