@camox/cli 0.5.2 → 0.6.1
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.mjs +33 -17
- package/package.json +2 -2
package/dist/index.mjs
CHANGED
|
@@ -30,23 +30,37 @@ const CAMOX_API_URL = process.env.CAMOX_API_URL || "https://api.camox.ai";
|
|
|
30
30
|
const AUTH_TIMEOUT_MS = 300 * 1e3;
|
|
31
31
|
const AUTH_DIR = path.join(os.homedir(), ".camox");
|
|
32
32
|
const AUTH_FILE = path.join(AUTH_DIR, "auth.json");
|
|
33
|
-
function
|
|
33
|
+
function normalizeUrl(url) {
|
|
34
|
+
return url.replace(/\/+$/, "");
|
|
35
|
+
}
|
|
36
|
+
function readAllTokens() {
|
|
34
37
|
try {
|
|
35
|
-
|
|
36
|
-
if (data?.token && data?.name) return data;
|
|
37
|
-
return null;
|
|
38
|
+
return JSON.parse(fs.readFileSync(AUTH_FILE, "utf-8"));
|
|
38
39
|
} catch {
|
|
39
|
-
return
|
|
40
|
+
return {};
|
|
40
41
|
}
|
|
41
42
|
}
|
|
42
|
-
function
|
|
43
|
+
function writeAllTokens(tokens) {
|
|
43
44
|
fs.mkdirSync(AUTH_DIR, { recursive: true });
|
|
44
|
-
fs.writeFileSync(AUTH_FILE, JSON.stringify(
|
|
45
|
+
fs.writeFileSync(AUTH_FILE, JSON.stringify(tokens, null, 2), { mode: 384 });
|
|
46
|
+
}
|
|
47
|
+
function readAuthToken() {
|
|
48
|
+
const entry = readAllTokens()[normalizeUrl(CAMOX_URL)];
|
|
49
|
+
if (entry?.token && entry?.name) return entry;
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
function writeAuthToken(token) {
|
|
53
|
+
const tokens = readAllTokens();
|
|
54
|
+
tokens[normalizeUrl(CAMOX_URL)] = token;
|
|
55
|
+
writeAllTokens(tokens);
|
|
45
56
|
}
|
|
46
57
|
function removeAuthToken() {
|
|
47
|
-
|
|
58
|
+
const tokens = readAllTokens();
|
|
59
|
+
delete tokens[normalizeUrl(CAMOX_URL)];
|
|
60
|
+
if (Object.keys(tokens).length === 0) try {
|
|
48
61
|
fs.unlinkSync(AUTH_FILE);
|
|
49
62
|
} catch {}
|
|
63
|
+
else writeAllTokens(tokens);
|
|
50
64
|
}
|
|
51
65
|
function openBrowser(url) {
|
|
52
66
|
const cmd = process.platform === "darwin" ? "open" : process.platform === "win32" ? "start" : "xdg-open";
|
|
@@ -112,23 +126,25 @@ async function verifyOtt(token) {
|
|
|
112
126
|
}
|
|
113
127
|
async function authenticateUser() {
|
|
114
128
|
const { port, ottPromise, close } = await startCallbackServer();
|
|
115
|
-
const loginUrl = `${CAMOX_URL}/cli-authorize?callback=${encodeURIComponent(`http://localhost:${port}/callback`)}`;
|
|
116
129
|
const action = await p.select({
|
|
117
|
-
message: "
|
|
130
|
+
message: "Connect to Camox",
|
|
118
131
|
options: [{
|
|
119
|
-
value: "
|
|
120
|
-
label: "
|
|
132
|
+
value: "signup",
|
|
133
|
+
label: "Sign up in browser"
|
|
121
134
|
}, {
|
|
122
|
-
value: "
|
|
123
|
-
label: "
|
|
135
|
+
value: "login",
|
|
136
|
+
label: "Log in in browser"
|
|
124
137
|
}]
|
|
125
138
|
});
|
|
126
139
|
if (p.isCancel(action)) {
|
|
127
140
|
close();
|
|
128
141
|
throw new Error("Authentication cancelled");
|
|
129
142
|
}
|
|
130
|
-
|
|
131
|
-
|
|
143
|
+
const callbackUrl = `http://localhost:${port}/callback`;
|
|
144
|
+
const cliAuthUrl = `/cli-authorize?callback=${encodeURIComponent(callbackUrl)}`;
|
|
145
|
+
const url = `${CAMOX_URL}${action === "signup" ? "/signup" : "/login"}?redirect=${encodeURIComponent(cliAuthUrl)}`;
|
|
146
|
+
openBrowser(url);
|
|
147
|
+
p.log.info(`Browser not opening? Visit:\n${url}`);
|
|
132
148
|
const s = p.spinner();
|
|
133
149
|
s.start("Waiting for authentication...");
|
|
134
150
|
try {
|
|
@@ -163,7 +179,7 @@ async function getOrAuthenticate() {
|
|
|
163
179
|
}
|
|
164
180
|
removeAuthToken();
|
|
165
181
|
p.log.warn("Session expired. Please log in again.");
|
|
166
|
-
} else p.log.info("Please
|
|
182
|
+
} else p.log.info("Please connect to Camox so we can create your project on the Camox API.");
|
|
167
183
|
return authenticateUser();
|
|
168
184
|
}
|
|
169
185
|
//#endregion
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@camox/cli",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.1",
|
|
4
4
|
"bin": {
|
|
5
5
|
"camox": "./dist/index.mjs"
|
|
6
6
|
},
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
"@typescript/native-preview": "7.0.0-dev.20260412.1",
|
|
26
26
|
"oxlint": "^0.15.0",
|
|
27
27
|
"tsdown": "^0.21.8",
|
|
28
|
-
"@camox/api": "0.
|
|
28
|
+
"@camox/api": "0.6.1"
|
|
29
29
|
},
|
|
30
30
|
"scripts": {
|
|
31
31
|
"build": "tsdown",
|