@42ailab/42plugin 0.1.8 → 0.1.9

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.
Files changed (3) hide show
  1. package/package.json +1 -2
  2. package/src/cli.ts +3 -5
  3. package/src/db.ts +61 -83
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@42ailab/42plugin",
3
- "version": "0.1.8",
3
+ "version": "0.1.9",
4
4
  "description": "活水插件",
5
5
  "type": "module",
6
6
  "main": "src/index.ts",
@@ -42,7 +42,6 @@
42
42
  },
43
43
  "dependencies": {
44
44
  "@inquirer/prompts": "^8.1.0",
45
- "@libsql/client": "^0.14.0",
46
45
  "better-auth": "^1.4.6",
47
46
  "chalk": "^5.4.1",
48
47
  "cli-progress": "^3.12.0",
package/src/cli.ts CHANGED
@@ -15,11 +15,9 @@ import {
15
15
  checkCommand,
16
16
  } from './commands';
17
17
 
18
- // 动态读取版本号
19
- const packageJson = await Bun.file(
20
- new URL('../package.json', import.meta.url)
21
- ).json();
22
- const version: string = packageJson.version;
18
+ // 版本号在构建时通过 --define 注入
19
+ declare const __VERSION__: string;
20
+ const version: string = typeof __VERSION__ !== 'undefined' ? __VERSION__ : '0.0.0-dev';
23
21
 
24
22
  const program = new Command();
25
23
 
package/src/db.ts CHANGED
@@ -2,9 +2,10 @@
2
2
  * 本地存储
3
3
  *
4
4
  * 单文件封装 SQLite 存储和文件操作
5
+ * 使用 Bun 内置 SQLite(bun:sqlite)以支持编译为独立二进制
5
6
  */
6
7
 
7
- import { createClient, type Client } from '@libsql/client';
8
+ import { Database } from 'bun:sqlite';
8
9
  import fs from 'fs/promises';
9
10
  import path from 'path';
10
11
  import crypto from 'crypto';
@@ -18,24 +19,22 @@ import type { LocalProject, LocalCache, LocalInstallation, PluginType, PluginDow
18
19
  const PROGRESS_THRESHOLD = 1024 * 1024; // 1MB
19
20
 
20
21
  // ============================================================================
21
- // SQLite 客户端
22
+ // SQLite 客户端 (bun:sqlite)
22
23
  // ============================================================================
23
24
 
24
- let db: Client | null = null;
25
+ let db: Database | null = null;
25
26
 
26
- async function getDb(): Promise<Client> {
27
+ async function getDb(): Promise<Database> {
27
28
  if (!db) {
28
29
  await fs.mkdir(path.dirname(config.dbPath), { recursive: true });
29
- db = createClient({ url: `file:${config.dbPath}` });
30
- await initSchema();
30
+ db = new Database(config.dbPath);
31
+ initSchema();
31
32
  }
32
33
  return db;
33
34
  }
34
35
 
35
- async function initSchema(): Promise<void> {
36
- const client = db!;
37
-
38
- await client.execute(`
36
+ function initSchema(): void {
37
+ db!.run(`
39
38
  CREATE TABLE IF NOT EXISTS projects (
40
39
  id TEXT PRIMARY KEY,
41
40
  path TEXT UNIQUE NOT NULL,
@@ -45,7 +44,7 @@ async function initSchema(): Promise<void> {
45
44
  )
46
45
  `);
47
46
 
48
- await client.execute(`
47
+ db!.run(`
49
48
  CREATE TABLE IF NOT EXISTS plugin_cache (
50
49
  full_name TEXT NOT NULL,
51
50
  version TEXT NOT NULL,
@@ -58,7 +57,7 @@ async function initSchema(): Promise<void> {
58
57
  )
59
58
  `);
60
59
 
61
- await client.execute(`
60
+ db!.run(`
62
61
  CREATE TABLE IF NOT EXISTS installations (
63
62
  id TEXT PRIMARY KEY,
64
63
  project_id TEXT NOT NULL,
@@ -86,18 +85,11 @@ export async function getOrCreateProject(projectPath: string): Promise<LocalProj
86
85
  const name = path.basename(absPath);
87
86
 
88
87
  // 查找现有项目
89
- const result = await client.execute({
90
- sql: 'SELECT * FROM projects WHERE path = ?',
91
- args: [absPath],
92
- });
88
+ const row = client.prepare('SELECT * FROM projects WHERE path = ?').get(absPath) as Record<string, unknown> | null;
93
89
 
94
- if (result.rows.length > 0) {
95
- const row = result.rows[0];
90
+ if (row) {
96
91
  // 更新最后使用时间
97
- await client.execute({
98
- sql: 'UPDATE projects SET last_used_at = ? WHERE id = ?',
99
- args: [new Date().toISOString(), row.id],
100
- });
92
+ client.prepare('UPDATE projects SET last_used_at = ? WHERE id = ?').run(new Date().toISOString(), row.id);
101
93
  return {
102
94
  id: row.id as string,
103
95
  path: row.path as string,
@@ -110,10 +102,7 @@ export async function getOrCreateProject(projectPath: string): Promise<LocalProj
110
102
  // 创建新项目
111
103
  const id = crypto.randomUUID();
112
104
  const now = new Date().toISOString();
113
- await client.execute({
114
- sql: 'INSERT INTO projects (id, path, name, registered_at, last_used_at) VALUES (?, ?, ?, ?, ?)',
115
- args: [id, absPath, name, now, now],
116
- });
105
+ client.prepare('INSERT INTO projects (id, path, name, registered_at, last_used_at) VALUES (?, ?, ?, ?, ?)').run(id, absPath, name, now, now);
117
106
 
118
107
  return { id, path: absPath, name, registeredAt: now, lastUsedAt: now };
119
108
  }
@@ -124,14 +113,10 @@ export async function getOrCreateProject(projectPath: string): Promise<LocalProj
124
113
 
125
114
  export async function getCache(fullName: string, version: string): Promise<LocalCache | null> {
126
115
  const client = await getDb();
127
- const result = await client.execute({
128
- sql: 'SELECT * FROM plugin_cache WHERE full_name = ? AND version = ?',
129
- args: [fullName, version],
130
- });
116
+ const row = client.prepare('SELECT * FROM plugin_cache WHERE full_name = ? AND version = ?').get(fullName, version) as Record<string, unknown> | null;
131
117
 
132
- if (result.rows.length === 0) return null;
118
+ if (!row) return null;
133
119
 
134
- const row = result.rows[0];
135
120
  return {
136
121
  fullName: row.full_name as string,
137
122
  type: row.type as PluginType,
@@ -147,20 +132,19 @@ export async function setCache(cache: Omit<LocalCache, 'cachedAt'>): Promise<voi
147
132
  const client = await getDb();
148
133
  const now = new Date().toISOString();
149
134
 
150
- await client.execute({
151
- sql: `INSERT OR REPLACE INTO plugin_cache
152
- (full_name, version, type, cache_path, checksum, size_bytes, cached_at)
153
- VALUES (?, ?, ?, ?, ?, ?, ?)`,
154
- args: [
155
- cache.fullName,
156
- cache.version,
157
- cache.type,
158
- cache.cachePath,
159
- cache.checksum,
160
- cache.sizeBytes,
161
- now,
162
- ],
163
- });
135
+ client.prepare(`
136
+ INSERT OR REPLACE INTO plugin_cache
137
+ (full_name, version, type, cache_path, checksum, size_bytes, cached_at)
138
+ VALUES (?, ?, ?, ?, ?, ?, ?)
139
+ `).run(
140
+ cache.fullName,
141
+ cache.version,
142
+ cache.type,
143
+ cache.cachePath,
144
+ cache.checksum,
145
+ cache.sizeBytes,
146
+ now,
147
+ );
164
148
  }
165
149
 
166
150
  export async function removeCache(fullName: string, version: string): Promise<boolean> {
@@ -187,12 +171,9 @@ export async function removeCache(fullName: string, version: string): Promise<bo
187
171
  }
188
172
 
189
173
  // 删除数据库记录
190
- const result = await client.execute({
191
- sql: 'DELETE FROM plugin_cache WHERE full_name = ? AND version = ?',
192
- args: [fullName, version],
193
- });
174
+ const result = client.prepare('DELETE FROM plugin_cache WHERE full_name = ? AND version = ?').run(fullName, version);
194
175
 
195
- return result.rowsAffected > 0;
176
+ return result.changes > 0;
196
177
  }
197
178
 
198
179
  // ============================================================================
@@ -213,23 +194,22 @@ export async function addInstallation(data: {
213
194
  const id = crypto.randomUUID();
214
195
  const now = new Date().toISOString();
215
196
 
216
- await client.execute({
217
- sql: `INSERT OR REPLACE INTO installations
218
- (id, project_id, full_name, type, version, cache_path, link_path, source, source_kit, installed_at)
219
- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
220
- args: [
221
- id,
222
- data.projectId,
223
- data.fullName,
224
- data.type,
225
- data.version,
226
- data.cachePath,
227
- data.linkPath,
228
- data.source,
229
- data.sourceKit || null,
230
- now,
231
- ],
232
- });
197
+ client.prepare(`
198
+ INSERT OR REPLACE INTO installations
199
+ (id, project_id, full_name, type, version, cache_path, link_path, source, source_kit, installed_at)
200
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
201
+ `).run(
202
+ id,
203
+ data.projectId,
204
+ data.fullName,
205
+ data.type,
206
+ data.version,
207
+ data.cachePath,
208
+ data.linkPath,
209
+ data.source,
210
+ data.sourceKit || null,
211
+ now,
212
+ );
233
213
 
234
214
  return { id, ...data, installedAt: now };
235
215
  }
@@ -238,15 +218,14 @@ export async function getInstallations(projectPath: string): Promise<LocalInstal
238
218
  const client = await getDb();
239
219
  const absPath = path.resolve(projectPath);
240
220
 
241
- const result = await client.execute({
242
- sql: `SELECT i.* FROM installations i
243
- JOIN projects p ON i.project_id = p.id
244
- WHERE p.path = ?
245
- ORDER BY i.installed_at DESC`,
246
- args: [absPath],
247
- });
221
+ const rows = client.prepare(`
222
+ SELECT i.* FROM installations i
223
+ JOIN projects p ON i.project_id = p.id
224
+ WHERE p.path = ?
225
+ ORDER BY i.installed_at DESC
226
+ `).all(absPath) as Record<string, unknown>[];
248
227
 
249
- return result.rows.map((row) => ({
228
+ return rows.map((row) => ({
250
229
  id: row.id as string,
251
230
  projectId: row.project_id as string,
252
231
  fullName: row.full_name as string,
@@ -264,14 +243,13 @@ export async function removeInstallation(projectPath: string, fullName: string):
264
243
  const client = await getDb();
265
244
  const absPath = path.resolve(projectPath);
266
245
 
267
- const result = await client.execute({
268
- sql: `DELETE FROM installations
269
- WHERE full_name = ?
270
- AND project_id IN (SELECT id FROM projects WHERE path = ?)`,
271
- args: [fullName, absPath],
272
- });
246
+ const result = client.prepare(`
247
+ DELETE FROM installations
248
+ WHERE full_name = ?
249
+ AND project_id IN (SELECT id FROM projects WHERE path = ?)
250
+ `).run(fullName, absPath);
273
251
 
274
- return result.rowsAffected > 0;
252
+ return result.changes > 0;
275
253
  }
276
254
 
277
255
  // ============================================================================