Compare commits
9 commits
dev
...
kit/ripgre
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3c272cd6a8 |
||
|
|
5adba445bc |
||
|
|
c9261dbd2a |
||
|
|
19dbeea71c |
||
|
|
a7576a78ae |
||
|
|
573a10e2f4 | ||
|
|
75a87ffc5e | ||
|
|
2a10e4e89e | ||
|
|
f2a83a0a00 |
2 changed files with 71 additions and 86 deletions
|
|
@ -3,7 +3,7 @@ import path from "path"
|
||||||
import { Global } from "../global"
|
import { Global } from "../global"
|
||||||
import fs from "fs/promises"
|
import fs from "fs/promises"
|
||||||
import z from "zod"
|
import z from "zod"
|
||||||
import { Effect, Layer, Context, Schema } from "effect"
|
import { Array as Arr, Effect, Layer, Context, Schema } from "effect"
|
||||||
import * as Stream from "effect/Stream"
|
import * as Stream from "effect/Stream"
|
||||||
import { ChildProcess } from "effect/unstable/process"
|
import { ChildProcess } from "effect/unstable/process"
|
||||||
import { ChildProcessSpawner } from "effect/unstable/process/ChildProcessSpawner"
|
import { ChildProcessSpawner } from "effect/unstable/process/ChildProcessSpawner"
|
||||||
|
|
@ -20,117 +20,99 @@ import { text } from "node:stream/consumers"
|
||||||
|
|
||||||
import { ZipReader, BlobReader, BlobWriter } from "@zip.js/zip.js"
|
import { ZipReader, BlobReader, BlobWriter } from "@zip.js/zip.js"
|
||||||
import { Log } from "@/util/log"
|
import { Log } from "@/util/log"
|
||||||
|
import { zod } from "@/util/effect-zod"
|
||||||
|
|
||||||
export namespace Ripgrep {
|
export namespace Ripgrep {
|
||||||
const log = Log.create({ service: "ripgrep" })
|
const log = Log.create({ service: "ripgrep" })
|
||||||
const Stats = z.object({
|
const stats = Schema.Struct({
|
||||||
elapsed: z.object({
|
elapsed: Schema.Struct({
|
||||||
secs: z.number(),
|
secs: Schema.Number,
|
||||||
nanos: z.number(),
|
nanos: Schema.Number,
|
||||||
human: z.string(),
|
human: Schema.String,
|
||||||
}),
|
}),
|
||||||
searches: z.number(),
|
searches: Schema.Number,
|
||||||
searches_with_match: z.number(),
|
searches_with_match: Schema.Number,
|
||||||
bytes_searched: z.number(),
|
bytes_searched: Schema.Number,
|
||||||
bytes_printed: z.number(),
|
bytes_printed: Schema.Number,
|
||||||
matched_lines: z.number(),
|
matched_lines: Schema.Number,
|
||||||
matches: z.number(),
|
matches: Schema.Number,
|
||||||
})
|
})
|
||||||
|
|
||||||
const Begin = z.object({
|
const begin = Schema.Struct({
|
||||||
type: z.literal("begin"),
|
type: Schema.Literal("begin"),
|
||||||
data: z.object({
|
|
||||||
path: z.object({
|
|
||||||
text: z.string(),
|
|
||||||
}),
|
|
||||||
}),
|
|
||||||
})
|
|
||||||
|
|
||||||
export const Match = z.object({
|
|
||||||
type: z.literal("match"),
|
|
||||||
data: z.object({
|
|
||||||
path: z.object({
|
|
||||||
text: z.string(),
|
|
||||||
}),
|
|
||||||
lines: z.object({
|
|
||||||
text: z.string(),
|
|
||||||
}),
|
|
||||||
line_number: z.number(),
|
|
||||||
absolute_offset: z.number(),
|
|
||||||
submatches: z.array(
|
|
||||||
z.object({
|
|
||||||
match: z.object({
|
|
||||||
text: z.string(),
|
|
||||||
}),
|
|
||||||
start: z.number(),
|
|
||||||
end: z.number(),
|
|
||||||
}),
|
|
||||||
),
|
|
||||||
}),
|
|
||||||
})
|
|
||||||
|
|
||||||
const End = z.object({
|
|
||||||
type: z.literal("end"),
|
|
||||||
data: z.object({
|
|
||||||
path: z.object({
|
|
||||||
text: z.string(),
|
|
||||||
}),
|
|
||||||
binary_offset: z.number().nullable(),
|
|
||||||
stats: Stats,
|
|
||||||
}),
|
|
||||||
})
|
|
||||||
|
|
||||||
const Summary = z.object({
|
|
||||||
type: z.literal("summary"),
|
|
||||||
data: z.object({
|
|
||||||
elapsed_total: z.object({
|
|
||||||
human: z.string(),
|
|
||||||
nanos: z.number(),
|
|
||||||
secs: z.number(),
|
|
||||||
}),
|
|
||||||
stats: Stats,
|
|
||||||
}),
|
|
||||||
})
|
|
||||||
|
|
||||||
const Result = z.union([Begin, Match, End, Summary])
|
|
||||||
|
|
||||||
const Hit = Schema.Struct({
|
|
||||||
type: Schema.Literal("match"),
|
|
||||||
data: Schema.Struct({
|
data: Schema.Struct({
|
||||||
path: Schema.Struct({
|
path: Schema.Struct({
|
||||||
text: Schema.String,
|
text: Schema.String,
|
||||||
}),
|
}),
|
||||||
lines: Schema.Struct({
|
|
||||||
text: Schema.String,
|
|
||||||
}),
|
|
||||||
line_number: Schema.Number,
|
|
||||||
absolute_offset: Schema.Number,
|
|
||||||
submatches: Schema.mutable(
|
|
||||||
Schema.Array(
|
|
||||||
Schema.Struct({
|
|
||||||
match: Schema.Struct({
|
|
||||||
text: Schema.String,
|
|
||||||
}),
|
|
||||||
start: Schema.Number,
|
|
||||||
end: Schema.Number,
|
|
||||||
}),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
}),
|
}),
|
||||||
})
|
})
|
||||||
|
|
||||||
const Row = Schema.Union([
|
const item = Schema.Struct({
|
||||||
Schema.Struct({ type: Schema.Literal("begin"), data: Schema.Unknown }),
|
path: Schema.Struct({
|
||||||
Hit,
|
text: Schema.String,
|
||||||
Schema.Struct({ type: Schema.Literal("end"), data: Schema.Unknown }),
|
}),
|
||||||
Schema.Struct({ type: Schema.Literal("summary"), data: Schema.Unknown }),
|
lines: Schema.Struct({
|
||||||
])
|
text: Schema.String,
|
||||||
|
}),
|
||||||
|
line_number: Schema.Number,
|
||||||
|
absolute_offset: Schema.Number,
|
||||||
|
submatches: Schema.mutable(
|
||||||
|
Schema.Array(
|
||||||
|
Schema.Struct({
|
||||||
|
match: Schema.Struct({
|
||||||
|
text: Schema.String,
|
||||||
|
}),
|
||||||
|
start: Schema.Number,
|
||||||
|
end: Schema.Number,
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
})
|
||||||
|
|
||||||
const decode = Schema.decodeUnknownEffect(Schema.fromJsonString(Row))
|
const match = Schema.Struct({
|
||||||
|
type: Schema.Literal("match"),
|
||||||
|
data: item,
|
||||||
|
})
|
||||||
|
|
||||||
|
const end = Schema.Struct({
|
||||||
|
type: Schema.Literal("end"),
|
||||||
|
data: Schema.Struct({
|
||||||
|
path: Schema.Struct({
|
||||||
|
text: Schema.String,
|
||||||
|
}),
|
||||||
|
binary_offset: Schema.NullOr(Schema.Number),
|
||||||
|
stats,
|
||||||
|
}),
|
||||||
|
})
|
||||||
|
|
||||||
|
const summary = Schema.Struct({
|
||||||
|
type: Schema.Literal("summary"),
|
||||||
|
data: Schema.Struct({
|
||||||
|
elapsed_total: Schema.Struct({
|
||||||
|
human: Schema.String,
|
||||||
|
nanos: Schema.Number,
|
||||||
|
secs: Schema.Number,
|
||||||
|
}),
|
||||||
|
stats,
|
||||||
|
}),
|
||||||
|
})
|
||||||
|
|
||||||
|
const row = Schema.Union([begin, match, end, summary])
|
||||||
|
|
||||||
|
const decode = Schema.decodeUnknownSync(Schema.fromJsonString(row))
|
||||||
|
|
||||||
|
export const Stats = zod(stats)
|
||||||
|
export const Begin = zod(begin)
|
||||||
|
export const Item = zod(item)
|
||||||
|
export const Match = zod(match)
|
||||||
|
export const End = zod(end)
|
||||||
|
export const Summary = zod(summary)
|
||||||
|
export const Result = zod(row)
|
||||||
|
|
||||||
|
export type Stats = z.infer<typeof Stats>
|
||||||
export type Result = z.infer<typeof Result>
|
export type Result = z.infer<typeof Result>
|
||||||
export type Match = z.infer<typeof Match>
|
export type Match = z.infer<typeof Match>
|
||||||
export type Item = Match["data"]
|
export type Item = z.infer<typeof Item>
|
||||||
export type Begin = z.infer<typeof Begin>
|
export type Begin = z.infer<typeof Begin>
|
||||||
export type End = z.infer<typeof End>
|
export type End = z.infer<typeof End>
|
||||||
export type Summary = z.infer<typeof Summary>
|
export type Summary = z.infer<typeof Summary>
|
||||||
|
|
@ -432,10 +414,13 @@ export namespace Ripgrep {
|
||||||
Stream.decodeText(handle.stdout).pipe(
|
Stream.decodeText(handle.stdout).pipe(
|
||||||
Stream.splitLines,
|
Stream.splitLines,
|
||||||
Stream.filter((line) => line.length > 0),
|
Stream.filter((line) => line.length > 0),
|
||||||
Stream.mapEffect((line) =>
|
Stream.mapArrayEffect((lines) =>
|
||||||
decode(line).pipe(Effect.mapError((cause) => new Error("invalid ripgrep output", { cause }))),
|
Effect.try({
|
||||||
|
try: () => Arr.map(lines, (line) => decode(line)),
|
||||||
|
catch: (cause) => new Error("invalid ripgrep output", { cause }),
|
||||||
|
}),
|
||||||
),
|
),
|
||||||
Stream.filter((row): row is Schema.Schema.Type<typeof Hit> => row.type === "match"),
|
Stream.filter((row): row is Schema.Schema.Type<typeof match> => row.type === "match"),
|
||||||
Stream.map((row): Item => row.data),
|
Stream.map((row): Item => row.data),
|
||||||
Stream.runCollect,
|
Stream.runCollect,
|
||||||
Effect.map((chunk) => [...chunk]),
|
Effect.map((chunk) => [...chunk]),
|
||||||
|
|
|
||||||
|
|
@ -22,7 +22,7 @@ export const FileRoutes = lazy(() =>
|
||||||
description: "Matches",
|
description: "Matches",
|
||||||
content: {
|
content: {
|
||||||
"application/json": {
|
"application/json": {
|
||||||
schema: resolver(Ripgrep.Match.shape.data.array()),
|
schema: resolver(Ripgrep.Item.array()),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue