Skip to content

Commit

Permalink
remove minimatch dependency in favor of sqlite glob
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelstroschein committed Nov 19, 2024
1 parent cae17bc commit 3007630
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 16 deletions.
1 change: 0 additions & 1 deletion packages/lix-sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
"dedent": "1.5.1",
"js-sha256": "^0.11.0",
"kysely": "^0.27.4",
"minimatch": "^10.0.1",
"sqlite-wasm-kysely": "workspace: *",
"uuid": "^10.0.0"
},
Expand Down
33 changes: 30 additions & 3 deletions packages/lix-sdk/src/change-queue/file-handlers.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { ChangeQueueEntry } from "../database/schema.js";
import type { DetectedChange } from "../plugin/lix-plugin.js";
import { minimatch } from "minimatch";
import { updateChangesInVersion } from "../version/update-changes-in-version.js";
import { changeIsLeafInVersion } from "../query-filter/change-is-leaf-in-version.js";
import { createSnapshot } from "../snapshot/create-snapshot.js";
import type { Lix } from "../lix/open-lix.js";
import { sql } from "kysely";

// start a new normalize path function that has the absolute minimum implementation.
function normalizePath(path: string) {
Expand All @@ -14,6 +14,21 @@ function normalizePath(path: string) {
return path;
}

async function glob(args: {
lix: Pick<Lix, "db">;
glob: string;
path: string;
}) {
const result =
await sql`SELECT CASE WHEN ${args.path} GLOB ${args.glob} THEN 1 ELSE 0 END AS matches`.execute(
args.lix.db,
);

// Extract the result from the response
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return (result.rows[0] as any)?.matches === 1;
}

// creates initial changes for new files
export async function handleFileInsert(args: {
lix: Pick<Lix, "db" | "plugin">;
Expand All @@ -34,7 +49,13 @@ export async function handleFileInsert(args: {

for (const plugin of plugins) {
// glob expressions are expressed relative without leading / but path has leading /
if (!minimatch(normalizePath(path), "/" + plugin.detectChangesGlob)) {
if (
!(await glob({
lix: args.lix,
path: normalizePath(path),
glob: "/" + plugin.detectChangesGlob,
}))
) {
break;
}

Expand Down Expand Up @@ -125,7 +146,13 @@ export async function handleFileChange(args: {

for (const plugin of plugins) {
// glob expressions are expressed relative without leading / but path has leading /
if (!minimatch(normalizePath(path), "/" + plugin.detectChangesGlob)) {
if (
!(await glob({
lix: args.lix,
path: normalizePath(path),
glob: "/" + plugin.detectChangesGlob,
}))
) {
break;
}
if (plugin.detectChanges === undefined) {
Expand Down
28 changes: 16 additions & 12 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3007630

Please sign in to comment.