diff --git a/.github/workflows/npm-publish.yml b/.github/workflows/npm-publish.yml index ab86a4cf7..fa9429157 100644 --- a/.github/workflows/npm-publish.yml +++ b/.github/workflows/npm-publish.yml @@ -142,9 +142,9 @@ jobs: if: steps.version.outputs.branch == 'dev' run: | echo "Replacing production API URL with dev API URL in dist/index.js" - sed -i 's|https://api.osc.earth/nemar|https://nemar-api-dev.shirazi-10f.workers.dev|g' dist/index.js + sed -i 's|https://api.nemar.org|https://nemar-api-dev.sccn-org.workers.dev|g' dist/index.js echo "Verifying replacement:" - grep -c "nemar-api-dev.shirazi-10f.workers.dev" dist/index.js || (echo "ERROR: URL replacement failed" && exit 1) + grep -c "nemar-api-dev.sccn-org.workers.dev" dist/index.js || (echo "ERROR: URL replacement failed" && exit 1) echo "✓ Dev backend URL injected successfully" - name: Publish to npm diff --git a/package.json b/package.json index 027b8ea02..033ff04f0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "nemar-cli", - "version": "0.7.26", + "version": "0.8.0-dev", "description": "CLI for NEMAR (Neuroelectromagnetic Data Archive and Tools Resource) dataset management", "type": "module", "main": "dist/index.js", diff --git a/scripts/generate-docs.ts b/scripts/generate-docs.ts index e97897bc6..f88f9d5a9 100644 --- a/scripts/generate-docs.ts +++ b/scripts/generate-docs.ts @@ -1054,7 +1054,7 @@ NEMAR CLI communicates with the NEMAR API. This reference is for advanced users. ## Base URL \`\`\` -https://api.osc.earth/nemar +https://api.nemar.org \`\`\` ## Authentication diff --git a/src/commands/auth.ts b/src/commands/auth.ts index 16a725a48..1e9080382 100644 --- a/src/commands/auth.ts +++ b/src/commands/auth.ts @@ -31,6 +31,7 @@ import { signup, } from "../lib/api.js"; import { + DEFAULT_API_URL, clearAllConfig, clearConfig, getAccounts, @@ -143,7 +144,7 @@ export async function loginAction(options: { key?: string } & ConfirmOptions): P // Store credentials as a named account and set as active storeAccount(result.user.username, { apiKey, - apiUrl: "https://api.osc.earth/nemar", + apiUrl: DEFAULT_API_URL, username: result.user.username, email: result.user.email, githubUsername: result.user.github_username, diff --git a/src/lib/api.ts b/src/lib/api.ts index 9542aaa7e..832e09a68 100644 --- a/src/lib/api.ts +++ b/src/lib/api.ts @@ -9,7 +9,7 @@ import { getConfig } from "./config.js"; import { printMaintenanceBanner } from "./maintenance-banner.js"; import { version } from "./version.js"; -const DEFAULT_API_URL = "https://api.osc.earth/nemar"; +const DEFAULT_API_URL = "https://api.nemar.org"; /** ORCID identifier format: XXXX-XXXX-XXXX-XXXX (last char may be X) */ export const ORCID_REGEX = /^\d{4}-\d{4}-\d{4}-\d{3}[\dX]$/; diff --git a/src/lib/config.ts b/src/lib/config.ts index ca559ef82..8f42e837e 100644 --- a/src/lib/config.ts +++ b/src/lib/config.ts @@ -22,7 +22,15 @@ import { join } from "node:path"; import Conf from "conf"; import { z } from "zod"; -const DEFAULT_API_URL = "https://api.osc.earth/nemar"; +export const DEFAULT_API_URL = "https://api.nemar.org"; + +/** + * Old default URL pre-Phase-9. Rewritten to DEFAULT_API_URL on CLI launch by + * migrateApiUrl(). The legacy redirect on personal Cloudflare keeps stale + * configs working too, but we proactively rewrite so users don't ride the + * redirect forever. + */ +const LEGACY_DEFAULT_API_URL = "https://api.osc.earth/nemar"; /** * Standardized config directory: ~/.config/nemar/ on all platforms. @@ -180,6 +188,35 @@ export function migrateConfig(): void { // Run migration on module load migrateConfig(); +/** + * Phase 9 (epic #314): rewrite stored apiUrl entries that still point at the + * pre-cutover personal-account URL. Legacy redirect keeps old configs working, + * but we rewrite proactively so users converge on the canonical host. + * + * Only rewrites *exact* matches of the legacy default. Custom URLs (dev + * builds, self-hosted, workers.dev fallbacks) are left alone. + * + * Idempotent and safe under concurrent CLI launches: the exact-match guard + * means a second run is a no-op, and Conf writes via atomic file replace, so + * a racing process sees pre- or post-state, never partial. + */ +export function migrateApiUrl(): void { + const accounts = config.get("accounts") as Record | undefined; + if (!accounts) return; + let changed = false; + for (const [name, account] of Object.entries(accounts)) { + if (account?.apiUrl === LEGACY_DEFAULT_API_URL) { + accounts[name] = { ...account, apiUrl: DEFAULT_API_URL }; + changed = true; + } + } + if (changed) { + config.store = { ...config.store, accounts }; + } +} + +migrateApiUrl(); + /** * Get the active account name */ diff --git a/test/config-migrate-api-url.test.ts b/test/config-migrate-api-url.test.ts new file mode 100644 index 000000000..30eff5591 --- /dev/null +++ b/test/config-migrate-api-url.test.ts @@ -0,0 +1,64 @@ +import { afterAll, beforeAll, describe, expect, test } from "bun:test"; +import { existsSync, mkdirSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; + +const LEGACY_URL = "https://api.osc.earth/nemar"; +const NEW_URL = "https://api.nemar.org"; +const DEV_URL = "https://nemar-api-dev.sccn-org.workers.dev"; +const CUSTOM_URL = "https://api.example.com/nemar"; + +// Module-level state in src/lib/config.ts captures NEMAR_CONFIG_DIR at first +// import. Bun caches modules across tests, so we set up the config once, +// import once, and assert all migration scenarios from one snapshot. + +const testDir = join(tmpdir(), `nemar-cfg-migrate-${Date.now()}-${process.pid}`); + +beforeAll(() => { + mkdirSync(testDir, { recursive: true }); + process.env.NEMAR_CONFIG_DIR = testDir; + writeFileSync( + join(testDir, "config.json"), + JSON.stringify({ + activeAccount: "alice", + accounts: { + alice: { apiUrl: LEGACY_URL, apiKey: "k1" }, + bob: { apiUrl: LEGACY_URL, apiKey: "k2" }, + carol: { apiUrl: NEW_URL, apiKey: "k3" }, + dev: { apiUrl: DEV_URL, apiKey: "k4" }, + custom: { apiUrl: CUSTOM_URL, apiKey: "k5" }, + // Legacy-flat migration produces accounts with no apiUrl when none + // was stored. Optional chaining in migrateApiUrl must not crash here + // and must leave the field absent. + partial: { apiKey: "k6" }, + }, + }), + ); +}); + +afterAll(() => { + delete process.env.NEMAR_CONFIG_DIR; + if (existsSync(testDir)) rmSync(testDir, { recursive: true, force: true }); +}); + +describe("migrateApiUrl", () => { + test("importing config.ts triggers migration of all stored apiUrl entries", async () => { + // Module load runs migrateApiUrl() at top level + await import("../src/lib/config.ts"); + + const after = JSON.parse(await Bun.file(join(testDir, "config.json")).text()) as { + accounts: Record; + }; + + // Both legacy URLs rewritten + expect(after.accounts.alice.apiUrl).toBe(NEW_URL); + expect(after.accounts.bob.apiUrl).toBe(NEW_URL); + // Already-correct URL stays + expect(after.accounts.carol.apiUrl).toBe(NEW_URL); + // Non-default URLs untouched + expect(after.accounts.dev.apiUrl).toBe(DEV_URL); + expect(after.accounts.custom.apiUrl).toBe(CUSTOM_URL); + // Account with no apiUrl: must not crash, must not invent one + expect(after.accounts.partial.apiUrl).toBeUndefined(); + }); +}); diff --git a/test/setup.ts b/test/setup.ts index 9696143f9..23e5ad2a3 100644 --- a/test/setup.ts +++ b/test/setup.ts @@ -25,7 +25,7 @@ if (existsSync(envPath)) { // Test configuration export const TEST_CONFIG = { - apiUrl: process.env.TEST_API_URL || "https://api.osc.earth/nemar", + apiUrl: process.env.TEST_API_URL || "https://api.nemar.org", password: process.env.TEST_PASSWORD || "TestPassword123!", adminApiKey: process.env.TEST_ADMIN_API_KEY || "", userApiKey: process.env.TEST_USER_API_KEY || "",