@develemail/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 +58 -39
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -170,49 +170,68 @@ var tailCommand = new Command3("tail").description("Poll and print new email eve
|
|
|
170
170
|
|
|
171
171
|
// src/commands/login.ts
|
|
172
172
|
import { Command as Command4 } from "commander";
|
|
173
|
-
import {
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
173
|
+
import {
|
|
174
|
+
createServer
|
|
175
|
+
} from "http";
|
|
176
|
+
import { exec } from "child_process";
|
|
177
|
+
import { platform } from "os";
|
|
178
|
+
function openBrowser(url) {
|
|
179
|
+
const cmd = platform() === "darwin" ? "open" : platform() === "win32" ? "start" : "xdg-open";
|
|
180
|
+
exec(`${cmd} "${url}"`);
|
|
180
181
|
}
|
|
181
|
-
var loginCommand = new Command4("login").description("Authenticate with your DevelEmail instance
|
|
182
|
+
var loginCommand = new Command4("login").description("Authenticate with your DevelEmail instance via browser").requiredOption(
|
|
182
183
|
"--url <url>",
|
|
183
|
-
"Base URL of your DevelEmail
|
|
184
|
-
).
|
|
185
|
-
const
|
|
186
|
-
const
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
184
|
+
"Base URL of your DevelEmail instance (e.g. https://develemail.com/api)"
|
|
185
|
+
).action(async (opts) => {
|
|
186
|
+
const apiUrl = opts.url.replace(/\/$/, "");
|
|
187
|
+
const baseUrl = apiUrl.replace(/\/api$/, "");
|
|
188
|
+
await new Promise((resolve2, reject) => {
|
|
189
|
+
const server = createServer(
|
|
190
|
+
(req, res) => {
|
|
191
|
+
const url = new URL(req.url ?? "/", `http://localhost`);
|
|
192
|
+
if (url.pathname !== "/callback") {
|
|
193
|
+
res.writeHead(404);
|
|
194
|
+
res.end("Not found");
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
const token = url.searchParams.get("token");
|
|
198
|
+
if (!token) {
|
|
199
|
+
res.writeHead(400);
|
|
200
|
+
res.end("Missing token");
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
saveOwnerSession({ url: apiUrl, token });
|
|
204
|
+
res.writeHead(200, { "Content-Type": "text/html" });
|
|
205
|
+
res.end(
|
|
206
|
+
'<html><body style="font-family:system-ui;display:flex;align-items:center;justify-content:center;height:100vh;margin:0"><div style="text-align:center"><h2>CLI Authorized</h2><p>You can close this tab and return to your terminal.</p></div></body></html>'
|
|
207
|
+
);
|
|
208
|
+
console.log("Authenticated successfully.");
|
|
209
|
+
console.log("Session saved to ~/.config/develemail/auth.json");
|
|
210
|
+
server.close();
|
|
211
|
+
resolve2();
|
|
212
|
+
}
|
|
213
|
+
);
|
|
214
|
+
server.listen(0, "127.0.0.1", () => {
|
|
215
|
+
const addr = server.address();
|
|
216
|
+
if (!addr || typeof addr === "string") {
|
|
217
|
+
reject(new Error("Failed to start callback server"));
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
const port = addr.port;
|
|
221
|
+
const authUrl = `${baseUrl}/cli-auth?port=${port}`;
|
|
222
|
+
console.log("Opening browser for authentication...");
|
|
223
|
+
console.log(`If the browser doesn't open, visit: ${authUrl}`);
|
|
224
|
+
openBrowser(authUrl);
|
|
194
225
|
});
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
226
|
+
server.on("error", reject);
|
|
227
|
+
setTimeout(
|
|
228
|
+
() => {
|
|
229
|
+
server.close();
|
|
230
|
+
reject(new Error("Authentication timed out after 5 minutes"));
|
|
231
|
+
},
|
|
232
|
+
5 * 60 * 1e3
|
|
198
233
|
);
|
|
199
|
-
|
|
200
|
-
}
|
|
201
|
-
if (!res.ok) {
|
|
202
|
-
const body = await res.json().catch(() => ({}));
|
|
203
|
-
console.error(`Error: ${body.message ?? res.statusText}`);
|
|
204
|
-
process.exit(1);
|
|
205
|
-
}
|
|
206
|
-
const setCookie = res.headers.get("set-cookie");
|
|
207
|
-
const token = setCookie?.match(/session=([^;]+)/)?.[1];
|
|
208
|
-
if (!token) {
|
|
209
|
-
console.error("Error: No session cookie returned from server.");
|
|
210
|
-
process.exit(1);
|
|
211
|
-
}
|
|
212
|
-
saveOwnerSession({ url, token });
|
|
213
|
-
const data = await res.json();
|
|
214
|
-
console.log(`Logged in as ${data.owner?.email ?? email}`);
|
|
215
|
-
console.log(`Session saved to ~/.config/develemail/auth.json`);
|
|
234
|
+
});
|
|
216
235
|
});
|
|
217
236
|
var logoutCommand = new Command4("logout").description("Clear the stored owner session").action(() => {
|
|
218
237
|
clearOwnerSession();
|