@maker-or/opencms 0.1.0 → 0.1.2

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 (2) hide show
  1. package/dist/index.js +29 -3
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -7,6 +7,7 @@ import { join, resolve } from "node:path";
7
7
  import { access, mkdir, readdir, readFile, rm, writeFile } from "node:fs/promises";
8
8
  import { constants } from "node:fs";
9
9
  import { spawn } from "node:child_process";
10
+ import { createInterface } from "node:readline/promises";
10
11
  import process from "node:process";
11
12
 
12
13
  // ../../packages/sdk/src/index.ts
@@ -121,6 +122,14 @@ function sdk(config, projectId) {
121
122
  getToken: () => tokenFor(config)
122
123
  });
123
124
  }
125
+ async function ask(question) {
126
+ const terminal = createInterface({ input: process.stdin, output: process.stdout });
127
+ try {
128
+ return (await terminal.question(question)).trim();
129
+ } finally {
130
+ terminal.close();
131
+ }
132
+ }
124
133
  function fileExists(path) {
125
134
  return access(path, constants.F_OK).then(() => true, () => false);
126
135
  }
@@ -198,6 +207,12 @@ async function ensureToken(config) {
198
207
  await writeConfig({ ...config, token: loggedInToken, apiUrl: config.apiUrl ?? apiUrl });
199
208
  return loggedInToken;
200
209
  }
210
+ async function reauthenticate(config) {
211
+ console.log("Your OpenCMS session has expired. Opening browser login…");
212
+ const loggedInToken = await browserLogin();
213
+ await writeConfig({ ...config, token: loggedInToken, apiUrl: config.apiUrl ?? apiUrl });
214
+ return loggedInToken;
215
+ }
201
216
  async function login() {
202
217
  const config = await readConfig();
203
218
  if (process.env.OPENCMS_CLERK_TOKEN) {
@@ -280,11 +295,22 @@ async function pullTemplate(destination) {
280
295
  async function createProject() {
281
296
  const config = await readConfig();
282
297
  await ensureToken(config);
283
- const name = prompt("Project name:")?.trim();
298
+ const name = await ask("Project name: ");
284
299
  if (!name)
285
300
  throw new Error("A project name is required.");
286
- const client = sdk(await readConfig());
287
- const project = await client.projects.create({ name });
301
+ let currentConfig = await readConfig();
302
+ let client = sdk(currentConfig);
303
+ let project;
304
+ try {
305
+ project = await client.projects.create({ name });
306
+ } catch (error) {
307
+ if (!(error instanceof OpenCmsApiError) || error.status !== 401)
308
+ throw error;
309
+ await reauthenticate(currentConfig);
310
+ currentConfig = await readConfig();
311
+ client = sdk(currentConfig);
312
+ project = await client.projects.create({ name });
313
+ }
288
314
  const destination = resolve(process.cwd(), slugify(project.name));
289
315
  await pullTemplate(destination);
290
316
  const baseUrl = process.env.OPENCMS_API_URL ?? config.apiUrl ?? apiUrl;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@maker-or/opencms",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "description": "The developer-first CLI for OpenCMS",
5
5
  "type": "module",
6
6
  "repository": {