cfpilot 0.9.0 → 0.9.2

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/dist/cli.js CHANGED
@@ -87,7 +87,7 @@ var VERSION, NAME;
87
87
  var init_version = __esm({
88
88
  "src/shared/version.ts"() {
89
89
  "use strict";
90
- VERSION = "0.9.0";
90
+ VERSION = "0.9.2";
91
91
  NAME = "cfpilot";
92
92
  }
93
93
  });
@@ -481,6 +481,23 @@ function saveConfig(patch) {
481
481
  mkdirSync(configDir(), { recursive: true });
482
482
  writeFileSync(configFile(), JSON.stringify(next, null, 2) + "\n", "utf8");
483
483
  }
484
+ function saveToken(token, keep = {}) {
485
+ const trimmed = token.trim();
486
+ const current = loadConfig();
487
+ const changed = (current.token ?? "") !== trimmed;
488
+ const explicit = {};
489
+ for (const key of Object.keys(keep)) {
490
+ const value = keep[key];
491
+ if (value !== void 0) explicit[key] = value;
492
+ }
493
+ if (!changed) {
494
+ saveConfig({ token: trimmed, ...explicit });
495
+ return;
496
+ }
497
+ const clearing = {};
498
+ for (const key of ACCOUNT_SCOPED_FIELDS) clearing[key] = "";
499
+ saveConfig({ ...clearing, ...explicit, token: trimmed });
500
+ }
484
501
  function clearConfig() {
485
502
  const file = configFile();
486
503
  if (existsSync(file)) writeFileSync(file, "{}\n", "utf8");
@@ -491,9 +508,19 @@ function resolveToken(fromArgs) {
491
508
  function isConfigured(cfg = loadConfig()) {
492
509
  return Boolean(cfg.token);
493
510
  }
511
+ var ACCOUNT_SCOPED_FIELDS;
494
512
  var init_store = __esm({
495
513
  "src/menu/store.ts"() {
496
514
  "use strict";
515
+ ACCOUNT_SCOPED_FIELDS = [
516
+ "accountId",
517
+ "accountName",
518
+ "kvNamespaceId",
519
+ "panelHost",
520
+ "dataHost",
521
+ "wsPath",
522
+ "panelPassword"
523
+ ];
497
524
  }
498
525
  });
499
526
 
@@ -666,7 +693,9 @@ var init_cf_api = __esm({
666
693
  }
667
694
  if (response.status === 404) return null;
668
695
  if (!response.ok) {
669
- throw new Error(`\u8BFB\u53D6 KV \u5931\u8D25\uFF08\u65E0\u6CD5\u786E\u8BA4\u5F52\u5C5E\uFF09\uFF1AHTTP ${response.status}`);
696
+ const text = await response.text().catch(() => "");
697
+ const detail = parseApiError(response.status, text).message;
698
+ throw new Error(`\u8BFB\u53D6 KV \u5931\u8D25\uFF08\u65E0\u6CD5\u786E\u8BA4\u5F52\u5C5E\uFF09\uFF1A${detail}`);
670
699
  }
671
700
  return await response.text();
672
701
  }
@@ -997,6 +1026,38 @@ var init_cf_api = __esm({
997
1026
  }
998
1027
  });
999
1028
 
1029
+ // src/deploy/account-resolve.ts
1030
+ async function findAccountByKv(client, accounts, kvId) {
1031
+ let unreadable = 0;
1032
+ for (const account of accounts) {
1033
+ let namespaces;
1034
+ try {
1035
+ namespaces = await client.listKvNamespaces(account.id);
1036
+ } catch {
1037
+ unreadable++;
1038
+ continue;
1039
+ }
1040
+ if (namespaces.some((ns) => ns.id === kvId)) {
1041
+ return { owner: account, unreadable };
1042
+ }
1043
+ }
1044
+ return { owner: null, unreadable };
1045
+ }
1046
+ async function checkKvInAccount(client, accountId, kvId) {
1047
+ let namespaces;
1048
+ try {
1049
+ namespaces = await client.listKvNamespaces(accountId);
1050
+ } catch {
1051
+ return "unknown";
1052
+ }
1053
+ return namespaces.some((ns) => ns.id === kvId) ? "yes" : "no";
1054
+ }
1055
+ var init_account_resolve = __esm({
1056
+ "src/deploy/account-resolve.ts"() {
1057
+ "use strict";
1058
+ }
1059
+ });
1060
+
1000
1061
  // src/deploy/utils.ts
1001
1062
  import { randomBytes } from "node:crypto";
1002
1063
  function randomAlnum(length) {
@@ -1788,20 +1849,29 @@ __export(pages_deploy_exports, {
1788
1849
  });
1789
1850
  import { randomBytes as randomBytes2, randomUUID } from "node:crypto";
1790
1851
  async function readManifest(client, accountId, namespaceId) {
1852
+ const raw = await client.getKvValue(accountId, namespaceId, MANIFEST_KEY);
1853
+ if (raw === null) return null;
1854
+ let parsed;
1791
1855
  try {
1792
- const raw = await client.getKvValue(accountId, namespaceId, MANIFEST_KEY);
1793
- if (!raw) return null;
1794
- const parsed = JSON.parse(raw);
1795
- return parsed && parsed.v === 1 ? parsed : null;
1796
- } catch {
1797
- return null;
1856
+ parsed = JSON.parse(raw);
1857
+ } catch (err) {
1858
+ throw new Error(
1859
+ `\u8BFB\u53D6 KV \u5931\u8D25\uFF08\u65E0\u6CD5\u786E\u8BA4\u5F52\u5C5E\uFF09\uFF1A\u90E8\u7F72\u6E05\u5355 ${MANIFEST_KEY} \u4E0D\u662F\u5408\u6CD5 JSON\uFF08${err instanceof Error ? err.message : String(err)}\uFF09`
1860
+ );
1798
1861
  }
1862
+ const manifest = parsed;
1863
+ return manifest && manifest.v === 1 ? manifest : null;
1799
1864
  }
1800
1865
  async function listCfPilotNamespaces(client, accountId) {
1801
1866
  const namespaces = await client.listKvNamespaces(accountId);
1802
1867
  const out = [];
1803
1868
  for (const ns of namespaces) {
1804
- const manifest = await readManifest(client, accountId, ns.id);
1869
+ let manifest = null;
1870
+ try {
1871
+ manifest = await readManifest(client, accountId, ns.id);
1872
+ } catch {
1873
+ continue;
1874
+ }
1805
1875
  if (manifest) out.push({ id: ns.id, title: ns.title, manifest });
1806
1876
  }
1807
1877
  return out;
@@ -1812,22 +1882,21 @@ async function writeManifest(client, accountId, namespaceId, manifest) {
1812
1882
  } catch {
1813
1883
  }
1814
1884
  }
1885
+ function unableToConfirm(err) {
1886
+ const reason = err instanceof Error ? err.message : String(err);
1887
+ return {
1888
+ verdict: "occupied",
1889
+ manifest: null,
1890
+ detail: `\u65E0\u6CD5\u786E\u8BA4 KV \u5F52\u5C5E\uFF08${reason}\uFF09\u2014\u2014 \u8BF7\u68C0\u67E5 Token \u6743\u9650\u4E0E\u7F51\u7EDC\u540E\u91CD\u8BD5`,
1891
+ blocking: true
1892
+ };
1893
+ }
1815
1894
  async function inspectKvOwnership(client, accountId, namespaceId, controlProject) {
1816
1895
  let manifest;
1817
- let legacyConfig;
1818
- let cfpilotConfig;
1819
1896
  try {
1820
1897
  manifest = await readManifest(client, accountId, namespaceId);
1821
- legacyConfig = await client.getKvValue(accountId, namespaceId, "config");
1822
- cfpilotConfig = await client.getKvValue(accountId, namespaceId, "CONFIG");
1823
1898
  } catch (err) {
1824
- const reason = err instanceof Error ? err.message : String(err);
1825
- return {
1826
- verdict: "occupied",
1827
- manifest: null,
1828
- detail: `\u65E0\u6CD5\u786E\u8BA4 KV \u5F52\u5C5E\uFF08${reason}\uFF09\u2014\u2014 \u8BF7\u68C0\u67E5 Token \u6743\u9650\u4E0E\u7F51\u7EDC\u540E\u91CD\u8BD5`,
1829
- blocking: true
1830
- };
1899
+ return unableToConfirm(err);
1831
1900
  }
1832
1901
  if (manifest) {
1833
1902
  if (manifest.controlProject === controlProject) {
@@ -1845,6 +1914,12 @@ async function inspectKvOwnership(client, accountId, namespaceId, controlProject
1845
1914
  blocking: true
1846
1915
  };
1847
1916
  }
1917
+ let legacyConfig;
1918
+ try {
1919
+ legacyConfig = await client.getKvValue(accountId, namespaceId, "config");
1920
+ } catch (err) {
1921
+ return unableToConfirm(err);
1922
+ }
1848
1923
  if (legacyConfig !== null) {
1849
1924
  return {
1850
1925
  verdict: "foreign",
@@ -1856,6 +1931,12 @@ async function inspectKvOwnership(client, accountId, namespaceId, controlProject
1856
1931
  blocking: true
1857
1932
  };
1858
1933
  }
1934
+ let cfpilotConfig;
1935
+ try {
1936
+ cfpilotConfig = await client.getKvValue(accountId, namespaceId, "CONFIG");
1937
+ } catch (err) {
1938
+ return unableToConfirm(err);
1939
+ }
1859
1940
  if (cfpilotConfig !== null) {
1860
1941
  return {
1861
1942
  verdict: "stale",
@@ -1900,15 +1981,27 @@ async function assertKvOwnership(client, accountId, namespaceId, options) {
1900
1981
  }
1901
1982
  async function writeEffectiveConfig(client, accountId, namespaceId, effective, options, warnings, log) {
1902
1983
  let existing = {};
1984
+ let raw;
1903
1985
  try {
1904
- const raw = await client.getKvValue(accountId, namespaceId, "CONFIG");
1905
- if (raw) {
1986
+ raw = await client.getKvValue(accountId, namespaceId, "CONFIG");
1987
+ } catch (err) {
1988
+ warnings.push(
1989
+ `\u8BFB\u53D6\u65E2\u6709 KV \u914D\u7F6E\u5931\u8D25\uFF1A${err instanceof Error ? err.message : String(err)}
1990
+ \u5DF2\u8DF3\u8FC7\u5199\u5165 CONFIG\uFF08\u907F\u514D\u8986\u76D6\u4F60\u5728\u9762\u677F\u91CC\u6539\u8FC7\u7684\u4F18\u9009\u6C60 / \u4EE3\u7406\u6C60 / \u8BA2\u9605\u57DF\u540D\uFF09\u3002
1991
+ \u5185\u6838\u4F1A\u56DE\u9000\u5230\u73AF\u5883\u53D8\u91CF\uFF08UUID / WS \u8DEF\u5F84 / Trojan \u5BC6\u7801\u6765\u81EA\u90E8\u7F72\u65F6\u6CE8\u5165\u7684 env\uFF09\u3002
1992
+ \u2192 \u8BF7\u68C0\u67E5 Token \u7684 KV \u8BFB\u53D6\u6743\u9650\u4E0E\u7F51\u7EDC\u8FDE\u901A\u6027\u540E\u91CD\u8BD5\u3002`
1993
+ );
1994
+ return;
1995
+ }
1996
+ if (raw) {
1997
+ try {
1906
1998
  const parsed = JSON.parse(raw);
1907
1999
  if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
1908
2000
  existing = parsed;
1909
2001
  }
2002
+ } catch {
2003
+ warnings.push("\u65E2\u6709 KV \u914D\u7F6E\u4E0D\u662F\u5408\u6CD5 JSON\uFF0C\u5DF2\u6309\u7A7A\u914D\u7F6E\u91CD\u5199");
1910
2004
  }
1911
- } catch {
1912
2005
  }
1913
2006
  const merged = {
1914
2007
  ...existing,
@@ -2054,7 +2147,16 @@ async function deployPagesSplit(options) {
2054
2147
  const kv = { KV: { namespace_id: options.kvNamespaceId } };
2055
2148
  const kernel = await resolveKernel(options, log, warnings);
2056
2149
  const effectiveBuild = kernel.build || (options.localBuild ?? "").trim();
2057
- const manifest = await readManifest(client, options.accountId, options.kvNamespaceId);
2150
+ let manifest;
2151
+ try {
2152
+ manifest = await readManifest(client, options.accountId, options.kvNamespaceId);
2153
+ } catch (err) {
2154
+ throw new Error(
2155
+ `\u8BFB\u53D6 KV \u90E8\u7F72\u6E05\u5355\u5931\u8D25\uFF0C\u5DF2\u4E2D\u6B62\u90E8\u7F72\uFF1A${err instanceof Error ? err.message : String(err)}
2156
+ \u2192 \u65E0\u6CD5\u786E\u8BA4\u8BE5 KV \u91CC\u662F\u5426\u5DF2\u6709\u90E8\u7F72\uFF0C\u7EE7\u7EED\u90E8\u7F72\u53EF\u80FD\u6539\u574F\u65E2\u6709\u5B9E\u4F8B\u7684\u51ED\u636E\u3002
2157
+ \u2192 \u8BF7\u68C0\u67E5 Token \u7684 KV \u8BFB\u53D6\u6743\u9650\u4E0E\u7F51\u7EDC\u8FDE\u901A\u6027\u540E\u91CD\u8BD5\u3002`
2158
+ );
2159
+ }
2058
2160
  let effective = {
2059
2161
  panelPath: options.panelPath,
2060
2162
  panelPassword: options.panelPassword,
@@ -2504,6 +2606,7 @@ Token \u6240\u9700\u6743\u9650:
2504
2606
  var command_exports = {};
2505
2607
  __export(command_exports, {
2506
2608
  listKvInstances: () => listKvInstances,
2609
+ resolveDeployAccount: () => resolveDeployAccount,
2507
2610
  runDeployCommand: () => runDeployCommand
2508
2611
  });
2509
2612
  async function listKvInstances(client, accountId) {
@@ -2543,6 +2646,87 @@ ${hint}
2543
2646
  ` + (instanceIds.size > 0 ? "\u66F4\u65B0\u67D0\u4E2A\u5B9E\u4F8B\uFF1Acfpilot deploy --kv <id>\n" : "") + "\u90E8\u7F72\u65B0\u5B9E\u4F8B\uFF1Acfpilot deploy --new-kv\n"
2544
2647
  );
2545
2648
  }
2649
+ async function resolveDeployAccount(client, options) {
2650
+ const { accountFlag } = options;
2651
+ const kvId = options.ignoreKv ? "" : options.kvId;
2652
+ if (accountFlag) {
2653
+ if (kvId) {
2654
+ const check = await checkKvInAccount(client, accountFlag, kvId);
2655
+ if (check === "no") {
2656
+ return {
2657
+ ok: false,
2658
+ accountId: "",
2659
+ accountName: "",
2660
+ source: "flag",
2661
+ message: `\u2717 KV ${kvId} \u4E0D\u5C5E\u4E8E\u8D26\u53F7 ${accountFlag}
2662
+ \u2192 \u8D26\u53F7\u4E0E KV \u5FC5\u987B\u5C5E\u4E8E\u540C\u4E00\u4E2A Cloudflare \u8D26\u53F7\uFF0C\u5426\u5219\u4F1A\u5EFA\u51FA\u65E0\u6CD5\u4F7F\u7528\u7684\u5B64\u513F\u9879\u76EE
2663
+ `
2664
+ };
2665
+ }
2666
+ if (check === "unknown") {
2667
+ return {
2668
+ ok: true,
2669
+ accountId: accountFlag,
2670
+ accountName: accountFlag,
2671
+ source: "flag",
2672
+ message: `\xB7 \u672A\u80FD\u786E\u8BA4 KV ${kvId} \u662F\u5426\u5C5E\u4E8E\u8BE5\u8D26\u53F7\uFF08\u8BFB\u53D6 KV \u5217\u8868\u5931\u8D25\uFF09\uFF0C\u7EE7\u7EED
2673
+ `
2674
+ };
2675
+ }
2676
+ }
2677
+ return {
2678
+ ok: true,
2679
+ accountId: accountFlag,
2680
+ accountName: accountFlag,
2681
+ source: "flag",
2682
+ message: `\u4F7F\u7528\u8D26\u53F7\uFF1A${accountFlag}
2683
+ `
2684
+ };
2685
+ }
2686
+ const accounts = await client.listAccounts();
2687
+ if (accounts.length === 0) {
2688
+ return {
2689
+ ok: false,
2690
+ accountId: "",
2691
+ accountName: "",
2692
+ source: "first",
2693
+ message: "\u2717 \u8BE5 Token \u6CA1\u6709\u53EF\u8BBF\u95EE\u7684\u8D26\u53F7\n"
2694
+ };
2695
+ }
2696
+ if (kvId) {
2697
+ const { owner, unreadable } = await findAccountByKv(client, accounts, kvId);
2698
+ if (!owner) {
2699
+ const names = accounts.map((a) => a.name).join("\u3001");
2700
+ return {
2701
+ ok: false,
2702
+ accountId: "",
2703
+ accountName: "",
2704
+ source: "kv-lookup",
2705
+ message: unreadable > 0 ? `\u2717 \u65E0\u6CD5\u786E\u5B9A KV ${kvId} \u5C5E\u4E8E\u54EA\u4E2A\u8D26\u53F7\uFF08${names}\uFF1B\u5176\u4E2D ${unreadable} \u4E2A\u8D26\u53F7\u7684 KV \u5217\u8868\u8BFB\u53D6\u5931\u8D25\uFF09
2706
+ \u2192 \u8BF7\u7528 --account <\u8D26\u53F7ID> \u4E0E --kv <KV ID> \u4E00\u8D77\u6307\u5B9A
2707
+ ` : `\u2717 KV ${kvId} \u4E0D\u5C5E\u4E8E\u8BE5 Token \u7684\u4EFB\u4F55\u8D26\u53F7\uFF08${names}\uFF09
2708
+ \u2192 \u8BF7\u7528 --account <\u8D26\u53F7ID> \u4E0E --kv <KV ID> \u4E00\u8D77\u6307\u5B9A
2709
+ `
2710
+ };
2711
+ }
2712
+ return {
2713
+ ok: true,
2714
+ accountId: owner.id,
2715
+ accountName: owner.name,
2716
+ source: "kv-lookup",
2717
+ message: `\u4F7F\u7528\u8D26\u53F7\uFF1A${owner.name}\uFF08${owner.id}\uFF0C\u7531 --kv \u53CD\u67E5\uFF09
2718
+ `
2719
+ };
2720
+ }
2721
+ return {
2722
+ ok: true,
2723
+ accountId: accounts[0].id,
2724
+ accountName: accounts[0].name,
2725
+ source: "first",
2726
+ message: `\u4F7F\u7528\u8D26\u53F7\uFF1A${accounts[0].name}\uFF08${accounts[0].id}\uFF09
2727
+ `
2728
+ };
2729
+ }
2546
2730
  async function runDeployCommand(args) {
2547
2731
  if (flagBool(args, "h") || flagBool(args, "help")) {
2548
2732
  process.stdout.write(DEPLOY_HELP);
@@ -2555,44 +2739,21 @@ async function runDeployCommand(args) {
2555
2739
  return;
2556
2740
  }
2557
2741
  const client = new CloudflareClient({ token });
2558
- const accountId = flagString(args, "account") || "";
2559
2742
  let kvId = flagString(args, "kv") || "";
2560
- let resolvedAccount = accountId;
2561
- if (!resolvedAccount) {
2562
- const accounts = await client.listAccounts();
2563
- if (accounts.length === 0) {
2564
- process.stderr.write("\u2717 \u8BE5 Token \u6CA1\u6709\u53EF\u8BBF\u95EE\u7684\u8D26\u53F7\n");
2565
- process.exitCode = 1;
2566
- return;
2567
- }
2568
- if (kvId) {
2569
- let found = "";
2570
- for (const acc2 of accounts) {
2571
- const namespaces = await client.listKvNamespaces(acc2.id);
2572
- if (namespaces.some((ns) => ns.id === kvId)) {
2573
- found = acc2.id;
2574
- break;
2575
- }
2576
- }
2577
- if (!found) {
2578
- process.stderr.write(
2579
- `\u2717 KV ${kvId} \u4E0D\u5C5E\u4E8E\u8BE5 Token \u7684\u4EFB\u4F55\u8D26\u53F7\uFF08${accounts.map((a) => a.name).join("\u3001")}\uFF09\u2014\u2014 \u8BF7\u7528 --account \u6307\u5B9A\u6B63\u786E\u7684\u8D26\u53F7
2580
- `
2581
- );
2582
- process.exitCode = 1;
2583
- return;
2584
- }
2585
- resolvedAccount = found;
2586
- const acc = accounts.find((a) => a.id === found);
2587
- process.stdout.write(`\u4F7F\u7528\u8D26\u53F7\uFF1A${acc.name}\uFF08${acc.id}\uFF0C\u7531 --kv \u53CD\u67E5\uFF09
2588
- `);
2589
- } else {
2590
- resolvedAccount = accounts[0].id;
2591
- process.stdout.write(`\u4F7F\u7528\u8D26\u53F7\uFF1A${accounts[0].name}\uFF08${accounts[0].id}\uFF09
2592
- `);
2593
- }
2743
+ const listKv = flagBool(args, "list-kv");
2744
+ const resolution = await resolveDeployAccount(client, {
2745
+ accountFlag: flagString(args, "account") || "",
2746
+ kvId,
2747
+ ignoreKv: listKv
2748
+ });
2749
+ if (!resolution.ok) {
2750
+ process.stderr.write(resolution.message);
2751
+ process.exitCode = 1;
2752
+ return;
2594
2753
  }
2595
- if (flagBool(args, "list-kv")) {
2754
+ const resolvedAccount = resolution.accountId;
2755
+ process.stdout.write(resolution.message);
2756
+ if (listKv) {
2596
2757
  await listKvInstances(client, resolvedAccount);
2597
2758
  return;
2598
2759
  }
@@ -2717,6 +2878,7 @@ var init_command = __esm({
2717
2878
  "use strict";
2718
2879
  init_args();
2719
2880
  init_cf_api();
2881
+ init_account_resolve();
2720
2882
  init_pages_deploy();
2721
2883
  init_kernel_remote();
2722
2884
  init_utils();
@@ -2736,16 +2898,59 @@ __export(deploy_exports, {
2736
2898
  function makeArgs(flags) {
2737
2899
  return { command: "", flags, positionals: [] };
2738
2900
  }
2901
+ async function resolveAccountId(token, cfg) {
2902
+ if (cfg.accountId) return cfg.accountId;
2903
+ const client = new CloudflareClient({ token });
2904
+ let accounts;
2905
+ try {
2906
+ accounts = await client.listAccounts();
2907
+ } catch (err) {
2908
+ status("fail", `\u8BFB\u53D6\u8D26\u53F7\u5217\u8868\u5931\u8D25\uFF1A${err instanceof Error ? err.message : String(err)}`);
2909
+ return "";
2910
+ }
2911
+ if (accounts.length === 0) {
2912
+ status("fail", "\u8BE5 Token \u6CA1\u6709\u53EF\u8BBF\u95EE\u7684\u8D26\u53F7");
2913
+ return "";
2914
+ }
2915
+ if (cfg.kvNamespaceId) {
2916
+ const { owner } = await findAccountByKv(client, accounts, cfg.kvNamespaceId);
2917
+ if (owner) {
2918
+ saveConfig({ accountId: owner.id, accountName: owner.name });
2919
+ return owner.id;
2920
+ }
2921
+ status("warn", `\u914D\u7F6E\u7684 KV ${cfg.kvNamespaceId} \u4E0D\u5728\u53EF\u89C1\u7684\u8D26\u53F7\u4E0B\uFF0C\u5C06\u91CD\u65B0\u9009\u62E9`);
2922
+ }
2923
+ if (accounts.length === 1) {
2924
+ const only = accounts[0];
2925
+ saveConfig({ accountId: only.id, accountName: only.name });
2926
+ return only.id;
2927
+ }
2928
+ const picked = await select(
2929
+ "\u68C0\u6D4B\u5230\u591A\u4E2A\u8D26\u53F7\uFF0C\u8BF7\u9009\u62E9\u672C\u6B21\u90E8\u7F72\u4F7F\u7528\u54EA\u4E2A",
2930
+ accounts.map((a) => ({ label: a.name, value: a.id, hint: a.id.slice(0, 12) + "\u2026" }))
2931
+ );
2932
+ if (!picked) {
2933
+ status("info", "\u5DF2\u53D6\u6D88");
2934
+ return "";
2935
+ }
2936
+ const chosen = accounts.find((a) => a.id === picked);
2937
+ if (!chosen) return "";
2938
+ saveConfig({ accountId: chosen.id, accountName: chosen.name });
2939
+ return chosen.id;
2940
+ }
2739
2941
  async function menuDeploy(mode) {
2740
- const cfg = loadConfig();
2942
+ let cfg = loadConfig();
2741
2943
  const token = resolveToken();
2742
2944
  if (!token) {
2743
2945
  status("warn", "\u5C1A\u672A\u914D\u7F6E API Token");
2744
2946
  status("info", "\u8BF7\u5148\u5230\u300C\u914D\u7F6E\u300D\u83DC\u5355\u8BBE\u7F6E\uFF0C\u6216\u8FD0\u884C cfpilot config --token xxx");
2745
2947
  return;
2746
2948
  }
2949
+ const accountId = await resolveAccountId(token, cfg);
2950
+ if (!accountId) return;
2951
+ cfg = loadConfig();
2747
2952
  box(mode === "fresh" ? "\u90E8\u7F72\u65B0\u5B9E\u4F8B" : "\u66F4\u65B0\u73B0\u6709\u5B9E\u4F8B", [
2748
- `${c.gray("\u8D26\u53F7")} ${cfg.accountName || c.yellow("\u5F85\u68C0\u6D4B")}`,
2953
+ `${c.gray("\u8D26\u53F7")} ${cfg.accountName || accountId}`,
2749
2954
  `${c.gray("KV")} ${cfg.kvNamespaceId || c.gray("\uFF08\u81EA\u52A8\uFF09")}`,
2750
2955
  `${c.gray("\u6A21\u5F0F")} ${mode === "fresh" ? "\u5168\u65B0\u90E8\u7F72" : "\u66F4\u65B0\u73B0\u6709"}`
2751
2956
  ]);
@@ -2767,12 +2972,13 @@ async function menuDeploy(mode) {
2767
2972
  const flags = {
2768
2973
  token,
2769
2974
  yes: true,
2770
- mode: mode === "fresh" ? "fresh" : "update"
2975
+ mode: mode === "fresh" ? "fresh" : "update",
2976
+ // ⚠️ 账号与 KV 来自同一份配置 → 不会错配。
2977
+ account: accountId
2771
2978
  };
2772
2979
  if (mode === "fresh") flags["new-kv"] = true;
2773
2980
  if (panelPassword) flags["panel-password"] = panelPassword;
2774
2981
  if (cfg.kvNamespaceId && mode === "update") flags["kv"] = cfg.kvNamespaceId;
2775
- if (cfg.accountId) flags["account"] = cfg.accountId;
2776
2982
  const { runDeployCommand: runDeployCommand2 } = await Promise.resolve().then(() => (init_command(), command_exports));
2777
2983
  await runDeployCommand2(makeArgs(flags));
2778
2984
  }
@@ -2790,7 +2996,13 @@ async function menuChangePassword() {
2790
2996
  status("info", "\u6B63\u5728\u8BFB\u53D6\u5B9E\u4F8B\u4FE1\u606F\u2026");
2791
2997
  try {
2792
2998
  const client = new CloudflareClient({ token });
2793
- const raw = await client.getKvValue(cfg.accountId, cfg.kvNamespaceId, "CFP_DEPLOY_MANIFEST");
2999
+ let raw;
3000
+ try {
3001
+ raw = await client.getKvValue(cfg.accountId, cfg.kvNamespaceId, "CFP_DEPLOY_MANIFEST");
3002
+ } catch (err) {
3003
+ status("fail", `\u8BFB\u53D6\u90E8\u7F72\u8BB0\u5F55\u5931\u8D25\uFF1A${err instanceof Error ? err.message : String(err)}`);
3004
+ return;
3005
+ }
2794
3006
  if (!raw) {
2795
3007
  status("fail", "\u8BE5 KV \u91CC\u6CA1\u6709\u90E8\u7F72\u8BB0\u5F55");
2796
3008
  return;
@@ -2875,6 +3087,7 @@ var init_deploy = __esm({
2875
3087
  init_prompt();
2876
3088
  init_store();
2877
3089
  init_cf_api();
3090
+ init_account_resolve();
2878
3091
  init_token_check();
2879
3092
  }
2880
3093
  });
@@ -2901,9 +3114,25 @@ async function menuStatus() {
2901
3114
  status("fail", "\u8BE5 Token \u6CA1\u6709\u53EF\u8BBF\u95EE\u7684\u8D26\u53F7");
2902
3115
  return;
2903
3116
  }
2904
- const accountId = cfg.accountId || accounts[0].id;
2905
- const accountName = accounts.find((a) => a.id === accountId)?.name ?? accounts[0].name;
2906
- if (!cfg.accountId) saveConfig({ accountId, accountName });
3117
+ let account = accounts.find((a) => a.id === cfg.accountId);
3118
+ if (!account) {
3119
+ if (accounts.length === 1) {
3120
+ account = accounts[0];
3121
+ } else {
3122
+ const picked = await select(
3123
+ "\u68C0\u6D4B\u5230\u591A\u4E2A\u8D26\u53F7\uFF0C\u8BF7\u9009\u62E9\u8981\u67E5\u770B\u54EA\u4E2A",
3124
+ accounts.map((a) => ({ label: a.name, value: a.id, hint: a.id.slice(0, 12) + "\u2026" }))
3125
+ );
3126
+ if (!picked) {
3127
+ status("info", "\u5DF2\u53D6\u6D88");
3128
+ return;
3129
+ }
3130
+ account = accounts.find((a) => a.id === picked) ?? accounts[0];
3131
+ }
3132
+ }
3133
+ const accountId = account.id;
3134
+ const accountName = account.name;
3135
+ if (cfg.accountId !== accountId) saveConfig({ accountId, accountName });
2907
3136
  process.stdout.write("\n");
2908
3137
  box("\u5F53\u524D\u5B9E\u4F8B", [`${c.gray("\u8D26\u53F7")} ${accountName}`, `${c.gray("ID")} ${accountId}`]);
2909
3138
  process.stdout.write("\n");
@@ -2919,6 +3148,7 @@ var init_status = __esm({
2919
3148
  "src/menu/status.ts"() {
2920
3149
  "use strict";
2921
3150
  init_ui();
3151
+ init_prompt();
2922
3152
  init_store();
2923
3153
  init_cf_api();
2924
3154
  init_command();
@@ -5104,7 +5334,7 @@ function normalizeWsPath(raw, fallback) {
5104
5334
  \u8FD9\u662F Git-Bash \u7684\u81EA\u52A8\u8DEF\u5F84\u8F6C\u6362\u884C\u4E3A\uFF0C\u4E0D\u662F\u672C\u5DE5\u5177\u7684\u95EE\u9898\u3002
5105
5335
  \u4E09\u79CD\u7ED5\u5F00\u65B9\u5F0F\uFF0C\u4EFB\u9009\u5176\u4E00\uFF1A
5106
5336
  1. \u7528\u7B49\u53F7\u5199\u6CD5\uFF1A --path=/vless
5107
- 2. \u52A0\u73AF\u5883\u53D8\u91CF\uFF1A MSYS_NO_PATHCONV=1 cfpilot-scanner scan ...
5337
+ 2. \u52A0\u73AF\u5883\u53D8\u91CF\uFF1A MSYS_NO_PATHCONV=1 cfpilot scan ...
5108
5338
  3. \u6362\u7528 PowerShell / CMD \u6267\u884C
5109
5339
  `
5110
5340
  );
@@ -5500,7 +5730,15 @@ async function fetchInstances(token) {
5500
5730
  const client = new CloudflareClient({ token });
5501
5731
  const cfg = loadConfig();
5502
5732
  const accounts = await client.listAccounts();
5503
- const accountId = cfg.accountId || accounts[0]?.id;
5733
+ if (accounts.length === 0) return [];
5734
+ let accountId = cfg.accountId || "";
5735
+ if (!accountId && cfg.kvNamespaceId) {
5736
+ const { owner } = await findAccountByKv(client, accounts, cfg.kvNamespaceId);
5737
+ if (owner) accountId = owner.id;
5738
+ }
5739
+ if (!accountId && accounts.length === 1) {
5740
+ accountId = accounts[0].id;
5741
+ }
5504
5742
  if (!accountId) return [];
5505
5743
  const list = await listCfPilotNamespaces(client, accountId);
5506
5744
  return list.map((i) => ({
@@ -5647,6 +5885,7 @@ var init_scan2 = __esm({
5647
5885
  init_store();
5648
5886
  init_countries();
5649
5887
  init_cf_api();
5888
+ init_account_resolve();
5650
5889
  init_pages_deploy();
5651
5890
  }
5652
5891
  });
@@ -5659,13 +5898,25 @@ __export(config_exports, {
5659
5898
  syncAccount: () => syncAccount
5660
5899
  });
5661
5900
  async function syncFromCloud(token) {
5901
+ if ((loadConfig().token ?? "").trim() !== token.trim()) {
5902
+ saveConfig({
5903
+ accountId: "",
5904
+ accountName: "",
5905
+ kvNamespaceId: "",
5906
+ panelHost: "",
5907
+ dataHost: "",
5908
+ wsPath: "",
5909
+ panelPassword: ""
5910
+ });
5911
+ }
5662
5912
  saveConfig({
5663
5913
  accountId: "",
5664
5914
  accountName: "",
5665
5915
  kvNamespaceId: "",
5666
5916
  panelHost: "",
5667
5917
  dataHost: "",
5668
- wsPath: ""
5918
+ wsPath: "",
5919
+ panelPassword: ""
5669
5920
  });
5670
5921
  try {
5671
5922
  const client = new CloudflareClient({ token });
@@ -5682,15 +5933,17 @@ async function syncFromCloud(token) {
5682
5933
  );
5683
5934
  account = accounts.find((a) => a.id === picked) ?? accounts[0];
5684
5935
  }
5685
- saveConfig({ accountId: account.id, accountName: account.name });
5686
5936
  status("ok", `\u8D26\u53F7\uFF1A${account.name}`);
5687
5937
  const namespaces = await client.listKvNamespaces(account.id);
5688
5938
  if (namespaces.length === 0) {
5689
5939
  saveConfig({
5940
+ accountId: account.id,
5941
+ accountName: account.name,
5690
5942
  kvNamespaceId: "",
5691
5943
  panelHost: "",
5692
5944
  dataHost: "",
5693
- wsPath: ""
5945
+ wsPath: "",
5946
+ panelPassword: ""
5694
5947
  });
5695
5948
  status("info", "\u8BE5\u8D26\u53F7\u4E0B\u8FD8\u6CA1\u6709 KV\uFF08\u9996\u6B21\u90E8\u7F72\u65F6\u4F1A\u81EA\u52A8\u521B\u5EFA\uFF09");
5696
5949
  return true;
@@ -5710,6 +5963,8 @@ async function syncFromCloud(token) {
5710
5963
  chosen = instances.find((i) => i.id === picked) ?? instances[0];
5711
5964
  }
5712
5965
  saveConfig({
5966
+ accountId: account.id,
5967
+ accountName: account.name,
5713
5968
  kvNamespaceId: chosen.id,
5714
5969
  panelHost: chosen.manifest.controlSubdomain,
5715
5970
  dataHost: chosen.manifest.dataSubdomain,
@@ -5722,10 +5977,13 @@ async function syncFromCloud(token) {
5722
5977
  }
5723
5978
  if (namespaces.length === 1) {
5724
5979
  saveConfig({
5980
+ accountId: account.id,
5981
+ accountName: account.name,
5725
5982
  kvNamespaceId: namespaces[0].id,
5726
5983
  panelHost: "",
5727
5984
  dataHost: "",
5728
- wsPath: ""
5985
+ wsPath: "",
5986
+ panelPassword: ""
5729
5987
  });
5730
5988
  status("info", `KV\uFF1A${namespaces[0].title}\uFF08\u5C1A\u65E0\u90E8\u7F72\uFF09`);
5731
5989
  } else {
@@ -5740,13 +5998,16 @@ async function syncFromCloud(token) {
5740
5998
  { label: "\u4E0D\u6307\u5B9A\uFF08\u90E8\u7F72\u65F6\u81EA\u52A8\u521B\u5EFA\uFF09", value: "" }
5741
5999
  ]
5742
6000
  );
6001
+ saveConfig({
6002
+ accountId: account.id,
6003
+ accountName: account.name,
6004
+ kvNamespaceId: picked ?? "",
6005
+ panelHost: "",
6006
+ dataHost: "",
6007
+ wsPath: "",
6008
+ panelPassword: ""
6009
+ });
5743
6010
  if (picked) {
5744
- saveConfig({
5745
- kvNamespaceId: picked,
5746
- panelHost: "",
5747
- dataHost: "",
5748
- wsPath: ""
5749
- });
5750
6011
  status("ok", "KV \u5DF2\u4FDD\u5B58");
5751
6012
  }
5752
6013
  }
@@ -5803,7 +6064,7 @@ async function menuConfig() {
5803
6064
  status("warn", "\u672A\u8F93\u5165\uFF0C\u5DF2\u53D6\u6D88");
5804
6065
  break;
5805
6066
  }
5806
- saveConfig({ token });
6067
+ saveToken(token);
5807
6068
  status("ok", "Token \u5DF2\u4FDD\u5B58");
5808
6069
  status("info", "\u6B63\u5728\u8BC6\u522B\u8D26\u53F7\u4E0E\u5B9E\u4F8B\u2026");
5809
6070
  process.stdout.write("\n");
@@ -5867,12 +6128,31 @@ async function runConfig(args) {
5867
6128
  return;
5868
6129
  }
5869
6130
  const patch = {};
6131
+ const explicit = {};
5870
6132
  if (typeof flags["token"] === "string") patch.token = flags["token"];
5871
- if (typeof flags["kv"] === "string") patch.kvNamespaceId = flags["kv"];
5872
- if (typeof flags["panel"] === "string") patch.panelHost = flags["panel"];
5873
- if (typeof flags["account"] === "string") patch.accountId = flags["account"];
5874
- saveConfig(patch);
6133
+ if (typeof flags["kv"] === "string") {
6134
+ patch.kvNamespaceId = flags["kv"];
6135
+ explicit.kvNamespaceId = flags["kv"];
6136
+ }
6137
+ if (typeof flags["panel"] === "string") {
6138
+ patch.panelHost = flags["panel"];
6139
+ explicit.panelHost = flags["panel"];
6140
+ }
6141
+ if (typeof flags["account"] === "string") {
6142
+ patch.accountId = flags["account"];
6143
+ explicit.accountId = flags["account"];
6144
+ }
6145
+ if (typeof patch.token === "string") {
6146
+ saveToken(patch.token, explicit);
6147
+ } else {
6148
+ saveConfig(patch);
6149
+ }
5875
6150
  process.stdout.write("\u5DF2\u4FDD\u5B58\uFF1A" + Object.keys(patch).join(", ") + "\n");
6151
+ if (typeof patch.token === "string") {
6152
+ process.stdout.write("\n");
6153
+ status("info", "\u6B63\u5728\u8BC6\u522B\u8D26\u53F7\u4E0E\u5B9E\u4F8B\u2026");
6154
+ await syncFromCloud(patch.token);
6155
+ }
5876
6156
  }
5877
6157
  async function syncAccount(token) {
5878
6158
  const t = token || resolveToken();