@aronnaxlin/bgm-cli 1.1.3 → 1.1.4

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aronnaxlin/bgm-cli",
3
- "version": "1.1.3",
3
+ "version": "1.1.4",
4
4
  "description": "Command-line client for Bangumi.",
5
5
  "license": "AGPL-3.0-only",
6
6
  "author": "aronnaxlin",
@@ -3,6 +3,7 @@ import path from "node:path";
3
3
  import { fileURLToPath } from "node:url";
4
4
  import { existsSync, readFileSync } from "node:fs";
5
5
  import { mkdir, readFile, writeFile } from "node:fs/promises";
6
+ import { APP_VERSION } from "../utils/version.js";
6
7
 
7
8
  export class ConfigError extends Error {
8
9
  constructor(message) {
@@ -23,7 +24,7 @@ const DEV_ENV_FILE = path.join(REPO_ROOT, "bgm-dev.env");
23
24
 
24
25
  const DEFAULT_CONFIG = {
25
26
  appName: "bgm-cli",
26
- appVersion: "1.0.2",
27
+ appVersion: APP_VERSION,
27
28
  homepageLink: "https://github.com/aronnaxlin/bgm-cli",
28
29
  developerId: "aronnaxlin",
29
30
  oauthServerBaseUrl: "https://oauth-backend-jet.vercel.app",
@@ -494,7 +495,7 @@ function normalizeUserAgent(config) {
494
495
  }
495
496
 
496
497
  const appName = config.appName ?? "bgm-cli";
497
- const version = config.appVersion ?? "1.0.0";
498
+ const version = config.appVersion ?? APP_VERSION;
498
499
  const genericValues = new Set([
499
500
  `${appName}/${version}`,
500
501
  `${appName}/0.1.0`,
@@ -504,17 +505,33 @@ function normalizeUserAgent(config) {
504
505
  "yourname/bgm-cli/0.1.0",
505
506
  ]);
506
507
 
507
- if ((genericValues.has(current) || current === legacyRecommended) && recommended) {
508
+ if ((genericValues.has(current) || current === legacyRecommended || isManagedUserAgent(current, config)) && recommended) {
508
509
  return recommended;
509
510
  }
510
511
 
511
512
  return current;
512
513
  }
513
514
 
515
+ // A User-Agent in the recommended format but carrying an older release's
516
+ // version is ours to refresh; anything else was customised by the user.
517
+ function isManagedUserAgent(current, config) {
518
+ const developerId = config.developerId ?? extractGithubUsername(config.homepageLink);
519
+ const appName = config.appName ?? "bgm-cli";
520
+ const prefix = developerId ? `${developerId}/${appName}/` : `${appName}/`;
521
+ const suffix = config.homepageLink ? ` (${config.homepageLink})` : "";
522
+
523
+ if (!current.startsWith(prefix) || !current.endsWith(suffix)) {
524
+ return false;
525
+ }
526
+
527
+ const version = current.slice(prefix.length, current.length - suffix.length);
528
+ return /^\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?$/.test(version);
529
+ }
530
+
514
531
  function buildRecommendedUserAgent(config) {
515
532
  const developerId = config.developerId ?? extractGithubUsername(config.homepageLink);
516
533
  const appName = config.appName ?? "bgm-cli";
517
- const version = config.appVersion ?? "1.0.0";
534
+ const version = config.appVersion ?? APP_VERSION;
518
535
  const homepageLink = config.homepageLink;
519
536
 
520
537
  let userAgent = developerId ? `${developerId}/${appName}/${version}` : `${appName}/${version}`;
package/src/utils/auth.js CHANGED
@@ -3,6 +3,7 @@
3
3
  */
4
4
 
5
5
  import { CommandError } from "../core/output.js";
6
+ import { APP_VERSION } from "./version.js";
6
7
 
7
8
  export function createState() {
8
9
  return `bgm-cli-${Date.now().toString(36)}`;
@@ -11,7 +12,7 @@ export function createState() {
11
12
  export function fallbackUserAgent(config) {
12
13
  const developerId = deriveDeveloperId(config);
13
14
  const appName = config.appName ?? "bgm-cli";
14
- const version = config.appVersion ?? "1.0.0";
15
+ const version = config.appVersion ?? APP_VERSION;
15
16
  const homepageLink = config.homepageLink;
16
17
 
17
18
  let userAgent = developerId
@@ -14,6 +14,7 @@ import {
14
14
  import { CommandError } from "../core/output.js";
15
15
  import { DEFAULT_TURNSTILE_TIMEOUT_MS } from "../core/turnstile.js";
16
16
  import { fallbackUserAgent } from "./auth.js";
17
+ import { APP_VERSION } from "./version.js";
17
18
 
18
19
  export function delayMs(ms) {
19
20
  return new Promise((resolve) => {
@@ -265,7 +266,7 @@ export function buildVersionStatusPayload(repoRoot) {
265
266
  return {
266
267
  resource: "version-status",
267
268
  name: config.appName ?? "bgm-cli",
268
- version: config.appVersion ?? "1.0.0",
269
+ version: config.appVersion ?? APP_VERSION,
269
270
  configScope: inferConfigScope(configFile, repoRoot),
270
271
  configFile,
271
272
  configSourceFile,
@@ -0,0 +1,24 @@
1
+ /**
2
+ * Single source of truth for the CLI version at runtime.
3
+ *
4
+ * The released version lives in git tags (vX.Y.Z). The release workflow stamps
5
+ * it into package.json right before `npm publish`, so a published package
6
+ * always reports its real version. A source checkout reports the placeholder
7
+ * committed to package.json, like a dev build.
8
+ */
9
+
10
+ import { readFileSync } from "node:fs";
11
+
12
+ export const DEV_VERSION = "0.0.0-dev";
13
+
14
+ function readPackageVersion() {
15
+ try {
16
+ const raw = readFileSync(new URL("../../package.json", import.meta.url), "utf8");
17
+ const version = JSON.parse(raw).version;
18
+ return typeof version === "string" && version ? version : DEV_VERSION;
19
+ } catch {
20
+ return DEV_VERSION;
21
+ }
22
+ }
23
+
24
+ export const APP_VERSION = readPackageVersion();