@maker-or/opencms 0.1.6 → 0.1.7

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.
Files changed (3) hide show
  1. package/README.md +8 -1
  2. package/dist/index.js +43 -22
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -25,4 +25,11 @@ The generated `cms/schema.json` file defines the project's content types and all
25
25
 
26
26
  The CLI stores its local login configuration in `~/.config/opencms/config.json` (or `$XDG_CONFIG_HOME/opencms/config.json` when configured).
27
27
 
28
- The CLI defaults to the hosted OpenCMS dashboard and API. Use `OPENCMS_DASHBOARD_URL` and `OPENCMS_API_URL` to target a local or self-hosted instance.
28
+ The CLI uses the OpenCMS control-plane origin from `OPENCMS_URL`. Set it to the dashboard/API origin for your hosted, local, or self-hosted instance. `OPENCMS_API_URL` and `OPENCMS_DASHBOARD_URL` remain supported as separate legacy overrides, but there is no baked-in deployment URL.
29
+
30
+ For example:
31
+
32
+ ```bash
33
+ export OPENCMS_URL=https://your-opencms-domain.example
34
+ npx @maker-or/opencms login
35
+ ```
package/dist/index.js CHANGED
@@ -67,10 +67,7 @@ class OpenCmsApiError extends Error {
67
67
  function createSdk(options = {}) {
68
68
  const defaultBaseUrl = typeof window === "undefined" ? undefined : window.location.origin;
69
69
  const configuredBaseUrl = options.baseUrl ?? defaultBaseUrl;
70
- if (!configuredBaseUrl) {
71
- throw new Error("baseUrl is required when using the OpenCMS SDK outside a browser.");
72
- }
73
- const baseUrl = configuredBaseUrl.replace(/\/$/, "");
70
+ const baseUrl = configuredBaseUrl?.replace(/\/$/, "") ?? "";
74
71
  const fetcher = options.fetch ?? globalThis.fetch;
75
72
  const projectId = options.projectId;
76
73
  const environment = options.environment ?? "development";
@@ -81,6 +78,9 @@ function createSdk(options = {}) {
81
78
  if (token) {
82
79
  headers.set("Authorization", `Bearer ${token}`);
83
80
  }
81
+ if (!baseUrl && typeof window === "undefined") {
82
+ throw new Error("baseUrl is required when making OpenCMS SDK requests outside a browser.");
83
+ }
84
84
  const response = await fetcher(`${baseUrl}${path}`, {
85
85
  ...init,
86
86
  headers
@@ -206,12 +206,8 @@ function createSdk(options = {}) {
206
206
  }
207
207
 
208
208
  // src/index.ts
209
- var hostedUrl = "https://web-eta-ten-16.vercel.app";
210
- var dashboardUrl = process.env.OPENCMS_DASHBOARD_URL ?? hostedUrl;
211
- var apiUrl = process.env.OPENCMS_API_URL ?? hostedUrl;
212
209
  var configRoot = process.env.XDG_CONFIG_HOME ?? join(homedir(), ".config");
213
210
  var configPath = join(configRoot, "opencms", "config.json");
214
- var legacyLocalApiUrl = "http://localhost:3000";
215
211
  async function readConfig() {
216
212
  if (!await fileExists(configPath))
217
213
  return {};
@@ -229,12 +225,18 @@ async function writeConfig(config) {
229
225
  function tokenFor(config) {
230
226
  return process.env.OPENCMS_CLERK_TOKEN ?? config.token ?? null;
231
227
  }
228
+ function requireEndpoint(endpoint) {
229
+ const normalized = endpoint?.trim().replace(/\/$/, "");
230
+ if (!normalized) {
231
+ throw new Error("OpenCMS endpoint is not configured. Set OPENCMS_URL to your OpenCMS dashboard/API origin and try again.");
232
+ }
233
+ return normalized;
234
+ }
232
235
  function apiUrlFor(config) {
233
- if (process.env.OPENCMS_API_URL)
234
- return process.env.OPENCMS_API_URL;
235
- if (config?.apiUrl && config.apiUrl !== legacyLocalApiUrl)
236
- return config.apiUrl;
237
- return apiUrl;
236
+ return requireEndpoint(process.env.OPENCMS_URL ?? process.env.OPENCMS_API_URL ?? config?.apiUrl ?? process.env.OPENCMS_DASHBOARD_URL);
237
+ }
238
+ function dashboardUrlFor(config) {
239
+ return requireEndpoint(process.env.OPENCMS_URL ?? process.env.OPENCMS_DASHBOARD_URL ?? process.env.OPENCMS_API_URL ?? config?.apiUrl);
238
240
  }
239
241
  function sdk(config, projectId) {
240
242
  return createSdk({
@@ -269,7 +271,7 @@ async function openBrowser(url) {
269
271
  const command = process.platform === "darwin" ? ["open", url] : process.platform === "win32" ? ["cmd", "/c", "start", "", url] : ["xdg-open", url];
270
272
  await runCommand(command[0], command.slice(1));
271
273
  }
272
- async function browserLogin() {
274
+ async function browserLogin(config) {
273
275
  let resolveToken = () => {
274
276
  return;
275
277
  };
@@ -305,7 +307,7 @@ async function browserLogin() {
305
307
  if (!address || typeof address === "string")
306
308
  throw new Error("Unable to start the login callback server.");
307
309
  const callback = `http://127.0.0.1:${address.port}/callback`;
308
- const loginUrl = `${dashboardUrl.replace(/\/$/, "")}/cli/login?redirect_uri=${encodeURIComponent(callback)}`;
310
+ const loginUrl = `${dashboardUrlFor(config)}/cli/login?redirect_uri=${encodeURIComponent(callback)}`;
309
311
  console.log(`Opening ${loginUrl}`);
310
312
  try {
311
313
  await openBrowser(loginUrl);
@@ -324,7 +326,7 @@ async function ensureToken(config) {
324
326
  const token = tokenFor(config);
325
327
  if (token)
326
328
  return token;
327
- const loggedInToken = await browserLogin();
329
+ const loggedInToken = await browserLogin(config);
328
330
  await writeConfig({ ...config, token: loggedInToken, apiUrl: apiUrlFor(config) });
329
331
  return loggedInToken;
330
332
  }
@@ -333,7 +335,7 @@ async function reauthenticate(config) {
333
335
  throw new Error("OPENCMS_CLERK_TOKEN was rejected or expired. Provide a fresh token or unset the variable to use browser login.");
334
336
  }
335
337
  console.log("Your OpenCMS session has expired. Opening browser login…");
336
- const loggedInToken = await browserLogin();
338
+ const loggedInToken = await browserLogin(config);
337
339
  await writeConfig({ ...config, token: loggedInToken, apiUrl: apiUrlFor(config) });
338
340
  return loggedInToken;
339
341
  }
@@ -344,7 +346,7 @@ async function login() {
344
346
  console.log("Saved OPENCMS_CLERK_TOKEN for local CLI use.");
345
347
  return;
346
348
  }
347
- const loggedInToken = await browserLogin();
349
+ const loggedInToken = await browserLogin(config);
348
350
  await writeConfig({ ...config, token: loggedInToken, apiUrl: apiUrlFor(config) });
349
351
  console.log("Logged in to OpenCMS.");
350
352
  }
@@ -381,8 +383,8 @@ async function ensureCmsDirectory(destination, project, baseUrl) {
381
383
  const configFile = join(cmsDirectory, "opencms.ts");
382
384
  if (!await fileExists(configFile)) {
383
385
  await writeFile(configFile, `export const opencms = {
384
- projectId: process.env.NEXT_PUBLIC_OPENCMS_PROJECT_ID ?? "${project.id}",
385
- apiUrl: process.env.OPENCMS_API_URL ?? "${baseUrl}",
386
+ projectId: process.env.NEXT_PUBLIC_OPENCMS_PROJECT_ID ?? "",
387
+ apiUrl: process.env.OPENCMS_API_URL ?? "",
386
388
  environment: process.env.OPENCMS_ENVIRONMENT ?? "development",
387
389
  } as const;
388
390
  `, "utf8");
@@ -451,7 +453,7 @@ async function createProject() {
451
453
  console.log(`
452
454
  Created ${project.name}.`);
453
455
  console.log(`Project ID: ${project.id}`);
454
- console.log(`Dashboard: ${dashboardUrl.replace(/\/$/, "")}/dashboard/${project.id}`);
456
+ console.log(`Dashboard: ${dashboardUrlFor(await readConfig())}/dashboard/${project.id}`);
455
457
  console.log(`
456
458
  Next steps:
457
459
  cd ${slugify(project.name)}
@@ -470,7 +472,8 @@ async function runDev() {
470
472
  await syncLocalSchema(await projectIdFromEnv(), await readConfig());
471
473
  }
472
474
  const manager = await packageManager(process.cwd());
473
- const command = manager[0] === "npm" ? ["npm", "run", "dev"] : manager[0] === "pnpm" ? ["pnpm", "dev"] : manager[0] === "yarn" ? ["yarn", "dev"] : ["bun", "run", "dev"];
475
+ const script = await nextDevScript(process.cwd());
476
+ const command = manager[0] === "npm" ? ["npm", "run", script] : manager[0] === "pnpm" ? ["pnpm", script] : manager[0] === "yarn" ? ["yarn", script] : ["bun", "run", script];
474
477
  process.exit(await runCommand(command[0], command.slice(1), {
475
478
  cwd: process.cwd(),
476
479
  env: {
@@ -481,6 +484,24 @@ async function runDev() {
481
484
  inherit: true
482
485
  }));
483
486
  }
487
+ async function nextDevScript(destination) {
488
+ const packagePath = join(destination, "package.json");
489
+ if (!await fileExists(packagePath))
490
+ return "dev";
491
+ try {
492
+ const packageJson = JSON.parse(await readFile(packagePath, "utf8"));
493
+ if (typeof packageJson.scripts?.["dev:next"] === "string")
494
+ return "dev:next";
495
+ if (typeof packageJson.scripts?.dev === "string" && /opencms(?:@[^\s]+)?\s+dev/.test(packageJson.scripts.dev)) {
496
+ throw new Error("This OpenCMS template is missing its dev:next script. Update the template before running opencms dev.");
497
+ }
498
+ } catch (error) {
499
+ if (error instanceof SyntaxError)
500
+ throw new Error("package.json is not valid JSON.");
501
+ throw error;
502
+ }
503
+ return "dev";
504
+ }
484
505
  async function syncLocalSchema(projectId, config) {
485
506
  if (!projectId)
486
507
  return;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@maker-or/opencms",
3
- "version": "0.1.6",
3
+ "version": "0.1.7",
4
4
  "description": "The developer-first CLI for OpenCMS",
5
5
  "type": "module",
6
6
  "repository": {