universal-plugin-hub 1.1.0 → 1.3.1
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/README.md +1 -1
- package/client/client.js +2891 -909
- package/package.json +1 -1
- package/src/icons.js +219 -36
- package/src/index.js +26 -2
- package/src/install.js +3088 -2649
- package/src/local.js +439 -71
- package/src/market.js +48 -17
- package/src/oauth-bridge.js +1 -1
- package/src/oauth.js +2 -1
- package/src/parser.js +1246 -459
- package/src/routes.js +369 -119
- package/src/skill-uploads.js +116 -12
- package/src/skills.js +8 -1
- package/src/store.js +171 -31
package/src/local.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { existsSync, readdirSync, statSync, cpSync, copyFileSync, mkdirSync, writeFileSync, mkdtempSync, readFileSync } from 'node:fs'
|
|
1
|
+
import { existsSync, readdirSync, statSync, cpSync, copyFileSync, mkdirSync, writeFileSync, mkdtempSync, readFileSync, createWriteStream, rmSync } from 'node:fs'
|
|
2
2
|
import { join, basename, resolve, dirname, relative, isAbsolute } from 'node:path'
|
|
3
3
|
import { fileURLToPath } from 'node:url'
|
|
4
4
|
import { execFile } from 'node:child_process'
|
|
@@ -6,8 +6,10 @@ import { promisify } from 'node:util'
|
|
|
6
6
|
import { load as parseYaml } from 'js-yaml'
|
|
7
7
|
import { sourceCacheDir, readState, writeState, safeRmDir, slugify } from './store.js'
|
|
8
8
|
import { safeSegment, pluginPath } from './market.js'
|
|
9
|
-
import { parsePluginRowFromDir, parseMarketplaceFromDisk, listSkills, listAgents, readPluginManifest, invalidateMarketplaceCache, formatDisplayName } from './parser.js'
|
|
10
|
-
import { installUploadedSkills } from './skill-uploads.js'
|
|
9
|
+
import { parsePluginRowFromDir, parseMarketplaceFromDisk, listSkills, listAgents, readPluginManifest, invalidateMarketplaceCache, invalidateParserCaches, formatDisplayName, normalizeVersion, MARKETPLACE_COMPONENT_KEYS } from './parser.js'
|
|
10
|
+
import { installUploadedSkills, takenSkillNames } from './skill-uploads.js'
|
|
11
|
+
import { invalidateLocalIconCache } from './icons.js'
|
|
12
|
+
import { refreshInstalledPluginsFromSource } from './install.js'
|
|
11
13
|
|
|
12
14
|
const execFileAsync = promisify(execFile)
|
|
13
15
|
|
|
@@ -47,23 +49,170 @@ export async function extractZip(zipPath, destDir) {
|
|
|
47
49
|
await execFileAsync(tar, ['-xf', zipPath, '-C', destDir], { windowsHide: true })
|
|
48
50
|
}
|
|
49
51
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
if (!/\.(zip|tgz|tar\.gz)$/i.test(
|
|
55
|
-
|
|
52
|
+
export const MAX_UPLOAD_BYTES = 500 * 1024 * 1024
|
|
53
|
+
|
|
54
|
+
function uploadedTargetFilename(filename) {
|
|
55
|
+
const raw = String(filename || '')
|
|
56
|
+
if (!/\.(zip|tgz|tar\.gz)$/i.test(raw) && !/^skill\.md$/i.test(raw)) {
|
|
57
|
+
const error = new Error('支持 .zip、.tgz、.tar.gz 压缩包或 SKILL.md 文件')
|
|
58
|
+
error.status = 400
|
|
59
|
+
throw error
|
|
56
60
|
}
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
61
|
+
return /^skill\.md$/i.test(raw)
|
|
62
|
+
? 'SKILL.md'
|
|
63
|
+
: (raw.replace(/[^a-zA-Z0-9._-]/g, '_') || 'plugin.zip')
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function createUploadFilePath(filename) {
|
|
67
|
+
const targetName = uploadedTargetFilename(filename)
|
|
68
|
+
sweepTemp()
|
|
60
69
|
mkdirSync(TEMP_ROOT(), { recursive: true })
|
|
61
70
|
const uploadDir = mkdtempSync(join(TEMP_ROOT(), 'upload-'))
|
|
62
|
-
|
|
71
|
+
return { uploadDir, file: join(uploadDir, targetName) }
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** Persist an uploaded archive or SKILL.md from base64 JSON payload. */
|
|
75
|
+
export function saveUploadedZip(dataBase64, filename) {
|
|
76
|
+
const targetName = uploadedTargetFilename(filename)
|
|
77
|
+
const data = Buffer.from(String(dataBase64 || ''), 'base64')
|
|
78
|
+
if (data.length > MAX_UPLOAD_BYTES) {
|
|
79
|
+
const error = new Error('上传文件超出 500MB 上限')
|
|
80
|
+
error.status = 413
|
|
81
|
+
throw error
|
|
82
|
+
}
|
|
83
|
+
const { file } = createUploadFilePath(targetName)
|
|
63
84
|
writeFileSync(file, data)
|
|
64
85
|
return file
|
|
65
86
|
}
|
|
66
87
|
|
|
88
|
+
/**
|
|
89
|
+
* Stream a raw application/octet-stream upload directly to disk with backpressure
|
|
90
|
+
* and incremental byte limit enforcement so large archives never buffer in V8 heap.
|
|
91
|
+
*/
|
|
92
|
+
export async function saveUploadedStream(req, filename, maxBytes = MAX_UPLOAD_BYTES) {
|
|
93
|
+
const targetName = uploadedTargetFilename(filename)
|
|
94
|
+
const declaredLength = Number(req?.headers?.['content-length'] || 0)
|
|
95
|
+
if (Number.isFinite(declaredLength) && declaredLength > maxBytes) {
|
|
96
|
+
const error = new Error('上传文件超出 500MB 上限')
|
|
97
|
+
error.status = 413
|
|
98
|
+
throw error
|
|
99
|
+
}
|
|
100
|
+
try { req?.setTimeout?.(0) } catch {}
|
|
101
|
+
try { req?.socket?.setTimeout?.(0) } catch {}
|
|
102
|
+
const { uploadDir, file } = createUploadFilePath(targetName)
|
|
103
|
+
const writer = createWriteStream(file)
|
|
104
|
+
let received = 0
|
|
105
|
+
try {
|
|
106
|
+
await new Promise((resolve, reject) => {
|
|
107
|
+
let settled = false
|
|
108
|
+
let ended = false
|
|
109
|
+
let finished = false
|
|
110
|
+
const cleanupReqListeners = () => {
|
|
111
|
+
req.removeListener('data', onData)
|
|
112
|
+
req.removeListener('end', onEnd)
|
|
113
|
+
req.removeListener('error', onReqError)
|
|
114
|
+
req.removeListener('aborted', onAbort)
|
|
115
|
+
req.removeListener('close', onReqClose)
|
|
116
|
+
}
|
|
117
|
+
const closeWriter = () => new Promise((done) => {
|
|
118
|
+
if (writer.closed || typeof writer.once !== 'function') {
|
|
119
|
+
done()
|
|
120
|
+
return
|
|
121
|
+
}
|
|
122
|
+
let finishedClosing = false
|
|
123
|
+
const timer = setTimeout(() => {
|
|
124
|
+
if (finishedClosing) return
|
|
125
|
+
finishedClosing = true
|
|
126
|
+
writer.removeListener('close', onClosed)
|
|
127
|
+
done()
|
|
128
|
+
}, 300)
|
|
129
|
+
timer.unref?.()
|
|
130
|
+
const onClosed = () => {
|
|
131
|
+
if (finishedClosing) return
|
|
132
|
+
finishedClosing = true
|
|
133
|
+
clearTimeout(timer)
|
|
134
|
+
done()
|
|
135
|
+
}
|
|
136
|
+
writer.once('close', onClosed)
|
|
137
|
+
try { writer.destroy() } catch { onClosed() }
|
|
138
|
+
})
|
|
139
|
+
const fail = (err) => {
|
|
140
|
+
if (settled) return
|
|
141
|
+
settled = true
|
|
142
|
+
cleanupReqListeners()
|
|
143
|
+
writer.removeListener('finish', onWriterFinish)
|
|
144
|
+
writer.removeListener('error', fail)
|
|
145
|
+
writer.removeListener('close', onWriterClose)
|
|
146
|
+
writer.on('error', () => {})
|
|
147
|
+
try { req.pause?.() } catch {}
|
|
148
|
+
void closeWriter().then(() => reject(err))
|
|
149
|
+
}
|
|
150
|
+
const onData = (chunk) => {
|
|
151
|
+
const buf = typeof chunk === 'string' ? Buffer.from(chunk) : chunk
|
|
152
|
+
received += buf.length
|
|
153
|
+
if (received > maxBytes) {
|
|
154
|
+
const error = new Error('上传文件超出 500MB 上限')
|
|
155
|
+
error.status = 413
|
|
156
|
+
fail(error)
|
|
157
|
+
return
|
|
158
|
+
}
|
|
159
|
+
if (!writer.write(buf)) {
|
|
160
|
+
try { req.pause?.() } catch {}
|
|
161
|
+
writer.once('drain', () => {
|
|
162
|
+
if (!settled) {
|
|
163
|
+
try { req.resume?.() } catch {}
|
|
164
|
+
}
|
|
165
|
+
})
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
const onEnd = () => {
|
|
169
|
+
ended = true
|
|
170
|
+
if (!settled) writer.end()
|
|
171
|
+
}
|
|
172
|
+
const onReqError = (err) => fail(err)
|
|
173
|
+
const onAbort = () => fail(Object.assign(new Error('上传已中断'), { status: 499 }))
|
|
174
|
+
const onReqClose = () => {
|
|
175
|
+
if (!ended && !req.complete && !req.readableEnded) {
|
|
176
|
+
onAbort()
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
const onWriterFinish = () => {
|
|
180
|
+
finished = true
|
|
181
|
+
if (writer.closed) onWriterClose()
|
|
182
|
+
}
|
|
183
|
+
const onWriterClose = () => {
|
|
184
|
+
if (settled || !finished) return
|
|
185
|
+
settled = true
|
|
186
|
+
cleanupReqListeners()
|
|
187
|
+
writer.removeListener('error', fail)
|
|
188
|
+
resolve()
|
|
189
|
+
}
|
|
190
|
+
writer.on('finish', onWriterFinish)
|
|
191
|
+
writer.on('close', onWriterClose)
|
|
192
|
+
writer.on('error', fail)
|
|
193
|
+
req.on('data', onData)
|
|
194
|
+
req.on('end', onEnd)
|
|
195
|
+
req.on('error', onReqError)
|
|
196
|
+
req.on('aborted', onAbort)
|
|
197
|
+
req.on('close', onReqClose)
|
|
198
|
+
})
|
|
199
|
+
return { file, uploadDir, bytes: received }
|
|
200
|
+
} catch (error) {
|
|
201
|
+
safeRmDir(uploadDir)
|
|
202
|
+
throw error
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* The one way this codebase tells a marketplace from a plain plugin: a
|
|
208
|
+
* marketplace carries its own manifest. A plugin never does.
|
|
209
|
+
*/
|
|
210
|
+
const MARKETPLACE_MANIFESTS = ['.claude-plugin/marketplace.json', '.agents/plugins/marketplace.json', 'marketplace.json']
|
|
211
|
+
|
|
212
|
+
function isMarketplaceDir(dir) {
|
|
213
|
+
return MARKETPLACE_MANIFESTS.some((path) => existsSync(join(dir, path)))
|
|
214
|
+
}
|
|
215
|
+
|
|
67
216
|
function isPluginDir(dir) {
|
|
68
217
|
if (!dir || !existsSync(dir)) return false
|
|
69
218
|
const rootSkill = existsSync(join(dir, 'SKILL.md')) || existsSync(join(dir, 'skill.md'))
|
|
@@ -77,7 +226,7 @@ function isPluginDir(dir) {
|
|
|
77
226
|
|
|
78
227
|
export function resolvePluginDir(rootDir) {
|
|
79
228
|
if (!existsSync(rootDir) || !statSync(rootDir).isDirectory()) return null
|
|
80
|
-
if (
|
|
229
|
+
if (isMarketplaceDir(rootDir)) return rootDir
|
|
81
230
|
if (isPluginDir(rootDir)) return rootDir
|
|
82
231
|
|
|
83
232
|
// Unwrap nested directory wrappers (common in repository archives).
|
|
@@ -154,7 +303,7 @@ function isPureSkillPackage(dir) {
|
|
|
154
303
|
}
|
|
155
304
|
|
|
156
305
|
function marketSourceName(dir, fallbackName) {
|
|
157
|
-
for (const path of
|
|
306
|
+
for (const path of MARKETPLACE_MANIFESTS) {
|
|
158
307
|
try {
|
|
159
308
|
const raw = JSON.parse(readFileSync(join(dir, path), 'utf8'))
|
|
160
309
|
if (typeof raw.name === 'string' && raw.name.trim()) return raw.name.trim()
|
|
@@ -203,7 +352,7 @@ export async function inspectLocalPlugin(rawPath) {
|
|
|
203
352
|
// Recognition follows the package formats, not the upload transport:
|
|
204
353
|
// marketplace -> Claude -> Codex -> pure skills. Claude/Codex precedence in
|
|
205
354
|
// readPluginManifest is shared with the existing source installation path.
|
|
206
|
-
const isMarket =
|
|
355
|
+
const isMarket = isMarketplaceDir(effectiveDir)
|
|
207
356
|
const pureSkills = !isMarket && isPureSkillPackage(effectiveDir)
|
|
208
357
|
const directSkills = pureSkills ? collectDirectSkills(effectiveDir, fallbackName) : []
|
|
209
358
|
const singleFile = !isZip && statSync(targetPath).isFile()
|
|
@@ -243,23 +392,46 @@ export async function inspectLocalPlugin(rawPath) {
|
|
|
243
392
|
const cleanPluginName = safeSegment(row.name, 'plugin name')
|
|
244
393
|
row.name = cleanPluginName
|
|
245
394
|
|
|
395
|
+
const names = kind === 'skills' ? takenSkillNames() : new Set()
|
|
246
396
|
const skills = entries.flatMap((e) => listSkills(e.dir))
|
|
247
397
|
const agents = entries.flatMap((e) => listAgents(e.dir))
|
|
248
398
|
|
|
249
|
-
const
|
|
250
|
-
|
|
251
|
-
const localDest = duplicate ? join(sourceCacheDir('local'), duplicate.row.name) : join(sourceCacheDir('local'), cleanPluginName)
|
|
399
|
+
const localConflicts = kind === 'skills' ? [] : collectLocalConflicts(entries)
|
|
400
|
+
const suggestedLabel = kind === 'marketplace' ? marketSourceName(effectiveDir, fallbackName) : ''
|
|
252
401
|
let exists = false
|
|
253
402
|
let existingVersion = undefined
|
|
254
403
|
let existingUpdatedAt = undefined
|
|
255
|
-
if (
|
|
404
|
+
if (kind === 'marketplace') {
|
|
405
|
+
// Only the same standalone market tab triggers immediate duplicate overwrite;
|
|
406
|
+
// overlaps with `local` are reported via `localConflicts` so the user can still
|
|
407
|
+
// pick an independent tab cleanly without false collision prompts.
|
|
408
|
+
const marketSlugId = `local-${slugify(suggestedLabel) || 'market'}`
|
|
409
|
+
const marketCache = sourceCacheDir(marketSlugId)
|
|
410
|
+
const hasMarket = readState().sources.some((s) => s.id === marketSlugId) && existsSync(marketCache)
|
|
411
|
+
if (hasMarket) {
|
|
412
|
+
exists = true
|
|
413
|
+
try {
|
|
414
|
+
existingUpdatedAt = statSync(marketCache).mtimeMs
|
|
415
|
+
const existingRows = parseMarketplaceFromDisk(marketSlugId, marketCache)
|
|
416
|
+
const matched = existingRows.find((r) => r.name === row.name) || existingRows[0]
|
|
417
|
+
existingVersion = normalizeVersion(matched?.version)
|
|
418
|
+
} catch {}
|
|
419
|
+
}
|
|
420
|
+
} else if (kind === 'skills') {
|
|
421
|
+
const duplicate = entries.find((e) => existsSync(join(sourceCacheDir('local'), safeSegment(e.row.name, 'plugin name'))))
|
|
422
|
+
|| entries.find((e) => (readState().uploadedSkills || []).some(record => record.name === e.row.name))
|
|
423
|
+
if (duplicate) {
|
|
424
|
+
const localDest = join(sourceCacheDir('local'), safeSegment(duplicate.row.name, 'plugin name'))
|
|
425
|
+
exists = true
|
|
426
|
+
try {
|
|
427
|
+
existingUpdatedAt = statSync(localDest).mtimeMs
|
|
428
|
+
existingVersion = normalizeVersion(readPluginManifest(localDest)?.version)
|
|
429
|
+
} catch {}
|
|
430
|
+
}
|
|
431
|
+
} else if (localConflicts.length > 0) {
|
|
256
432
|
exists = true
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
existingUpdatedAt = existingStat.mtimeMs
|
|
260
|
-
const existingManifest = readPluginManifest(localDest)
|
|
261
|
-
existingVersion = existingManifest?.version || '1.0.0'
|
|
262
|
-
} catch {}
|
|
433
|
+
existingVersion = localConflicts[0].existingVersion
|
|
434
|
+
existingUpdatedAt = localConflicts[0].existingUpdatedAt
|
|
263
435
|
}
|
|
264
436
|
|
|
265
437
|
return {
|
|
@@ -277,14 +449,45 @@ export async function inspectLocalPlugin(rawPath) {
|
|
|
277
449
|
exists,
|
|
278
450
|
existingVersion,
|
|
279
451
|
existingUpdatedAt,
|
|
452
|
+
localConflicts,
|
|
453
|
+
suggestedName: kind === 'marketplace' ? formatDisplayName(suggestedLabel) : undefined,
|
|
454
|
+
// Whether the local tab is already named decides if this upload still has
|
|
455
|
+
// to ask for a name, so inspect reports it instead of the dialog guessing.
|
|
456
|
+
localTabName: String(readState().sources.find((source) => source.id === 'local')?.name || ''),
|
|
457
|
+
// Every skill in the package is checked, not just the aggregate row: a
|
|
458
|
+
// collection with one clashing member must still offer the overwrite.
|
|
459
|
+
takenSkillNames: kind === 'skills' ? entries.map((e) => e.row.name).filter((name) => names.has(name)) : [],
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
function collectLocalConflicts(entries) {
|
|
464
|
+
const localDir = sourceCacheDir('local')
|
|
465
|
+
const conflicts = []
|
|
466
|
+
for (const e of entries) {
|
|
467
|
+
const dest = join(localDir, safeSegment(e.row.name, 'plugin name'))
|
|
468
|
+
if (!existsSync(dest)) continue
|
|
469
|
+
let existingUpdatedAt
|
|
470
|
+
let existingVersion
|
|
471
|
+
try {
|
|
472
|
+
existingUpdatedAt = statSync(dest).mtimeMs
|
|
473
|
+
existingVersion = normalizeVersion(readPluginManifest(dest)?.version)
|
|
474
|
+
} catch {}
|
|
475
|
+
conflicts.push({
|
|
476
|
+
name: e.row.name,
|
|
477
|
+
displayName: e.row.displayName || e.row.name,
|
|
478
|
+
existingVersion,
|
|
479
|
+
newVersion: normalizeVersion(e.row.version),
|
|
480
|
+
existingUpdatedAt,
|
|
481
|
+
})
|
|
280
482
|
}
|
|
483
|
+
return conflicts
|
|
281
484
|
}
|
|
282
485
|
|
|
283
486
|
/**
|
|
284
487
|
* Pure skill packages go straight into the Skills tab instead of the plugin
|
|
285
488
|
* install pipeline: no mirrored copy, no agents/commands conversion.
|
|
286
489
|
*/
|
|
287
|
-
export async function importLocalSkills(rawPath, { overwrite = false } = {}) {
|
|
490
|
+
export async function importLocalSkills(rawPath, { overwrite = false, overwriteNames = null } = {}) {
|
|
288
491
|
const inspectRes = await inspectLocalPlugin(rawPath)
|
|
289
492
|
if (!inspectRes.ok) throw new Error(inspectRes.error)
|
|
290
493
|
if (inspectRes.kind !== 'skills') throw new Error('该内容不是纯 Skills 包')
|
|
@@ -293,7 +496,7 @@ export async function importLocalSkills(rawPath, { overwrite = false } = {}) {
|
|
|
293
496
|
// A bare SKILL.md upload is one flat skill. When the folder it came from is
|
|
294
497
|
// a collection, the walk up yields several entries, and flattening those
|
|
295
498
|
// would drop each skill's assets along with its directory.
|
|
296
|
-
const installed = installUploadedSkills(entries, { overwrite, singleFile: singleFile && entries.length === 1 })
|
|
499
|
+
const installed = installUploadedSkills(entries, { overwrite, overwriteNames, singleFile: singleFile && entries.length === 1 })
|
|
297
500
|
return { ok: true, sourceId: 'skills-local', skills: installed, kind: 'skills' }
|
|
298
501
|
} finally {
|
|
299
502
|
if (tempExtractDir) safeRmDir(tempExtractDir)
|
|
@@ -305,16 +508,146 @@ export async function importLocalSkills(rawPath, { overwrite = false } = {}) {
|
|
|
305
508
|
* plugins are copied under the source cache and listed by the normal market
|
|
306
509
|
* reader, so install/update/uninstall behave exactly like a Git source.
|
|
307
510
|
*/
|
|
308
|
-
|
|
511
|
+
const PLUGIN_MANIFEST_RELATIVE = ['.claude-plugin/plugin.json', 'plugin.json', '.codex-plugin/plugin.json']
|
|
512
|
+
|
|
513
|
+
function syncPluginManifestMetadata(destDir, row, { stripRootMarketplace = false } = {}) {
|
|
514
|
+
if (stripRootMarketplace) {
|
|
515
|
+
for (const relativePath of MARKETPLACE_MANIFESTS) {
|
|
516
|
+
const mPath = join(destDir, relativePath)
|
|
517
|
+
if (existsSync(mPath)) rmSync(mPath, { force: true })
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
const componentKeys = ['strict', ...MARKETPLACE_COMPONENT_KEYS]
|
|
521
|
+
const fields = {
|
|
522
|
+
name: row.name,
|
|
523
|
+
...(row.displayName ? { displayName: row.displayName } : {}),
|
|
524
|
+
...(row.version ? { version: row.version } : {}),
|
|
525
|
+
...(row.description ? { description: row.description } : {}),
|
|
526
|
+
...(row.author ? { author: row.author } : {}),
|
|
527
|
+
...(row.homepage ? { homepage: row.homepage } : {}),
|
|
528
|
+
...(row.category ? { category: row.category } : {}),
|
|
529
|
+
}
|
|
530
|
+
for (const k of componentKeys) {
|
|
531
|
+
if (row[k] !== undefined) fields[k] = row[k]
|
|
532
|
+
}
|
|
533
|
+
for (const rel of PLUGIN_MANIFEST_RELATIVE) {
|
|
534
|
+
const file = join(destDir, rel)
|
|
535
|
+
if (!existsSync(file)) continue
|
|
536
|
+
try {
|
|
537
|
+
const json = JSON.parse(readFileSync(file, 'utf8'))
|
|
538
|
+
if (!json || typeof json !== 'object' || Array.isArray(json)) continue
|
|
539
|
+
let changed = false
|
|
540
|
+
if (!json.name && row.name) { json.name = row.name; changed = true }
|
|
541
|
+
const hasVer = normalizeVersion(json.version)
|
|
542
|
+
|| normalizeVersion(json.extensions?.['com.openai']?.version)
|
|
543
|
+
|| normalizeVersion(json.interface?.version)
|
|
544
|
+
if (!hasVer && row.version) { json.version = row.version; changed = true }
|
|
545
|
+
if (!json.description && row.description) { json.description = row.description; changed = true }
|
|
546
|
+
if (!json.displayName && !json.interface?.displayName && row.displayName) { json.displayName = row.displayName; changed = true }
|
|
547
|
+
if (!json.author && !json.interface?.developerName && row.author) { json.author = row.author; changed = true }
|
|
548
|
+
if (!json.homepage && !json.interface?.websiteURL && row.homepage) { json.homepage = row.homepage; changed = true }
|
|
549
|
+
for (const k of componentKeys) {
|
|
550
|
+
if (row[k] !== undefined && (row.strict === false || json[k] === undefined)) {
|
|
551
|
+
json[k] = row[k]
|
|
552
|
+
changed = true
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
if (changed) writeFileSync(file, JSON.stringify(json, null, 2), 'utf8')
|
|
556
|
+
return
|
|
557
|
+
} catch {}
|
|
558
|
+
}
|
|
559
|
+
const out = join(destDir, '.claude-plugin', 'plugin.json')
|
|
560
|
+
mkdirSync(dirname(out), { recursive: true })
|
|
561
|
+
writeFileSync(out, JSON.stringify(fields, null, 2), 'utf8')
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
async function copyEntriesToLocalCache(entries, { overwrite = false, tempExtractDir = null, stripRootMarketplace = false } = {}) {
|
|
565
|
+
const localDir = sourceCacheDir('local')
|
|
566
|
+
mkdirSync(localDir, { recursive: true })
|
|
567
|
+
const clashing = []
|
|
568
|
+
for (const { row, dir } of entries) {
|
|
569
|
+
const cleanName = safeSegment(row.name, 'plugin name')
|
|
570
|
+
const destDir = resolve(localDir, cleanName)
|
|
571
|
+
const srcDir = resolve(dir)
|
|
572
|
+
if (destDir !== srcDir && (containsPath(destDir, srcDir) || containsPath(srcDir, destDir))) {
|
|
573
|
+
if (tempExtractDir) safeRmDir(tempExtractDir)
|
|
574
|
+
throw new Error('导入源与目标目录重叠,请从独立文件夹导入')
|
|
575
|
+
}
|
|
576
|
+
if (existsSync(destDir)) clashing.push(cleanName)
|
|
577
|
+
}
|
|
578
|
+
if (clashing.length > 0 && !overwrite) {
|
|
579
|
+
if (tempExtractDir) safeRmDir(tempExtractDir)
|
|
580
|
+
if (clashing.length === 1) throw new Error(`已存在同名插件「${clashing[0]}」`)
|
|
581
|
+
throw new Error(`本地插件标签中已存在同名插件:${clashing.join('、')}`)
|
|
582
|
+
}
|
|
583
|
+
const importedNames = []
|
|
584
|
+
try {
|
|
585
|
+
for (const { row, dir } of entries) {
|
|
586
|
+
const cleanName = safeSegment(row.name, 'plugin name')
|
|
587
|
+
const destDir = join(localDir, cleanName)
|
|
588
|
+
if (resolve(destDir) !== resolve(dir)) {
|
|
589
|
+
safeRmDir(destDir)
|
|
590
|
+
cpSync(dir, destDir, { recursive: true, force: true })
|
|
591
|
+
}
|
|
592
|
+
if (stripRootMarketplace) {
|
|
593
|
+
syncPluginManifestMetadata(destDir, row, { stripRootMarketplace: true })
|
|
594
|
+
}
|
|
595
|
+
importedNames.push(cleanName)
|
|
596
|
+
}
|
|
597
|
+
} finally {
|
|
598
|
+
if (tempExtractDir) safeRmDir(tempExtractDir)
|
|
599
|
+
}
|
|
600
|
+
invalidateMarketplaceCache('local')
|
|
601
|
+
invalidateParserCaches()
|
|
602
|
+
invalidateLocalIconCache('local')
|
|
603
|
+
if (overwrite && importedNames.length > 0) {
|
|
604
|
+
await refreshInstalledPluginsFromSource('local', importedNames)
|
|
605
|
+
}
|
|
606
|
+
return importedNames
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
/**
|
|
610
|
+
* Import an uploaded Claude/Codex marketplace locally. When `target === 'local'`,
|
|
611
|
+
* entries merge cleanly into the existing `local` tab as individual plugin folders
|
|
612
|
+
* without writing a root marketplace.json; otherwise plugins keep their manifest
|
|
613
|
+
* layout inside `market/local-<slug>/` as an independent tab.
|
|
614
|
+
*/
|
|
615
|
+
export async function importLocalMarketplace(rawPath, { sourceName = '', overwrite = false, target = '' } = {}) {
|
|
309
616
|
const inspectRes = await inspectLocalPlugin(rawPath)
|
|
310
617
|
if (!inspectRes.ok) throw new Error(inspectRes.error)
|
|
311
618
|
if (inspectRes.kind !== 'marketplace') throw new Error('该内容不是插件市场')
|
|
312
619
|
const { effectiveDir, tempExtractDir, entries } = inspectRes
|
|
620
|
+
const state = readState()
|
|
621
|
+
|
|
622
|
+
if (target === 'local') {
|
|
623
|
+
const localSource = state.sources.find((s) => s.id === 'local')
|
|
624
|
+
if (!localSource) {
|
|
625
|
+
if (tempExtractDir) safeRmDir(tempExtractDir)
|
|
626
|
+
throw new Error('尚未创建本地插件标签,请先上传单个本地插件或将其作为独立标签页添加')
|
|
627
|
+
}
|
|
628
|
+
await copyEntriesToLocalCache(entries, { overwrite, tempExtractDir, stripRootMarketplace: true })
|
|
629
|
+
return {
|
|
630
|
+
ok: true,
|
|
631
|
+
kind: 'marketplace',
|
|
632
|
+
target: 'local',
|
|
633
|
+
sourceId: 'local',
|
|
634
|
+
source: localSource,
|
|
635
|
+
plugins: entries.map((entry) => entry.row),
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
|
|
313
639
|
const label = marketSourceName(effectiveDir, basename(effectiveDir))
|
|
314
|
-
const name = String(sourceName || '').trim().slice(0, 30) || label
|
|
640
|
+
const name = String(sourceName || '').trim().slice(0, 30) || formatDisplayName(label)
|
|
315
641
|
const sourceId = `local-${slugify(label) || 'market'}`
|
|
316
642
|
const dest = sourceCacheDir(sourceId)
|
|
317
|
-
const
|
|
643
|
+
const existing = state.sources.find(source => source.id === sourceId)
|
|
644
|
+
// Only a *new* tab can collide with an existing one. An overwrite reuses its
|
|
645
|
+
// own tab, and the name it compares here is the manifest's, not whatever the
|
|
646
|
+
// user renamed that tab to.
|
|
647
|
+
if (!existing && state.sources.some(source => String(source.name || '').trim() === name)) {
|
|
648
|
+
if (tempExtractDir) safeRmDir(tempExtractDir)
|
|
649
|
+
throw new Error(`分类标签「${name}」已存在,请换一个名称`)
|
|
650
|
+
}
|
|
318
651
|
if (existsSync(dest) && !overwrite && state.sources.some(source => source.id === sourceId)) {
|
|
319
652
|
if (tempExtractDir) safeRmDir(tempExtractDir)
|
|
320
653
|
throw new Error(`已存在同名插件市场「${label}」`)
|
|
@@ -332,14 +665,14 @@ export async function importLocalMarketplace(rawPath, { sourceName = '', overwri
|
|
|
332
665
|
// Keep each plugin on the path its manifest declares: a rewritten layout
|
|
333
666
|
// (`plugins/<name>`) would break `./external_plugins/x` and `./`, and the
|
|
334
667
|
// reader resolves sources against the copied manifest after a restart.
|
|
335
|
-
const
|
|
336
|
-
mkdirSync(dirname(
|
|
337
|
-
if (resolve(
|
|
338
|
-
else copyTreeContents(dir,
|
|
668
|
+
const targetDir = pluginPath(dest, `./${row.source.replace(/^\.\//, '')}`)
|
|
669
|
+
mkdirSync(dirname(targetDir), { recursive: true })
|
|
670
|
+
if (resolve(targetDir) !== resolve(dest)) cpSync(dir, targetDir, { recursive: true, force: true })
|
|
671
|
+
else copyTreeContents(dir, targetDir)
|
|
339
672
|
}
|
|
340
673
|
// Carry the marketplace manifest itself so the standard reader finds the
|
|
341
674
|
// same rows on the next start, not only during this upload.
|
|
342
|
-
for (const relativePath of
|
|
675
|
+
for (const relativePath of MARKETPLACE_MANIFESTS) {
|
|
343
676
|
const file = join(effectiveDir, relativePath)
|
|
344
677
|
if (!existsSync(file)) continue
|
|
345
678
|
const out = join(dest, relativePath)
|
|
@@ -349,11 +682,24 @@ export async function importLocalMarketplace(rawPath, { sourceName = '', overwri
|
|
|
349
682
|
} finally {
|
|
350
683
|
if (tempExtractDir) safeRmDir(tempExtractDir)
|
|
351
684
|
}
|
|
352
|
-
if (
|
|
685
|
+
if (existing) {
|
|
686
|
+
// Overwriting keeps the tab's name unless the user typed a new one;
|
|
687
|
+
// otherwise a plain "overwrite update" would revert their naming.
|
|
688
|
+
const renamed = String(sourceName || '').trim().slice(0, 30)
|
|
689
|
+
if (renamed && existing.name !== renamed) {
|
|
690
|
+
existing.name = renamed
|
|
691
|
+
writeState(state)
|
|
692
|
+
}
|
|
693
|
+
} else {
|
|
353
694
|
state.sources.push({ id: sourceId, name, url: `local-market://${sourceId}`, addedAt: Date.now() })
|
|
354
695
|
writeState(state)
|
|
355
696
|
}
|
|
356
697
|
invalidateMarketplaceCache(sourceId)
|
|
698
|
+
invalidateParserCaches()
|
|
699
|
+
invalidateLocalIconCache(sourceId)
|
|
700
|
+
if (overwrite) {
|
|
701
|
+
await refreshInstalledPluginsFromSource(sourceId, entries.map((entry) => entry.row.name))
|
|
702
|
+
}
|
|
357
703
|
return { ok: true, kind: 'marketplace', sourceId, source: state.sources.find(source => source.id === sourceId), plugins: entries.map(entry => entry.row) }
|
|
358
704
|
}
|
|
359
705
|
|
|
@@ -372,42 +718,15 @@ export async function importLocalPlugin({ path: rawPath, overwrite = false, sour
|
|
|
372
718
|
throw new Error(`已存在同名插件「${cleanPluginName}」`)
|
|
373
719
|
}
|
|
374
720
|
|
|
375
|
-
|
|
376
|
-
mkdirSync(localDir, { recursive: true })
|
|
377
|
-
for (const { row, dir } of entries) {
|
|
378
|
-
const destDir = resolve(localDir, safeSegment(row.name, 'plugin name'))
|
|
379
|
-
const srcDir = resolve(dir)
|
|
380
|
-
const contains = (root, child) => {
|
|
381
|
-
const rel = relative(root, child)
|
|
382
|
-
return !isAbsolute(rel) && rel !== '..' && !rel.startsWith('..\\') && !rel.startsWith('../')
|
|
383
|
-
}
|
|
384
|
-
if (destDir !== srcDir && (contains(destDir, srcDir) || contains(srcDir, destDir))) {
|
|
385
|
-
if (tempExtractDir) safeRmDir(tempExtractDir)
|
|
386
|
-
throw new Error('导入源与目标目录重叠,请从独立文件夹导入')
|
|
387
|
-
}
|
|
388
|
-
}
|
|
389
|
-
try {
|
|
390
|
-
for (const { row, dir } of entries) {
|
|
391
|
-
const destDir = join(localDir, safeSegment(row.name, 'plugin name'))
|
|
392
|
-
if (resolve(destDir) !== resolve(dir)) {
|
|
393
|
-
safeRmDir(destDir)
|
|
394
|
-
cpSync(dir, destDir, { recursive: true, force: true })
|
|
395
|
-
}
|
|
396
|
-
}
|
|
397
|
-
} finally {
|
|
398
|
-
if (tempExtractDir) safeRmDir(tempExtractDir)
|
|
399
|
-
}
|
|
721
|
+
await copyEntriesToLocalCache(entries, { overwrite, tempExtractDir, stripRootMarketplace: false })
|
|
400
722
|
|
|
401
|
-
//
|
|
723
|
+
// The tab is named once, on the first import. Later imports land in that
|
|
724
|
+
// same tab: re-applying a name here would silently rename a tab the user
|
|
725
|
+
// already chose, and every upload would ask the same question again.
|
|
402
726
|
const srcName = String(sourceName || '').trim().slice(0, 30)
|
|
403
727
|
const state = readState()
|
|
404
728
|
let localSource = state.sources.find((s) => s.id === 'local')
|
|
405
|
-
if (localSource) {
|
|
406
|
-
if (srcName && localSource.name !== srcName) {
|
|
407
|
-
localSource.name = srcName
|
|
408
|
-
writeState(state)
|
|
409
|
-
}
|
|
410
|
-
} else {
|
|
729
|
+
if (!localSource) {
|
|
411
730
|
localSource = {
|
|
412
731
|
id: 'local',
|
|
413
732
|
name: srcName || '本地插件',
|
|
@@ -418,8 +737,6 @@ export async function importLocalPlugin({ path: rawPath, overwrite = false, sour
|
|
|
418
737
|
writeState(state)
|
|
419
738
|
}
|
|
420
739
|
|
|
421
|
-
invalidateMarketplaceCache('local')
|
|
422
|
-
|
|
423
740
|
return {
|
|
424
741
|
ok: true,
|
|
425
742
|
sourceId: 'local',
|
|
@@ -427,3 +744,54 @@ export async function importLocalPlugin({ path: rawPath, overwrite = false, sour
|
|
|
427
744
|
plugins: entries.map((e) => e.row),
|
|
428
745
|
}
|
|
429
746
|
}
|
|
747
|
+
|
|
748
|
+
const UPLOADED_MARKET_URL = 'local-market://'
|
|
749
|
+
|
|
750
|
+
function isUploadedMarket(source) {
|
|
751
|
+
return Boolean(source) && String(source.url || '').startsWith(UPLOADED_MARKET_URL)
|
|
752
|
+
}
|
|
753
|
+
|
|
754
|
+
/**
|
|
755
|
+
* Remove an uploaded marketplace tab that old versions created *beside* the
|
|
756
|
+
* local tab under the very same name. That duplicate is what put two
|
|
757
|
+
* identical labels in the tab bar.
|
|
758
|
+
*
|
|
759
|
+
* It is removed, never merged: an uploaded marketplace is a marketplace (its
|
|
760
|
+
* cache root is a market, read through its own marketplace.json), while the
|
|
761
|
+
* local tab is a flat list of plugin directories. Flattening one into the
|
|
762
|
+
* other would silently turn a market into a plugin.
|
|
763
|
+
*
|
|
764
|
+
* Narrow on purpose: only an uploaded-market source, only when its display
|
|
765
|
+
* name *exactly* equals the local tab's, only when its cache really holds a
|
|
766
|
+
* marketplace manifest, and never when one of its plugins is still installed
|
|
767
|
+
* (that deletion is the user's call, not a startup side effect).
|
|
768
|
+
*
|
|
769
|
+
* Idempotent: with nothing to remove it writes nothing.
|
|
770
|
+
*/
|
|
771
|
+
export function removeDuplicateLocalMarketTabs() {
|
|
772
|
+
const state = readState()
|
|
773
|
+
const localName = String(state.sources.find((source) => source.id === 'local')?.name || '').trim()
|
|
774
|
+
if (!localName) return { removed: [] }
|
|
775
|
+
|
|
776
|
+
const duplicates = state.sources.filter((source) =>
|
|
777
|
+
source && isUploadedMarket(source) &&
|
|
778
|
+
source.id !== 'local' &&
|
|
779
|
+
String(source.name || '').trim() === localName &&
|
|
780
|
+
isMarketplaceDir(sourceCacheDir(source.id)),
|
|
781
|
+
)
|
|
782
|
+
if (!duplicates.length) return { removed: [] }
|
|
783
|
+
|
|
784
|
+
const ids = duplicates.map((source) => source.id)
|
|
785
|
+
const installed = state.plugins.filter((record) => ids.includes(record.sourceId))
|
|
786
|
+
if (installed.length) {
|
|
787
|
+
// Keep the tab rather than orphan install records behind the user's back.
|
|
788
|
+
return { removed: [], kept: ids, installed: installed.map((record) => record.name) }
|
|
789
|
+
}
|
|
790
|
+
|
|
791
|
+
for (const id of ids) safeRmDir(sourceCacheDir(id))
|
|
792
|
+
state.sources = state.sources.filter((source) => !ids.includes(source.id))
|
|
793
|
+
if (Array.isArray(state.tabOrder)) state.tabOrder = state.tabOrder.filter((id) => !ids.includes(id))
|
|
794
|
+
writeState(state)
|
|
795
|
+
for (const id of ids) invalidateMarketplaceCache(id)
|
|
796
|
+
return { removed: ids }
|
|
797
|
+
}
|