@emit-vision/cli 0.2.7 → 0.3.0
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/README.md +33 -1
- package/dist/index.js +507 -156
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -29,6 +29,37 @@ Point at a self-hosted instance with `--api-url`:
|
|
|
29
29
|
emit-vision login --token <pat> --api-url https://api.your-host.com
|
|
30
30
|
```
|
|
31
31
|
|
|
32
|
+
## Multiple accounts
|
|
33
|
+
|
|
34
|
+
Each login is stored as a named profile, so one machine can talk to several accounts or instances.
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
emit-vision login --profile work --token <pat> # save a token under "work"
|
|
38
|
+
emit-vision profile list # * marks the profile in use here
|
|
39
|
+
emit-vision profile use work # set the current profile
|
|
40
|
+
emit-vision status --profile personal # --profile works on any command
|
|
41
|
+
emit-vision logout --profile work # remove one profile
|
|
42
|
+
emit-vision logout --all # remove every profile
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
A repo can pin its own account and default project with a `.emit-vision.json` at the repo root:
|
|
46
|
+
|
|
47
|
+
```json
|
|
48
|
+
{ "profile": "work", "projectId": "proj_abc123" }
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
This file is safe to commit. Never put a token in it; the CLI refuses to read it if it contains `pat`, `token`, or `apiKey`. With `projectId` set, commands that take a single `[projectId]` argument (`overview`, `events recent`, `errors recent|grouped`, `logs`, `key get|rotate`, `users list`, `audience export|sync`, `flags list|evaluate`, and `pulse --project`) can omit it. An explicit ID always wins. `flags` mutations and `project env create` still require an explicit project ID.
|
|
52
|
+
|
|
53
|
+
The account is chosen in this order, first match wins:
|
|
54
|
+
|
|
55
|
+
1. `--profile <name>`
|
|
56
|
+
2. `EMIT_VISION_PAT` (use this token directly)
|
|
57
|
+
3. `EMIT_VISION_PROFILE`
|
|
58
|
+
4. `.emit-vision.json` `profile`
|
|
59
|
+
5. the current profile (`profile use`)
|
|
60
|
+
|
|
61
|
+
`EMIT_VISION_API_URL` sets the API URL when using `EMIT_VISION_PAT`, and `EMIT_VISION_CONFIG_DIR` changes the config directory (default `~/.emit-vision`).
|
|
62
|
+
|
|
32
63
|
## Feature flags
|
|
33
64
|
|
|
34
65
|
```bash
|
|
@@ -54,11 +85,12 @@ cat flag.json | emit-vision flags create <projectId> --from-json -
|
|
|
54
85
|
```bash
|
|
55
86
|
emit-vision overview <projectId>
|
|
56
87
|
emit-vision events recent <projectId> --limit 20
|
|
88
|
+
emit-vision events recent <projectId> --system
|
|
57
89
|
emit-vision errors grouped <projectId> --environment production
|
|
58
90
|
emit-vision logs <projectId> --severity error
|
|
59
91
|
```
|
|
60
92
|
|
|
61
|
-
Most commands accept `--from`/`--to` (ISO timestamps), `--environment`, `--limit`, and `--json`.
|
|
93
|
+
Most commands accept `--from`/`--to` (ISO timestamps), `--environment`, `--limit`, and `--json`. `events recent` also accepts `--system` to show system/heartbeat traffic instead of regular events.
|
|
62
94
|
|
|
63
95
|
## Projects and keys
|
|
64
96
|
|
package/dist/index.js
CHANGED
|
@@ -7,30 +7,214 @@ import { exec } from "child_process";
|
|
|
7
7
|
// src/config.ts
|
|
8
8
|
import { homedir } from "os";
|
|
9
9
|
import { join } from "path";
|
|
10
|
-
import {
|
|
10
|
+
import {
|
|
11
|
+
readFileSync,
|
|
12
|
+
writeFileSync,
|
|
13
|
+
mkdirSync,
|
|
14
|
+
existsSync,
|
|
15
|
+
chmodSync
|
|
16
|
+
} from "fs";
|
|
17
|
+
|
|
18
|
+
// src/config-shape.ts
|
|
19
|
+
var DEFAULT_PROFILE_NAME = "default";
|
|
20
|
+
var PROFILE_NAME_PATTERN = /^[a-z0-9][a-z0-9_-]{0,39}$/;
|
|
21
|
+
function validateProfileName(name) {
|
|
22
|
+
if (!PROFILE_NAME_PATTERN.test(name)) {
|
|
23
|
+
throw new Error(
|
|
24
|
+
`Invalid profile name "${name}". Use 1-40 lowercase letters, digits, "-" or "_", starting with a letter or digit.`
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
function isRecord(value) {
|
|
29
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
30
|
+
}
|
|
31
|
+
function normalizeConfig(raw) {
|
|
32
|
+
const config = normalizeProfiles(raw);
|
|
33
|
+
if (isRecord(raw) && typeof raw.projectId === "string" && raw.projectId) {
|
|
34
|
+
config.projectId = raw.projectId;
|
|
35
|
+
}
|
|
36
|
+
return config;
|
|
37
|
+
}
|
|
38
|
+
function normalizeProfiles(raw) {
|
|
39
|
+
if (!isRecord(raw)) return { profiles: {} };
|
|
40
|
+
if (isRecord(raw.profiles)) {
|
|
41
|
+
const profiles = {};
|
|
42
|
+
for (const [name, value] of Object.entries(raw.profiles)) {
|
|
43
|
+
if (isRecord(value)) profiles[name] = value;
|
|
44
|
+
}
|
|
45
|
+
const currentProfile = typeof raw.currentProfile === "string" ? raw.currentProfile : void 0;
|
|
46
|
+
return { currentProfile, profiles };
|
|
47
|
+
}
|
|
48
|
+
const pat = typeof raw.pat === "string" ? raw.pat : void 0;
|
|
49
|
+
const apiUrl = typeof raw.apiUrl === "string" ? raw.apiUrl : void 0;
|
|
50
|
+
if (!pat && !apiUrl) return { profiles: {} };
|
|
51
|
+
return {
|
|
52
|
+
currentProfile: DEFAULT_PROFILE_NAME,
|
|
53
|
+
profiles: { [DEFAULT_PROFILE_NAME]: { apiUrl, pat } }
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// src/config.ts
|
|
11
58
|
var DEFAULT_API_URL = "https://api.emitvision.com";
|
|
12
|
-
|
|
13
|
-
|
|
59
|
+
function configDir() {
|
|
60
|
+
return process.env.EMIT_VISION_CONFIG_DIR || join(homedir(), ".emit-vision");
|
|
61
|
+
}
|
|
62
|
+
function configPath() {
|
|
63
|
+
return join(configDir(), "config.json");
|
|
64
|
+
}
|
|
14
65
|
function loadConfig() {
|
|
15
|
-
|
|
66
|
+
const path = configPath();
|
|
67
|
+
if (!existsSync(path)) return { profiles: {} };
|
|
16
68
|
try {
|
|
17
|
-
return JSON.parse(readFileSync(
|
|
69
|
+
return normalizeConfig(JSON.parse(readFileSync(path, "utf-8")));
|
|
18
70
|
} catch {
|
|
19
|
-
return {};
|
|
71
|
+
return { profiles: {} };
|
|
20
72
|
}
|
|
21
73
|
}
|
|
22
|
-
function
|
|
23
|
-
mkdirSync(
|
|
24
|
-
const
|
|
25
|
-
writeFileSync(
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
);
|
|
74
|
+
function writeConfig(config) {
|
|
75
|
+
mkdirSync(configDir(), { recursive: true });
|
|
76
|
+
const path = configPath();
|
|
77
|
+
writeFileSync(path, JSON.stringify(config, null, 2), {
|
|
78
|
+
encoding: "utf-8",
|
|
79
|
+
mode: 384
|
|
80
|
+
});
|
|
81
|
+
chmodSync(path, 384);
|
|
82
|
+
}
|
|
83
|
+
function saveProfile(name, profile) {
|
|
84
|
+
validateProfileName(name);
|
|
85
|
+
const config = loadConfig();
|
|
86
|
+
const entry = { pat: profile.pat };
|
|
87
|
+
if (profile.apiUrl) entry.apiUrl = profile.apiUrl;
|
|
88
|
+
config.profiles[name] = entry;
|
|
89
|
+
if (!config.currentProfile || !config.profiles[config.currentProfile]) {
|
|
90
|
+
config.currentProfile = name;
|
|
91
|
+
}
|
|
92
|
+
writeConfig(config);
|
|
93
|
+
}
|
|
94
|
+
function removeProfile(name) {
|
|
95
|
+
const config = loadConfig();
|
|
96
|
+
if (!config.profiles[name]) return false;
|
|
97
|
+
delete config.profiles[name];
|
|
98
|
+
if (config.currentProfile === name) delete config.currentProfile;
|
|
99
|
+
writeConfig(config);
|
|
100
|
+
return true;
|
|
101
|
+
}
|
|
102
|
+
function setCurrentProfile(name) {
|
|
103
|
+
validateProfileName(name);
|
|
104
|
+
const config = loadConfig();
|
|
105
|
+
if (!config.profiles[name]) {
|
|
106
|
+
throw new Error(`Profile "${name}" not found.`);
|
|
107
|
+
}
|
|
108
|
+
config.currentProfile = name;
|
|
109
|
+
writeConfig(config);
|
|
30
110
|
}
|
|
31
111
|
function clearConfig() {
|
|
32
|
-
|
|
33
|
-
|
|
112
|
+
writeConfig({ profiles: {} });
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// src/repo-config.ts
|
|
116
|
+
import { existsSync as existsSync2, readFileSync as readFileSync2 } from "fs";
|
|
117
|
+
import { dirname, join as join2, resolve } from "path";
|
|
118
|
+
var REPO_CONFIG_FILENAME = ".emit-vision.json";
|
|
119
|
+
var SECRET_KEYS = ["pat", "token", "apiKey"];
|
|
120
|
+
function parseRepoConfig(path) {
|
|
121
|
+
let raw;
|
|
122
|
+
try {
|
|
123
|
+
raw = JSON.parse(readFileSync2(path, "utf-8"));
|
|
124
|
+
} catch (err) {
|
|
125
|
+
throw new Error(
|
|
126
|
+
`Could not parse ${path}: ${err instanceof Error ? err.message : String(err)}`
|
|
127
|
+
);
|
|
128
|
+
}
|
|
129
|
+
if (typeof raw !== "object" || raw === null || Array.isArray(raw)) {
|
|
130
|
+
throw new Error(`${path} must contain a JSON object.`);
|
|
131
|
+
}
|
|
132
|
+
const record = raw;
|
|
133
|
+
const secret = SECRET_KEYS.find((key) => key in record);
|
|
134
|
+
if (secret) {
|
|
135
|
+
throw new Error(
|
|
136
|
+
`${path} contains a "${secret}" key. This file is meant to be committed, so it must not hold a token. Run \`emit-vision login --profile <name>\` to store the token, then delete "${secret}" from the file.`
|
|
137
|
+
);
|
|
138
|
+
}
|
|
139
|
+
const config = { path };
|
|
140
|
+
if (typeof record.profile === "string" && record.profile) {
|
|
141
|
+
config.profile = record.profile;
|
|
142
|
+
}
|
|
143
|
+
if (typeof record.projectId === "string" && record.projectId) {
|
|
144
|
+
config.projectId = record.projectId;
|
|
145
|
+
}
|
|
146
|
+
return config;
|
|
147
|
+
}
|
|
148
|
+
function findRepoConfig(startDir = process.cwd()) {
|
|
149
|
+
let dir = resolve(startDir);
|
|
150
|
+
for (; ; ) {
|
|
151
|
+
const candidate = join2(dir, REPO_CONFIG_FILENAME);
|
|
152
|
+
if (existsSync2(candidate)) return parseRepoConfig(candidate);
|
|
153
|
+
if (existsSync2(join2(dir, ".git"))) return null;
|
|
154
|
+
const parent = dirname(dir);
|
|
155
|
+
if (parent === dir) return null;
|
|
156
|
+
dir = parent;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// src/credentials.ts
|
|
161
|
+
var activeProfileFlag;
|
|
162
|
+
function setActiveProfileFlag(name) {
|
|
163
|
+
activeProfileFlag = name;
|
|
164
|
+
}
|
|
165
|
+
function resolveCredentials(options = {}) {
|
|
166
|
+
const env = options.env ?? process.env;
|
|
167
|
+
const profileFlag = options.profileFlag ?? activeProfileFlag;
|
|
168
|
+
const config = normalizeConfig(loadConfig());
|
|
169
|
+
const named = (name, source) => {
|
|
170
|
+
const profile = config.profiles[name];
|
|
171
|
+
if (!profile) {
|
|
172
|
+
throw new Error(
|
|
173
|
+
`Profile "${name}" not found. Run \`emit-vision login --profile ${name}\`.`
|
|
174
|
+
);
|
|
175
|
+
}
|
|
176
|
+
return {
|
|
177
|
+
pat: profile.pat,
|
|
178
|
+
apiUrl: profile.apiUrl ?? DEFAULT_API_URL,
|
|
179
|
+
profile: name,
|
|
180
|
+
source
|
|
181
|
+
};
|
|
182
|
+
};
|
|
183
|
+
if (profileFlag) return named(profileFlag, "flag");
|
|
184
|
+
if (env.EMIT_VISION_PAT) {
|
|
185
|
+
const current = config.currentProfile ? config.profiles[config.currentProfile] : void 0;
|
|
186
|
+
return {
|
|
187
|
+
pat: env.EMIT_VISION_PAT,
|
|
188
|
+
apiUrl: env.EMIT_VISION_API_URL || current?.apiUrl || DEFAULT_API_URL,
|
|
189
|
+
source: "env-pat"
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
if (env.EMIT_VISION_PROFILE) {
|
|
193
|
+
return named(env.EMIT_VISION_PROFILE, "env-profile");
|
|
194
|
+
}
|
|
195
|
+
const repo = findRepoConfig(options.repoStartDir);
|
|
196
|
+
if (repo?.profile) {
|
|
197
|
+
if (!config.profiles[repo.profile]) {
|
|
198
|
+
throw new Error(
|
|
199
|
+
`This repo (${repo.path}) uses profile "${repo.profile}", which isn't logged in here. Run \`emit-vision login --profile ${repo.profile}\`.`
|
|
200
|
+
);
|
|
201
|
+
}
|
|
202
|
+
return { ...named(repo.profile, "repo"), repoConfigPath: repo.path };
|
|
203
|
+
}
|
|
204
|
+
if (config.currentProfile && config.profiles[config.currentProfile]) {
|
|
205
|
+
return named(config.currentProfile, "current");
|
|
206
|
+
}
|
|
207
|
+
return {
|
|
208
|
+
apiUrl: DEFAULT_API_URL,
|
|
209
|
+
profile: config.currentProfile ?? DEFAULT_PROFILE_NAME,
|
|
210
|
+
source: "default"
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
function targetProfileName() {
|
|
214
|
+
if (activeProfileFlag) return activeProfileFlag;
|
|
215
|
+
const env = process.env.EMIT_VISION_PROFILE;
|
|
216
|
+
if (env) return env;
|
|
217
|
+
return normalizeConfig(loadConfig()).currentProfile ?? DEFAULT_PROFILE_NAME;
|
|
34
218
|
}
|
|
35
219
|
|
|
36
220
|
// src/api.ts
|
|
@@ -71,9 +255,10 @@ function assertShape(data, expect, path) {
|
|
|
71
255
|
}
|
|
72
256
|
}
|
|
73
257
|
async function apiFetch(path, options = {}) {
|
|
74
|
-
const
|
|
75
|
-
const
|
|
76
|
-
const
|
|
258
|
+
const needsResolve = !options.anonymous && (options.pat === void 0 || options.apiUrl === void 0);
|
|
259
|
+
const resolved = needsResolve ? resolveCredentials() : void 0;
|
|
260
|
+
const apiUrl = options.apiUrl ?? resolved?.apiUrl ?? DEFAULT_API_URL;
|
|
261
|
+
const pat = options.pat ?? resolved?.pat;
|
|
77
262
|
const headers = {};
|
|
78
263
|
if (options.body !== void 0) headers["Content-Type"] = "application/json";
|
|
79
264
|
if (pat) headers["Authorization"] = `Bearer ${pat}`;
|
|
@@ -126,26 +311,32 @@ async function runLogin(args) {
|
|
|
126
311
|
if (apiUrlIdx !== -1 && args[apiUrlIdx + 1]) {
|
|
127
312
|
apiUrl = args[apiUrlIdx + 1];
|
|
128
313
|
}
|
|
314
|
+
const profile = targetProfileName();
|
|
315
|
+
apiUrl ??= loadConfig().profiles[profile]?.apiUrl ?? DEFAULT_API_URL;
|
|
129
316
|
const tokenFlagIdx = args.indexOf("--token");
|
|
130
317
|
const token = (tokenFlagIdx !== -1 ? args[tokenFlagIdx + 1] : void 0) ?? process.env.EMIT_VISION_PAT;
|
|
131
318
|
if (token) {
|
|
132
|
-
await runDirectLogin(token, apiUrl);
|
|
319
|
+
await runDirectLogin(token, apiUrl, profile);
|
|
133
320
|
return;
|
|
134
321
|
}
|
|
135
322
|
const force = args.includes("--yes") || args.includes("-y");
|
|
136
323
|
if (args.includes("--browser")) {
|
|
137
|
-
await runBrowserLogin(apiUrl, force);
|
|
324
|
+
await runBrowserLogin(apiUrl, profile, force);
|
|
138
325
|
return;
|
|
139
326
|
}
|
|
140
|
-
await runPasteLogin(apiUrl);
|
|
327
|
+
await runPasteLogin(apiUrl, profile);
|
|
328
|
+
}
|
|
329
|
+
function persistLogin(pat, apiUrl, profile) {
|
|
330
|
+
saveProfile(profile, {
|
|
331
|
+
pat,
|
|
332
|
+
apiUrl: apiUrl === DEFAULT_API_URL ? void 0 : apiUrl
|
|
333
|
+
});
|
|
334
|
+
console.log(`Logged in successfully (profile: ${profile}).`);
|
|
141
335
|
}
|
|
142
|
-
async function runDirectLogin(pat, apiUrl) {
|
|
336
|
+
async function runDirectLogin(pat, apiUrl, profile) {
|
|
143
337
|
try {
|
|
144
338
|
await apiFetch("/v1/management/projects", { pat, apiUrl });
|
|
145
|
-
|
|
146
|
-
if (apiUrl) patch.apiUrl = apiUrl;
|
|
147
|
-
saveConfig(patch);
|
|
148
|
-
console.log("Logged in successfully.");
|
|
339
|
+
persistLogin(pat, apiUrl, profile);
|
|
149
340
|
} catch (err) {
|
|
150
341
|
console.error(
|
|
151
342
|
`Login failed: ${err instanceof Error ? err.message : String(err)}`
|
|
@@ -153,7 +344,7 @@ async function runDirectLogin(pat, apiUrl) {
|
|
|
153
344
|
process.exit(1);
|
|
154
345
|
}
|
|
155
346
|
}
|
|
156
|
-
async function runPasteLogin(apiUrl) {
|
|
347
|
+
async function runPasteLogin(apiUrl, profile) {
|
|
157
348
|
if (!process.stdin.isTTY) {
|
|
158
349
|
console.error(
|
|
159
350
|
"stdin is not a terminal.\nPass --token <pat> or set EMIT_VISION_PAT=<token> to log in non-interactively."
|
|
@@ -161,10 +352,10 @@ async function runPasteLogin(apiUrl) {
|
|
|
161
352
|
process.exit(1);
|
|
162
353
|
}
|
|
163
354
|
const rl = createInterface({ input: process.stdin, output: process.stdout });
|
|
164
|
-
const pat = await new Promise((
|
|
355
|
+
const pat = await new Promise((resolve2) => {
|
|
165
356
|
rl.question("Paste your Personal Access Token: ", (answer) => {
|
|
166
357
|
rl.close();
|
|
167
|
-
|
|
358
|
+
resolve2(answer.trim());
|
|
168
359
|
});
|
|
169
360
|
});
|
|
170
361
|
if (!pat) {
|
|
@@ -173,10 +364,7 @@ async function runPasteLogin(apiUrl) {
|
|
|
173
364
|
}
|
|
174
365
|
try {
|
|
175
366
|
await apiFetch("/v1/management/projects", { pat, apiUrl });
|
|
176
|
-
|
|
177
|
-
if (apiUrl) patch.apiUrl = apiUrl;
|
|
178
|
-
saveConfig(patch);
|
|
179
|
-
console.log("Logged in successfully.");
|
|
367
|
+
persistLogin(pat, apiUrl, profile);
|
|
180
368
|
} catch (err) {
|
|
181
369
|
console.error(
|
|
182
370
|
`Login failed: ${err instanceof Error ? err.message : String(err)}`
|
|
@@ -184,20 +372,23 @@ async function runPasteLogin(apiUrl) {
|
|
|
184
372
|
process.exit(1);
|
|
185
373
|
}
|
|
186
374
|
}
|
|
187
|
-
async function runBrowserLogin(apiUrl, force = false) {
|
|
188
|
-
const
|
|
189
|
-
if (
|
|
375
|
+
async function runBrowserLogin(apiUrl, profile, force = false) {
|
|
376
|
+
const existing = loadConfig().profiles[profile];
|
|
377
|
+
if (existing?.pat && !force) {
|
|
190
378
|
if (!process.stdin.isTTY) {
|
|
191
379
|
} else {
|
|
192
380
|
const rl = createInterface({
|
|
193
381
|
input: process.stdin,
|
|
194
382
|
output: process.stdout
|
|
195
383
|
});
|
|
196
|
-
const answer = await new Promise((
|
|
197
|
-
rl.question(
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
384
|
+
const answer = await new Promise((resolve2) => {
|
|
385
|
+
rl.question(
|
|
386
|
+
`Profile "${profile}" is already logged in. Overwrite? [y/N] `,
|
|
387
|
+
(a) => {
|
|
388
|
+
rl.close();
|
|
389
|
+
resolve2(a.trim().toLowerCase());
|
|
390
|
+
}
|
|
391
|
+
);
|
|
201
392
|
});
|
|
202
393
|
if (answer !== "y" && answer !== "yes") {
|
|
203
394
|
console.log("Aborted.");
|
|
@@ -209,7 +400,7 @@ async function runBrowserLogin(apiUrl, force = false) {
|
|
|
209
400
|
try {
|
|
210
401
|
initResult = await apiFetch(
|
|
211
402
|
"/v1/cli-auth/init",
|
|
212
|
-
{ method: "POST", apiUrl }
|
|
403
|
+
{ method: "POST", apiUrl, anonymous: true }
|
|
213
404
|
);
|
|
214
405
|
} catch (err) {
|
|
215
406
|
console.error(
|
|
@@ -242,7 +433,7 @@ async function runBrowserLogin(apiUrl, force = false) {
|
|
|
242
433
|
try {
|
|
243
434
|
pollResult = await apiFetch(
|
|
244
435
|
`/v1/cli-auth/poll/${encodeURIComponent(code)}`,
|
|
245
|
-
{ apiUrl }
|
|
436
|
+
{ apiUrl, anonymous: true }
|
|
246
437
|
);
|
|
247
438
|
} catch {
|
|
248
439
|
continue;
|
|
@@ -252,12 +443,7 @@ async function runBrowserLogin(apiUrl, force = false) {
|
|
|
252
443
|
console.error("Authorization succeeded but no token was returned.");
|
|
253
444
|
process.exit(1);
|
|
254
445
|
}
|
|
255
|
-
|
|
256
|
-
pat: pollResult.rawToken
|
|
257
|
-
};
|
|
258
|
-
if (apiUrl) patch.apiUrl = apiUrl;
|
|
259
|
-
saveConfig(patch);
|
|
260
|
-
console.log("Logged in successfully.");
|
|
446
|
+
persistLogin(pollResult.rawToken, apiUrl, profile);
|
|
261
447
|
return;
|
|
262
448
|
}
|
|
263
449
|
if (pollResult.status === "expired") {
|
|
@@ -277,13 +463,22 @@ function openBrowser(url) {
|
|
|
277
463
|
});
|
|
278
464
|
}
|
|
279
465
|
function sleep(ms) {
|
|
280
|
-
return new Promise((
|
|
466
|
+
return new Promise((resolve2) => setTimeout(resolve2, ms));
|
|
281
467
|
}
|
|
282
468
|
|
|
283
469
|
// src/commands/logout.ts
|
|
284
|
-
function runLogout() {
|
|
285
|
-
|
|
286
|
-
|
|
470
|
+
function runLogout(args = []) {
|
|
471
|
+
if (args.includes("--all")) {
|
|
472
|
+
clearConfig();
|
|
473
|
+
console.log("Logged out of all profiles.");
|
|
474
|
+
return;
|
|
475
|
+
}
|
|
476
|
+
const profile = targetProfileName();
|
|
477
|
+
if (removeProfile(profile)) {
|
|
478
|
+
console.log(`Logged out (profile: ${profile}).`);
|
|
479
|
+
} else {
|
|
480
|
+
console.log(`Profile "${profile}" is not logged in.`);
|
|
481
|
+
}
|
|
287
482
|
}
|
|
288
483
|
|
|
289
484
|
// src/lib/format-date.ts
|
|
@@ -389,11 +584,35 @@ function printTable(headers, rows) {
|
|
|
389
584
|
|
|
390
585
|
// src/commands/key.ts
|
|
391
586
|
import { createInterface as createInterface2 } from "readline";
|
|
587
|
+
|
|
588
|
+
// src/lib/project-arg.ts
|
|
589
|
+
var PROJECT_ID_TIP = 'Tip: add "projectId" to .emit-vision.json to skip this argument.';
|
|
590
|
+
function repoProjectId(findConfig) {
|
|
591
|
+
return findConfig()?.projectId;
|
|
592
|
+
}
|
|
593
|
+
function withDefaultProjectId(args, findConfig = findRepoConfig) {
|
|
594
|
+
const first = args[0];
|
|
595
|
+
if (first && !first.startsWith("--")) return args;
|
|
596
|
+
const projectId = repoProjectId(findConfig);
|
|
597
|
+
return projectId ? [projectId, ...args] : args;
|
|
598
|
+
}
|
|
599
|
+
function withDefaultProjectFlag(args, findConfig = findRepoConfig) {
|
|
600
|
+
const idx = args.indexOf("--project");
|
|
601
|
+
if (idx !== -1 && args[idx + 1]) return args;
|
|
602
|
+
const projectId = repoProjectId(findConfig);
|
|
603
|
+
return projectId ? [...args, "--project", projectId] : args;
|
|
604
|
+
}
|
|
605
|
+
function exitMissingProjectId(usage) {
|
|
606
|
+
console.error(usage);
|
|
607
|
+
console.error(PROJECT_ID_TIP);
|
|
608
|
+
process.exit(1);
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
// src/commands/key.ts
|
|
392
612
|
async function runKeyGet(args) {
|
|
393
|
-
const projectId = args[0];
|
|
613
|
+
const projectId = withDefaultProjectId(args)[0];
|
|
394
614
|
if (!projectId) {
|
|
395
|
-
|
|
396
|
-
process.exit(1);
|
|
615
|
+
exitMissingProjectId("Usage: emit-vision key get [projectId]");
|
|
397
616
|
}
|
|
398
617
|
const key = await apiFetch(
|
|
399
618
|
`/v1/management/projects/${encodeURIComponent(projectId)}/keys`
|
|
@@ -405,10 +624,9 @@ async function runKeyGet(args) {
|
|
|
405
624
|
);
|
|
406
625
|
}
|
|
407
626
|
async function runKeyRotate(args) {
|
|
408
|
-
const projectId = args[0];
|
|
627
|
+
const projectId = withDefaultProjectId(args)[0];
|
|
409
628
|
if (!projectId) {
|
|
410
|
-
|
|
411
|
-
process.exit(1);
|
|
629
|
+
exitMissingProjectId("Usage: emit-vision key rotate [projectId] [--yes]");
|
|
412
630
|
}
|
|
413
631
|
const confirmed = args.includes("--yes") || args.includes("-y");
|
|
414
632
|
if (!confirmed) {
|
|
@@ -422,12 +640,12 @@ async function runKeyRotate(args) {
|
|
|
422
640
|
input: process.stdin,
|
|
423
641
|
output: process.stdout
|
|
424
642
|
});
|
|
425
|
-
const answer = await new Promise((
|
|
643
|
+
const answer = await new Promise((resolve2) => {
|
|
426
644
|
rl.question(
|
|
427
645
|
"Rotate the ingest key? This will invalidate the current key. [y/N] ",
|
|
428
646
|
(a) => {
|
|
429
647
|
rl.close();
|
|
430
|
-
|
|
648
|
+
resolve2(a.trim().toLowerCase());
|
|
431
649
|
}
|
|
432
650
|
);
|
|
433
651
|
});
|
|
@@ -460,6 +678,8 @@ function parseFlags(args) {
|
|
|
460
678
|
flags[arg.slice(2)] = args[++i];
|
|
461
679
|
} else if (arg === "--json") {
|
|
462
680
|
flags.json = "true";
|
|
681
|
+
} else if (arg === "--system") {
|
|
682
|
+
flags.system = "true";
|
|
463
683
|
}
|
|
464
684
|
}
|
|
465
685
|
return flags;
|
|
@@ -492,16 +712,16 @@ function truncate(value, max) {
|
|
|
492
712
|
return value.length > max ? `${value.slice(0, max - 3)}...` : value;
|
|
493
713
|
}
|
|
494
714
|
async function runEventsRecent(args) {
|
|
495
|
-
const [projectId, ...rest2] = args;
|
|
715
|
+
const [projectId, ...rest2] = withDefaultProjectId(args);
|
|
496
716
|
if (!projectId) {
|
|
497
|
-
|
|
498
|
-
"Usage: emit-vision events recent
|
|
717
|
+
exitMissingProjectId(
|
|
718
|
+
"Usage: emit-vision events recent [projectId] [--from <iso>] [--to <iso>] [--environment <env>] [--limit <n>] [--system] [--json]"
|
|
499
719
|
);
|
|
500
|
-
process.exit(1);
|
|
501
720
|
}
|
|
502
721
|
const flags = parseFlags(rest2);
|
|
722
|
+
const qs = buildQs(projectId, flags);
|
|
503
723
|
const events = await apiFetch(
|
|
504
|
-
`/v1/events/recent?${
|
|
724
|
+
`/v1/events/recent?${qs}${flags.system ? "&eventType=system" : ""}`,
|
|
505
725
|
{ expect: RECENT_FIELDS }
|
|
506
726
|
);
|
|
507
727
|
if (flags.json) {
|
|
@@ -523,12 +743,11 @@ async function runEventsRecent(args) {
|
|
|
523
743
|
);
|
|
524
744
|
}
|
|
525
745
|
async function runErrorsRecent(args) {
|
|
526
|
-
const [projectId, ...rest2] = args;
|
|
746
|
+
const [projectId, ...rest2] = withDefaultProjectId(args);
|
|
527
747
|
if (!projectId) {
|
|
528
|
-
|
|
529
|
-
"Usage: emit-vision errors recent
|
|
748
|
+
exitMissingProjectId(
|
|
749
|
+
"Usage: emit-vision errors recent [projectId] [--from <iso>] [--to <iso>] [--environment <env>] [--limit <n>] [--json]"
|
|
530
750
|
);
|
|
531
|
-
process.exit(1);
|
|
532
751
|
}
|
|
533
752
|
const flags = parseFlags(rest2);
|
|
534
753
|
const errors = await apiFetch(
|
|
@@ -554,12 +773,11 @@ async function runErrorsRecent(args) {
|
|
|
554
773
|
);
|
|
555
774
|
}
|
|
556
775
|
async function runErrorsGrouped(args) {
|
|
557
|
-
const [projectId, ...rest2] = args;
|
|
776
|
+
const [projectId, ...rest2] = withDefaultProjectId(args);
|
|
558
777
|
if (!projectId) {
|
|
559
|
-
|
|
560
|
-
"Usage: emit-vision errors grouped
|
|
778
|
+
exitMissingProjectId(
|
|
779
|
+
"Usage: emit-vision errors grouped [projectId] [--from <iso>] [--to <iso>] [--environment <env>] [--limit <n>] [--json]"
|
|
561
780
|
);
|
|
562
|
-
process.exit(1);
|
|
563
781
|
}
|
|
564
782
|
const flags = parseFlags(rest2);
|
|
565
783
|
const groups = await apiFetch(
|
|
@@ -585,12 +803,11 @@ async function runErrorsGrouped(args) {
|
|
|
585
803
|
);
|
|
586
804
|
}
|
|
587
805
|
async function runOverview(args) {
|
|
588
|
-
const [projectId, ...rest2] = args;
|
|
806
|
+
const [projectId, ...rest2] = withDefaultProjectId(args);
|
|
589
807
|
if (!projectId) {
|
|
590
|
-
|
|
591
|
-
"Usage: emit-vision overview
|
|
808
|
+
exitMissingProjectId(
|
|
809
|
+
"Usage: emit-vision overview [projectId] [--from <iso>] [--to <iso>] [--environment <env>] [--json]"
|
|
592
810
|
);
|
|
593
|
-
process.exit(1);
|
|
594
811
|
}
|
|
595
812
|
const flags = parseFlags(rest2);
|
|
596
813
|
const data = await apiFetch(
|
|
@@ -615,12 +832,11 @@ async function runOverview(args) {
|
|
|
615
832
|
}
|
|
616
833
|
}
|
|
617
834
|
async function runLogs(args) {
|
|
618
|
-
const [projectId, ...rest2] = args;
|
|
835
|
+
const [projectId, ...rest2] = withDefaultProjectId(args);
|
|
619
836
|
if (!projectId) {
|
|
620
|
-
|
|
621
|
-
"Usage: emit-vision logs
|
|
837
|
+
exitMissingProjectId(
|
|
838
|
+
"Usage: emit-vision logs [projectId] [--from <iso>] [--to <iso>] [--severity <level>] [--limit <n>] [--json]"
|
|
622
839
|
);
|
|
623
|
-
process.exit(1);
|
|
624
840
|
}
|
|
625
841
|
const flags = parseFlags(rest2);
|
|
626
842
|
const { logs } = await apiFetch(
|
|
@@ -646,13 +862,48 @@ async function runLogs(args) {
|
|
|
646
862
|
}
|
|
647
863
|
|
|
648
864
|
// src/commands/status.ts
|
|
865
|
+
function describeSource(creds) {
|
|
866
|
+
switch (creds.source) {
|
|
867
|
+
case "flag":
|
|
868
|
+
return "from --profile";
|
|
869
|
+
case "env-profile":
|
|
870
|
+
return "from EMIT_VISION_PROFILE";
|
|
871
|
+
case "repo":
|
|
872
|
+
return `from ${creds.repoConfigPath}`;
|
|
873
|
+
default:
|
|
874
|
+
return "current profile";
|
|
875
|
+
}
|
|
876
|
+
}
|
|
649
877
|
async function runStatus(args = []) {
|
|
650
|
-
const config = loadConfig();
|
|
651
|
-
const apiUrl = config.apiUrl ?? DEFAULT_API_URL;
|
|
652
878
|
const json = args.includes("--json");
|
|
879
|
+
let config;
|
|
880
|
+
try {
|
|
881
|
+
config = resolveCredentials();
|
|
882
|
+
} catch (err) {
|
|
883
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
884
|
+
if (json) {
|
|
885
|
+
console.log(
|
|
886
|
+
JSON.stringify({ tokenPrefix: null, valid: false, error: message })
|
|
887
|
+
);
|
|
888
|
+
} else {
|
|
889
|
+
console.log(message);
|
|
890
|
+
}
|
|
891
|
+
return;
|
|
892
|
+
}
|
|
893
|
+
const apiUrl = config.apiUrl;
|
|
894
|
+
const profile = config.profile ?? null;
|
|
895
|
+
const source = config.source;
|
|
653
896
|
if (!config.pat) {
|
|
654
897
|
if (json) {
|
|
655
|
-
console.log(
|
|
898
|
+
console.log(
|
|
899
|
+
JSON.stringify({
|
|
900
|
+
apiUrl,
|
|
901
|
+
tokenPrefix: null,
|
|
902
|
+
valid: false,
|
|
903
|
+
profile,
|
|
904
|
+
source
|
|
905
|
+
})
|
|
906
|
+
);
|
|
656
907
|
} else {
|
|
657
908
|
console.log("Not logged in.");
|
|
658
909
|
console.log(` API: ${apiUrl}`);
|
|
@@ -667,10 +918,19 @@ async function runStatus(args = []) {
|
|
|
667
918
|
} catch {
|
|
668
919
|
}
|
|
669
920
|
if (json) {
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
921
|
+
const out = {
|
|
922
|
+
apiUrl,
|
|
923
|
+
tokenPrefix: config.pat.slice(0, 12),
|
|
924
|
+
valid,
|
|
925
|
+
profile,
|
|
926
|
+
source
|
|
927
|
+
};
|
|
928
|
+
if (config.repoConfigPath) out.repoConfigPath = config.repoConfigPath;
|
|
929
|
+
console.log(JSON.stringify(out));
|
|
673
930
|
} else {
|
|
931
|
+
if (profile) {
|
|
932
|
+
console.log(` Profile: ${profile} (${describeSource(config)})`);
|
|
933
|
+
}
|
|
674
934
|
console.log(` API: ${apiUrl}`);
|
|
675
935
|
console.log(` Token: ${config.pat.slice(0, 12)}\u2026`);
|
|
676
936
|
console.log(
|
|
@@ -679,6 +939,53 @@ async function runStatus(args = []) {
|
|
|
679
939
|
}
|
|
680
940
|
}
|
|
681
941
|
|
|
942
|
+
// src/commands/profile.ts
|
|
943
|
+
function resolvedProfileName() {
|
|
944
|
+
try {
|
|
945
|
+
return resolveCredentials().profile;
|
|
946
|
+
} catch {
|
|
947
|
+
return void 0;
|
|
948
|
+
}
|
|
949
|
+
}
|
|
950
|
+
function runProfileList(args = []) {
|
|
951
|
+
const config = loadConfig();
|
|
952
|
+
const active = resolvedProfileName();
|
|
953
|
+
const rows = Object.entries(config.profiles).map(([name, profile]) => ({
|
|
954
|
+
name,
|
|
955
|
+
apiUrl: profile.apiUrl ?? DEFAULT_API_URL,
|
|
956
|
+
tokenPrefix: profile.pat ? profile.pat.slice(0, 12) : null,
|
|
957
|
+
active: name === active
|
|
958
|
+
}));
|
|
959
|
+
if (args.includes("--json")) {
|
|
960
|
+
console.log(JSON.stringify(rows));
|
|
961
|
+
return;
|
|
962
|
+
}
|
|
963
|
+
if (rows.length === 0) {
|
|
964
|
+
console.log("No profiles. Run `emit-vision login --profile <name>`.");
|
|
965
|
+
return;
|
|
966
|
+
}
|
|
967
|
+
for (const row of rows) {
|
|
968
|
+
const token = row.tokenPrefix ? `${row.tokenPrefix}\u2026` : "(no token)";
|
|
969
|
+
console.log(
|
|
970
|
+
`${row.active ? "*" : " "} ${row.name} ${row.apiUrl} ${token}`
|
|
971
|
+
);
|
|
972
|
+
}
|
|
973
|
+
}
|
|
974
|
+
function runProfileUse(args = []) {
|
|
975
|
+
const name = args[0];
|
|
976
|
+
if (!name) {
|
|
977
|
+
throw new Error("Usage: emit-vision profile use <name>");
|
|
978
|
+
}
|
|
979
|
+
setCurrentProfile(name);
|
|
980
|
+
console.log(`Current profile: ${name}`);
|
|
981
|
+
const repo = findRepoConfig();
|
|
982
|
+
if (repo?.profile && repo.profile !== name) {
|
|
983
|
+
console.log(
|
|
984
|
+
`Note: ${repo.path} pins this repo to "${repo.profile}", which takes precedence here.`
|
|
985
|
+
);
|
|
986
|
+
}
|
|
987
|
+
}
|
|
988
|
+
|
|
682
989
|
// src/commands/audience.ts
|
|
683
990
|
import { writeFileSync as writeFileSync2 } from "fs";
|
|
684
991
|
var VALUE_FLAGS = /* @__PURE__ */ new Set([
|
|
@@ -718,8 +1025,8 @@ function buildQs2(projectId, flags, extras) {
|
|
|
718
1025
|
return params.toString();
|
|
719
1026
|
}
|
|
720
1027
|
async function apiFetchText(path) {
|
|
721
|
-
const config =
|
|
722
|
-
const apiUrl = config.apiUrl
|
|
1028
|
+
const config = resolveCredentials();
|
|
1029
|
+
const apiUrl = config.apiUrl;
|
|
723
1030
|
const headers = {};
|
|
724
1031
|
if (config.pat) headers["Authorization"] = `Bearer ${config.pat}`;
|
|
725
1032
|
const res = await fetch(`${apiUrl}${path}`, { headers });
|
|
@@ -727,12 +1034,11 @@ async function apiFetchText(path) {
|
|
|
727
1034
|
return res.text();
|
|
728
1035
|
}
|
|
729
1036
|
async function runUsersList(args) {
|
|
730
|
-
const [projectId, ...rest2] = args;
|
|
1037
|
+
const [projectId, ...rest2] = withDefaultProjectId(args);
|
|
731
1038
|
if (!projectId) {
|
|
732
|
-
|
|
733
|
-
"Usage: emit-vision users list
|
|
1039
|
+
exitMissingProjectId(
|
|
1040
|
+
"Usage: emit-vision users list [projectId] [--limit <n>] [--json]"
|
|
734
1041
|
);
|
|
735
|
-
process.exit(1);
|
|
736
1042
|
}
|
|
737
1043
|
const flags = parseFlags2(rest2);
|
|
738
1044
|
const data = await apiFetch(
|
|
@@ -756,12 +1062,11 @@ async function runUsersList(args) {
|
|
|
756
1062
|
);
|
|
757
1063
|
}
|
|
758
1064
|
async function runAudienceExport(args) {
|
|
759
|
-
const [projectId, ...rest2] = args;
|
|
1065
|
+
const [projectId, ...rest2] = withDefaultProjectId(args);
|
|
760
1066
|
if (!projectId) {
|
|
761
|
-
|
|
762
|
-
"Usage: emit-vision audience export
|
|
1067
|
+
exitMissingProjectId(
|
|
1068
|
+
"Usage: emit-vision audience export [projectId] [--segment dormant|power-user|high-errors|new] [--format csv|json] [--limit <n>] [--output <file>]"
|
|
763
1069
|
);
|
|
764
|
-
process.exit(1);
|
|
765
1070
|
}
|
|
766
1071
|
const flags = parseFlags2(rest2);
|
|
767
1072
|
const format = flags.format === "csv" ? "csv" : "json";
|
|
@@ -804,12 +1109,11 @@ async function deleMailFetch(baseUrl, apiKey, path, body) {
|
|
|
804
1109
|
return res.json();
|
|
805
1110
|
}
|
|
806
1111
|
async function runAudienceSync(args) {
|
|
807
|
-
const [projectId, ...rest2] = args;
|
|
1112
|
+
const [projectId, ...rest2] = withDefaultProjectId(args);
|
|
808
1113
|
if (!projectId) {
|
|
809
|
-
|
|
810
|
-
"Usage: emit-vision audience sync
|
|
1114
|
+
exitMissingProjectId(
|
|
1115
|
+
"Usage: emit-vision audience sync [projectId] [--segment <name>] [--tag <tag>] [--develemail-url <url>] [--develemail-key <key>] [--develemail-project <id>] [--limit <n>] [--dry-run]"
|
|
811
1116
|
);
|
|
812
|
-
process.exit(1);
|
|
813
1117
|
}
|
|
814
1118
|
const flags = parseFlags2(rest2);
|
|
815
1119
|
const deleUrl = flags["develemail-url"] ?? process.env.DEVELEMAIL_BASE_URL;
|
|
@@ -957,13 +1261,14 @@ function parseFlag(args, flag) {
|
|
|
957
1261
|
function resolveProjectId(args) {
|
|
958
1262
|
const fromFlag = parseFlag(args, "--project");
|
|
959
1263
|
if (fromFlag) return fromFlag;
|
|
1264
|
+
const repoValue = parseFlag(withDefaultProjectFlag(args), "--project");
|
|
1265
|
+
if (repoValue) return repoValue;
|
|
960
1266
|
const config = loadConfig();
|
|
961
1267
|
const fromConfig = config.projectId;
|
|
962
1268
|
if (typeof fromConfig === "string" && fromConfig) return fromConfig;
|
|
963
|
-
|
|
964
|
-
"Missing --project flag. Usage: emit-vision pulse init --project <projectId> --name <name>"
|
|
1269
|
+
exitMissingProjectId(
|
|
1270
|
+
"Missing --project flag. Usage: emit-vision pulse init [--project <projectId>] --name <name>"
|
|
965
1271
|
);
|
|
966
|
-
process.exit(1);
|
|
967
1272
|
}
|
|
968
1273
|
async function runPulseInit(args) {
|
|
969
1274
|
const projectId = resolveProjectId(args);
|
|
@@ -995,8 +1300,7 @@ async function runPulseInit(args) {
|
|
|
995
1300
|
}
|
|
996
1301
|
}
|
|
997
1302
|
);
|
|
998
|
-
const
|
|
999
|
-
const apiUrl = config.apiUrl ?? "https://api.emitvision.com";
|
|
1303
|
+
const apiUrl = resolveCredentials().apiUrl;
|
|
1000
1304
|
const pingUrl = `${apiUrl}/v1/pulse/${check.slug}`;
|
|
1001
1305
|
console.log(`Check created: ${check.name}`);
|
|
1002
1306
|
console.log(` Slug: ${check.slug}`);
|
|
@@ -1135,12 +1439,11 @@ function repeatedValues(args, flag) {
|
|
|
1135
1439
|
return values;
|
|
1136
1440
|
}
|
|
1137
1441
|
async function runFlagsList(args) {
|
|
1138
|
-
const [projectId, ...rest2] = args;
|
|
1442
|
+
const [projectId, ...rest2] = withDefaultProjectId(args);
|
|
1139
1443
|
if (!projectId) {
|
|
1140
|
-
|
|
1141
|
-
"Usage: emit-vision flags list
|
|
1444
|
+
exitMissingProjectId(
|
|
1445
|
+
"Usage: emit-vision flags list [projectId] [--limit <n>] [--offset <n>] [--json]"
|
|
1142
1446
|
);
|
|
1143
|
-
process.exit(1);
|
|
1144
1447
|
}
|
|
1145
1448
|
const limit = valueOf(rest2, "--limit");
|
|
1146
1449
|
const offset = valueOf(rest2, "--offset");
|
|
@@ -1171,12 +1474,9 @@ async function runFlagsList(args) {
|
|
|
1171
1474
|
);
|
|
1172
1475
|
}
|
|
1173
1476
|
async function runFlagsEvaluate(args) {
|
|
1174
|
-
const usage = "Usage: emit-vision flags evaluate
|
|
1175
|
-
const [projectId, ...rest2] = args;
|
|
1176
|
-
if (!projectId)
|
|
1177
|
-
console.error(usage);
|
|
1178
|
-
process.exit(1);
|
|
1179
|
-
}
|
|
1477
|
+
const usage = "Usage: emit-vision flags evaluate [projectId] --environment <env> [--user-key <key>] [--flag <key>]... [--json]";
|
|
1478
|
+
const [projectId, ...rest2] = withDefaultProjectId(args);
|
|
1479
|
+
if (!projectId) exitMissingProjectId(usage);
|
|
1180
1480
|
const environment = valueOf(rest2, "--environment");
|
|
1181
1481
|
if (!environment) {
|
|
1182
1482
|
console.error(usage);
|
|
@@ -1310,12 +1610,12 @@ async function runFlagsArchive(args) {
|
|
|
1310
1610
|
input: process.stdin,
|
|
1311
1611
|
output: process.stdout
|
|
1312
1612
|
});
|
|
1313
|
-
const answer = await new Promise((
|
|
1613
|
+
const answer = await new Promise((resolve2) => {
|
|
1314
1614
|
rl.question(
|
|
1315
1615
|
`Archive flag "${flagKey}"? This cannot be undone. [y/N] `,
|
|
1316
1616
|
(a) => {
|
|
1317
1617
|
rl.close();
|
|
1318
|
-
|
|
1618
|
+
resolve2(a.trim().toLowerCase());
|
|
1319
1619
|
}
|
|
1320
1620
|
);
|
|
1321
1621
|
});
|
|
@@ -1337,7 +1637,7 @@ async function runFlagsArchive(args) {
|
|
|
1337
1637
|
}
|
|
1338
1638
|
|
|
1339
1639
|
// src/commands/flags-create.ts
|
|
1340
|
-
import { readFileSync as
|
|
1640
|
+
import { readFileSync as readFileSync3 } from "fs";
|
|
1341
1641
|
|
|
1342
1642
|
// src/commands/flags-validate.ts
|
|
1343
1643
|
var FLAG_KEY_RE = /^[a-z0-9_-]+$/;
|
|
@@ -1422,7 +1722,7 @@ function readJsonSource(source) {
|
|
|
1422
1722
|
const label = source === "-" ? "stdin" : source;
|
|
1423
1723
|
let raw;
|
|
1424
1724
|
try {
|
|
1425
|
-
raw = source === "-" ?
|
|
1725
|
+
raw = source === "-" ? readFileSync3(0, "utf-8") : readFileSync3(source, "utf-8");
|
|
1426
1726
|
} catch (err) {
|
|
1427
1727
|
console.error(
|
|
1428
1728
|
`Could not read ${label}: ${err instanceof Error ? err.message : String(err)}`
|
|
@@ -1566,7 +1866,7 @@ At least one of --name, --description, --environments, --default-variant is requ
|
|
|
1566
1866
|
// src/commands/migration-export.ts
|
|
1567
1867
|
import { mkdirSync as mkdirSync2, createWriteStream } from "fs";
|
|
1568
1868
|
import { writeFile } from "fs/promises";
|
|
1569
|
-
import { join as
|
|
1869
|
+
import { join as join3 } from "path";
|
|
1570
1870
|
|
|
1571
1871
|
// src/commands/migration-shared.ts
|
|
1572
1872
|
var MIGRATION_SCHEMA_VERSION = 1;
|
|
@@ -1636,7 +1936,7 @@ async function runMigrationExport(args) {
|
|
|
1636
1936
|
"/v1/admin/migration/export/postgres",
|
|
1637
1937
|
{ apiUrl }
|
|
1638
1938
|
);
|
|
1639
|
-
await writeFile(
|
|
1939
|
+
await writeFile(join3(out, "postgres.json"), JSON.stringify(data, null, 2));
|
|
1640
1940
|
const writtenCounts = {
|
|
1641
1941
|
organization: 1,
|
|
1642
1942
|
projects: data.projects.length,
|
|
@@ -1668,7 +1968,7 @@ async function runMigrationExport(args) {
|
|
|
1668
1968
|
dataRange: dataRange.result()
|
|
1669
1969
|
};
|
|
1670
1970
|
await writeFile(
|
|
1671
|
-
|
|
1971
|
+
join3(out, "manifest.json"),
|
|
1672
1972
|
JSON.stringify(manifest, null, 2)
|
|
1673
1973
|
);
|
|
1674
1974
|
console.log(`
|
|
@@ -1678,7 +1978,7 @@ Export complete: ${out}`);
|
|
|
1678
1978
|
);
|
|
1679
1979
|
}
|
|
1680
1980
|
async function exportClickHouseTable(params) {
|
|
1681
|
-
const file = createWriteStream(
|
|
1981
|
+
const file = createWriteStream(join3(params.out, `${params.table}.ndjson`));
|
|
1682
1982
|
const trackTimestamp = MIGRATION_TIMESTAMPED_TABLES.has(params.table);
|
|
1683
1983
|
let cursor = null;
|
|
1684
1984
|
let total = 0;
|
|
@@ -1697,9 +1997,9 @@ async function exportClickHouseTable(params) {
|
|
|
1697
1997
|
total += page.count;
|
|
1698
1998
|
cursor = page.nextCursor;
|
|
1699
1999
|
} while (cursor !== null);
|
|
1700
|
-
await new Promise((
|
|
2000
|
+
await new Promise((resolve2, reject) => {
|
|
1701
2001
|
file.end(
|
|
1702
|
-
(err) => err ? reject(err) :
|
|
2002
|
+
(err) => err ? reject(err) : resolve2()
|
|
1703
2003
|
);
|
|
1704
2004
|
});
|
|
1705
2005
|
return total;
|
|
@@ -1707,9 +2007,9 @@ async function exportClickHouseTable(params) {
|
|
|
1707
2007
|
|
|
1708
2008
|
// src/commands/migration-import.ts
|
|
1709
2009
|
import { createInterface as createInterface4 } from "readline";
|
|
1710
|
-
import { createReadStream, existsSync as
|
|
2010
|
+
import { createReadStream, existsSync as existsSync3 } from "fs";
|
|
1711
2011
|
import { readFile } from "fs/promises";
|
|
1712
|
-
import { join as
|
|
2012
|
+
import { join as join4 } from "path";
|
|
1713
2013
|
var BATCH_SIZE = 2e3;
|
|
1714
2014
|
async function runMigrationImport(args) {
|
|
1715
2015
|
const dir = args.find((a) => !a.startsWith("--"));
|
|
@@ -1723,9 +2023,9 @@ async function runMigrationImport(args) {
|
|
|
1723
2023
|
process.exit(1);
|
|
1724
2024
|
}
|
|
1725
2025
|
const manifest = await readJson(
|
|
1726
|
-
|
|
2026
|
+
join4(dir, "manifest.json")
|
|
1727
2027
|
);
|
|
1728
|
-
const data = await readJson(
|
|
2028
|
+
const data = await readJson(join4(dir, "postgres.json"));
|
|
1729
2029
|
const planResponse = await apiFetch(
|
|
1730
2030
|
"/v1/admin/migration/import",
|
|
1731
2031
|
{
|
|
@@ -1747,10 +2047,10 @@ async function runMigrationImport(args) {
|
|
|
1747
2047
|
input: process.stdin,
|
|
1748
2048
|
output: process.stdout
|
|
1749
2049
|
});
|
|
1750
|
-
const answer = await new Promise((
|
|
2050
|
+
const answer = await new Promise((resolve2) => {
|
|
1751
2051
|
rl.question("Proceed with this import? [y/N] ", (a) => {
|
|
1752
2052
|
rl.close();
|
|
1753
|
-
|
|
2053
|
+
resolve2(a.trim().toLowerCase());
|
|
1754
2054
|
});
|
|
1755
2055
|
});
|
|
1756
2056
|
if (answer !== "y" && answer !== "yes") {
|
|
@@ -1770,8 +2070,8 @@ async function runMigrationImport(args) {
|
|
|
1770
2070
|
Created organization ${result.organizationId}.`);
|
|
1771
2071
|
const clickhouseCounts = {};
|
|
1772
2072
|
for (const table of MIGRATION_CLICKHOUSE_TABLES) {
|
|
1773
|
-
const file =
|
|
1774
|
-
if (!
|
|
2073
|
+
const file = join4(dir, `${table}.ndjson`);
|
|
2074
|
+
if (!existsSync3(file)) {
|
|
1775
2075
|
clickhouseCounts[table] = 0;
|
|
1776
2076
|
continue;
|
|
1777
2077
|
}
|
|
@@ -1890,9 +2190,40 @@ Invited (pending acceptance): ${result.invitedMembers.join(", ")}`
|
|
|
1890
2190
|
}
|
|
1891
2191
|
}
|
|
1892
2192
|
|
|
2193
|
+
// src/lib/global-flags.ts
|
|
2194
|
+
function extractGlobalFlags(argv) {
|
|
2195
|
+
const rest2 = [];
|
|
2196
|
+
let profile;
|
|
2197
|
+
for (let i = 0; i < argv.length; i++) {
|
|
2198
|
+
const arg = argv[i];
|
|
2199
|
+
if (arg === "--profile") {
|
|
2200
|
+
const value = argv[i + 1];
|
|
2201
|
+
if (value === void 0 || value.startsWith("--")) {
|
|
2202
|
+
throw new Error("--profile requires a profile name.");
|
|
2203
|
+
}
|
|
2204
|
+
profile = value;
|
|
2205
|
+
i++;
|
|
2206
|
+
} else if (arg.startsWith("--profile=")) {
|
|
2207
|
+
profile = arg.slice("--profile=".length);
|
|
2208
|
+
if (!profile) throw new Error("--profile requires a profile name.");
|
|
2209
|
+
} else {
|
|
2210
|
+
rest2.push(arg);
|
|
2211
|
+
}
|
|
2212
|
+
}
|
|
2213
|
+
return { profile, argv: rest2 };
|
|
2214
|
+
}
|
|
2215
|
+
|
|
1893
2216
|
// src/index.ts
|
|
1894
|
-
var VERSION = "0.
|
|
1895
|
-
var
|
|
2217
|
+
var VERSION = "0.3.0";
|
|
2218
|
+
var globalFlags;
|
|
2219
|
+
try {
|
|
2220
|
+
globalFlags = extractGlobalFlags(process.argv.slice(2));
|
|
2221
|
+
} catch (err) {
|
|
2222
|
+
console.error(`Error: ${err instanceof Error ? err.message : String(err)}`);
|
|
2223
|
+
process.exit(1);
|
|
2224
|
+
}
|
|
2225
|
+
setActiveProfileFlag(globalFlags.profile);
|
|
2226
|
+
var [cmd, subcmd, ...rest] = globalFlags.argv;
|
|
1896
2227
|
async function main() {
|
|
1897
2228
|
if (cmd === "--version" || cmd === "-v") {
|
|
1898
2229
|
console.log(VERSION);
|
|
@@ -1902,7 +2233,16 @@ async function main() {
|
|
|
1902
2233
|
if (cmd === "login") {
|
|
1903
2234
|
await runLogin(subcmd ? [subcmd, ...rest] : rest);
|
|
1904
2235
|
} else if (cmd === "logout") {
|
|
1905
|
-
runLogout();
|
|
2236
|
+
runLogout(subcmd ? [subcmd, ...rest] : rest);
|
|
2237
|
+
} else if (cmd === "profile") {
|
|
2238
|
+
if (subcmd === "list") {
|
|
2239
|
+
runProfileList(rest);
|
|
2240
|
+
} else if (subcmd === "use") {
|
|
2241
|
+
runProfileUse(rest);
|
|
2242
|
+
} else {
|
|
2243
|
+
printHelp();
|
|
2244
|
+
process.exit(1);
|
|
2245
|
+
}
|
|
1906
2246
|
} else if (cmd === "project") {
|
|
1907
2247
|
if (subcmd === "list") {
|
|
1908
2248
|
await runProjectList();
|
|
@@ -2028,44 +2368,46 @@ function printHelp() {
|
|
|
2028
2368
|
Commands:
|
|
2029
2369
|
login [--token <pat>] [--api-url <url>] Authenticate (non-interactive: --token or EMIT_VISION_PAT env)
|
|
2030
2370
|
login --browser [--yes] [--api-url <url>] Authenticate via browser OAuth
|
|
2031
|
-
logout
|
|
2032
|
-
status [--json] Show auth status and verify token
|
|
2371
|
+
logout [--all] Remove the active profile's credentials (--all removes every profile)
|
|
2372
|
+
status [--json] Show auth status, active profile and why, and verify token
|
|
2373
|
+
profile list [--json] List login profiles (* marks the one in use here)
|
|
2374
|
+
profile use <name> Set the current profile
|
|
2033
2375
|
|
|
2034
2376
|
project list List your projects
|
|
2035
2377
|
project create <name> [--json] Create a new project
|
|
2036
2378
|
project env create <projectId> <name> [--type ...] [--json] Create a new environment
|
|
2037
|
-
key get
|
|
2038
|
-
key rotate
|
|
2379
|
+
key get [projectId] Get ingest key info for a project
|
|
2380
|
+
key rotate [projectId] [--yes] [--json] Rotate the ingest key (--yes skips confirmation)
|
|
2039
2381
|
|
|
2040
|
-
overview
|
|
2382
|
+
overview [projectId] [--from <iso>] [--to <iso>] [--environment <env>] [--json]
|
|
2041
2383
|
Project summary (events, errors, top events)
|
|
2042
|
-
events recent
|
|
2384
|
+
events recent [projectId] [--from <iso>] [--to <iso>] [--environment <env>] [--limit <n>] [--json]
|
|
2043
2385
|
Recent events
|
|
2044
|
-
errors recent
|
|
2386
|
+
errors recent [projectId] [--from <iso>] [--to <iso>] [--environment <env>] [--limit <n>] [--json]
|
|
2045
2387
|
Recent error occurrences
|
|
2046
|
-
errors grouped
|
|
2388
|
+
errors grouped [projectId] [--from <iso>] [--to <iso>] [--environment <env>] [--limit <n>] [--json]
|
|
2047
2389
|
Grouped/deduplicated errors with counts
|
|
2048
|
-
logs
|
|
2390
|
+
logs [projectId] [--from <iso>] [--to <iso>] [--severity <level>] [--limit <n>] [--json]
|
|
2049
2391
|
Recent log entries
|
|
2050
2392
|
|
|
2051
|
-
users list
|
|
2052
|
-
audience export
|
|
2393
|
+
users list [projectId] [--limit <n>] [--json] Identified users with email, name, and activity stats
|
|
2394
|
+
audience export [projectId] [--segment dormant|power-user|high-errors|new] [--format csv|json] [--limit <n>] [--output <file>]
|
|
2053
2395
|
Export audience contacts (default: JSON to stdout)
|
|
2054
|
-
audience sync
|
|
2396
|
+
audience sync [projectId] [--segment <name>] [--tag <tag>] [--limit <n>] [--dry-run]
|
|
2055
2397
|
[--develemail-url <url>] [--develemail-key <key>] [--develemail-project <id>]
|
|
2056
2398
|
Push audience to develemail contacts
|
|
2057
2399
|
|
|
2058
|
-
pulse init --project <projectId> --name <name> [--interval <s>] [--grace <s>] [--verify] [--snippet <type>]
|
|
2400
|
+
pulse init [--project <projectId>] --name <name> [--interval <s>] [--grace <s>] [--verify] [--snippet <type>]
|
|
2059
2401
|
Create a pulse check, print ping snippets (cURL, Docker, systemd, crontab)
|
|
2060
|
-
pulse list --project <projectId> [--json] List pulse checks for a project
|
|
2061
|
-
pulse status --project <projectId> [--json] Show check health with colored status indicators
|
|
2402
|
+
pulse list [--project <projectId>] [--json] List pulse checks for a project
|
|
2403
|
+
pulse status [--project <projectId>] [--json] Show check health with colored status indicators
|
|
2062
2404
|
|
|
2063
2405
|
export --all --out <directory> [--api-url <url>] Export an organization's full data for self-host \u2192 cloud migration
|
|
2064
2406
|
import <archiveDirectory> [--dry-run] [--yes] [--api-url <url>]
|
|
2065
2407
|
Import an export archive into a fresh organization
|
|
2066
2408
|
|
|
2067
|
-
flags list
|
|
2068
|
-
flags evaluate
|
|
2409
|
+
flags list [projectId] [--limit <n>] [--offset <n>] [--json] List feature flags for a project
|
|
2410
|
+
flags evaluate [projectId] --environment <env> [--user-key <key>] [--flag <key>]... [--json]
|
|
2069
2411
|
Evaluate flags for a user/environment
|
|
2070
2412
|
flags enable <projectId> <flagKey> [--json] Enable a flag (requires admin role)
|
|
2071
2413
|
flags disable <projectId> <flagKey> [--json] Disable a flag (requires admin role)
|
|
@@ -2077,7 +2419,16 @@ Commands:
|
|
|
2077
2419
|
flags update <projectId> <flagKey> [--name <name>] [--description <text>] [--environments <csv>] [--default-variant <key>] [--json]
|
|
2078
2420
|
Update non-variant fields on a flag (requires admin role)
|
|
2079
2421
|
|
|
2422
|
+
--profile <name> Use a named login profile (any command; login saves to it)
|
|
2080
2423
|
--version, -v Print CLI version
|
|
2424
|
+
|
|
2425
|
+
Environment:
|
|
2426
|
+
EMIT_VISION_PAT Use this token directly, no login needed
|
|
2427
|
+
EMIT_VISION_API_URL API URL to use with EMIT_VISION_PAT
|
|
2428
|
+
EMIT_VISION_PROFILE Profile to use when --profile is not given
|
|
2429
|
+
.emit-vision.json Repo file {"profile": "<name>", "projectId": "proj_..."} pins the profile and the
|
|
2430
|
+
default [projectId] for that repo (no tokens!)
|
|
2431
|
+
EMIT_VISION_CONFIG_DIR Config directory (default ~/.emit-vision)
|
|
2081
2432
|
`);
|
|
2082
2433
|
}
|
|
2083
2434
|
main();
|
package/package.json
CHANGED