@maker-or/opencms 0.1.4 → 0.1.6
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 +2 -0
- package/dist/index.js +46 -10
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -19,6 +19,8 @@ npx @maker-or/opencms deploy
|
|
|
19
19
|
|
|
20
20
|
`opencms create` authenticates you, creates an OpenCMS project, pulls the Next.js template, writes the project configuration, and installs dependencies.
|
|
21
21
|
|
|
22
|
+
`opencms login` refreshes the saved browser session. The CLI also retries a management request once with a fresh browser session when the saved session has expired.
|
|
23
|
+
|
|
22
24
|
The generated `cms/schema.json` file defines the project's content types and allowed blocks. The CLI syncs it to the development environment when `dev` or `deploy` runs.
|
|
23
25
|
|
|
24
26
|
The CLI stores its local login configuration in `~/.config/opencms/config.json` (or `$XDG_CONFIG_HOME/opencms/config.json` when configured).
|
package/dist/index.js
CHANGED
|
@@ -211,6 +211,7 @@ var dashboardUrl = process.env.OPENCMS_DASHBOARD_URL ?? hostedUrl;
|
|
|
211
211
|
var apiUrl = process.env.OPENCMS_API_URL ?? hostedUrl;
|
|
212
212
|
var configRoot = process.env.XDG_CONFIG_HOME ?? join(homedir(), ".config");
|
|
213
213
|
var configPath = join(configRoot, "opencms", "config.json");
|
|
214
|
+
var legacyLocalApiUrl = "http://localhost:3000";
|
|
214
215
|
async function readConfig() {
|
|
215
216
|
if (!await fileExists(configPath))
|
|
216
217
|
return {};
|
|
@@ -228,9 +229,16 @@ async function writeConfig(config) {
|
|
|
228
229
|
function tokenFor(config) {
|
|
229
230
|
return process.env.OPENCMS_CLERK_TOKEN ?? config.token ?? null;
|
|
230
231
|
}
|
|
232
|
+
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;
|
|
238
|
+
}
|
|
231
239
|
function sdk(config, projectId) {
|
|
232
240
|
return createSdk({
|
|
233
|
-
baseUrl:
|
|
241
|
+
baseUrl: apiUrlFor(config),
|
|
234
242
|
projectId,
|
|
235
243
|
getToken: () => tokenFor(config)
|
|
236
244
|
});
|
|
@@ -317,23 +325,27 @@ async function ensureToken(config) {
|
|
|
317
325
|
if (token)
|
|
318
326
|
return token;
|
|
319
327
|
const loggedInToken = await browserLogin();
|
|
320
|
-
await writeConfig({ ...config, token: loggedInToken, apiUrl: config
|
|
328
|
+
await writeConfig({ ...config, token: loggedInToken, apiUrl: apiUrlFor(config) });
|
|
321
329
|
return loggedInToken;
|
|
322
330
|
}
|
|
323
331
|
async function reauthenticate(config) {
|
|
332
|
+
if (process.env.OPENCMS_CLERK_TOKEN) {
|
|
333
|
+
throw new Error("OPENCMS_CLERK_TOKEN was rejected or expired. Provide a fresh token or unset the variable to use browser login.");
|
|
334
|
+
}
|
|
324
335
|
console.log("Your OpenCMS session has expired. Opening browser login…");
|
|
325
336
|
const loggedInToken = await browserLogin();
|
|
326
|
-
await writeConfig({ ...config, token: loggedInToken, apiUrl: config
|
|
337
|
+
await writeConfig({ ...config, token: loggedInToken, apiUrl: apiUrlFor(config) });
|
|
327
338
|
return loggedInToken;
|
|
328
339
|
}
|
|
329
340
|
async function login() {
|
|
330
341
|
const config = await readConfig();
|
|
331
342
|
if (process.env.OPENCMS_CLERK_TOKEN) {
|
|
332
|
-
await writeConfig({ ...config, token: process.env.OPENCMS_CLERK_TOKEN, apiUrl: config
|
|
343
|
+
await writeConfig({ ...config, token: process.env.OPENCMS_CLERK_TOKEN, apiUrl: apiUrlFor(config) });
|
|
333
344
|
console.log("Saved OPENCMS_CLERK_TOKEN for local CLI use.");
|
|
334
345
|
return;
|
|
335
346
|
}
|
|
336
|
-
await
|
|
347
|
+
const loggedInToken = await browserLogin();
|
|
348
|
+
await writeConfig({ ...config, token: loggedInToken, apiUrl: apiUrlFor(config) });
|
|
337
349
|
console.log("Logged in to OpenCMS.");
|
|
338
350
|
}
|
|
339
351
|
async function logout() {
|
|
@@ -431,7 +443,7 @@ async function createProject() {
|
|
|
431
443
|
}
|
|
432
444
|
const destination = resolve(process.cwd(), slugify(project.name));
|
|
433
445
|
await pullTemplate(destination);
|
|
434
|
-
const baseUrl =
|
|
446
|
+
const baseUrl = apiUrlFor(config);
|
|
435
447
|
await writeProjectEnv(destination, project, baseUrl);
|
|
436
448
|
await ensureCmsDirectory(destination, project, baseUrl);
|
|
437
449
|
await installDependencies(destination);
|
|
@@ -449,10 +461,25 @@ Next steps:
|
|
|
449
461
|
async function runDev() {
|
|
450
462
|
const config = await readConfig();
|
|
451
463
|
await ensureToken(config);
|
|
452
|
-
|
|
464
|
+
try {
|
|
465
|
+
await syncLocalSchema(await projectIdFromEnv(), await readConfig());
|
|
466
|
+
} catch (error) {
|
|
467
|
+
if (!(error instanceof OpenCmsApiError) || error.status !== 401)
|
|
468
|
+
throw error;
|
|
469
|
+
await reauthenticate(await readConfig());
|
|
470
|
+
await syncLocalSchema(await projectIdFromEnv(), await readConfig());
|
|
471
|
+
}
|
|
453
472
|
const manager = await packageManager(process.cwd());
|
|
454
473
|
const command = manager[0] === "npm" ? ["npm", "run", "dev"] : manager[0] === "pnpm" ? ["pnpm", "dev"] : manager[0] === "yarn" ? ["yarn", "dev"] : ["bun", "run", "dev"];
|
|
455
|
-
process.exit(await runCommand(command[0], command.slice(1), {
|
|
474
|
+
process.exit(await runCommand(command[0], command.slice(1), {
|
|
475
|
+
cwd: process.cwd(),
|
|
476
|
+
env: {
|
|
477
|
+
...process.env,
|
|
478
|
+
OPENCMS_API_URL: apiUrlFor(await readConfig()),
|
|
479
|
+
OPENCMS_ENVIRONMENT: "development"
|
|
480
|
+
},
|
|
481
|
+
inherit: true
|
|
482
|
+
}));
|
|
456
483
|
}
|
|
457
484
|
async function syncLocalSchema(projectId, config) {
|
|
458
485
|
if (!projectId)
|
|
@@ -484,8 +511,17 @@ async function deploy() {
|
|
|
484
511
|
const projectId = await projectIdFromEnv() ?? config.projectId;
|
|
485
512
|
if (!projectId)
|
|
486
513
|
throw new Error("No OpenCMS project is configured in this directory.");
|
|
487
|
-
|
|
488
|
-
|
|
514
|
+
let deployment;
|
|
515
|
+
try {
|
|
516
|
+
await syncLocalSchema(projectId, await readConfig());
|
|
517
|
+
deployment = await sdk(await readConfig()).deploy(projectId);
|
|
518
|
+
} catch (error) {
|
|
519
|
+
if (!(error instanceof OpenCmsApiError) || error.status !== 401)
|
|
520
|
+
throw error;
|
|
521
|
+
await reauthenticate(await readConfig());
|
|
522
|
+
await syncLocalSchema(projectId, await readConfig());
|
|
523
|
+
deployment = await sdk(await readConfig()).deploy(projectId);
|
|
524
|
+
}
|
|
489
525
|
console.log(`Deployed ${deployment.sourceEnvironment} content to ${deployment.targetEnvironment}.`);
|
|
490
526
|
if (process.env.VERCEL_TOKEN) {
|
|
491
527
|
console.log("Deploying the application to Vercel…");
|