@frevana/cli 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 (4) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +68 -0
  3. package/dist/cli.js +213 -0
  4. package/package.json +43 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Frevana
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,68 @@
1
+ # frevana
2
+
3
+ Frevana is the command-line client for `frevana-server`.
4
+
5
+ It is designed to help users authenticate with Frevana and run terminal-based workflows backed by Frevana services.
6
+
7
+ ## What This CLI Does
8
+
9
+ - Sign in with device authorization
10
+ - Store the API key locally for reuse
11
+ - Connect terminal workflows to Frevana server capabilities
12
+
13
+ ## Requirements
14
+
15
+ - Node.js 20 or later
16
+ - npm
17
+
18
+ ## Install
19
+
20
+ ```bash
21
+ npm install -g @frevana/cli
22
+ ```
23
+
24
+ ## Available Commands
25
+
26
+ ### Show help
27
+
28
+ ```bash
29
+ frevana --help
30
+ ```
31
+
32
+ ### Show version
33
+
34
+ ```bash
35
+ frevana --version
36
+ ```
37
+
38
+ ### Login
39
+
40
+ By default, the CLI uses the production server:
41
+
42
+ ```bash
43
+ frevana login
44
+ ```
45
+
46
+ Use a custom server when needed:
47
+
48
+ ```bash
49
+ frevana login --server http://localhost:3000
50
+ ```
51
+
52
+ Current default server:
53
+
54
+ ```text
55
+ https://api.frevana.com
56
+ ```
57
+
58
+ ## How Login Works
59
+
60
+ 1. Run `frevana login`
61
+ 2. Complete the device authorization flow in your browser
62
+ 3. The CLI stores your API key locally after a successful login
63
+
64
+ The saved config file is:
65
+
66
+ ```text
67
+ ~/.frevana/cli-config.json
68
+ ```
package/dist/cli.js ADDED
@@ -0,0 +1,213 @@
1
+ #!/usr/bin/env node
2
+
3
+ // src/cli.ts
4
+ import fs2 from "fs";
5
+ import { Command } from "commander";
6
+ import chalk2 from "chalk";
7
+
8
+ // src/commands/login.ts
9
+ import chalk from "chalk";
10
+ import inquirer from "inquirer";
11
+
12
+ // src/lib/auth/device-authorization.ts
13
+ async function startDeviceAuthorization(serverUrl, scopes) {
14
+ const response = await fetch(`${serverUrl}/device-authorization/start`, {
15
+ method: "POST",
16
+ headers: {
17
+ "content-type": "application/json"
18
+ },
19
+ body: JSON.stringify({
20
+ client_name: "frevana-cli",
21
+ scopes
22
+ })
23
+ });
24
+ if (!response.ok) {
25
+ const text = await response.text();
26
+ throw new Error(`Start authorization failed (${response.status}): ${text}`);
27
+ }
28
+ return await response.json();
29
+ }
30
+ async function pollDeviceAuthorization(serverUrl, deviceCode) {
31
+ const response = await fetch(`${serverUrl}/device-authorization/poll`, {
32
+ method: "POST",
33
+ headers: {
34
+ "content-type": "application/json"
35
+ },
36
+ body: JSON.stringify({
37
+ device_code: deviceCode
38
+ })
39
+ });
40
+ if (!response.ok) {
41
+ const text = await response.text();
42
+ throw new Error(`Poll failed (${response.status}): ${text}`);
43
+ }
44
+ return await response.json();
45
+ }
46
+
47
+ // src/lib/config/credentials.ts
48
+ import fs from "fs";
49
+ import os from "os";
50
+ import path from "path";
51
+ var CONFIG_DIR = path.join(os.homedir(), ".frevana");
52
+ var CONFIG_PATH = path.join(CONFIG_DIR, "cli-config.json");
53
+ function saveCredentials(credentials) {
54
+ fs.mkdirSync(CONFIG_DIR, { recursive: true });
55
+ fs.writeFileSync(
56
+ CONFIG_PATH,
57
+ `${JSON.stringify(credentials, null, 2)}
58
+ `,
59
+ "utf-8"
60
+ );
61
+ }
62
+
63
+ // src/lib/utils/browser.ts
64
+ import { spawn } from "child_process";
65
+ function openUrl(url) {
66
+ const platform = process.platform;
67
+ const command = platform === "darwin" ? "open" : platform === "win32" ? "cmd" : "xdg-open";
68
+ const args = platform === "win32" ? ["/c", "start", "", url] : [url];
69
+ try {
70
+ const child = spawn(command, args, {
71
+ stdio: "ignore",
72
+ detached: true
73
+ });
74
+ child.unref();
75
+ return true;
76
+ } catch {
77
+ return false;
78
+ }
79
+ }
80
+ function getApiKeyPageUrl(verificationUri) {
81
+ try {
82
+ const parsed = new URL(verificationUri);
83
+ return `${parsed.origin}/dashboard`;
84
+ } catch {
85
+ return `${verificationUri}/dashboard`;
86
+ }
87
+ }
88
+
89
+ // src/commands/login.ts
90
+ function sleep(ms) {
91
+ return new Promise((resolve) => setTimeout(resolve, ms));
92
+ }
93
+ async function sleepWithLoading(ms, label) {
94
+ if (!process.stdout.isTTY) {
95
+ await sleep(ms);
96
+ return;
97
+ }
98
+ const frames = ["", ".", "..", "..."];
99
+ let frameIndex = 0;
100
+ const intervalMs = 250;
101
+ const endAt = Date.now() + ms;
102
+ const render = () => {
103
+ const text = `${label}${frames[frameIndex]}`;
104
+ process.stdout.write(`\r${chalk.gray(text.padEnd(48, " "))}`);
105
+ frameIndex = (frameIndex + 1) % frames.length;
106
+ };
107
+ render();
108
+ await new Promise((resolve) => {
109
+ const timer = setInterval(() => {
110
+ if (Date.now() >= endAt) {
111
+ clearInterval(timer);
112
+ resolve();
113
+ return;
114
+ }
115
+ render();
116
+ }, intervalMs);
117
+ });
118
+ process.stdout.write(`\r${" ".repeat(64)}\r`);
119
+ }
120
+ async function loginCommand(serverUrl) {
121
+ const normalizedServer = serverUrl.replace(/\/+$/, "");
122
+ const start = await startDeviceAuthorization(normalizedServer, []);
123
+ const expiresAt = Date.now() + start.expires_in * 1e3;
124
+ let waitSeconds = start.interval;
125
+ let waitingHintPrinted = false;
126
+ console.log(chalk.blue("\nDevice authorization started.\n"));
127
+ console.log(`Code: ${chalk.bold(start.user_code)}`);
128
+ console.log(`Authorization URL: ${start.verification_uri_complete}
129
+ `);
130
+ const opened = openUrl(start.verification_uri_complete);
131
+ if (opened) {
132
+ console.log(chalk.green("Opened authorization page in your browser.\n"));
133
+ } else {
134
+ console.log(
135
+ chalk.yellow("Could not open browser automatically. Open URL manually.\n")
136
+ );
137
+ }
138
+ while (Date.now() < expiresAt) {
139
+ const result = await pollDeviceAuthorization(normalizedServer, start.device_code);
140
+ if (result.status === "approved" && result.api_key) {
141
+ saveCredentials({
142
+ server_url: normalizedServer,
143
+ api_key: result.api_key,
144
+ updated_at: (/* @__PURE__ */ new Date()).toISOString()
145
+ });
146
+ console.log(chalk.green("Login successful. API key saved to local config."));
147
+ return;
148
+ }
149
+ if (result.status === "pending") {
150
+ if (!waitingHintPrinted && !process.stdout.isTTY) {
151
+ console.log(chalk.gray("Waiting for authorization..."));
152
+ waitingHintPrinted = true;
153
+ }
154
+ await sleepWithLoading(waitSeconds * 1e3, "Waiting for authorization");
155
+ continue;
156
+ }
157
+ if (result.status === "slow_down") {
158
+ waitSeconds = Math.min(waitSeconds + 2, 15);
159
+ if (!waitingHintPrinted && !process.stdout.isTTY) {
160
+ console.log(chalk.gray("Waiting for authorization..."));
161
+ waitingHintPrinted = true;
162
+ }
163
+ await sleepWithLoading(waitSeconds * 1e3, "Waiting for authorization");
164
+ continue;
165
+ }
166
+ if (result.status === "denied" || result.status === "expired") {
167
+ throw new Error(result.error_message || "Authorization failed.");
168
+ }
169
+ await sleep(waitSeconds * 1e3);
170
+ }
171
+ const apiKeyPage = getApiKeyPageUrl(start.verification_uri);
172
+ console.log(chalk.yellow("\nAuthorization timed out."));
173
+ console.log(`Open API Key page: ${apiKeyPage}`);
174
+ const { apiKey } = await inquirer.prompt([
175
+ {
176
+ type: "password",
177
+ name: "apiKey",
178
+ message: "Paste API Key:",
179
+ mask: "*",
180
+ validate: (value) => value.trim() ? true : "API Key is required"
181
+ }
182
+ ]);
183
+ saveCredentials({
184
+ server_url: normalizedServer,
185
+ api_key: apiKey.trim(),
186
+ updated_at: (/* @__PURE__ */ new Date()).toISOString()
187
+ });
188
+ console.log(chalk.green("API key saved to local config."));
189
+ }
190
+
191
+ // src/cli.ts
192
+ var packageJson = JSON.parse(
193
+ fs2.readFileSync(new URL("../package.json", import.meta.url), "utf-8")
194
+ );
195
+ var program = new Command();
196
+ program.name("frevana").description("Frevana command line tool").version(packageJson.version);
197
+ program.command("login").description("Login with device authorization flow").option(
198
+ "--server <url>",
199
+ "Frevana server URL",
200
+ process.env.FREVANA_SERVER_URL || "https://api.frevana.com"
201
+ ).action(async (opts) => {
202
+ try {
203
+ await loginCommand(opts.server);
204
+ } catch (error) {
205
+ console.error(chalk2.red(`Login failed: ${error}`));
206
+ process.exit(1);
207
+ }
208
+ });
209
+ program.parseAsync().catch((error) => {
210
+ console.error(chalk2.red(`Command failed: ${error}`));
211
+ process.exit(1);
212
+ });
213
+ //# sourceMappingURL=cli.js.map
package/package.json ADDED
@@ -0,0 +1,43 @@
1
+ {
2
+ "name": "@frevana/cli",
3
+ "version": "0.1.2",
4
+ "description": "Frevana CLI for calling frevana-server APIs and using server capabilities",
5
+ "type": "module",
6
+ "bin": {
7
+ "frevana": "dist/cli.js"
8
+ },
9
+ "files": [
10
+ "dist",
11
+ "!dist/**/*.map",
12
+ "README.md",
13
+ "LICENSE"
14
+ ],
15
+ "engines": {
16
+ "node": ">=20"
17
+ },
18
+ "scripts": {
19
+ "build": "tsup",
20
+ "dev": "tsup --watch",
21
+ "check": "tsc --noEmit",
22
+ "release": "npm run build && npm publish --access public",
23
+ "release:dry-run": "npm run build && npm publish --access public --dry-run"
24
+ },
25
+ "keywords": [
26
+ "frevana",
27
+ "cli",
28
+ "device-auth",
29
+ "api-key"
30
+ ],
31
+ "license": "MIT",
32
+ "dependencies": {
33
+ "chalk": "^5.6.2",
34
+ "commander": "^14.0.3",
35
+ "inquirer": "^9.3.7"
36
+ },
37
+ "devDependencies": {
38
+ "@types/inquirer": "^9.0.9",
39
+ "@types/node": "^25.5.0",
40
+ "tsup": "^8.5.1",
41
+ "typescript": "^5.9.3"
42
+ }
43
+ }