@caphub/cli 0.1.3 → 0.1.4
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/bin/caphub.js +27 -7
- package/package.json +1 -1
package/bin/caphub.js
CHANGED
|
@@ -4,6 +4,7 @@ import { mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
|
4
4
|
import os from "node:os";
|
|
5
5
|
import { dirname, resolve } from "node:path";
|
|
6
6
|
import { spawn } from "node:child_process";
|
|
7
|
+
import { createInterface } from "node:readline/promises";
|
|
7
8
|
import { fileURLToPath } from "node:url";
|
|
8
9
|
|
|
9
10
|
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
@@ -157,6 +158,22 @@ async function fetchJson(url, { method = "GET", body, apiKey = "" } = {}) {
|
|
|
157
158
|
return data;
|
|
158
159
|
}
|
|
159
160
|
|
|
161
|
+
async function waitForEnterToOpen(url) {
|
|
162
|
+
if (process.env.CAPHUB_NO_OPEN === "1") return false;
|
|
163
|
+
if (!process.stdin.isTTY || !process.stdout.isTTY) return false;
|
|
164
|
+
|
|
165
|
+
const rl = createInterface({
|
|
166
|
+
input: process.stdin,
|
|
167
|
+
output: process.stdout,
|
|
168
|
+
});
|
|
169
|
+
try {
|
|
170
|
+
await rl.question("Press Enter to open the browser login page, or Ctrl+C to cancel.");
|
|
171
|
+
} finally {
|
|
172
|
+
rl.close();
|
|
173
|
+
}
|
|
174
|
+
return openUrl(url);
|
|
175
|
+
}
|
|
176
|
+
|
|
160
177
|
function parseFlag(args, name) {
|
|
161
178
|
const idx = args.indexOf(name);
|
|
162
179
|
if (idx === -1) return "";
|
|
@@ -336,19 +353,22 @@ async function commandAuth(args) {
|
|
|
336
353
|
if (!explicitApiKey && !args.includes("--api-key")) {
|
|
337
354
|
const apiUrl = getApiUrl();
|
|
338
355
|
const started = await fetchJson(`${apiUrl}/v1/auth/cli/start`, { method: "POST", body: {} });
|
|
339
|
-
const opened = openUrl(started.approval_url);
|
|
340
356
|
process.stdout.write([
|
|
341
357
|
"caphub auth login",
|
|
342
358
|
"",
|
|
343
|
-
|
|
344
|
-
? "Opened the browser approval page. If it did not open, use this URL:"
|
|
345
|
-
: "Open this URL in your browser and finish sign-in:",
|
|
346
|
-
started.approval_url,
|
|
347
|
-
"",
|
|
359
|
+
"This will open Caphub in your browser to approve CLI login.",
|
|
348
360
|
`code: ${started.code}`,
|
|
349
361
|
`expires_in_seconds: ${started.expires_in_seconds}`,
|
|
350
|
-
|
|
362
|
+
`url: ${started.approval_url}`,
|
|
351
363
|
"",
|
|
364
|
+
].join("\n"));
|
|
365
|
+
|
|
366
|
+
const opened = await waitForEnterToOpen(started.approval_url);
|
|
367
|
+
process.stdout.write([
|
|
368
|
+
process.env.CAPHUB_NO_OPEN === "1"
|
|
369
|
+
? "browser_open: disabled by CAPHUB_NO_OPEN=1"
|
|
370
|
+
: `browser_open: ${opened ? "attempted" : "not attempted"}`,
|
|
371
|
+
opened ? "If the browser did not open, copy the URL above." : "Open the URL above in your browser.",
|
|
352
372
|
"Waiting for website approval...",
|
|
353
373
|
"",
|
|
354
374
|
].join("\n"));
|