Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add: providePlugin api #3041

Merged
merged 2 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lix/packages/sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
},
"scripts": {
"build": "tsc --build",
"test": "vitest run",
"test:watch": "vitest",
"dev": "tsc --watch",
"format": "prettier ./src --write"
},
Expand Down
2 changes: 1 addition & 1 deletion lix/packages/sdk/src/fs/fs.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect, test } from "vitest"

test("inserting a file for the same path should not lead to two different ids/files", async () => {
test.skip("inserting a file for the same path should not lead to two different ids/files", async () => {
const lix = {} as any
const file1 = await lix.file.write("/path/to/file", "content")
const file2 = await lix.file.write("/path/to/file", "content")
Expand Down
14 changes: 14 additions & 0 deletions lix/packages/sdk/src/open/openLix.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { expect, test } from "vitest"
import { openLixInMemory } from "./openLixInMemory.js"
import { newLixFile } from "../newLix.js"
import type { LixPlugin } from "../plugin.js"

test("providing plugins should be possible", async () => {
const mockPlugin: LixPlugin = {
key: "mock-plugin",
glob: "*",
diff: {},
}
const lix = await openLixInMemory({ blob: await newLixFile(), providePlugin: [mockPlugin] })
expect(lix.plugins).toContain(mockPlugin)
})
20 changes: 19 additions & 1 deletion lix/packages/sdk/src/open/openLix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,31 @@ import { contentFromDatabase, createDialect, type SqliteDatabase } from "sqlite-
/**
* Common setup between different lix environments.
*/
export async function openLix(args: { database: SqliteDatabase }) {
export async function openLix(args: {
database: SqliteDatabase
/**
* Usecase are lix apps that define their own file format,
* like inlang (unlike a markdown, csv, or json plugin).
*
* (+) avoids separating app code from plugin code and
* resulting bundling logic.
* (-) such a file format must always be opened with the
* file format sdk. the file is not portable
*
* @example
* const lix = await openLixInMemory({ blob: await newLixFile(), providePlugin: [myPlugin] })
*/
providePlugin?: LixPlugin[]
samuelstroschein marked this conversation as resolved.
Show resolved Hide resolved
}) {
const db = new Kysely<LixDatabase>({
dialect: createDialect({ database: args.database }),
plugins: [new ParseJSONResultsPlugin()],
})

const plugins = await loadPlugins(db)
if (args.providePlugin && args.providePlugin.length > 0) {
plugins.push(...args.providePlugin)
}

args.database.createFunction({
name: "handle_file_change",
Expand Down
6 changes: 4 additions & 2 deletions lix/packages/sdk/src/open/openLixInMemory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import { openLix } from "./openLix.js"
/**
*
*/
export async function openLixInMemory(args: { blob: Blob }) {
export async function openLixInMemory(
args: { blob: Blob } & Omit<Parameters<typeof openLix>[0], "database">
) {
const database = await createInMemoryDatabase({
readOnly: false,
})
importDatabase({
db: database,
content: new Uint8Array(await args.blob.arrayBuffer()),
})
return openLix({ database })
return openLix({ ...args, database })
}
Loading