@livestore/cli 0.0.0-snapshot-787c029038cb898b80318bc0495055e6737a40e6 → 0.0.0-snapshot-0f27d343553b9bb260543bf20de36d216f53c5d8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/__tests__/fixtures/mock-config.d.ts +56 -0
- package/dist/__tests__/fixtures/mock-config.d.ts.map +1 -0
- package/dist/__tests__/fixtures/mock-config.js +88 -0
- package/dist/__tests__/fixtures/mock-config.js.map +1 -0
- package/dist/__tests__/sync-operations.test.d.ts +2 -0
- package/dist/__tests__/sync-operations.test.d.ts.map +1 -0
- package/dist/__tests__/sync-operations.test.js +167 -0
- package/dist/__tests__/sync-operations.test.js.map +1 -0
- package/dist/cli.d.ts +15 -1
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +2 -1
- package/dist/cli.js.map +1 -1
- package/dist/commands/import-export.d.ts +34 -0
- package/dist/commands/import-export.d.ts.map +1 -0
- package/dist/commands/import-export.js +133 -0
- package/dist/commands/import-export.js.map +1 -0
- package/dist/commands/mcp-coach.d.ts +2 -2
- package/dist/commands/mcp-coach.d.ts.map +1 -1
- package/dist/commands/mcp-tool-handlers.d.ts +5 -1
- package/dist/commands/mcp-tool-handlers.d.ts.map +1 -1
- package/dist/commands/mcp-tool-handlers.js +42 -4
- package/dist/commands/mcp-tool-handlers.js.map +1 -1
- package/dist/commands/mcp-tools-defs.d.ts +31 -1
- package/dist/commands/mcp-tools-defs.d.ts.map +1 -1
- package/dist/commands/mcp-tools-defs.js +87 -5
- package/dist/commands/mcp-tools-defs.js.map +1 -1
- package/dist/commands/new-project.d.ts +1 -1
- package/dist/mcp-runtime/runtime.d.ts +4 -3
- package/dist/mcp-runtime/runtime.d.ts.map +1 -1
- package/dist/mcp-runtime/runtime.js +20 -53
- package/dist/mcp-runtime/runtime.js.map +1 -1
- package/dist/module-loader.d.ts +22 -0
- package/dist/module-loader.d.ts.map +1 -0
- package/dist/module-loader.js +75 -0
- package/dist/module-loader.js.map +1 -0
- package/dist/sync-operations.d.ts +121 -0
- package/dist/sync-operations.d.ts.map +1 -0
- package/dist/sync-operations.js +180 -0
- package/dist/sync-operations.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +15 -8
- package/src/__tests__/fixtures/mock-config.ts +104 -0
- package/src/__tests__/sync-operations.test.ts +230 -0
- package/src/cli.ts +2 -1
- package/src/commands/import-export.ts +278 -0
- package/src/commands/mcp-tool-handlers.ts +50 -5
- package/src/commands/mcp-tools-defs.ts +92 -4
- package/src/mcp-runtime/runtime.ts +32 -65
- package/src/module-loader.ts +93 -0
- package/src/sync-operations.ts +360 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@livestore/cli",
|
|
3
|
-
"version": "0.0.0-snapshot-
|
|
3
|
+
"version": "0.0.0-snapshot-0f27d343553b9bb260543bf20de36d216f53c5d8",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"exports": {
|
|
@@ -10,18 +10,25 @@
|
|
|
10
10
|
"livestore": "./dist/bin.js"
|
|
11
11
|
},
|
|
12
12
|
"dependencies": {
|
|
13
|
+
"@effect/ai": "0.29.0",
|
|
13
14
|
"@effect/ai-openai": "0.32.0",
|
|
14
|
-
"@
|
|
15
|
-
"@
|
|
16
|
-
"@
|
|
17
|
-
"@
|
|
18
|
-
"@
|
|
19
|
-
"
|
|
15
|
+
"@effect/experimental": "0.56.0",
|
|
16
|
+
"@effect/opentelemetry": "0.58.0",
|
|
17
|
+
"@effect/platform": "0.92.1",
|
|
18
|
+
"@effect/rpc": "0.71.1",
|
|
19
|
+
"@standard-schema/spec": "1.0.0",
|
|
20
|
+
"effect": "3.18.4",
|
|
21
|
+
"@livestore/common": "0.0.0-snapshot-0f27d343553b9bb260543bf20de36d216f53c5d8",
|
|
22
|
+
"@livestore/sync-cf": "0.0.0-snapshot-0f27d343553b9bb260543bf20de36d216f53c5d8",
|
|
23
|
+
"@livestore/adapter-node": "0.0.0-snapshot-0f27d343553b9bb260543bf20de36d216f53c5d8",
|
|
24
|
+
"@livestore/livestore": "0.0.0-snapshot-0f27d343553b9bb260543bf20de36d216f53c5d8",
|
|
25
|
+
"@livestore/peer-deps": "0.0.0-snapshot-0f27d343553b9bb260543bf20de36d216f53c5d8",
|
|
26
|
+
"@livestore/utils": "0.0.0-snapshot-0f27d343553b9bb260543bf20de36d216f53c5d8"
|
|
20
27
|
},
|
|
21
28
|
"devDependencies": {
|
|
22
29
|
"@types/node": "24.10.1",
|
|
23
30
|
"typescript": "5.9.2",
|
|
24
|
-
"@livestore/utils-dev": "0.0.0-snapshot-
|
|
31
|
+
"@livestore/utils-dev": "0.0.0-snapshot-0f27d343553b9bb260543bf20de36d216f53c5d8"
|
|
25
32
|
},
|
|
26
33
|
"files": [
|
|
27
34
|
"package.json",
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import path from 'node:path'
|
|
2
|
+
import { fileURLToPath, pathToFileURL } from 'node:url'
|
|
3
|
+
import { Events, makeSchema, State } from '@livestore/common/schema'
|
|
4
|
+
import type { MockSyncBackend } from '@livestore/common/sync'
|
|
5
|
+
import { EventFactory } from '@livestore/common/testing'
|
|
6
|
+
import { Effect, FileSystem, type Mailbox, Schema } from '@livestore/utils/effect'
|
|
7
|
+
|
|
8
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url))
|
|
9
|
+
|
|
10
|
+
const items = State.SQLite.table({
|
|
11
|
+
name: 'items',
|
|
12
|
+
columns: {
|
|
13
|
+
id: State.SQLite.text({ primaryKey: true }),
|
|
14
|
+
title: State.SQLite.text({ default: '', nullable: false }),
|
|
15
|
+
},
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
export const events = {
|
|
19
|
+
itemAdded: Events.synced({
|
|
20
|
+
name: 'itemAdded',
|
|
21
|
+
schema: Schema.Struct({ id: Schema.String, title: Schema.String }),
|
|
22
|
+
}),
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const materializers = State.SQLite.materializers(events, {
|
|
26
|
+
itemAdded: ({ id, title }) => items.insert({ id, title }),
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
const state = State.SQLite.makeState({
|
|
30
|
+
tables: { items },
|
|
31
|
+
materializers,
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
export const schema = makeSchema({ state, events })
|
|
35
|
+
|
|
36
|
+
const tmpDir = path.join(process.cwd(), 'tmp', 'cli-sync-tests')
|
|
37
|
+
const schemaModuleUrl = pathToFileURL(path.join(__dirname, 'mock-config.ts')).href
|
|
38
|
+
|
|
39
|
+
/** Generates a per-test config module exporting schema, a mock backend, and connection event taps. */
|
|
40
|
+
const makeTempConfig = () => {
|
|
41
|
+
const moduleSource = `
|
|
42
|
+
import { schema } from ${JSON.stringify(schemaModuleUrl)}
|
|
43
|
+
import { makeMockSyncBackend } from '@livestore/common/sync'
|
|
44
|
+
import { Effect, Mailbox } from '@livestore/utils/effect'
|
|
45
|
+
|
|
46
|
+
export const mockBackend = await Effect.runPromise(Effect.scoped(makeMockSyncBackend({ startConnected: true })))
|
|
47
|
+
export const connectionEvents = await Effect.runPromise(Mailbox.make<'connect' | 'disconnect'>())
|
|
48
|
+
|
|
49
|
+
export { schema }
|
|
50
|
+
|
|
51
|
+
export const syncBackend = (_args) =>
|
|
52
|
+
mockBackend.makeSyncBackend.pipe(
|
|
53
|
+
Effect.tap(() => connectionEvents.offer('connect')),
|
|
54
|
+
Effect.map((backend) => {
|
|
55
|
+
const disconnect = backend.disconnect ?? Effect.void
|
|
56
|
+
return {
|
|
57
|
+
...backend,
|
|
58
|
+
disconnect: disconnect.pipe(Effect.tap(() => connectionEvents.offer('disconnect'))),
|
|
59
|
+
}
|
|
60
|
+
}),
|
|
61
|
+
)
|
|
62
|
+
`
|
|
63
|
+
|
|
64
|
+
return moduleSource
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Creates a temporary config module (schema + mock backend) and cleans it up afterwards.
|
|
69
|
+
* Returns the module path plus handles to the backend and connection event mailbox, keeping lifecycle assertions local to each test.
|
|
70
|
+
*/
|
|
71
|
+
export const useMockConfig = Effect.acquireRelease(
|
|
72
|
+
Effect.gen(function* () {
|
|
73
|
+
const fs = yield* FileSystem.FileSystem
|
|
74
|
+
|
|
75
|
+
yield* fs.makeDirectory(tmpDir, { recursive: true })
|
|
76
|
+
|
|
77
|
+
const tempPath = path.join(tmpDir, `mock-config-${Date.now()}-${Math.random().toString(16).slice(2)}.ts`)
|
|
78
|
+
const moduleSource = makeTempConfig()
|
|
79
|
+
|
|
80
|
+
yield* fs.writeFileString(tempPath, moduleSource)
|
|
81
|
+
|
|
82
|
+
const mod = (yield* Effect.tryPromise({
|
|
83
|
+
try: () => import(pathToFileURL(tempPath).href),
|
|
84
|
+
catch: (cause) => (cause instanceof Error ? cause : new Error(String(cause))),
|
|
85
|
+
})) as {
|
|
86
|
+
mockBackend: MockSyncBackend
|
|
87
|
+
connectionEvents: Mailbox.Mailbox<'connect' | 'disconnect'>
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
return { configPath: tempPath, mockBackend: mod.mockBackend, connectionEvents: mod.connectionEvents }
|
|
91
|
+
}),
|
|
92
|
+
({ configPath }) =>
|
|
93
|
+
Effect.gen(function* () {
|
|
94
|
+
const fs = yield* FileSystem.FileSystem
|
|
95
|
+
yield* fs.remove(configPath, { recursive: false }).pipe(Effect.catchAll(() => Effect.void))
|
|
96
|
+
}),
|
|
97
|
+
)
|
|
98
|
+
|
|
99
|
+
export const makeEventFactory = () =>
|
|
100
|
+
EventFactory.makeFactory(events)({
|
|
101
|
+
client: EventFactory.clientIdentity('cli-test-client'),
|
|
102
|
+
startSeq: 1,
|
|
103
|
+
initialParent: 'root',
|
|
104
|
+
})
|
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
import type { LiveStoreEvent } from '@livestore/common/schema'
|
|
2
|
+
import { Chunk, Effect, FetchHttpClient, Layer, Mailbox, Stream } from '@livestore/utils/effect'
|
|
3
|
+
import { PlatformNode } from '@livestore/utils/node'
|
|
4
|
+
import { Vitest } from '@livestore/utils-dev/node-vitest'
|
|
5
|
+
import { expect } from 'vitest'
|
|
6
|
+
import { pullEventsFromSyncBackend, pushEventsToSyncBackend } from '../sync-operations.ts'
|
|
7
|
+
import { makeEventFactory, useMockConfig } from './fixtures/mock-config.ts'
|
|
8
|
+
|
|
9
|
+
const baseLayer = Layer.mergeAll(PlatformNode.NodeFileSystem.layer, FetchHttpClient.layer)
|
|
10
|
+
const withTestCtx = Vitest.makeWithTestCtx({ makeLayer: () => baseLayer })
|
|
11
|
+
|
|
12
|
+
/** Each test acquires its own temporary config module via useMockConfig, avoiding shared state. */
|
|
13
|
+
Vitest.describe('sync-operations', { timeout: 10_000 }, () => {
|
|
14
|
+
const storeId = 'test-store'
|
|
15
|
+
const clientId = 'test-client'
|
|
16
|
+
|
|
17
|
+
/** Collects the connect + disconnect lifecycle emitted by the mock sync backend. */
|
|
18
|
+
const expectConnectLifecycle = (
|
|
19
|
+
mailbox: Mailbox.Mailbox<'connect' | 'disconnect'>,
|
|
20
|
+
): Effect.Effect<ReadonlyArray<'connect' | 'disconnect'>> =>
|
|
21
|
+
Mailbox.toStream(mailbox).pipe(
|
|
22
|
+
Stream.take(2),
|
|
23
|
+
Stream.runCollect,
|
|
24
|
+
Effect.map((chunk) => Chunk.toReadonlyArray(chunk)),
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
Vitest.scopedLive('exports events and releases the backend connection', (test: Vitest.TestContext) =>
|
|
28
|
+
Effect.gen(function* () {
|
|
29
|
+
const { mockBackend, connectionEvents, configPath } = yield* useMockConfig
|
|
30
|
+
const factory = makeEventFactory()
|
|
31
|
+
|
|
32
|
+
const batch = [
|
|
33
|
+
factory.itemAdded.next({ id: 'e1', title: 'First' }),
|
|
34
|
+
factory.itemAdded.next({ id: 'e2', title: 'Second' }),
|
|
35
|
+
]
|
|
36
|
+
|
|
37
|
+
yield* mockBackend.advance(...batch)
|
|
38
|
+
|
|
39
|
+
const result = yield* pullEventsFromSyncBackend({
|
|
40
|
+
configPath,
|
|
41
|
+
storeId,
|
|
42
|
+
clientId,
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
expect(result.eventCount).toBe(2)
|
|
46
|
+
expect(result.data.events).toHaveLength(2)
|
|
47
|
+
|
|
48
|
+
const lifecycle = yield* expectConnectLifecycle(connectionEvents)
|
|
49
|
+
expect(lifecycle).toEqual(['connect', 'disconnect'])
|
|
50
|
+
}).pipe(withTestCtx(test)),
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
Vitest.scopedLive('fails import when backend is not empty', (test: Vitest.TestContext) =>
|
|
54
|
+
Effect.gen(function* () {
|
|
55
|
+
const { mockBackend, connectionEvents, configPath } = yield* useMockConfig
|
|
56
|
+
const factory = makeEventFactory()
|
|
57
|
+
|
|
58
|
+
yield* mockBackend.advance(factory.itemAdded.next({ id: 'existing', title: 'Present' }))
|
|
59
|
+
|
|
60
|
+
const importBatch = [
|
|
61
|
+
factory.itemAdded.next({ id: 'incoming-1', title: 'Incoming' }),
|
|
62
|
+
factory.itemAdded.next({ id: 'incoming-2', title: 'Incoming 2' }),
|
|
63
|
+
]
|
|
64
|
+
|
|
65
|
+
const result = yield* pushEventsToSyncBackend({
|
|
66
|
+
configPath,
|
|
67
|
+
storeId,
|
|
68
|
+
clientId,
|
|
69
|
+
data: {
|
|
70
|
+
version: 1,
|
|
71
|
+
storeId,
|
|
72
|
+
exportedAt: new Date().toISOString(),
|
|
73
|
+
eventCount: importBatch.length,
|
|
74
|
+
events: importBatch,
|
|
75
|
+
},
|
|
76
|
+
force: false,
|
|
77
|
+
dryRun: false,
|
|
78
|
+
}).pipe(Effect.either)
|
|
79
|
+
|
|
80
|
+
expect(result._tag).toBe('Left')
|
|
81
|
+
if (result._tag === 'Left') {
|
|
82
|
+
expect(result.left._tag).toBe('ImportError')
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const lifecycle = yield* expectConnectLifecycle(connectionEvents)
|
|
86
|
+
expect(lifecycle).toEqual(['connect', 'disconnect'])
|
|
87
|
+
}).pipe(withTestCtx(test)),
|
|
88
|
+
)
|
|
89
|
+
|
|
90
|
+
Vitest.scopedLive('supports dry-run import and releases backend', (test: Vitest.TestContext) =>
|
|
91
|
+
Effect.gen(function* () {
|
|
92
|
+
const { configPath, connectionEvents } = yield* useMockConfig
|
|
93
|
+
const factory = makeEventFactory()
|
|
94
|
+
const importBatch = [factory.itemAdded.next({ id: 'dry-run', title: 'Simulated' })]
|
|
95
|
+
|
|
96
|
+
const result = yield* pushEventsToSyncBackend({
|
|
97
|
+
configPath,
|
|
98
|
+
storeId,
|
|
99
|
+
clientId,
|
|
100
|
+
data: {
|
|
101
|
+
version: 1,
|
|
102
|
+
storeId,
|
|
103
|
+
exportedAt: new Date().toISOString(),
|
|
104
|
+
eventCount: importBatch.length,
|
|
105
|
+
events: importBatch,
|
|
106
|
+
},
|
|
107
|
+
force: false,
|
|
108
|
+
dryRun: true,
|
|
109
|
+
})
|
|
110
|
+
|
|
111
|
+
expect(result.dryRun).toBe(true)
|
|
112
|
+
expect(result.eventCount).toBe(importBatch.length)
|
|
113
|
+
|
|
114
|
+
const lifecycle = yield* expectConnectLifecycle(connectionEvents)
|
|
115
|
+
expect(lifecycle).toEqual(['connect', 'disconnect'])
|
|
116
|
+
}).pipe(withTestCtx(test)),
|
|
117
|
+
)
|
|
118
|
+
|
|
119
|
+
Vitest.scopedLive('imports events into empty backend with progress and batching', (test: Vitest.TestContext) =>
|
|
120
|
+
Effect.gen(function* () {
|
|
121
|
+
const { mockBackend, configPath, connectionEvents } = yield* useMockConfig
|
|
122
|
+
const factory = makeEventFactory()
|
|
123
|
+
const importBatch = Array.from({ length: 120 }, (_, idx) =>
|
|
124
|
+
factory.itemAdded.next({ id: `id-${idx + 1}`, title: `Item ${idx + 1}` }),
|
|
125
|
+
)
|
|
126
|
+
|
|
127
|
+
const progress: Array<{ pushed: number; total: number }> = []
|
|
128
|
+
|
|
129
|
+
const result = yield* pushEventsToSyncBackend({
|
|
130
|
+
configPath,
|
|
131
|
+
storeId,
|
|
132
|
+
clientId,
|
|
133
|
+
data: {
|
|
134
|
+
version: 1,
|
|
135
|
+
storeId,
|
|
136
|
+
exportedAt: new Date().toISOString(),
|
|
137
|
+
eventCount: importBatch.length,
|
|
138
|
+
events: importBatch,
|
|
139
|
+
},
|
|
140
|
+
force: false,
|
|
141
|
+
dryRun: false,
|
|
142
|
+
onProgress: (pushed, total) => Effect.sync(() => progress.push({ pushed, total })),
|
|
143
|
+
})
|
|
144
|
+
|
|
145
|
+
expect(result.dryRun).toBe(false)
|
|
146
|
+
expect(result.eventCount).toBe(importBatch.length)
|
|
147
|
+
expect(progress).toEqual([
|
|
148
|
+
{ pushed: 100, total: 120 },
|
|
149
|
+
{ pushed: 120, total: 120 },
|
|
150
|
+
])
|
|
151
|
+
|
|
152
|
+
const pushedEvents = yield* mockBackend.pushedEvents.pipe(
|
|
153
|
+
Stream.take(importBatch.length),
|
|
154
|
+
Stream.runCollect,
|
|
155
|
+
Effect.map((chunk: Chunk.Chunk<LiveStoreEvent.Global.Encoded>) => Chunk.toReadonlyArray(chunk)),
|
|
156
|
+
)
|
|
157
|
+
expect(pushedEvents.map((event) => event.seqNum)).toHaveLength(importBatch.length)
|
|
158
|
+
|
|
159
|
+
const lifecycle = yield* expectConnectLifecycle(connectionEvents)
|
|
160
|
+
expect(lifecycle).toEqual(['connect', 'disconnect'])
|
|
161
|
+
}).pipe(withTestCtx(test)),
|
|
162
|
+
)
|
|
163
|
+
|
|
164
|
+
Vitest.scopedLive('allows force import on store ID mismatch', (test: Vitest.TestContext) =>
|
|
165
|
+
Effect.gen(function* () {
|
|
166
|
+
const { mockBackend, configPath, connectionEvents } = yield* useMockConfig
|
|
167
|
+
const factory = makeEventFactory()
|
|
168
|
+
const importBatch = [factory.itemAdded.next({ id: 'force-1', title: 'Force' })]
|
|
169
|
+
|
|
170
|
+
const result = yield* pushEventsToSyncBackend({
|
|
171
|
+
configPath,
|
|
172
|
+
storeId,
|
|
173
|
+
clientId,
|
|
174
|
+
data: {
|
|
175
|
+
version: 1,
|
|
176
|
+
storeId: 'different-store',
|
|
177
|
+
exportedAt: new Date().toISOString(),
|
|
178
|
+
eventCount: importBatch.length,
|
|
179
|
+
events: importBatch,
|
|
180
|
+
},
|
|
181
|
+
force: true,
|
|
182
|
+
dryRun: false,
|
|
183
|
+
})
|
|
184
|
+
|
|
185
|
+
expect(result.dryRun).toBe(false)
|
|
186
|
+
expect(result.eventCount).toBe(importBatch.length)
|
|
187
|
+
|
|
188
|
+
const pushedEvents = yield* mockBackend.pushedEvents.pipe(
|
|
189
|
+
Stream.take(1),
|
|
190
|
+
Stream.runCollect,
|
|
191
|
+
Effect.map((chunk: Chunk.Chunk<LiveStoreEvent.Global.Encoded>) => Chunk.toReadonlyArray(chunk)),
|
|
192
|
+
)
|
|
193
|
+
expect(pushedEvents).toHaveLength(1)
|
|
194
|
+
|
|
195
|
+
const lifecycle = yield* expectConnectLifecycle(connectionEvents)
|
|
196
|
+
expect(lifecycle).toEqual(['connect', 'disconnect'])
|
|
197
|
+
}).pipe(withTestCtx(test)),
|
|
198
|
+
)
|
|
199
|
+
|
|
200
|
+
Vitest.scopedLive('rejects store ID mismatch without force', (test: Vitest.TestContext) =>
|
|
201
|
+
Effect.gen(function* () {
|
|
202
|
+
const { configPath, connectionEvents } = yield* useMockConfig
|
|
203
|
+
const factory = makeEventFactory()
|
|
204
|
+
const importBatch = [factory.itemAdded.next({ id: 'mismatch', title: 'Mismatch' })]
|
|
205
|
+
|
|
206
|
+
const result = yield* pushEventsToSyncBackend({
|
|
207
|
+
configPath,
|
|
208
|
+
storeId,
|
|
209
|
+
clientId,
|
|
210
|
+
data: {
|
|
211
|
+
version: 1,
|
|
212
|
+
storeId: 'other-store',
|
|
213
|
+
exportedAt: new Date().toISOString(),
|
|
214
|
+
eventCount: importBatch.length,
|
|
215
|
+
events: importBatch,
|
|
216
|
+
},
|
|
217
|
+
force: false,
|
|
218
|
+
dryRun: false,
|
|
219
|
+
}).pipe(Effect.either)
|
|
220
|
+
|
|
221
|
+
expect(result._tag).toBe('Left')
|
|
222
|
+
if (result._tag === 'Left') {
|
|
223
|
+
expect(result.left._tag).toBe('ImportError')
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
const lifecycle = yield* expectConnectLifecycle(connectionEvents)
|
|
227
|
+
expect(lifecycle).toEqual(['connect', 'disconnect'])
|
|
228
|
+
}).pipe(withTestCtx(test)),
|
|
229
|
+
)
|
|
230
|
+
})
|
package/src/cli.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { Cli } from '@livestore/utils/node'
|
|
2
|
+
import { syncCommand } from './commands/import-export.ts'
|
|
2
3
|
import { mcpCommand } from './commands/mcp.ts'
|
|
3
4
|
import { createCommand } from './commands/new-project.ts'
|
|
4
5
|
|
|
5
6
|
export const command = Cli.Command.make('livestore', {
|
|
6
7
|
verbose: Cli.Options.boolean('verbose').pipe(Cli.Options.withDefault(false)),
|
|
7
|
-
}).pipe(Cli.Command.withSubcommands([mcpCommand, createCommand]))
|
|
8
|
+
}).pipe(Cli.Command.withSubcommands([mcpCommand, createCommand, syncCommand]))
|
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
import path from 'node:path'
|
|
2
|
+
import type { UnknownError } from '@livestore/common'
|
|
3
|
+
import { Console, Effect, FileSystem, type HttpClient, type Scope } from '@livestore/utils/effect'
|
|
4
|
+
import { Cli } from '@livestore/utils/node'
|
|
5
|
+
import * as SyncOps from '../sync-operations.ts'
|
|
6
|
+
|
|
7
|
+
const LARGE_EVENT_WARNING_THRESHOLD = 100_000
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Export events from the sync backend to a JSON file.
|
|
11
|
+
*/
|
|
12
|
+
const exportEvents = ({
|
|
13
|
+
configPath,
|
|
14
|
+
storeId,
|
|
15
|
+
clientId,
|
|
16
|
+
outputPath,
|
|
17
|
+
}: {
|
|
18
|
+
configPath: string
|
|
19
|
+
storeId: string
|
|
20
|
+
clientId: string
|
|
21
|
+
outputPath: string
|
|
22
|
+
}): Effect.Effect<
|
|
23
|
+
void,
|
|
24
|
+
SyncOps.ExportError | SyncOps.ConnectionError | UnknownError,
|
|
25
|
+
FileSystem.FileSystem | HttpClient.HttpClient | Scope.Scope
|
|
26
|
+
> =>
|
|
27
|
+
Effect.gen(function* () {
|
|
28
|
+
yield* Console.log(`Connecting to sync backend...`)
|
|
29
|
+
|
|
30
|
+
const result = yield* SyncOps.pullEventsFromSyncBackend({ configPath, storeId, clientId })
|
|
31
|
+
|
|
32
|
+
yield* Console.log(`✓ Connected to sync backend: ${result.backendName}`)
|
|
33
|
+
yield* Console.log(`Pulled ${result.eventCount} events`)
|
|
34
|
+
if (result.eventCount > LARGE_EVENT_WARNING_THRESHOLD) {
|
|
35
|
+
yield* Console.log(
|
|
36
|
+
`Warning: exporting ${result.eventCount} events may consume significant memory. Consider exporting on a machine with enough RAM.`,
|
|
37
|
+
)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const fs = yield* FileSystem.FileSystem
|
|
41
|
+
const absOutputPath = path.isAbsolute(outputPath) ? outputPath : path.resolve(process.cwd(), outputPath)
|
|
42
|
+
|
|
43
|
+
yield* fs.writeFileString(absOutputPath, JSON.stringify(result.data, null, 2)).pipe(
|
|
44
|
+
Effect.mapError(
|
|
45
|
+
(cause) =>
|
|
46
|
+
new SyncOps.ExportError({
|
|
47
|
+
cause,
|
|
48
|
+
note: `Failed to write export file: ${cause}`,
|
|
49
|
+
}),
|
|
50
|
+
),
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
yield* Console.log(`Exported ${result.eventCount} events to ${absOutputPath}`)
|
|
54
|
+
}).pipe(Effect.withSpan('cli:export'))
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Import events from a JSON file to the sync backend.
|
|
58
|
+
*/
|
|
59
|
+
const importEvents = ({
|
|
60
|
+
configPath,
|
|
61
|
+
storeId,
|
|
62
|
+
clientId,
|
|
63
|
+
inputPath,
|
|
64
|
+
force,
|
|
65
|
+
dryRun,
|
|
66
|
+
}: {
|
|
67
|
+
configPath: string
|
|
68
|
+
storeId: string
|
|
69
|
+
clientId: string
|
|
70
|
+
inputPath: string
|
|
71
|
+
force: boolean
|
|
72
|
+
dryRun: boolean
|
|
73
|
+
}): Effect.Effect<
|
|
74
|
+
void,
|
|
75
|
+
SyncOps.ImportError | SyncOps.ConnectionError | UnknownError,
|
|
76
|
+
FileSystem.FileSystem | HttpClient.HttpClient | Scope.Scope
|
|
77
|
+
> =>
|
|
78
|
+
Effect.gen(function* () {
|
|
79
|
+
const fs = yield* FileSystem.FileSystem
|
|
80
|
+
const absInputPath = path.isAbsolute(inputPath) ? inputPath : path.resolve(process.cwd(), inputPath)
|
|
81
|
+
|
|
82
|
+
const exists = yield* fs.exists(absInputPath).pipe(
|
|
83
|
+
Effect.mapError(
|
|
84
|
+
(cause) =>
|
|
85
|
+
new SyncOps.ImportError({
|
|
86
|
+
cause,
|
|
87
|
+
note: `Failed to check file existence: ${cause}`,
|
|
88
|
+
}),
|
|
89
|
+
),
|
|
90
|
+
)
|
|
91
|
+
if (!exists) {
|
|
92
|
+
return yield* new SyncOps.ImportError({
|
|
93
|
+
cause: new Error(`File not found: ${absInputPath}`),
|
|
94
|
+
note: `Import file does not exist at ${absInputPath}`,
|
|
95
|
+
})
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
yield* Console.log(`Reading import file...`)
|
|
99
|
+
|
|
100
|
+
const fileContent = yield* fs.readFileString(absInputPath).pipe(
|
|
101
|
+
Effect.mapError(
|
|
102
|
+
(cause) =>
|
|
103
|
+
new SyncOps.ImportError({
|
|
104
|
+
cause,
|
|
105
|
+
note: `Failed to read import file: ${cause}`,
|
|
106
|
+
}),
|
|
107
|
+
),
|
|
108
|
+
)
|
|
109
|
+
|
|
110
|
+
const parsedContent = yield* Effect.try({
|
|
111
|
+
try: () => JSON.parse(fileContent),
|
|
112
|
+
catch: (error) =>
|
|
113
|
+
new SyncOps.ImportError({
|
|
114
|
+
cause: new Error(`Failed to parse JSON: ${error instanceof Error ? error.message : String(error)}`),
|
|
115
|
+
note: `Invalid JSON in import file: ${error instanceof Error ? error.message : String(error)}`,
|
|
116
|
+
}),
|
|
117
|
+
})
|
|
118
|
+
|
|
119
|
+
/** Validate export file format before proceeding */
|
|
120
|
+
const validation = yield* SyncOps.validateExportData({ data: parsedContent, targetStoreId: storeId })
|
|
121
|
+
|
|
122
|
+
if (validation.storeIdMismatch) {
|
|
123
|
+
if (!force) {
|
|
124
|
+
return yield* new SyncOps.ImportError({
|
|
125
|
+
cause: new Error(`Store ID mismatch: file has '${validation.sourceStoreId}', expected '${storeId}'`),
|
|
126
|
+
note: `The export file was created for a different store. Use --force to import anyway.`,
|
|
127
|
+
})
|
|
128
|
+
}
|
|
129
|
+
yield* Console.log(
|
|
130
|
+
`Store ID mismatch: file has '${validation.sourceStoreId}', importing to '${storeId}' (--force)`,
|
|
131
|
+
)
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
yield* Console.log(`Found ${validation.eventCount} events in export file`)
|
|
135
|
+
if (validation.eventCount > LARGE_EVENT_WARNING_THRESHOLD) {
|
|
136
|
+
yield* Console.log(
|
|
137
|
+
`Warning: importing ${validation.eventCount} events may consume significant memory. Ensure the machine has enough RAM.`,
|
|
138
|
+
)
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
if (dryRun) {
|
|
142
|
+
yield* Console.log(`Dry run - validating import file...`)
|
|
143
|
+
yield* Console.log(`Dry run complete. ${validation.eventCount} events would be imported.`)
|
|
144
|
+
return
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
yield* Console.log(`Checking for existing events...`)
|
|
148
|
+
|
|
149
|
+
yield* Console.log(`Connecting to sync backend...`)
|
|
150
|
+
yield* Console.log(`Pushing events to sync backend...`)
|
|
151
|
+
|
|
152
|
+
const result = yield* SyncOps.pushEventsToSyncBackend({
|
|
153
|
+
configPath,
|
|
154
|
+
storeId,
|
|
155
|
+
clientId,
|
|
156
|
+
data: parsedContent,
|
|
157
|
+
force,
|
|
158
|
+
dryRun: false,
|
|
159
|
+
onProgress: (pushed, total) => Console.log(` Pushed ${pushed}/${total} events`),
|
|
160
|
+
})
|
|
161
|
+
|
|
162
|
+
yield* Console.log(`✓ Connected to sync backend: ${result.backendName ?? 'unknown'}`)
|
|
163
|
+
yield* Console.log(`Successfully imported ${result.eventCount} events`)
|
|
164
|
+
}).pipe(Effect.withSpan('cli:import'))
|
|
165
|
+
|
|
166
|
+
export const exportCommand = Cli.Command.make(
|
|
167
|
+
'export',
|
|
168
|
+
{
|
|
169
|
+
config: Cli.Options.text('config').pipe(
|
|
170
|
+
Cli.Options.withAlias('c'),
|
|
171
|
+
Cli.Options.withDescription('Path to the config module that exports schema and syncBackend'),
|
|
172
|
+
),
|
|
173
|
+
storeId: Cli.Options.text('store-id').pipe(
|
|
174
|
+
Cli.Options.withAlias('i'),
|
|
175
|
+
Cli.Options.withDescription('Store identifier'),
|
|
176
|
+
),
|
|
177
|
+
clientId: Cli.Options.text('client-id').pipe(
|
|
178
|
+
Cli.Options.withDefault('cli-export'),
|
|
179
|
+
Cli.Options.withDescription('Client identifier for the sync connection'),
|
|
180
|
+
),
|
|
181
|
+
output: Cli.Args.text({ name: 'file' }).pipe(Cli.Args.withDescription('Output JSON file path')),
|
|
182
|
+
},
|
|
183
|
+
Effect.fn(function* ({
|
|
184
|
+
config,
|
|
185
|
+
storeId,
|
|
186
|
+
clientId,
|
|
187
|
+
output,
|
|
188
|
+
}: {
|
|
189
|
+
config: string
|
|
190
|
+
storeId: string
|
|
191
|
+
clientId: string
|
|
192
|
+
output: string
|
|
193
|
+
}) {
|
|
194
|
+
yield* Console.log(`Exporting events from LiveStore...`)
|
|
195
|
+
yield* Console.log(` Config: ${config}`)
|
|
196
|
+
yield* Console.log(` Store ID: ${storeId}`)
|
|
197
|
+
yield* Console.log(` Output: ${output}`)
|
|
198
|
+
yield* Console.log('')
|
|
199
|
+
|
|
200
|
+
yield* exportEvents({
|
|
201
|
+
configPath: config,
|
|
202
|
+
storeId,
|
|
203
|
+
clientId,
|
|
204
|
+
outputPath: output,
|
|
205
|
+
}).pipe(Effect.scoped)
|
|
206
|
+
}),
|
|
207
|
+
).pipe(
|
|
208
|
+
Cli.Command.withDescription(
|
|
209
|
+
'Export all events from the sync backend to a JSON file. Useful for backup and migration.',
|
|
210
|
+
),
|
|
211
|
+
)
|
|
212
|
+
|
|
213
|
+
export const importCommand = Cli.Command.make(
|
|
214
|
+
'import',
|
|
215
|
+
{
|
|
216
|
+
config: Cli.Options.text('config').pipe(
|
|
217
|
+
Cli.Options.withAlias('c'),
|
|
218
|
+
Cli.Options.withDescription('Path to the config module that exports schema and syncBackend'),
|
|
219
|
+
),
|
|
220
|
+
storeId: Cli.Options.text('store-id').pipe(
|
|
221
|
+
Cli.Options.withAlias('i'),
|
|
222
|
+
Cli.Options.withDescription('Store identifier'),
|
|
223
|
+
),
|
|
224
|
+
clientId: Cli.Options.text('client-id').pipe(
|
|
225
|
+
Cli.Options.withDefault('cli-import'),
|
|
226
|
+
Cli.Options.withDescription('Client identifier for the sync connection'),
|
|
227
|
+
),
|
|
228
|
+
force: Cli.Options.boolean('force').pipe(
|
|
229
|
+
Cli.Options.withAlias('f'),
|
|
230
|
+
Cli.Options.withDefault(false),
|
|
231
|
+
Cli.Options.withDescription('Force import even if store ID does not match'),
|
|
232
|
+
),
|
|
233
|
+
dryRun: Cli.Options.boolean('dry-run').pipe(
|
|
234
|
+
Cli.Options.withDefault(false),
|
|
235
|
+
Cli.Options.withDescription('Validate the import file without actually importing'),
|
|
236
|
+
),
|
|
237
|
+
input: Cli.Args.text({ name: 'file' }).pipe(Cli.Args.withDescription('Input JSON file to import')),
|
|
238
|
+
},
|
|
239
|
+
Effect.fn(function* ({
|
|
240
|
+
config,
|
|
241
|
+
storeId,
|
|
242
|
+
clientId,
|
|
243
|
+
force,
|
|
244
|
+
dryRun,
|
|
245
|
+
input,
|
|
246
|
+
}: {
|
|
247
|
+
config: string
|
|
248
|
+
storeId: string
|
|
249
|
+
clientId: string
|
|
250
|
+
force: boolean
|
|
251
|
+
dryRun: boolean
|
|
252
|
+
input: string
|
|
253
|
+
}) {
|
|
254
|
+
yield* Console.log(`Importing events to LiveStore...`)
|
|
255
|
+
yield* Console.log(` Config: ${config}`)
|
|
256
|
+
yield* Console.log(` Store ID: ${storeId}`)
|
|
257
|
+
yield* Console.log(` Input: ${input}`)
|
|
258
|
+
if (force) yield* Console.log(` Force: enabled`)
|
|
259
|
+
if (dryRun) yield* Console.log(` Dry run: enabled`)
|
|
260
|
+
yield* Console.log('')
|
|
261
|
+
|
|
262
|
+
yield* importEvents({
|
|
263
|
+
configPath: config,
|
|
264
|
+
storeId,
|
|
265
|
+
clientId,
|
|
266
|
+
inputPath: input,
|
|
267
|
+
force,
|
|
268
|
+
dryRun,
|
|
269
|
+
}).pipe(Effect.scoped)
|
|
270
|
+
}),
|
|
271
|
+
).pipe(
|
|
272
|
+
Cli.Command.withDescription('Import events from a JSON file to the sync backend. The sync backend must be empty.'),
|
|
273
|
+
)
|
|
274
|
+
|
|
275
|
+
export const syncCommand = Cli.Command.make('sync').pipe(
|
|
276
|
+
Cli.Command.withSubcommands([exportCommand, importCommand]),
|
|
277
|
+
Cli.Command.withDescription('Import and export events from the sync backend'),
|
|
278
|
+
)
|