@boxcrew/cli 0.1.2 → 0.1.3
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/dist/index.js +21 -83
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -5,7 +5,6 @@ import { Command } from "commander";
|
|
|
5
5
|
|
|
6
6
|
// src/commands/login.ts
|
|
7
7
|
import { createInterface } from "readline";
|
|
8
|
-
import { createServer } from "http";
|
|
9
8
|
import open from "open";
|
|
10
9
|
|
|
11
10
|
// src/config.ts
|
|
@@ -19,102 +18,41 @@ var config = new Conf({
|
|
|
19
18
|
|
|
20
19
|
// src/commands/login.ts
|
|
21
20
|
var DEFAULT_FRONTEND_URL = "https://boxcrew.ai";
|
|
22
|
-
var LOGIN_TIMEOUT_MS = 5 * 60 * 1e3;
|
|
23
21
|
function registerLoginCommand(program2) {
|
|
24
22
|
program2.command("login").description("Authenticate with BoxCrew").option("--api-url <url>", "BoxCrew API URL").option(
|
|
25
23
|
"--frontend-url <url>",
|
|
26
24
|
"BoxCrew frontend URL",
|
|
27
25
|
process.env.BOXCREW_FRONTEND_URL || DEFAULT_FRONTEND_URL
|
|
28
|
-
).
|
|
26
|
+
).action(
|
|
29
27
|
async (options) => {
|
|
30
28
|
if (options.apiUrl) {
|
|
31
29
|
config.set("apiUrl", options.apiUrl);
|
|
32
30
|
}
|
|
33
|
-
|
|
34
|
-
await pasteLogin();
|
|
35
|
-
} else {
|
|
36
|
-
await browserLogin(options.frontendUrl);
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
);
|
|
40
|
-
}
|
|
41
|
-
async function pasteLogin() {
|
|
42
|
-
console.log(
|
|
43
|
-
"Create an API key in the BoxCrew dashboard (Settings > API Keys),"
|
|
44
|
-
);
|
|
45
|
-
console.log("then paste it below.\n");
|
|
46
|
-
const rl = createInterface({
|
|
47
|
-
input: process.stdin,
|
|
48
|
-
output: process.stdout
|
|
49
|
-
});
|
|
50
|
-
const apiKey = await new Promise((resolve) => {
|
|
51
|
-
rl.question("API key: ", (answer) => {
|
|
52
|
-
rl.close();
|
|
53
|
-
resolve(answer.trim());
|
|
54
|
-
});
|
|
55
|
-
});
|
|
56
|
-
if (!apiKey.startsWith("bxk_")) {
|
|
57
|
-
console.error('Invalid API key. Keys should start with "bxk_".');
|
|
58
|
-
process.exit(1);
|
|
59
|
-
}
|
|
60
|
-
config.set("apiKey", apiKey);
|
|
61
|
-
console.log("\nAuthenticated successfully! Credentials stored.");
|
|
62
|
-
console.log(`API URL: ${config.get("apiUrl")}`);
|
|
63
|
-
}
|
|
64
|
-
async function browserLogin(frontendUrl) {
|
|
65
|
-
return new Promise((resolve, reject) => {
|
|
66
|
-
const server = createServer((req, res) => {
|
|
67
|
-
const url = new URL(req.url || "/", `http://localhost`);
|
|
68
|
-
res.setHeader("Access-Control-Allow-Origin", "*");
|
|
69
|
-
res.setHeader("Access-Control-Allow-Methods", "GET, OPTIONS");
|
|
70
|
-
if (req.method === "OPTIONS") {
|
|
71
|
-
res.writeHead(204);
|
|
72
|
-
res.end();
|
|
73
|
-
return;
|
|
74
|
-
}
|
|
75
|
-
if (url.pathname === "/callback") {
|
|
76
|
-
const key = url.searchParams.get("key");
|
|
77
|
-
if (key && key.startsWith("bxk_")) {
|
|
78
|
-
config.set("apiKey", key);
|
|
79
|
-
res.writeHead(200, { "Content-Type": "text/plain" });
|
|
80
|
-
res.end("ok");
|
|
81
|
-
console.log("\nAuthenticated successfully! Credentials stored.");
|
|
82
|
-
console.log(`API URL: ${config.get("apiUrl")}`);
|
|
83
|
-
server.close();
|
|
84
|
-
clearTimeout(timeout);
|
|
85
|
-
resolve();
|
|
86
|
-
} else {
|
|
87
|
-
res.writeHead(400, { "Content-Type": "text/plain" });
|
|
88
|
-
res.end("Invalid key");
|
|
89
|
-
}
|
|
90
|
-
} else {
|
|
91
|
-
res.writeHead(404, { "Content-Type": "text/plain" });
|
|
92
|
-
res.end("Not found");
|
|
93
|
-
}
|
|
94
|
-
});
|
|
95
|
-
server.listen(0, "127.0.0.1", () => {
|
|
96
|
-
const addr = server.address();
|
|
97
|
-
if (!addr || typeof addr === "string") {
|
|
98
|
-
reject(new Error("Failed to start local server"));
|
|
99
|
-
return;
|
|
100
|
-
}
|
|
101
|
-
const port = addr.port;
|
|
102
|
-
const authUrl = `${frontendUrl}/cli-auth?port=${port}`;
|
|
31
|
+
const authUrl = `${options.frontendUrl}/cli-auth`;
|
|
103
32
|
console.log("Opening browser to authenticate...");
|
|
104
33
|
console.log(`If the browser doesn't open, visit: ${authUrl}
|
|
105
34
|
`);
|
|
106
|
-
console.log("Waiting for authentication...");
|
|
107
35
|
open(authUrl).catch(() => {
|
|
108
36
|
});
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
)
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
37
|
+
const rl = createInterface({
|
|
38
|
+
input: process.stdin,
|
|
39
|
+
output: process.stdout
|
|
40
|
+
});
|
|
41
|
+
const apiKey = await new Promise((resolve) => {
|
|
42
|
+
rl.question("Paste your API key: ", (answer) => {
|
|
43
|
+
rl.close();
|
|
44
|
+
resolve(answer.trim());
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
if (!apiKey.startsWith("bxk_")) {
|
|
48
|
+
console.error('Invalid API key. Keys should start with "bxk_".');
|
|
49
|
+
process.exit(1);
|
|
50
|
+
}
|
|
51
|
+
config.set("apiKey", apiKey);
|
|
52
|
+
console.log("\nAuthenticated successfully! Credentials stored.");
|
|
53
|
+
console.log(`API URL: ${config.get("apiUrl")}`);
|
|
54
|
+
}
|
|
55
|
+
);
|
|
118
56
|
}
|
|
119
57
|
|
|
120
58
|
// src/commands/logout.ts
|