@xi-ix/dsh-multi-sync 0.1.5 → 0.1.7

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 (2) hide show
  1. package/index.js +127 -6
  2. package/package.json +3 -1
package/index.js CHANGED
@@ -14,11 +14,12 @@
14
14
  * registration is the plugin manager's job, and enable/disable belongs to the
15
15
  * market. That boundary is deliberate — two writers on those files fight.
16
16
  */
17
- import { existsSync, readdirSync } from 'node:fs'
17
+ import { existsSync, readdirSync, writeFileSync } from 'node:fs'
18
18
  import { readFile, writeFile } from 'node:fs/promises'
19
19
  import { execFile } from 'node:child_process'
20
- import { homedir, platform } from 'node:os'
20
+ import { homedir, platform, tmpdir } 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']
@@ -518,6 +519,123 @@ async function runDsh(args) {
518
519
  return { ...(last ?? { ok: false, stdout: '', stderr: '' }), command: 'none', ok: false, tried }
519
520
  }
520
521
 
522
+ /**
523
+ * Run the real CLI in a child process, with the desktop-profile switch turned on.
524
+ *
525
+ * The shipped `bin.js` entry calls `runCli()` without `manageDesktopProfile`, so it
526
+ * refuses the desktop profile by design. Its exported `runCli(options)` accepts the
527
+ * switch, so a tiny wrapper that imports the module and calls it with the option is
528
+ * still the ordinary CLI — same argument parsing, same pnpm run, same exit codes —
529
+ * just launched the way the installed carrier launches it.
530
+ */
531
+ function wrapperScript() {
532
+ const file = join(tmpdir(), 'dsh-multi-sync-cli.mjs')
533
+ if (!existsSync(file)) {
534
+ writeFileSync(
535
+ file,
536
+ `import { pathToFileURL } from 'node:url'
537
+ const [entry, ...args] = process.argv.slice(2)
538
+ const cli = await import(pathToFileURL(entry).href)
539
+ const original = process.argv
540
+ process.argv = [process.execPath, entry, ...args]
541
+ try { await cli.runCli({ manageDesktopProfile: true }) } finally { process.argv = original }
542
+ `,
543
+ { mode: 0o600 },
544
+ )
545
+ }
546
+ return file
547
+ }
548
+
549
+ async function installViaCli(profile, specs) {
550
+ const resources = resourcesRoot()
551
+ if (!resources) return undefined
552
+ const entry = join(resources, 'app.asar', 'dsh', 'node_modules', '@deepseek-ai', 'dsh', 'lib', 'bin.js')
553
+ if (!existsSync(entry)) return undefined
554
+ const script = wrapperScript()
555
+ const argv = [script, entry, 'plugin', '--profile', profile.name, 'add', ...specs]
556
+ const tail = (text) => text.trim().split(/\r?\n/).slice(-40).join('\n')
557
+ const run = await runOnce(process.execPath, argv, {
558
+ env: { ...process.env, ELECTRON_RUN_AS_NODE: '1' },
559
+ timeout: 15 * 60 * 1000,
560
+ })
561
+ // A module that fails to load is not a CLI verdict; let the next strategy try.
562
+ const loadFailed = !run.ok && /\b(?:Cannot find module|ERR_MODULE_NOT_FOUND|ERR_UNSUPPORTED_ESM_URL_SCHEME|SyntaxError)\b/.test(run.stderr)
563
+ if (loadFailed) return undefined
564
+ return {
565
+ ok: run.ok,
566
+ command: `dsh plugin --profile ${profile.name} add … (carrier switch)`,
567
+ tried: [`${process.execPath} ${script} ${entry} plugin --profile ${profile.name} add …`],
568
+ exitCode: typeof run.code === 'number' ? run.code : null,
569
+ stdout: tail(run.stdout),
570
+ stderr: tail(run.stderr),
571
+ message: run.ok ? null : run.message ?? 'dsh plugin add failed',
572
+ }
573
+ }
574
+
575
+ /**
576
+ * Fallback: drive the plugin manager's operations directly, in this process.
577
+ *
578
+ * Reached only when no CLI entry is available; it is the same module the CLI and the
579
+ * plugin_manager tool call.
580
+ */
581
+ async function installInProcess(profile, specs) {
582
+ const resources = resourcesRoot()
583
+ if (!resources) return undefined
584
+ const dshRoot = join(resources, 'app.asar', 'dsh', 'node_modules', '@deepseek-ai')
585
+ const operations = join(dshRoot, 'dsh-plugin-manager', 'lib', 'operations.js')
586
+ if (!existsSync(operations)) return undefined
587
+
588
+ let module
589
+ try {
590
+ module = await import(pathToFileURL(operations).href)
591
+ } catch (error) {
592
+ return undefined
593
+ }
594
+ if (typeof module.runProfilePnpm !== 'function') return undefined
595
+
596
+ const output = []
597
+ const tail = (text) => text.trim().split(/\r?\n/).slice(-40).join('\n')
598
+ try {
599
+ const result = await module.runProfilePnpm(
600
+ {
601
+ profile: profile.name,
602
+ dir: profile.dir,
603
+ installAnchor: join(dshRoot, 'dsh', 'package.json'),
604
+ cwd: process.cwd(),
605
+ },
606
+ ['add', ...specs],
607
+ {
608
+ execution: 'cli',
609
+ outputBytes: 64 * 1024,
610
+ lockWaitMs: 120_000,
611
+ lookupTimeoutMs: 120_000,
612
+ onOutput: (text) => output.push(String(text)),
613
+ },
614
+ )
615
+ const exitCode = typeof result?.exitCode === 'number' ? result.exitCode : null
616
+ const text = output.join('')
617
+ return {
618
+ ok: exitCode === 0,
619
+ command: 'in-process plugin manager',
620
+ tried: [operations],
621
+ exitCode,
622
+ stdout: tail(text),
623
+ stderr: exitCode === 0 ? '' : tail(text),
624
+ message: exitCode === 0 ? null : 'plugin manager refused the install',
625
+ }
626
+ } catch (error) {
627
+ return {
628
+ ok: false,
629
+ command: 'in-process plugin manager',
630
+ tried: [operations],
631
+ exitCode: null,
632
+ stdout: '',
633
+ stderr: String(error?.message ?? error),
634
+ message: 'plugin manager threw',
635
+ }
636
+ }
637
+ }
638
+
521
639
  async function applyList(items) {
522
640
  const profile = resolveProfile()
523
641
  const specs = []
@@ -528,17 +646,20 @@ async function applyList(items) {
528
646
  }
529
647
  if (specs.length === 0) throw new Error('nothing to install: need name@version entries')
530
648
 
531
- const run = await runDsh(['plugin', '--profile', profile.name, 'add', ...specs])
532
649
  const tail = (text) => text.trim().split(/\r?\n/).slice(-40).join('\n')
650
+ const run =
651
+ (await installViaCli(profile, specs)) ??
652
+ (await installInProcess(profile, specs)) ??
653
+ (await runDsh(['plugin', '--profile', profile.name, 'add', ...specs]))
533
654
  return {
534
655
  ok: run.ok,
535
656
  specs,
536
657
  command: run.command,
537
658
  tried: run.tried ?? [],
538
659
  manual: `dsh plugin --profile ${profile.name} add ${specs.join(' ')}`,
539
- exitCode: typeof run.code === 'number' ? run.code : null,
540
- stdout: tail(run.stdout),
541
- stderr: tail(run.stderr),
660
+ exitCode: typeof run.exitCode === 'number' ? run.exitCode : typeof run.code === 'number' ? run.code : null,
661
+ stdout: tail(run.stdout ?? ''),
662
+ stderr: tail(run.stderr ?? ''),
542
663
  message: run.ok ? null : run.message ?? 'dsh plugin add failed',
543
664
  }
544
665
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xi-ix/dsh-multi-sync",
3
- "version": "0.1.5",
3
+ "version": "0.1.7",
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",
@@ -53,3 +53,5 @@
53
53
 
54
54
 
55
55
 
56
+
57
+