@42ailab/42plugin 0.1.8 → 0.1.10
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/package.json +1 -2
- package/src/cli.ts +16 -5
- 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.
|
|
3
|
+
"version": "0.1.10",
|
|
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,22 @@ import {
|
|
|
15
15
|
checkCommand,
|
|
16
16
|
} from './commands';
|
|
17
17
|
|
|
18
|
-
//
|
|
19
|
-
const
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
18
|
+
// 版本号:编译时通过 --define 注入,源码运行时从 package.json 读取
|
|
19
|
+
declare const __VERSION__: string;
|
|
20
|
+
async function getVersion(): Promise<string> {
|
|
21
|
+
// 编译后的二进制使用注入的版本号
|
|
22
|
+
if (typeof __VERSION__ !== 'undefined') {
|
|
23
|
+
return __VERSION__;
|
|
24
|
+
}
|
|
25
|
+
// 源码运行(bun run / npm)时从 package.json 读取
|
|
26
|
+
try {
|
|
27
|
+
const pkg = await import('../package.json');
|
|
28
|
+
return pkg.default?.version || pkg.version || '0.0.0-dev';
|
|
29
|
+
} catch {
|
|
30
|
+
return '0.0.0-dev';
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
const version: string = await getVersion();
|
|
23
34
|
|
|
24
35
|
const program = new Command();
|
|
25
36
|
|
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 {
|
|
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:
|
|
25
|
+
let db: Database | null = null;
|
|
25
26
|
|
|
26
|
-
async function getDb(): Promise<
|
|
27
|
+
async function getDb(): Promise<Database> {
|
|
27
28
|
if (!db) {
|
|
28
29
|
await fs.mkdir(path.dirname(config.dbPath), { recursive: true });
|
|
29
|
-
db =
|
|
30
|
-
|
|
30
|
+
db = new Database(config.dbPath);
|
|
31
|
+
initSchema();
|
|
31
32
|
}
|
|
32
33
|
return db;
|
|
33
34
|
}
|
|
34
35
|
|
|
35
|
-
|
|
36
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
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 (
|
|
95
|
-
const row = result.rows[0];
|
|
90
|
+
if (row) {
|
|
96
91
|
// 更新最后使用时间
|
|
97
|
-
|
|
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
|
-
|
|
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
|
|
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 (
|
|
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
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
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 =
|
|
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.
|
|
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
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
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
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
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
|
|
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 =
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
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.
|
|
252
|
+
return result.changes > 0;
|
|
275
253
|
}
|
|
276
254
|
|
|
277
255
|
// ============================================================================
|