|
1 | 1 | import { Command } from "commander"; |
| 2 | +import Ora from 'ora'; |
| 3 | +import express from 'express'; |
| 4 | +import cors from 'cors'; |
| 5 | +import open from 'open'; |
| 6 | +import readline from 'readline/promises'; |
| 7 | +import { loadSettings, saveSettings } from "./services/settings.js"; |
| 8 | +import { getEnv } from "./services/env.js"; |
| 9 | +import { checkAuth } from "./services/check-auth.js"; |
| 10 | +import { saveApiKey } from "./services/api-key.js"; |
2 | 11 |
|
3 | 12 | export default new Command() |
4 | 13 | .command("auth") |
5 | | - .description("Authenticate with Replexica") |
| 14 | + .description("Authenticate with Replexica API") |
6 | 15 | .helpOption("-h, --help", "Show help") |
7 | | - .action(async () => { |
8 | | - const message = ` |
9 | | -To obtain Replexica API key, please follow the following link: |
| 16 | + .option("-d, --delete", "Delete existing authentication") |
| 17 | + .option("-l, --login", "Authenticate with Replexica API") |
| 18 | + .action(async (options) => { |
| 19 | + const env = getEnv(); |
| 20 | + let config = await loadSettings(); |
10 | 21 |
|
11 | | -https://replexica.com/app/settings |
| 22 | + if (options.delete) { |
| 23 | + await logout(); |
| 24 | + } |
| 25 | + if (options.login) { |
| 26 | + await login(env.REPLEXICA_WEB_URL); |
| 27 | + config = await loadSettings(); |
| 28 | + } |
12 | 29 |
|
13 | | -Once you have your API key, please save it in the .env file in the root of your project. |
14 | | - `.trim(); |
| 30 | + await checkAuth(); |
| 31 | + }); |
| 32 | + |
| 33 | +async function logout() { |
| 34 | + const spinner = Ora().start('Logging out'); |
| 35 | + await saveApiKey(null); |
| 36 | + spinner.succeed('Logged out'); |
| 37 | +} |
| 38 | + |
| 39 | +async function login(apiUrl: string) { |
| 40 | + await readline.createInterface({ |
| 41 | + input: process.stdin, |
| 42 | + output: process.stdout, |
| 43 | + }).question('Press Enter to open the browser for authentication\n'); |
| 44 | + |
| 45 | + const spinner = Ora().start('Waiting for the API key'); |
| 46 | + const apiKey = await waitForApiKey(async (port) => { |
| 47 | + await open(`${apiUrl}/app/cli?port=${port}`, { wait: false }); |
| 48 | + }); |
| 49 | + spinner.succeed('API key received'); |
| 50 | + await saveApiKey(apiKey); |
| 51 | +} |
| 52 | + |
| 53 | +async function waitForApiKey(cb: (port: string) => void): Promise<string> { |
| 54 | + // start a sever on an ephemeral port and return the port number |
| 55 | + // from the function |
| 56 | + const app = express(); |
| 57 | + app.use(express.json()); |
| 58 | + app.use(cors()); |
| 59 | + |
| 60 | + return new Promise((resolve) => { |
| 61 | + const server = app.listen(0, async () => { |
| 62 | + const port = (server.address() as any).port; |
| 63 | + cb(port.toString()); |
| 64 | + }); |
15 | 65 |
|
16 | | - console.log(message); |
| 66 | + app.post("/", (req, res) => { |
| 67 | + const apiKey = req.body.apiKey; |
| 68 | + res.end(); |
| 69 | + server.close(() => { |
| 70 | + resolve(apiKey); |
| 71 | + }); |
| 72 | + }); |
17 | 73 | }); |
| 74 | +} |
0 commit comments