@optima-chat/browser-cli 0.1.4 → 0.1.6
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 +71 -14
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,29 +1,82 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
3
|
// src/index.ts
|
|
4
|
+
import { createRequire } from "module";
|
|
4
5
|
import { Command } from "commander";
|
|
5
6
|
|
|
6
7
|
// src/config.ts
|
|
7
8
|
import { readFileSync, existsSync } from "fs";
|
|
8
9
|
import { join } from "path";
|
|
9
10
|
import { homedir } from "os";
|
|
10
|
-
var
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
11
|
+
var ENV_CONFIG = {
|
|
12
|
+
production: {
|
|
13
|
+
apiUrl: "https://browser-api.optima.onl"
|
|
14
|
+
},
|
|
15
|
+
stage: {
|
|
16
|
+
apiUrl: "https://browser-api.stage.optima.onl"
|
|
17
|
+
},
|
|
18
|
+
development: {
|
|
19
|
+
apiUrl: "http://localhost:7292"
|
|
20
|
+
}
|
|
15
21
|
};
|
|
16
|
-
|
|
17
|
-
|
|
22
|
+
var OPTIMA_TOKEN_FILE = join(homedir(), ".optima", "token.json");
|
|
23
|
+
function tokenEnvToBrowserEnv(tokenEnv) {
|
|
24
|
+
switch (tokenEnv) {
|
|
25
|
+
case "prod":
|
|
26
|
+
case "production":
|
|
27
|
+
return "production";
|
|
28
|
+
case "stage":
|
|
29
|
+
return "stage";
|
|
30
|
+
case "ci":
|
|
31
|
+
case "development":
|
|
32
|
+
return "development";
|
|
33
|
+
default:
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
function readOptimaToken() {
|
|
38
|
+
if (!existsSync(OPTIMA_TOKEN_FILE)) return null;
|
|
18
39
|
try {
|
|
19
|
-
return JSON.parse(readFileSync(
|
|
40
|
+
return JSON.parse(readFileSync(OPTIMA_TOKEN_FILE, "utf-8"));
|
|
20
41
|
} catch {
|
|
21
42
|
return null;
|
|
22
43
|
}
|
|
23
44
|
}
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
45
|
+
function getCurrentEnv() {
|
|
46
|
+
const browserEnv = process.env.BROWSER_ENV;
|
|
47
|
+
if (browserEnv && ["production", "stage", "development"].includes(browserEnv)) {
|
|
48
|
+
return browserEnv;
|
|
49
|
+
}
|
|
50
|
+
const optimaEnv = process.env.OPTIMA_ENV;
|
|
51
|
+
if (optimaEnv && ["production", "stage", "development"].includes(optimaEnv)) {
|
|
52
|
+
return optimaEnv;
|
|
53
|
+
}
|
|
54
|
+
const optimaToken = readOptimaToken();
|
|
55
|
+
if (optimaToken?.env) {
|
|
56
|
+
const mapped = tokenEnvToBrowserEnv(optimaToken.env);
|
|
57
|
+
if (mapped) return mapped;
|
|
58
|
+
}
|
|
59
|
+
return "development";
|
|
60
|
+
}
|
|
61
|
+
function loadConfig() {
|
|
62
|
+
const env = getCurrentEnv();
|
|
63
|
+
const envConfig = ENV_CONFIG[env];
|
|
64
|
+
let token = process.env.BROWSER_TOKEN || null;
|
|
65
|
+
if (!token) {
|
|
66
|
+
const optimaToken = readOptimaToken();
|
|
67
|
+
if (optimaToken?.access_token) {
|
|
68
|
+
token = optimaToken.access_token;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return {
|
|
72
|
+
apiUrl: process.env.BROWSER_API_URL || envConfig.apiUrl,
|
|
73
|
+
token,
|
|
74
|
+
env
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
var config = loadConfig();
|
|
78
|
+
var BROWSER_API_URL = config.apiUrl;
|
|
79
|
+
var ACCESS_TOKEN = config.token;
|
|
27
80
|
|
|
28
81
|
// src/api.ts
|
|
29
82
|
var BrowserAPIError = class extends Error {
|
|
@@ -62,7 +115,8 @@ async function request(path, options = {}) {
|
|
|
62
115
|
`HTTP ${response.status}: ${response.statusText}`
|
|
63
116
|
);
|
|
64
117
|
}
|
|
65
|
-
|
|
118
|
+
const detail = body.detail || body;
|
|
119
|
+
throw new BrowserAPIError(response.status, detail.error, detail.message);
|
|
66
120
|
}
|
|
67
121
|
return await response.json();
|
|
68
122
|
}
|
|
@@ -91,7 +145,8 @@ async function requestBinary(path) {
|
|
|
91
145
|
`HTTP ${response.status}: ${response.statusText}`
|
|
92
146
|
);
|
|
93
147
|
}
|
|
94
|
-
|
|
148
|
+
const detail = body.detail || body;
|
|
149
|
+
throw new BrowserAPIError(response.status, detail.error, detail.message);
|
|
95
150
|
}
|
|
96
151
|
const arrayBuffer = await response.arrayBuffer();
|
|
97
152
|
return Buffer.from(arrayBuffer);
|
|
@@ -507,8 +562,10 @@ function registerTabCommands(program2) {
|
|
|
507
562
|
}
|
|
508
563
|
|
|
509
564
|
// src/index.ts
|
|
565
|
+
var require2 = createRequire(import.meta.url);
|
|
566
|
+
var { version } = require2("../package.json");
|
|
510
567
|
var program = new Command();
|
|
511
|
-
program.name("browser-cli").description("CLI client for browser-backend").version(
|
|
568
|
+
program.name("browser-cli").description("CLI client for browser-backend").version(version);
|
|
512
569
|
registerLifecycleCommands(program);
|
|
513
570
|
registerNavigationCommands(program);
|
|
514
571
|
registerInteractionCommands(program);
|