@wkgeo/cli 2.0.3 → 2.2.0

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/lib/config.mjs CHANGED
@@ -17,6 +17,7 @@ import { readFileSync } from 'node:fs';
17
17
 
18
18
  import { UsageError } from './errors.mjs';
19
19
  import { PLATFORM_WEB_ORIGIN } from './hosts.mjs';
20
+ import { findSiteCredentialForCwd } from './site-credentials.mjs';
20
21
 
21
22
  /**
22
23
  * 线上 API 基址(业务接口 `/api/data`、OAuth 服务端 `/api/oauth/*` 都在这个域下)。
@@ -126,6 +127,25 @@ export function defaultCredentialsFile(env = process.env) {
126
127
  return path.join(home, '.wkongai', 'credentials.json');
127
128
  }
128
129
 
130
+ /**
131
+ * **CMS 管理面专用凭据**:`os.homedir()/.wkongai/credentials.json`(账号登录令牌)。
132
+ *
133
+ * 为什么单独算一条路径(2026-11-22,Key 模式拉模板修):
134
+ * * `/cms` 的 Bearer 身份桥**只认账号 OAuth 令牌**(`public.oauth_token` 里那枚);
135
+ * API Key(`wkgeo_sk_…`)和按站短票都不在 `oauth_token` 表里 ⇒ 一律 401
136
+ * 「OAuth 令牌无效或已过期」。
137
+ * * 面板 / CLI 用 Key 模式时 `credentialsFile` 指向的是 Key 文件(`WKONGAI_CREDENTIALS`),
138
+ * 直接拿它调 CMS 必然 401;但同一台机器上**通常已经 `wkgeo login` 过**,
139
+ * 账号令牌就在主目录。这条路径让 CMS 动作能自动回落到账号身份。
140
+ * * 显式覆盖:`WKONGAI_CMS_CREDENTIALS`(与 `WKONGAI_HOME` 隔离,便于测试 / 多身份)。
141
+ *
142
+ * ⚠️ 这里只解析**路径**,是否存在 / 是否过期由 runtime 读的时候判断(没有就回落原凭据)。
143
+ */
144
+ export function defaultCmsCredentialsFile(env = process.env) {
145
+ if (env.WKONGAI_CMS_CREDENTIALS) return path.resolve(env.WKONGAI_CMS_CREDENTIALS);
146
+ return path.join(os.homedir(), '.wkongai', 'credentials.json');
147
+ }
148
+
129
149
  /**
130
150
  * 本地站点状态文件:**凭据文件同目录**的 `config.json`(默认 `~/.wkongai/config.json`)。
131
151
  *
@@ -284,10 +304,20 @@ export function resolveConfig(flags = {}, env = process.env) {
284
304
  /**
285
305
  * 凭据文件路径先算出来:站点状态文件放在**它同目录**(见 defaultSiteStateFile 的说明),
286
306
  * 这样 `--credentials` 换一套身份时,选中的站点也跟着换,不会串站。
307
+ *
308
+ * ★ 2026-11-22(v14 按站票据):**没有显式指定凭据**时,先看当前目录是不是某个站点目录,
309
+ * 命中该站的短票就用票(`%LOCALAPPDATA%\wkgeo-ops\creds\<siteId>.json`)。
310
+ * `--credentials` / `WKONGAI_CREDENTIALS` 是显式身份 ⇒ 永远优先,票不覆盖。
311
+ * 票过期 / 找不到 ⇒ 原样回账号凭据(普通目录行为零变化)。
287
312
  */
288
- const credentialsFile = flags.credentials
289
- ? path.resolve(String(flags.credentials))
290
- : defaultCredentialsFile(env);
313
+ const explicitCredentialsFile = flags.credentials ? path.resolve(String(flags.credentials)) : '';
314
+ let credentialsFile = explicitCredentialsFile || defaultCredentialsFile(env);
315
+ let siteCredential = null;
316
+ if (!explicitCredentialsFile && !String(env.WKONGAI_CREDENTIALS || '').trim()) {
317
+ const cwd = String(env.WKONGAI_CWD || '').trim() || process.cwd();
318
+ siteCredential = findSiteCredentialForCwd({ cwd, env });
319
+ if (siteCredential && siteCredential.file) credentialsFile = siteCredential.file;
320
+ }
291
321
 
292
322
  return {
293
323
  base: stripTrailingSlash(pick(flags.base, 'WKONGAI_BASE', DEFAULT_BASE)),
@@ -304,6 +334,20 @@ export function resolveConfig(flags = {}, env = process.env) {
304
334
  clientId: String(pick(flags.clientId, 'WKONGAI_CLIENT_ID', DEFAULT_CLIENT_ID)),
305
335
  scope: splitScope(scopeRaw).join(' '),
306
336
  credentialsFile,
337
+ /**
338
+ * CMS 管理面专用凭据路径(账号登录令牌)。
339
+ * 仅当主凭据是 Key / 按站短票时才会被 runtime 用到 —— `/cms` 不认那两类身份。
340
+ */
341
+ cmsCredentialsFile: defaultCmsCredentialsFile(env),
342
+ /** 命中的按站票据(没命中 = null);`file` 就是上面 credentialsFile 指向的票文件 */
343
+ siteCredential: siteCredential
344
+ ? {
345
+ file: siteCredential.file,
346
+ siteId: siteCredential.siteId,
347
+ siteDir: siteCredential.siteDir,
348
+ keyPrefix: siteCredential.keyPrefix,
349
+ }
350
+ : null,
307
351
  /** 本地"当前站点"记录(`wkgeo site use` 写它;**不是**凭据) */
308
352
  siteStateFile: defaultSiteStateFile(env, credentialsFile),
309
353
  callbackHost: String(pick(flags.callbackHost, 'WKONGAI_CALLBACK_HOST', DEFAULT_CALLBACK_HOST)),
@@ -350,6 +394,15 @@ export function describeConfigSources(flags = {}, env = process.env) {
350
394
  ? '命令行'
351
395
  : env.WKONGAI_CREDENTIALS
352
396
  ? '环境变量 WKONGAI_CREDENTIALS'
353
- : '内置默认 ~/.wkongai/credentials.json',
397
+ : (() => {
398
+ /* v14:没有显式凭据时可能命中当前目录的按站票据(只报"命中了",不报 token) */
399
+ try {
400
+ const hit = findSiteCredentialForCwd({ cwd: String(env.WKONGAI_CWD || '').trim() || process.cwd(), env });
401
+ if (hit) return '按站票据(当前目录命中的站点短票)';
402
+ } catch {
403
+ /* 查票失败不影响展示 */
404
+ }
405
+ return '内置默认 ~/.wkongai/credentials.json';
406
+ })(),
354
407
  };
355
408
  }