@maker-or/opencms 0.1.5 → 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.
- package/README.md +8 -1
- package/dist/index.js +60 -23
- 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
|
|
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
|
-
|
|
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,9 +206,6 @@ 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
211
|
async function readConfig() {
|
|
@@ -228,9 +225,22 @@ async function writeConfig(config) {
|
|
|
228
225
|
function tokenFor(config) {
|
|
229
226
|
return process.env.OPENCMS_CLERK_TOKEN ?? config.token ?? null;
|
|
230
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
|
+
}
|
|
235
|
+
function apiUrlFor(config) {
|
|
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);
|
|
240
|
+
}
|
|
231
241
|
function sdk(config, projectId) {
|
|
232
242
|
return createSdk({
|
|
233
|
-
baseUrl:
|
|
243
|
+
baseUrl: apiUrlFor(config),
|
|
234
244
|
projectId,
|
|
235
245
|
getToken: () => tokenFor(config)
|
|
236
246
|
});
|
|
@@ -261,7 +271,7 @@ async function openBrowser(url) {
|
|
|
261
271
|
const command = process.platform === "darwin" ? ["open", url] : process.platform === "win32" ? ["cmd", "/c", "start", "", url] : ["xdg-open", url];
|
|
262
272
|
await runCommand(command[0], command.slice(1));
|
|
263
273
|
}
|
|
264
|
-
async function browserLogin() {
|
|
274
|
+
async function browserLogin(config) {
|
|
265
275
|
let resolveToken = () => {
|
|
266
276
|
return;
|
|
267
277
|
};
|
|
@@ -297,7 +307,7 @@ async function browserLogin() {
|
|
|
297
307
|
if (!address || typeof address === "string")
|
|
298
308
|
throw new Error("Unable to start the login callback server.");
|
|
299
309
|
const callback = `http://127.0.0.1:${address.port}/callback`;
|
|
300
|
-
const loginUrl = `${
|
|
310
|
+
const loginUrl = `${dashboardUrlFor(config)}/cli/login?redirect_uri=${encodeURIComponent(callback)}`;
|
|
301
311
|
console.log(`Opening ${loginUrl}`);
|
|
302
312
|
try {
|
|
303
313
|
await openBrowser(loginUrl);
|
|
@@ -316,8 +326,8 @@ async function ensureToken(config) {
|
|
|
316
326
|
const token = tokenFor(config);
|
|
317
327
|
if (token)
|
|
318
328
|
return token;
|
|
319
|
-
const loggedInToken = await browserLogin();
|
|
320
|
-
await writeConfig({ ...config, token: loggedInToken, apiUrl: config
|
|
329
|
+
const loggedInToken = await browserLogin(config);
|
|
330
|
+
await writeConfig({ ...config, token: loggedInToken, apiUrl: apiUrlFor(config) });
|
|
321
331
|
return loggedInToken;
|
|
322
332
|
}
|
|
323
333
|
async function reauthenticate(config) {
|
|
@@ -325,19 +335,19 @@ async function reauthenticate(config) {
|
|
|
325
335
|
throw new Error("OPENCMS_CLERK_TOKEN was rejected or expired. Provide a fresh token or unset the variable to use browser login.");
|
|
326
336
|
}
|
|
327
337
|
console.log("Your OpenCMS session has expired. Opening browser login…");
|
|
328
|
-
const loggedInToken = await browserLogin();
|
|
329
|
-
await writeConfig({ ...config, token: loggedInToken, apiUrl: config
|
|
338
|
+
const loggedInToken = await browserLogin(config);
|
|
339
|
+
await writeConfig({ ...config, token: loggedInToken, apiUrl: apiUrlFor(config) });
|
|
330
340
|
return loggedInToken;
|
|
331
341
|
}
|
|
332
342
|
async function login() {
|
|
333
343
|
const config = await readConfig();
|
|
334
344
|
if (process.env.OPENCMS_CLERK_TOKEN) {
|
|
335
|
-
await writeConfig({ ...config, token: process.env.OPENCMS_CLERK_TOKEN, apiUrl: config
|
|
345
|
+
await writeConfig({ ...config, token: process.env.OPENCMS_CLERK_TOKEN, apiUrl: apiUrlFor(config) });
|
|
336
346
|
console.log("Saved OPENCMS_CLERK_TOKEN for local CLI use.");
|
|
337
347
|
return;
|
|
338
348
|
}
|
|
339
|
-
const loggedInToken = await browserLogin();
|
|
340
|
-
await writeConfig({ ...config, token: loggedInToken, apiUrl: config
|
|
349
|
+
const loggedInToken = await browserLogin(config);
|
|
350
|
+
await writeConfig({ ...config, token: loggedInToken, apiUrl: apiUrlFor(config) });
|
|
341
351
|
console.log("Logged in to OpenCMS.");
|
|
342
352
|
}
|
|
343
353
|
async function logout() {
|
|
@@ -373,8 +383,8 @@ async function ensureCmsDirectory(destination, project, baseUrl) {
|
|
|
373
383
|
const configFile = join(cmsDirectory, "opencms.ts");
|
|
374
384
|
if (!await fileExists(configFile)) {
|
|
375
385
|
await writeFile(configFile, `export const opencms = {
|
|
376
|
-
projectId: process.env.NEXT_PUBLIC_OPENCMS_PROJECT_ID ?? "
|
|
377
|
-
apiUrl: process.env.OPENCMS_API_URL ?? "
|
|
386
|
+
projectId: process.env.NEXT_PUBLIC_OPENCMS_PROJECT_ID ?? "",
|
|
387
|
+
apiUrl: process.env.OPENCMS_API_URL ?? "",
|
|
378
388
|
environment: process.env.OPENCMS_ENVIRONMENT ?? "development",
|
|
379
389
|
} as const;
|
|
380
390
|
`, "utf8");
|
|
@@ -435,7 +445,7 @@ async function createProject() {
|
|
|
435
445
|
}
|
|
436
446
|
const destination = resolve(process.cwd(), slugify(project.name));
|
|
437
447
|
await pullTemplate(destination);
|
|
438
|
-
const baseUrl =
|
|
448
|
+
const baseUrl = apiUrlFor(config);
|
|
439
449
|
await writeProjectEnv(destination, project, baseUrl);
|
|
440
450
|
await ensureCmsDirectory(destination, project, baseUrl);
|
|
441
451
|
await installDependencies(destination);
|
|
@@ -443,7 +453,7 @@ async function createProject() {
|
|
|
443
453
|
console.log(`
|
|
444
454
|
Created ${project.name}.`);
|
|
445
455
|
console.log(`Project ID: ${project.id}`);
|
|
446
|
-
console.log(`Dashboard: ${
|
|
456
|
+
console.log(`Dashboard: ${dashboardUrlFor(await readConfig())}/dashboard/${project.id}`);
|
|
447
457
|
console.log(`
|
|
448
458
|
Next steps:
|
|
449
459
|
cd ${slugify(project.name)}
|
|
@@ -462,8 +472,35 @@ async function runDev() {
|
|
|
462
472
|
await syncLocalSchema(await projectIdFromEnv(), await readConfig());
|
|
463
473
|
}
|
|
464
474
|
const manager = await packageManager(process.cwd());
|
|
465
|
-
const
|
|
466
|
-
|
|
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];
|
|
477
|
+
process.exit(await runCommand(command[0], command.slice(1), {
|
|
478
|
+
cwd: process.cwd(),
|
|
479
|
+
env: {
|
|
480
|
+
...process.env,
|
|
481
|
+
OPENCMS_API_URL: apiUrlFor(await readConfig()),
|
|
482
|
+
OPENCMS_ENVIRONMENT: "development"
|
|
483
|
+
},
|
|
484
|
+
inherit: true
|
|
485
|
+
}));
|
|
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";
|
|
467
504
|
}
|
|
468
505
|
async function syncLocalSchema(projectId, config) {
|
|
469
506
|
if (!projectId)
|