Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/npm-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion scripts/generate-docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/commands/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
signup,
} from "../lib/api.js";
import {
DEFAULT_API_URL,
clearAllConfig,
clearConfig,
getAccounts,
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]$/;
Expand Down
39 changes: 38 additions & 1 deletion src/lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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<string, Config> | 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
*/
Expand Down
64 changes: 64 additions & 0 deletions test/config-migrate-api-url.test.ts
Original file line number Diff line number Diff line change
@@ -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<string, { apiUrl: string }>;
};

// 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();
});
});
2 changes: 1 addition & 1 deletion test/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 || "",
Expand Down
Loading