@xi-ix/dsh-multi-sync 0.1.6 → 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 +62 -9
  2. package/package.json +2 -1
package/index.js CHANGED
@@ -14,10 +14,10 @@
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
22
  import { pathToFileURL } from 'node:url'
23
23
 
@@ -520,12 +520,63 @@ async function runDsh(args) {
520
520
  }
521
521
 
522
522
  /**
523
- * Install through the plugin manager's own operations, inside this process.
523
+ * Run the real CLI in a child process, with the desktop-profile switch turned on.
524
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.
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.
529
580
  */
530
581
  async function installInProcess(profile, specs) {
531
582
  const resources = resourcesRoot()
@@ -596,8 +647,10 @@ async function applyList(items) {
596
647
  if (specs.length === 0) throw new Error('nothing to install: need name@version entries')
597
648
 
598
649
  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]))
650
+ const run =
651
+ (await installViaCli(profile, specs)) ??
652
+ (await installInProcess(profile, specs)) ??
653
+ (await runDsh(['plugin', '--profile', profile.name, 'add', ...specs]))
601
654
  return {
602
655
  ok: run.ok,
603
656
  specs,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xi-ix/dsh-multi-sync",
3
- "version": "0.1.6",
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",
@@ -54,3 +54,4 @@
54
54
 
55
55
 
56
56
 
57
+