@xi-ix/dsh-multi-sync 0.1.4 → 0.1.6

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/client.js +10 -1
  2. package/index.js +115 -25
  3. package/package.json +3 -1
package/client.js CHANGED
@@ -58,6 +58,7 @@ window.__ModuleLoader__.load({
58
58
  added: '新增',
59
59
  local: '本地',
60
60
  'local-unpublished': '本地(该版本未发布)',
61
+ 'local-unknown': '本地(registry 未响应)',
61
62
  localOrigin: '本地挂载 · 已发布',
62
63
  remote: '非 registry',
63
64
  unknown: '未知',
@@ -115,6 +116,7 @@ window.__ModuleLoader__.load({
115
116
  added: 'new',
116
117
  local: 'local',
117
118
  'local-unpublished': 'local (version not published)',
119
+ 'local-unknown': 'local (registry unreachable)',
118
120
  localOrigin: 'local mount · published',
119
121
  remote: 'non-registry',
120
122
  unknown: 'unknown',
@@ -475,7 +477,14 @@ window.__ModuleLoader__.load({
475
477
  { key: row.name },
476
478
  h('td', null, row.name),
477
479
  h('td', null, row.version || '—'),
478
- h('td', null, (row.bundle ? 'bundle' : 'dep') + (row.origin === 'local' ? ' · ' + L.localOrigin : '')),
480
+ h(
481
+ 'td',
482
+ null,
483
+ (row.bundle ? 'bundle' : 'dep') +
484
+ (row.origin === 'local'
485
+ ? ' · ' + (row.localVersion ? `${L.localOrigin} (${row.localVersion} → ${row.version})` : L.localOrigin)
486
+ : ''),
487
+ ),
479
488
  ),
480
489
  ),
481
490
  list.skipped.map((row) =>
package/index.js CHANGED
@@ -19,6 +19,7 @@ import { readFile, writeFile } from 'node:fs/promises'
19
19
  import { execFile } from 'node:child_process'
20
20
  import { homedir, platform } from 'node:os'
21
21
  import { basename, dirname, join } from 'node:path'
22
+ import { pathToFileURL } from 'node:url'
22
23
 
23
24
  export const name = '@xi-ix/dsh-multi-sync'
24
25
  export const inject = ['webServer']
@@ -226,21 +227,25 @@ function compareVersions(a, b) {
226
227
  // ---------------------------------------------------------------- operations
227
228
 
228
229
  /**
229
- * Whether the registry already serves this exact version.
230
+ * What the registry knows about a package: whether this exact version is served,
231
+ * and which version `latest` points at.
230
232
  *
231
- * A locally linked plugin that has been published is a normal downloadable entry
232
- * for the other machine, so the scan asks instead of assuming that every `link:`
233
- * or `file:` spec is unshareable. An unreachable registry answers "no", which
234
- * degrades to the previous behaviour rather than failing the scan.
233
+ * A locally linked plugin whose own version is unpublished can still travel as the
234
+ * latest published release, which is what the user actually wants on the other
235
+ * machine. An unreachable registry reports nothing rather than guessing.
235
236
  */
236
- async function publishedAt(packageName, version) {
237
+ async function registryFacts(packageName, version) {
237
238
  const base = process.env.npm_config_registry || 'https://registry.npmjs.org/'
238
- const url = `${base.replace(/\/+$/, '')}/${encodeURIComponent(packageName)}/${encodeURIComponent(version)}`
239
+ const url = `${base.replace(/\/+$/, '')}/${encodeURIComponent(packageName)}`
239
240
  try {
240
- const response = await fetch(url, { headers: { accept: 'application/json' }, signal: AbortSignal.timeout(6000) })
241
- return response.ok
241
+ const response = await fetch(url, { headers: { accept: 'application/json' }, signal: AbortSignal.timeout(8000) })
242
+ if (!response.ok) return { reachable: true, has: false, latest: null }
243
+ const document = await response.json()
244
+ const versions = document?.versions ?? {}
245
+ const latest = typeof document?.['dist-tags']?.latest === 'string' ? document['dist-tags'].latest : null
246
+ return { reachable: true, has: typeof versions[version] === 'object', latest }
242
247
  } catch {
243
- return false
248
+ return { reachable: false, has: false, latest: null }
244
249
  }
245
250
  }
246
251
 
@@ -255,20 +260,38 @@ async function scan() {
255
260
  for (const [packageName, spec] of Object.entries(dependencies)) {
256
261
  const kind = specKind(String(spec))
257
262
  if (kind !== 'registry') {
258
- // Local mount: it still travels if the same version is on the registry.
263
+ // Local mount: it still travels if the registry serves the same version, and
264
+ // falls back to the latest published release when the local one is ahead.
259
265
  const facts = await installedFacts(profile.dir, packageName)
260
- if (facts?.version && PACKAGE_NAME.test(packageName) && (await publishedAt(packageName, facts.version))) {
261
- plugins.push({
262
- name: packageName,
263
- version: facts.version,
264
- spec: String(spec),
265
- bundle: facts.bundle ?? bundles.has(packageName),
266
- present: true,
267
- origin: 'local',
268
- })
266
+ if (facts?.version && PACKAGE_NAME.test(packageName)) {
267
+ const meta = await registryFacts(packageName, facts.version)
268
+ if (meta.has) {
269
+ plugins.push({
270
+ name: packageName,
271
+ version: facts.version,
272
+ spec: String(spec),
273
+ bundle: facts.bundle ?? bundles.has(packageName),
274
+ present: true,
275
+ origin: 'local',
276
+ })
277
+ continue
278
+ }
279
+ if (meta.latest !== null) {
280
+ plugins.push({
281
+ name: packageName,
282
+ version: meta.latest,
283
+ spec: String(spec),
284
+ bundle: facts.bundle ?? bundles.has(packageName),
285
+ present: true,
286
+ origin: 'local',
287
+ localVersion: facts.version,
288
+ })
289
+ continue
290
+ }
291
+ skipped.push({ name: packageName, spec: String(spec), reason: meta.reachable ? 'local-unpublished' : 'local-unknown' })
269
292
  continue
270
293
  }
271
- skipped.push({ name: packageName, spec: String(spec), reason: facts?.version ? 'local-unpublished' : kind })
294
+ skipped.push({ name: packageName, spec: String(spec), reason: kind })
272
295
  continue
273
296
  }
274
297
  if (!PACKAGE_NAME.test(packageName)) {
@@ -496,6 +519,72 @@ async function runDsh(args) {
496
519
  return { ...(last ?? { ok: false, stdout: '', stderr: '' }), command: 'none', ok: false, tried }
497
520
  }
498
521
 
522
+ /**
523
+ * Install through the plugin manager's own operations, inside this process.
524
+ *
525
+ * The CLI refuses `--profile desktop` by design — `manageDesktopProfile` is reserved
526
+ * for the installed carrier — so spawning it can never work against the desktop app.
527
+ * The plugin manager is mounted in this very process, so its operations module is the
528
+ * sanctioned entry point: the same code the CLI and the plugin_manager tool call.
529
+ */
530
+ async function installInProcess(profile, specs) {
531
+ const resources = resourcesRoot()
532
+ if (!resources) return undefined
533
+ const dshRoot = join(resources, 'app.asar', 'dsh', 'node_modules', '@deepseek-ai')
534
+ const operations = join(dshRoot, 'dsh-plugin-manager', 'lib', 'operations.js')
535
+ if (!existsSync(operations)) return undefined
536
+
537
+ let module
538
+ try {
539
+ module = await import(pathToFileURL(operations).href)
540
+ } catch (error) {
541
+ return undefined
542
+ }
543
+ if (typeof module.runProfilePnpm !== 'function') return undefined
544
+
545
+ const output = []
546
+ const tail = (text) => text.trim().split(/\r?\n/).slice(-40).join('\n')
547
+ try {
548
+ const result = await module.runProfilePnpm(
549
+ {
550
+ profile: profile.name,
551
+ dir: profile.dir,
552
+ installAnchor: join(dshRoot, 'dsh', 'package.json'),
553
+ cwd: process.cwd(),
554
+ },
555
+ ['add', ...specs],
556
+ {
557
+ execution: 'cli',
558
+ outputBytes: 64 * 1024,
559
+ lockWaitMs: 120_000,
560
+ lookupTimeoutMs: 120_000,
561
+ onOutput: (text) => output.push(String(text)),
562
+ },
563
+ )
564
+ const exitCode = typeof result?.exitCode === 'number' ? result.exitCode : null
565
+ const text = output.join('')
566
+ return {
567
+ ok: exitCode === 0,
568
+ command: 'in-process plugin manager',
569
+ tried: [operations],
570
+ exitCode,
571
+ stdout: tail(text),
572
+ stderr: exitCode === 0 ? '' : tail(text),
573
+ message: exitCode === 0 ? null : 'plugin manager refused the install',
574
+ }
575
+ } catch (error) {
576
+ return {
577
+ ok: false,
578
+ command: 'in-process plugin manager',
579
+ tried: [operations],
580
+ exitCode: null,
581
+ stdout: '',
582
+ stderr: String(error?.message ?? error),
583
+ message: 'plugin manager threw',
584
+ }
585
+ }
586
+ }
587
+
499
588
  async function applyList(items) {
500
589
  const profile = resolveProfile()
501
590
  const specs = []
@@ -506,17 +595,18 @@ async function applyList(items) {
506
595
  }
507
596
  if (specs.length === 0) throw new Error('nothing to install: need name@version entries')
508
597
 
509
- const run = await runDsh(['plugin', '--profile', profile.name, 'add', ...specs])
510
598
  const tail = (text) => text.trim().split(/\r?\n/).slice(-40).join('\n')
599
+ const inProcess = await installInProcess(profile, specs)
600
+ const run = inProcess ?? (await runDsh(['plugin', '--profile', profile.name, 'add', ...specs]))
511
601
  return {
512
602
  ok: run.ok,
513
603
  specs,
514
604
  command: run.command,
515
605
  tried: run.tried ?? [],
516
606
  manual: `dsh plugin --profile ${profile.name} add ${specs.join(' ')}`,
517
- exitCode: typeof run.code === 'number' ? run.code : null,
518
- stdout: tail(run.stdout),
519
- stderr: tail(run.stderr),
607
+ exitCode: typeof run.exitCode === 'number' ? run.exitCode : typeof run.code === 'number' ? run.code : null,
608
+ stdout: tail(run.stdout ?? ''),
609
+ stderr: tail(run.stderr ?? ''),
520
610
  message: run.ok ? null : run.message ?? 'dsh plugin add failed',
521
611
  }
522
612
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xi-ix/dsh-multi-sync",
3
- "version": "0.1.4",
3
+ "version": "0.1.6",
4
4
  "description": "Copy one machine's installed DSH plugin list to another: scan the profile, export a pinned list, then install the selected entries on the other machine.",
5
5
  "type": "module",
6
6
  "main": "index.js",
@@ -52,3 +52,5 @@
52
52
 
53
53
 
54
54
 
55
+
56
+